bs-demo-ds / app.py
Rioo26's picture
Update app.py
fd03949 verified
import gradio as gr
from huggingface_hub import InferenceClient
import os
from datetime import datetime
client = InferenceClient(
api_key=os.getenv("HF_token")
)
# 初始化聊天历史
def chat_with_model(user_input, history):
if history is None:
history = []
messages = [{"role": "user", "content": user_input}]
try:
completion = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3-0324",
messages=messages,
max_tokens=500,
)
reply = completion.choices[0].message.content
history.append((user_input, reply))
return history, history
except Exception as e:
error_message = f"出错了:{str(e)}"
history.append((user_input, error_message))
return history, history
# Gradio 多轮对话界面
with gr.Blocks(title="🌐 AI 医疗问答系统") as demo:
gr.Markdown("## 💬 医疗问答助手\n使用 Hugging Face 上的 DeepSeek 模型进行问答。")
chatbot = gr.Chatbot(label="对话记录")
user_input = gr.Textbox(lines=3, label="你的问题")
state = gr.State([]) # 用来保存历史
send = gr.Button("发送")
send.click(fn=chat_with_model, inputs=[user_input, state], outputs=[chatbot, state])
if __name__ == "__main__":
demo.launch(share=True)