File size: 865 Bytes
7ceb903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import gradio as gr
from utils import format_chat_history, get_qwen_response

def chat_with_qwen(message, history):
    """使用通义千问API进行对话"""
    # 格式化历史消息
    messages = format_chat_history(history, message)
    
    # 调用通义千问API
    response = get_qwen_response(messages)
    
    if response['success']:
        return response['content']
    else:
        return f"⚠️ {response['content']}"

# 创建Gradio界面
demo = gr.ChatInterface(
    fn=chat_with_qwen,
    title="通义千问聊天机器人",
    description="这是一个使用DashScope API调用通义千问大模型的聊天机器人",
    examples=["你好,请介绍一下自己", "什么是人工智能?", "给我讲个故事"],
    theme="soft"
)

# 启动应用
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)