admin commited on
Commit
8fc3eaa
·
1 Parent(s): dd081a5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -84
app.py CHANGED
@@ -1,95 +1,27 @@
1
- import re
2
- import json
3
- import requests
4
  import gradio as gr
5
 
6
- HEADERS = {
7
- "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",
8
- }
9
 
10
-
11
- def is_valid_url(url):
12
- pattern = re.compile(
13
- r"^(https?://)?" r"([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}" r"(:\d+)?" r"(/[^ ]*)?$"
14
- )
15
- return bool(pattern.match(url))
16
-
17
-
18
- def noxlink(longUrl: str):
19
- domain = "https://noxlink.net"
20
- api = f"{domain}/zh-CN/shorten"
21
- payload = {"longUrl": longUrl}
22
  try:
23
- response = requests.post(api, json=payload, headers=HEADERS)
24
- if response.status_code == 200:
25
- retcode = json.loads(response.text)
26
- if retcode["success"]:
27
- return f"{domain}/" + retcode["message"]
28
-
29
- return response.text
30
-
31
- except Exception as e:
32
- return f"{e}"
33
-
34
-
35
- def monojson(longUrl: str):
36
- api = "https://monojson.com/api/short-link"
37
- payload = {"url": longUrl}
38
- try:
39
- response = requests.post(api, json=payload, headers=HEADERS)
40
- if response.status_code == 200:
41
- return json.loads(response.text)["shortUrl"]
42
-
43
- else:
44
- return response.text
45
-
46
- except Exception as e:
47
- return f"{e}"
48
-
49
-
50
- def infer(longUrl: str, tool: str):
51
- shortUrl = ""
52
- if tool == "monojson":
53
- shortUrl = monojson(longUrl)
54
- elif tool == "noxlink":
55
- shortUrl = noxlink(longUrl)
56
- else:
57
- shortUrl = "Please select an API provider!"
58
-
59
- if is_valid_url(shortUrl):
60
- return f'<a href="{shortUrl}" target="_blank">{shortUrl}</a>'
61
 
62
- return shortUrl
 
63
 
64
 
65
  if __name__ == "__main__":
66
  gr.Interface(
67
- fn=infer,
68
- inputs=[
69
- gr.Textbox(
70
- label="Input a long URL",
71
- placeholder="Input a long URL",
72
- show_copy_button=True,
73
- ),
74
- gr.Dropdown(
75
- choices=["noxlink", "monojson"],
76
- label="Select an API provider",
77
- value="noxlink",
78
- ),
79
- ],
80
- outputs=[
81
- gr.HTML(
82
- container=True,
83
- show_label=True,
84
- label="Output short URL",
85
- )
86
- ],
87
- title="Short link generator",
88
- description="Convert long urls into short, easy-to-share links",
89
  flagging_mode="never",
90
- examples=[
91
- ["https://www.bing.com", "noxlink"],
92
- ["https://www.baidu.com", "monojson"],
93
- ],
94
- cache_examples=False,
95
  ).launch()
 
1
+ import subprocess
 
 
2
  import gradio as gr
3
 
 
 
 
4
 
5
+ # 定义推理函数
6
+ def infer(command):
 
 
 
 
 
 
 
 
 
 
7
  try:
8
+ return subprocess.check_output(
9
+ command,
10
+ shell=True,
11
+ stderr=subprocess.STDOUT,
12
+ text=True,
13
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ except subprocess.CalledProcessError as e:
16
+ return f"Error: {e.output}" # 返回错误信息
17
 
18
 
19
  if __name__ == "__main__":
20
  gr.Interface(
21
+ fn=infer, # 推理函数
22
+ inputs=gr.Textbox(label="输入 Linux 命令", value="ls"),
23
+ outputs=gr.TextArea(label="执行结果", show_copy_button=True),
24
+ title="Linux 命令执行测试工具", # 接口标题
25
+ description="输入一个 Linux 命令, 点击提交查看其执行结果",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  flagging_mode="never",
 
 
 
 
 
27
  ).launch()