Spaces:
Running
Running
admin
commited on
Commit
·
f9794a6
1
Parent(s):
285808c
upl
Browse files
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*__pycache__*
|
| 2 |
+
test.*
|
| 3 |
+
rename.sh
|
| 4 |
+
.gradio/*
|
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: URL Shortner
|
| 3 |
+
emoji: 🔗
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.22.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: Shorten links
|
| 12 |
+
---
|
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from utils import is_valid_url, HEADER, EN_US, API_URL
|
| 5 |
+
|
| 6 |
+
ZH2EN = {
|
| 7 |
+
"输入长链接": "Input a long URL",
|
| 8 |
+
"输出短链接": "Output short URL",
|
| 9 |
+
"预览短链接": "Preview short URL",
|
| 10 |
+
"将长链接转换为短的、易于共享的链接": "Convert long urls into short, easy-to-share links",
|
| 11 |
+
"状态栏": "Status",
|
| 12 |
+
"短链接生成": "URL Shortner",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _L(zh_txt: str):
|
| 17 |
+
return ZH2EN[zh_txt] if EN_US else zh_txt
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# outer func
|
| 21 |
+
def infer(longUrl: str):
|
| 22 |
+
status = "Success"
|
| 23 |
+
shortUrl = preview = None
|
| 24 |
+
try:
|
| 25 |
+
response = requests.post(
|
| 26 |
+
API_URL, json={"url": longUrl}, headers=HEADER)
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
shortUrl = json.loads(response.text)["shortUrl"]
|
| 29 |
+
else:
|
| 30 |
+
raise ConnectionError(response.text)
|
| 31 |
+
|
| 32 |
+
if is_valid_url(shortUrl):
|
| 33 |
+
preview = f'<a href="{shortUrl}" target="_blank">{shortUrl}</a>'
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
status = f"{e}"
|
| 37 |
+
|
| 38 |
+
return status, shortUrl, preview
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
gr.Interface(
|
| 43 |
+
fn=infer,
|
| 44 |
+
inputs=gr.Textbox(
|
| 45 |
+
label=_L("输入长链接"),
|
| 46 |
+
placeholder=_L("将长链接转换为短的、易于共享的链接"),
|
| 47 |
+
),
|
| 48 |
+
outputs=[
|
| 49 |
+
gr.Textbox(label=_L("状态栏"), show_copy_button=True),
|
| 50 |
+
gr.Textbox(label=_L("输出短链接"), show_copy_button=True),
|
| 51 |
+
gr.HTML(
|
| 52 |
+
container=True,
|
| 53 |
+
show_label=True,
|
| 54 |
+
label=_L("预览短链接"),
|
| 55 |
+
),
|
| 56 |
+
],
|
| 57 |
+
flagging_mode="never",
|
| 58 |
+
examples=["https://www.bing.com", "https://www.baidu.com"],
|
| 59 |
+
cache_examples=False,
|
| 60 |
+
title=_L("短链接生成")
|
| 61 |
+
).launch()
|
utils.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import shutil
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 7 |
+
API_URL = os.getenv("api_url")
|
| 8 |
+
if not API_URL:
|
| 9 |
+
print("请检查环境变量")
|
| 10 |
+
exit()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
HEADER = {
|
| 14 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def is_valid_url(url):
|
| 19 |
+
# 定义 URL 的正则表达式
|
| 20 |
+
pattern = re.compile(
|
| 21 |
+
r"^(https?://)?" # 协议(http 或 https,可选)
|
| 22 |
+
r"([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}" # 域名
|
| 23 |
+
r"(:\d+)?" # 端口号(可选)
|
| 24 |
+
r"(/[^ ]*)?$" # 路径(可选)
|
| 25 |
+
)
|
| 26 |
+
# 使用正则表达式匹配 URL
|
| 27 |
+
return bool(pattern.match(url))
|