HOPStudio commited on
Commit
897ef7a
·
verified ·
1 Parent(s): 5a0b0cb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -49
main.py CHANGED
@@ -4,56 +4,27 @@ import gradio as gr
4
  import requests
5
  import json
6
 
7
- # 处理 API 请求的函数
8
- def api_call(input_text):
9
- api_token = os.getenv("API_TOKEN")
10
- if not api_token:
11
- return "API token 未设置,请检查环境变量。"
12
-
13
- # 如果输入以 "cmd_run" 开头,则执行系统命令
14
- if input_text.startswith("cmd_run "):
15
- command = input_text[len("cmd_run "):]
16
- try:
17
- # 不使用 sudo 执行命令
18
- result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
19
- return result.stdout or result.stderr
20
- except subprocess.CalledProcessError as e:
21
- return f"执行命令时出错: {e}"
22
-
23
- # 否则,调用API
24
- url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
25
- headers = {
26
- 'Content-Type': 'application/json',
27
- "Authorization": f"Bearer {api_token}"
28
- }
29
-
30
- data = {
31
- "model": "internlm2.5-latest",
32
- "messages": [{"role": "user", "content": input_text}],
33
- "n": 1,
34
- "temperature": 0.8,
35
- "top_p": 0.9
36
- }
37
-
38
  try:
39
- response = requests.post(url, headers=headers, data=json.dumps(data))
40
- response.raise_for_status()
41
- return response.json()["choices"][0]["message"]["content"]
42
- except Exception as e:
43
- return f"解析响应时出错: {e}"
44
-
45
- # 启动 Gradio 界面
46
- def start_gradio():
47
- with gr.Blocks() as demo:
48
- input_box = gr.Textbox(label="输入", placeholder="输入对话内容")
49
- output_box = gr.Textbox(label="回复", placeholder="等待API响应...")
50
- send_button = gr.Button("发送")
51
-
52
- send_button.click(api_call, inputs=input_box, outputs=output_box)
53
-
54
- demo.launch(share=False)
55
 
56
- # 从仓库中克隆文件
57
  def clone_repository():
58
  destination_path = "/root/modelscope" # 更改目标路径为 /root/modelscope
59
  repo_url = "https://hf-mirror.com/HOPStudio/modelscope"
@@ -72,7 +43,7 @@ def clone_repository():
72
  except subprocess.CalledProcessError as e:
73
  return f"克隆仓库时出错: {e}"
74
 
75
- # 从仓库安装 frp
76
  def install_frp():
77
  frp_repo_path = "/root/modelscope" # 仓库路径
78
  frpc_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64") # 假设 `frpc` 二进制文件存在该路径
@@ -164,6 +135,10 @@ def main():
164
  frp_message = start_frp()
165
  print(frp_message)
166
 
 
 
 
 
167
  # 启动 Gradio 界面
168
  start_gradio()
169
 
 
4
  import requests
5
  import json
6
 
7
+ # 安装并启动 JupyterLab
8
+ def install_and_start_jupyterlab():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  try:
10
+ # 安装 JupyterLab
11
+ subprocess.run(["pip", "install", "jupyterlab"], check=True)
12
+ print("JupyterLab 安装成功!")
13
+
14
+ # 启动 JupyterLab
15
+ command = [
16
+ "jupyter-lab",
17
+ "--ip=0.0.0.0", # 接受外部连接
18
+ "--port=8888", # 指定端口
19
+ "--no-browser", # 禁止自动打开浏览器
20
+ "--NotebookApp.token=''" # 禁用 token,方便外部直接访问
21
+ ]
22
+ subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
23
+ return "JupyterLab 已启动,请通过远程端口访问。"
24
+ except subprocess.CalledProcessError as e:
25
+ return f"安装或启动 JupyterLab 时出错: {e}"
26
 
27
+ # 克隆仓库
28
  def clone_repository():
29
  destination_path = "/root/modelscope" # 更改目标路径为 /root/modelscope
30
  repo_url = "https://hf-mirror.com/HOPStudio/modelscope"
 
43
  except subprocess.CalledProcessError as e:
44
  return f"克隆仓库时出错: {e}"
45
 
46
+ # 安装 frp
47
  def install_frp():
48
  frp_repo_path = "/root/modelscope" # 仓库路径
49
  frpc_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64") # 假设 `frpc` 二进制文件存在该路径
 
135
  frp_message = start_frp()
136
  print(frp_message)
137
 
138
+ # 安装并启动 JupyterLab
139
+ jupyter_message = install_and_start_jupyterlab()
140
+ print(jupyter_message)
141
+
142
  # 启动 Gradio 界面
143
  start_gradio()
144