HOPStudio commited on
Commit
6f5bb4a
·
verified ·
1 Parent(s): 3479606

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +42 -94
main.py CHANGED
@@ -1,32 +1,29 @@
1
  import os
2
  import subprocess
3
  import threading
4
- import shlex
5
  import gradio as gr
6
  import requests
7
  import json
 
8
 
9
- # 仓库路径
10
  REPO_URL = "https://hf-mirror.com/HOPStudio/modelscope"
11
  REPO_PATH = "/root/modelscope"
12
 
13
- # 当前 shell 工作目录
14
  CURRENT_DIR = REPO_PATH
15
 
16
 
17
- # 确保克隆仓库
18
- # 修复:
19
- # 1. 禁用 git lfs smudge
20
- # 2. 禁用 xet
21
- # 3. 避免 frp 下载失败
22
- # =====================================
23
  def clone_repository():
24
  if os.path.exists(REPO_PATH):
25
- print(f"仓库已存在跳过克隆: {REPO_PATH}")
26
- return True
 
 
 
 
 
27
 
28
  try:
29
- print(f"正在克隆仓库到 {REPO_PATH}...")
30
 
31
  env = os.environ.copy()
32
  env["GIT_LFS_SKIP_SMUDGE"] = "1"
@@ -46,24 +43,15 @@ def clone_repository():
46
  return False
47
 
48
 
49
- # =====================================
50
- # 支持 cd 命令的 shell 执行器
51
- # =====================================
52
  def execute_shell_command(command):
53
  global CURRENT_DIR
54
 
55
  command = command.strip()
56
 
57
- # =========================
58
- # 处理 cd 命令
59
- # =========================
60
  if command.startswith("cd "):
61
  path = command[3:].strip()
62
-
63
- # 支持 ~
64
  path = os.path.expanduser(path)
65
 
66
- # 相对路径 -> 绝对路径
67
  if not os.path.isabs(path):
68
  path = os.path.join(CURRENT_DIR, path)
69
 
@@ -72,12 +60,8 @@ def execute_shell_command(command):
72
  if os.path.isdir(path):
73
  CURRENT_DIR = path
74
  return f"已切换目录: {CURRENT_DIR}"
75
- else:
76
- return f"目录不存在: {path}"
77
 
78
- # =========================
79
- # 执行普通命令
80
- # =========================
81
  try:
