HOPStudio commited on
Commit
22b1d0c
·
verified ·
1 Parent(s): 85be6db

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +36 -191
main.py CHANGED
@@ -1,219 +1,64 @@
1
  import os
2
- import subprocess
3
- import threading
4
- import gradio as gr
5
- import requests
6
- import json
7
-
8
- # 仓库路径
9
- REPO_URL = "https://hf-mirror.com/HOPStudio/modelscope"
10
- REPO_PATH = "/root/modelscope"
11
-
12
- # 确保克隆仓库
13
- def clone_repository():
14
- if os.path.exists(REPO_PATH):
15
- print(f"仓库已存在,跳过克隆: {REPO_PATH}")
16
- return True
17
- try:
18
- print(f"正在克隆仓库到 {REPO_PATH}...")
19
- subprocess.run(["git", "clone", REPO_URL, REPO_PATH], check=True)
20
- print("仓库克隆成功!")
21
- return True
22
- except subprocess.CalledProcessError as e:
23
- print(f"克隆仓库时出错: {e}")
24
- return False
25
-
26
- # 处理 API 请求的函数
27
- def api_call(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9, n=1):
28
- api_token = os.getenv("API_TOKEN")
29
- if not api_token:
30
- return "API token 未设置,请检查环境变量。"
31
-
32
- if input_text.startswith("cmd_run "):
33
- command = input_text[len("cmd_run "):].strip()
34
- if "sudo" in command:
35
- print("警告: 您正在使用 'sudo' 运行命令,请确保了解潜在风险。")
36
-
37
- try:
38
- command_list = command.split()
39
- result = subprocess.run(
40
- command_list,
41
- check=True,
42
- stdout=subprocess.PIPE,
43
- stderr=subprocess.PIPE,
44
- text=True
45
  )
46
- return result.stdout or result.stderr
47
- except FileNotFoundError:
48
- return "命令未找到,请检查输入是否正确。"
49
- except subprocess.CalledProcessError as e:
50
- return f"执行命令时出错: {e.stderr}"
51
- except Exception as e:
52
- return f"未知错误: {str(e)}"
53
-
54
- url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
55
- headers = {
56
- 'Content-Type': 'application/json',
57
- "Authorization": f"Bearer {api_token}"
58
- }
59
- data = {
60
- "model": model,
61
- "messages": [{"role": "user", "content": input_text}],
62
- "n": n,
63
- "temperature": temperature,
64
- "top_p": top_p
65
- }
66
- try:
67
- response = requests.post(url, headers=headers, data=json.dumps(data))
68
- response.raise_for_status()
69
- return response.json()["choices"][0]["message"]["content"]
70
- except requests.exceptions.HTTPError as http_err:
71
- return f"HTTP 错误: {http_err}"
72
- except requests.exceptions.RequestException as req_err:
73
- return f"请求错误: {req_err}"
74
- except KeyError:
75
- return "解析响应时出错,响应结构可能发生变化。"
76
- except Exception as e:
77
- return f"未知错误: {str(e)}"
78
-
79
-
80
- # 创建 frpc.toml 配置文件
81
- def create_frpc_toml():
82
- frpc_toml_path = os.path.join(REPO_PATH, "frpc.toml")
83
- toml_content = """
84
- [common]
85
- server_addr = 47.98.223.72
86
- server_port = 7000
87
- token = hop20030219
88
-
89
- [jupyterlab]
90
- type = tcp
91
- local_ip = 127.0.0.1
92
- local_port = 18888
93
- remote_port = 18888
94
-
95
- [comfyui]
96
- type = tcp
97
- local_ip = 127.0.0.1
98
- local_port = 8188
99
- remote_port = 8188
100
- """
101
-
102
- if os.path.exists(frpc_toml_path):
103
- print(f"配置文件 {frpc_toml_path} 已存在,无需重新创建。")
104
- return
105
-
106
- try:
107
- with open(frpc_toml_path, 'w') as file:
108
- file.write(toml_content)
109
- print(f"frpc.toml 配置文件已创建:{frpc_toml_path}")
110
- except Exception as e:
111
- print(f"创建 frpc.toml 配置文件时出错: {e}")
112
-
113
- def install_frp():
114
- frp_tar_path = os.path.join(REPO_PATH, "frp_0.61.0_linux_amd64.tar.gz")
115
- frp_install_path = os.path.join(REPO_PATH, "frp_0.61.0_linux_amd64")
116
-
117
- if os.path.exists(frp_install_path):
118
- print(f"frp 已安装: {frp_install_path}")
119
- return True
120
-
121
- if not os.path.exists(frp_tar_path):
122
- print(f"未找到 frp 安装包: {frp_tar_path}")
123
- return False
124
-
125
- try:
126
- print(f"正在解压 frp 安装包到 {REPO_PATH}...")
127
- subprocess.run(["tar", "-xvzf", frp_tar_path, "-C", REPO_PATH], check=True)
128
- print("frp 安装成功!")
129
- return True
130
- except subprocess.CalledProcessError as e:
131
- print(f"解压 frp 时出错: {e}")
132
- return False
133
-
134
- # 启动 start_jupyterlab.py 的线程函数
135
- def start_jupyterlab_process():
136
- jupyter_script = os.path.join(REPO_PATH, "start_jupyterlab.py")
137
- if not os.path.exists(jupyter_script):
138
- print(f"无法找到 {jupyter_script},请确保脚本已存在于仓库中。")
139
- return
140
-
141
- try:
142
- subprocess.Popen(
143
- ["python3", jupyter_script],
144
- stdout=subprocess.PIPE,
145
- stderr=subprocess.PIPE,
146
- text=True
147
- )
148
- print("JupyterLab 启动进程已开始...")
149
- except Exception as e:
150
- print(f"启动 JupyterLab 进程时出错: {str(e)}")
151
-
152
-
153
- # 启�� frp 的线程函数
154
- def start_frp():
155
- frpc_path = os.path.join(REPO_PATH, "frp_0.61.0_linux_amd64/frpc")
156
- frpc_config_path = os.path.join(REPO_PATH, "frpc.toml")
157
-
158
- if not os.path.exists(frpc_path):
159
- print("frpc 可执行文件不存在,请检查路径。")
160
- return
161
-
162
- if not os.path.exists(frpc_config_path):
163
- print("frpc 配置文件不存在,正在创建...")
164
- create_frpc_toml()
165
-
166
- try:
167
- subprocess.Popen([frpc_path, "-c", frpc_config_path])
168
- print("frp 已启动。")
169
- except Exception as e:
170
- print(f"启动 frp 时出错: {str(e)}")
171
 
