Update main.py
Browse files
main.py
CHANGED
|
@@ -4,132 +4,25 @@ import gradio as gr
|
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 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'
|
| 42 |
-
headers = {
|
| 43 |
-
'Content-Type': 'application/json',
|
| 44 |
-
"Authorization": f"Bearer {api_token}"
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
data = {
|
| 48 |
-
"model": model,
|
| 49 |
-
"messages": [{"role": "user", "content": input_text}],
|
| 50 |
-
"n": n,
|
| 51 |
-
"temperature": temperature,
|
| 52 |
-
"top_p": top_p
|
| 53 |
-
}
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
|
| 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,
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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"
|
| 133 |
|
| 134 |
# 克隆仓库
|
| 135 |
def clone_repository():
|
|
@@ -213,6 +106,32 @@ def start_frp():
|
|
| 213 |
except subprocess.CalledProcessError as e:
|
| 214 |
return f"启动 frp 时出错: {e}"
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
# 主函数
|
| 217 |
def main():
|
| 218 |
clone_message = clone_repository()
|
|
@@ -226,10 +145,11 @@ def main():
|
|
| 226 |
frp_message = start_frp()
|
| 227 |
print(frp_message)
|
| 228 |
|
| 229 |
-
jupyter_message =
|
| 230 |
print(jupyter_message)
|
| 231 |
|
| 232 |
start_gradio()
|
| 233 |
|
| 234 |
if __name__ == "__main__":
|
| 235 |
main()
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# 替换原有的 JupyterLab 安装和启动功能
|
| 8 |
+
def start_jupyterlab_process():
|
| 9 |
+
# 检查 start_jupyterlab.py 是否存在
|
| 10 |
+
jupyter_script = "./start_jupyterlab.py"
|
| 11 |
+
if not os.path.exists(jupyter_script):
|
| 12 |
+
return "无法找到 start_jupyterlab.py,请确保脚本存在于当前目录下。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# 启动 start_jupyterlab.py 脚本
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
process = subprocess.Popen(
|
| 17 |
+
["python3", jupyter_script],
|
| 18 |
+
stdout=subprocess.PIPE,
|
| 19 |
+
stderr=subprocess.PIPE,
|
| 20 |
+
text=True
|
| 21 |
)
|
| 22 |
+
print("JupyterLab 启动进程已开始...")
|
| 23 |
+
return "JupyterLab 启动成功,详情请查看日志输出。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"启动 JupyterLab 进程时出错: {str(e)}"
|
| 26 |
|
| 27 |
# 克隆仓库
|
| 28 |
def clone_repository():
|
|
|
|
| 106 |
except subprocess.CalledProcessError as e:
|
| 107 |
return f"启动 frp 时出错: {e}"
|
| 108 |
|
| 109 |
+
# 启动 Gradio 界面
|
| 110 |
+
def start_gradio():
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
with gr.Row():
|
| 113 |
+
input_box = gr.Textbox(label="输入", placeholder="输入对话内容")
|
| 114 |
+
with gr.Row():
|
| 115 |
+
model_box = gr.Textbox(value="internlm2.5-latest", label="模型", placeholder="输入模型名称")
|
| 116 |
+
with gr.Row():
|
| 117 |
+
temperature_slider = gr.Slider(minimum=0, maximum=1, value=0.8, step=0.01, label="Temperature")
|
| 118 |
+
top_p_slider = gr.Slider(minimum=0, maximum=1, value=0.9, step=0.01, label="Top-p")
|
| 119 |
+
with gr.Row():
|
| 120 |
+
n_slider = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="生成回复数量")
|
| 121 |
+
with gr.Row():
|
| 122 |
+
send_button = gr.Button("发送")
|
| 123 |
+
with gr.Row():
|
| 124 |
+
output_box = gr.Textbox(label="回复", placeholder="等待API响应...", interactive=False)
|
| 125 |
+
|
| 126 |
+
# 定义按钮点击事件
|
| 127 |
+
send_button.click(
|
| 128 |
+
api_call,
|
| 129 |
+
inputs=[input_box, model_box, temperature_slider, top_p_slider, n_slider],
|
| 130 |
+
outputs=output_box
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
demo.launch(share=False)
|
| 134 |
+
|
| 135 |
# 主函数
|
| 136 |
def main():
|
| 137 |
clone_message = clone_repository()
|
|
|
|
| 145 |
frp_message = start_frp()
|
| 146 |
print(frp_message)
|
| 147 |
|
| 148 |
+
jupyter_message = start_jupyterlab_process()
|
| 149 |
print(jupyter_message)
|
| 150 |
|
| 151 |
start_gradio()
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
main()
|
| 155 |
+
|