82
  result = subprocess.run(
83
  command,
@@ -99,37 +83,21 @@ def execute_shell_command(command):
99
  return f"执行命令时发生错误: {str(e)}"
100
 
101
 
102
- # =====================================
103
- # API 请求函数
104
- # =====================================
105
  def api_call(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9, n=1):
106
  api_token = os.getenv("API_TOKEN")
107
 
108
- if not api_token:
109
- return "API token 未设置,请检查环境变量。"
110
-
111
- # =========================
112
- # CMD 模式
113
- # 使用:
114
- # cmd_run ls
115
- # cmd_run cd /root
116
- # =========================
117
  if input_text.startswith("cmd_run "):
118
  command = input_text[len("cmd_run "):].strip()
119
-
120
- if "sudo" in command:
121
- print("警告: 正在使用 sudo 命令")
122
-
123
  return execute_shell_command(command)
124
 
125
- # =========================
126
- # AI 对话模式
127
- # =========================
128
- url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
129
 
130
  headers = {
131
- 'Content-Type': 'application/json',
132
- 'Authorization': f'Bearer {api_token}'
133
  }
134
 
135
  data = {
@@ -153,7 +121,6 @@ def api_call(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9,
153
  )
154
 
155
  response.raise_for_status()
156
-
157
  return response.json()["choices"][0]["message"]["content"]
158
 
159
  except requests.exceptions.HTTPError as http_err:
@@ -169,9 +136,6 @@ def api_call(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9,
169
  return f"未知错误: {str(e)}"
170
 
171
 
172
- # =====================================
173
- # 创建 frpc.toml
174
- # =====================================
175
  def create_frpc_toml():
176
  frpc_toml_path = os.path.join(REPO_PATH, "frpc.toml")
177
 
@@ -196,23 +160,16 @@ localPort = 8188
196
  remotePort = 8188
197
  """
198
 
199
- if os.path.exists(frpc_toml_path):
200
- print(f"配置文件已存在: {frpc_toml_path}")
201
- return
202
-
203
  try:
204
- with open(frpc_toml_path, 'w', encoding='utf-8') as file:
205
  file.write(toml_content)
206
 
207
- print(f"frpc.toml 创建成功: {frpc_toml_path}")
208
 
209
  except Exception as e:
210
- print(f"创建 frpc.toml 时出错: {e}")
211
 
212
 
213
- # =====================================
214
- # 安装 frp
215
- # =====================================
216
  def install_frp():
217
  frp_tar_path = os.path.join(
218
  REPO_PATH,
@@ -230,11 +187,10 @@ def install_frp():
230
 
231
  if not os.path.exists(frp_tar_path):
232
  print(f"未找到 frp ��装包: {frp_tar_path}")
233
- print("请手动上传 frp_0.61.0_linux_amd64.tar.gz")
234
  return False
235
 
236
  try:
237
- print("正在解压 frp...")
238
 
239
  subprocess.run(
240
  ["tar", "-xvzf", frp_tar_path, "-C", REPO_PATH],
@@ -249,14 +205,11 @@ def install_frp():
249
  return False
250
 
251
 
252
- # =====================================
253
- # 启动 JupyterLab
254
- # =====================================
255
  def start_jupyterlab_process():
256
  jupyter_script = os.path.join(REPO_PATH, "start_jupyterlab.py")
257
 
258
  if not os.path.exists(jupyter_script):
259
- print(f"找文件: {jupyter_script}")
260
  return
261
 
262
  try:
@@ -267,55 +220,50 @@ def start_jupyterlab_process():
267
  text=True
268
  )
269
 
270
- print("JupyterLab 启动")
271
 
272
  except Exception as e:
273
- print(f"启动 JupyterLab 出错: {str(e)}")
274
 
275
 
276
- # =====================================
277
- # 启动 frp
278
- # =====================================
279
  def start_frp():
280
  frpc_path = os.path.join(
281
  REPO_PATH,
282
- "frp_0.61.0_linux_amd64/frpc"
 
283
  )
284
 
285
  frpc_config_path = os.path.join(REPO_PATH, "frpc.toml")
286
 
287
  if not os.path.exists(frpc_path):
288
- print("frpc 不存在")
289
  return
290
 
291
  if not os.path.exists(frpc_config_path):
292
  create_frpc_toml()
293
 
294
  try:
295
- subprocess.Popen([
296
- frpc_path,
297
- "-c",
298
- frpc_config_path
299
- ])
 
300
 
301
- print("frp 已启动")
302
 
303
  except Exception as e:
304
- print(f"启动 frp 出错: {str(e)}")
305
 
306
 
307
- # =====================================
308
- # 启动 Gradio
309
- # =====================================
310
  def start_gradio():
311
  with gr.Blocks() as demo:
312
-
313
  gr.Markdown("# InternLM 控制台")
314
 
315
  with gr.Row():
316
  input_box = gr.Textbox(
317
  label="输入",
318
- placeholder="输入对话或 cmd_run 命令"
319
  )
320
 
321
  with gr.Row():
@@ -379,14 +327,15 @@ def start_gradio():
379
  )
380
 
381
 
382
- # =====================================
383
- # 主函数
384
- # =====================================
385
  def main():
386
- clone_repository()
387
 
388
- install_frp()
389
 
 
 
 
 
390
  create_frpc_toml()
391
 
392
  frp_thread = threading.Thread(
@@ -406,5 +355,4 @@ def main():
406
 
407
 
408
  if __name__ == "__main__":
409
- main()
410
-
 
1
  import os
2
  import subprocess
3
  import threading
 
4
  import gradio as gr
5
  import requests
6
  import json
7
+ import shutil
8
 
 
9
  REPO_URL = "https://hf-mirror.com/HOPStudio/modelscope"
10
  REPO_PATH = "/root/modelscope"
11
 
 
12
  CURRENT_DIR = REPO_PATH
13
 
14
 
 
 
 
 
 
 
15
  def clone_repository():
16
  if os.path.exists(REPO_PATH):
17
+ print(f"检测到已有仓库目录正在清空: {REPO_PATH}")
18
+ try:
19
+ shutil.rmtree(REPO_PATH)
20
+ print("原仓库目录已清空。")
21
+ except Exception as e:
22
+ print(f"清空原仓库目录失败: {e}")
23
+ return False
24
 
25
  try:
26
+ print(f"正在强制克隆仓库到 {REPO_PATH}...")
27
 
28
  env = os.environ.copy()
29
  env["GIT_LFS_SKIP_SMUDGE"] = "1"
 
43
  return False
44
 
45
 
 
 
 
46
  def execute_shell_command(command):
47
  global CURRENT_DIR
48
 
49
  command = command.strip()
50
 
 
 
 
51
  if command.startswith("cd "):
52
  path = command[3:].strip()
 
 
53
  path = os.path.expanduser(path)
54
 
 
55
  if not os.path.isabs(path):
56
  path = os.path.join(CURRENT_DIR, path)
57
 
 
60
  if os.path.isdir(path):
61
  CURRENT_DIR = path
62
  return f"已切换目录: {CURRENT_DIR}"
63
+ return f"目录不存在: {path}"
 
64
 
 
 
 
65
  try:
66
  result = subprocess.run(
67
  command,
 
83
  return f"执行命令时发生错误: {str(e)}"
84
 
85
 
 
 
 
86
  def api_call(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9, n=1):
87
  api_token = os.getenv("API_TOKEN")
88
 
 
 
 
 
 
 
 
 
 
89
  if input_text.startswith("cmd_run "):
90
  command = input_text[len("cmd_run "):].strip()
 
 
 
 
91
  return execute_shell_command(command)
92
 
93
+ if not api_token:
94
+ return "API token 未设置,请检查环境变量。"
95
+
96
+ url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions"
97
 
98
  headers = {
99
+ "Content-Type": "application/json",
100
+ "Authorization": f"Bearer {api_token}"
101
  }
102
 
103
  data = {
 
121
  )
122
 
123
  response.raise_for_status()
 
124
  return response.json()["choices"][0]["message"]["content"]
125
 
126
  except requests.exceptions.HTTPError as http_err:
 
136
  return f"未知错误: {str(e)}"
137
 
138
 
 
 
 
139
  def create_frpc_toml():
140
  frpc_toml_path = os.path.join(REPO_PATH, "frpc.toml")
141
 
 
160
  remotePort = 8188
161
  """
162
 
 
 
 
 
163
  try:
164
+ with open(frpc_toml_path, "w", encoding="utf-8") as file:
165
  file.write(toml_content)
166
 
167
+ print(f"frpc.toml 配置文件已创建: {frpc_toml_path}")
168
 
169
  except Exception as e:
170
+ print(f"创建 frpc.toml 配置文件时出错: {e}")
171
 
172
 
 
 
 
173
  def install_frp():
174
  frp_tar_path = os.path.join(
175
  REPO_PATH,
 
187
 
188
  if not os.path.exists(frp_tar_path):
189
  print(f"未找到 frp ��装包: {frp_tar_path}")
 
190
  return False
191
 
192
  try:
193
+ print(f"正在解压 frp 安装包到 {REPO_PATH}...")
194
 
195
  subprocess.run(
196
  ["tar", "-xvzf", frp_tar_path, "-C", REPO_PATH],
 
205
  return False
206
 
207
 
 
 
 
208
  def start_jupyterlab_process():
209
  jupyter_script = os.path.join(REPO_PATH, "start_jupyterlab.py")
210
 
211
  if not os.path.exists(jupyter_script):
212
+ print(f"无法找到 {jupyter_script}")
213
  return
214
 
215
  try:
 
220
  text=True
221
  )
222
 
223
+ print("JupyterLab 启动进程已开始。")
224
 
225
  except Exception as e:
226
+ print(f"启动 JupyterLab 进程时出错: {str(e)}")
227
 
228
 
 
 
 
229
  def start_frp():
230
  frpc_path = os.path.join(
231
  REPO_PATH,
232
+ "frp_0.61.0_linux_amd64",
233
+ "frpc"
234
  )
235
 
236
  frpc_config_path = os.path.join(REPO_PATH, "frpc.toml")
237
 
238
  if not os.path.exists(frpc_path):
239
+ print("frpc 可执行文件不存在,请检查路径。")
240
  return
241
 
242
  if not os.path.exists(frpc_config_path):
243
  create_frpc_toml()
244
 
245
  try:
246
+ subprocess.Popen(
247
+ [frpc_path, "-c", frpc_config_path],
248
+ stdout=subprocess.PIPE,
249
+ stderr=subprocess.PIPE,
250
+ text=True
251
+ )
252
 
253
+ print("frp 已启动")
254
 
255
  except Exception as e:
256
+ print(f"启动 frp 出错: {str(e)}")
257
 
258
 
 
 
 
259
  def start_gradio():
260
  with gr.Blocks() as demo:
 
261
  gr.Markdown("# InternLM 控制台")
262
 
263
  with gr.Row():
264
  input_box = gr.Textbox(
265
  label="输入",
266
+ placeholder="输入对话内容,使用 cmd_run 执行命令"
267
  )
268
 
269
  with gr.Row():
 
327
  )
328
 
329
 
 
 
 
330
  def main():
331
+ global CURRENT_DIR
332
 
333
+ clone_success = clone_repository()
334
 
335
+ if clone_success:
336
+ CURRENT_DIR = REPO_PATH
337
+
338
+ install_frp()
339
  create_frpc_toml()
340
 
341
  frp_thread = threading.Thread(
 
355
 
356
 
357
  if __name__ == "__main__":
358
+ main()