把”# -*- coding: utf-8 -*-
import gradio as gr
import requests
import time
import threading
# 添加全局状态跟踪
server_status = {
"last_check": 0,
"is_online": False,
"loading": False
}
def check_server_status():
"""检查模型服务器状态"""
try:
# 尝试检查状态端点
resp = requests.get("http://127.0.0.1:5000/status", timeout=3)
if resp.status_code == 200:
data = resp.json()
# 检查服务是否运行且模型已加载
server_status["is_online"] = data.get("model_loaded", False) and data.get("status") == "running"
server_status["last_check"] = time.time()
return server_status["is_online"]
except Exception as e:
print(f"状态检查错误: {str(e)}")
server_status["is_online"] = False
return server_status["is_online"]
def chat_interface(user_input, history):
"""处理用户输入并获取模型响应"""
# 每30秒检查一次服务器状态
if time.time() - server_status["last_check"] > 30:
threading.Thread(target=check_server_status).start()
# 显示服务器状态提示
if not server_status["is_online"]:
return "[系统] 模型服务器未响应,请检查服务是否已启动", history
try:
server_status["loading"] = True
start_time = time.time()
# 构建包含历史记录的完整上下文
full_context = "\n".join([f"User: {h[0]}\nAI: {h[1]}" for h in history])
full_context += f"\nUser: {user_input}"
response = requests.post(
"http://127.0.0.1:5000/generate",
json={
"prompt": full_context,
"max_length": 1024 # 添加长度限制
},
timeout=180 # 更长超时时间
)
if response.status_code == 200:
ai_response = response.json().get("response", "No response")
response_time = time.time() - start_time
formatted_response = f"{ai_response}\n\n⏱️ 响应时间: {response_time:.2f}秒"
return formatted_response, history
else:
return f"[错误] 服务器返回状态码 {response.status_code}", history
except requests.exceptions.Timeout:
return "[超时] 模型响应时间过长,请稍后重试", history
except Exception as e:
return f"[错误] 发生异常: {str(e)}", history
finally:
server_status["loading"] = False
# 创建聊天界面
with gr.Blocks(title="DeepSeek-7B Chat") as demo:
gr.Markdown("# 🧠 DeepSeek-7B 对话系统")
gr.Markdown("> 输入问题后按Enter提交,模型可能需要10-30秒响应")
with gr.Row():
chatbot = gr.Chatbot(label="对话历史", height=500)
with gr.Column():
gr.Markdown("### 使用说明")
gr.Markdown("1. 输入问题后按Enter提交")
gr.Markdown("2. 长回复可能需要30秒以上")
gr.Markdown("3. 清除按钮会重置对话")
server_status_box = gr.Textbox(label="服务状态", value="正在检测服务...", interactive=False)
msg = gr.Textbox(label="输入消息", placeholder="输入您的问题...")
with gr.Row():
submit_btn = gr.Button("发送")
clear = gr.Button("清除对话")
retry_btn = gr.Button("重试连接")
# 更新服务器状态函数
def update_status():
is_online = check_server_status()
status = "🟢 在线" if is_online else "🔴 离线"
return f"{status} | 最后检查: {time.strftime('%H:%M:%S')}"
# 响应处理函数
def respond(message, chat_history):
bot_message, _ = chat_interface(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
# 清除对话
def clear_chat():
return []
# 重试连接
def retry_connection():
is_online = check_server_status()
status = "🟢 在线" if is_online else "🔴 离线"
return f"{status} | 最后检查: {time.strftime('%H:%M:%S')}"
# 组件交互
msg.submit(respond, [msg, chatbot], [msg, chatbot])
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
clear.click(clear_chat, outputs=[chatbot])
retry_btn.click(retry_connection, outputs=[server_status_box])
# 初始化检查
demo.load(update_status, outputs=[server_status_box])
if __name__ == "__main__":
# 初始状态检查
check_server_status()
# 添加连接测试
print("="*50)
print("测试模型服务器连接...")
try:
test_resp = requests.get("http://127.0.0.1:5000/status", timeout=3)
print(f"连接测试结果: 状态码 {test_resp.status_code}")
if test_resp.status_code == 200:
print(f"服务状态: {test_resp.json()}")
except Exception as e:
print(f"连接失败: {str(e)}")
print("="*50)
# 启动界面
demo.launch(
server_port=7860,
share=False,
server_name="0.0.0.0"
)“改成# model_server/simple_ui.py
import sys
import threading
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QSplitter, QTabWidget, QTextEdit,
QPushButton, QComboBox, QSlider, QLabel, QGroupBox)
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap, QImage
import requests
import numpy as np
import cv2
class AIStudioUI(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("AI 工作室")
self.setGeometry(100, 100, 1200, 800)
# 主布局
main_widget = QWidget()
main_layout = QHBoxLayout()
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
# 左侧控制面板
control_panel = self.create_control_panel()
main_layout.addWidget(control_panel, 1) # 占1份宽度
# 右侧主内容区
splitter = QSplitter(Qt.Vertical)
# 输入面板
input_panel = self.create_input_panel()
splitter.addWidget(input_panel)
# 输出面板
output_panel = self.create_output_panel()
splitter.addWidget(output_panel)
# 状态面板
status_panel = self.create_status_panel()
splitter.addWidget(status_panel)
splitter.setSizes([300, 400, 100]) # 设置各区域高度比例
main_layout.addWidget(splitter, 3) # 占3份宽度
# 定时更新状态
self.timer = QTimer()
self.timer.timeout.connect(self.update_system_status)
self.timer.start(2000) # 每2秒更新一次状态
# 后续定义各面板创建函数...
对吗?