Spaces:
Running
Running
File size: 1,628 Bytes
f9794a6 6860015 f9794a6 81cab4c f9794a6 81cab4c f9794a6 81cab4c 6860015 f9794a6 6860015 f9794a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import json
import requests
import gradio as gr
from utils import is_valid_url, HEADER, EN_US, API_URL
ZH2EN = {
"输入长链接": "Input a long URL",
"输出短链接": "Output short URL",
"预览短链接": "Preview short URL",
"将长链接转换为短的、易于共享的链接": "Convert long urls into short, easy-to-share links",
"状态栏": "Status",
"短链接生成": "URL Shortner",
}
def _L(zh_txt: str):
return ZH2EN[zh_txt] if EN_US else zh_txt
# outer func
def infer(longUrl: str):
status = "Success"
shortUrl = preview = None
try:
response = requests.post(API_URL, json={"url": longUrl}, headers=HEADER)
if response.status_code == 200:
shortUrl = json.loads(response.text)["shortUrl"]
else:
raise ConnectionError(response.text)
if is_valid_url(shortUrl):
preview = f"<{shortUrl}>"
except Exception as e:
status = f"{e}"
return status, shortUrl, preview
if __name__ == "__main__":
gr.Interface(
fn=infer,
inputs=gr.Textbox(
label=_L("输入长链接"),
placeholder=_L("将长链接转换为短的、易于共享的链接"),
),
outputs=[
gr.Textbox(label=_L("状态栏"), show_copy_button=True),
gr.Textbox(label=_L("输出短链接"), show_copy_button=True),
gr.Markdown(container=True),
],
flagging_mode="never",
examples=["https://www.bing.com", "https://www.baidu.com"],
cache_examples=False,
title=_L("短链接生成"),
).launch()
|