172
-
173
- # 启动 Gradio 界面
174
- def start_gradio():
175
- with gr.Blocks() as demo:
176
- with gr.Row():
177
- input_box = gr.Textbox(label="输入", placeholder="输入对话内容")
178
- with gr.Row():
179
- model_box = gr.Textbox(value="internlm2.5-latest", label="模型", placeholder="输入模型名称")
180
- with gr.Row():
181
- temperature_slider = gr.Slider(minimum=0, maximum=1, value=0.8, step=0.01, label="Temperature")
182
- top_p_slider = gr.Slider(minimum=0, maximum=1, value=0.9, step=0.01, label="Top-p")
183
- with gr.Row():
184
- n_slider = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="生成回复数量")
185
  with gr.Row():
186
  send_button = gr.Button("发送")
 
187
  with gr.Row():
188
- output_box = gr.Textbox(label="回复", placeholder="等待API响应...", interactive=False)
189
-
 
 
 
 
190
  send_button.click(
191
  api_call,
192
- inputs=[input_box, model_box, temperature_slider, top_p_slider, n_slider],
 
 
 
 
 
 
193
  outputs=output_box
194
  )
195
 
196
- demo.launch(share=False)
 
 
 
 
197
 
198
 
 
199
  # 主函数
 
200
  def main():
201
  clone_repository()
 
202
  install_frp()
203
 
204
- # 创建 frp 配置文件(如果不存在)
205
  create_frpc_toml()
206
 
207
- # 创建并启动非阻塞线程运行 frp 和 JupyterLab
208
- frp_thread = threading.Thread(target=start_frp, daemon=True)
209
- jupyter_thread = threading.Thread(target=start_jupyterlab_process, daemon=True)
 
 
 
 
 
 
210
 
211
  frp_thread.start()
212
  jupyter_thread.start()
213
 
214
- # 主线程运行 Gradio 界面
215
  start_gradio()
216
 
217
 
218
  if __name__ == "__main__":
219
- main()
 
1
  import os
2
+ maximum=10,
3
+ value=1,
4
+ step=1,
5
+ label="生成回复数量"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  with gr.Row():
9
  send_button = gr.Button("发送")
10
+
11
  with gr.Row():
12
+ output_box = gr.Textbox(
13
+ label="回复",
14
+ lines=20,
15
+ interactive=False
16
+ )
17
+
18
  send_button.click(
19
  api_call,
20
+ inputs=[
21
+ input_box,
22
+ model_box,
23
+ temperature_slider,
24
+ top_p_slider,
25
+ n_slider
26
+ ],
27
  outputs=output_box
28
  )
29
 
30
+ demo.launch(
31
+ server_name="0.0.0.0",
32
+ server_port=7860,
33
+ share=False
34
+ )
35
 
36
 
37
+ # =====================================
38
  # 主函数
39
+ # =====================================
40
  def main():
41
  clone_repository()
42
+
43
  install_frp()
44
 
 
45
  create_frpc_toml()
46
 
47
+ frp_thread = threading.Thread(
48
+ target=start_frp,
49
+ daemon=True
50
+ )
51
+
52
+ jupyter_thread = threading.Thread(
53
+ target=start_jupyterlab_process,
54
+ daemon=True
55
+ )
56
 
57
  frp_thread.start()
58
  jupyter_thread.start()
59
 
 
60
  start_gradio()
61
 
62
 
63
  if __name__ == "__main__":
64
+ main()