VertinYi commited on
Commit
1afb041
·
verified ·
1 Parent(s): e869165

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # 加载 Hugging Face 上的预训练模型(以 DeepSeek 为例)
5
+ model_name = "deepseek-ai/deepseek-7b"
6
+ pipe = pipeline("text-generation", model=model_name, tokenizer=model_name)
7
+
8
+ # 定义与模型对话的函数
9
+ def chat_with_ai(prompt):
10
+ # 使用模型生成文本
11
+ response = pipe(prompt, max_length=100, do_sample=True)
12
+ return response[0]["generated_text"]
13
+
14
+ # 创建 Gradio 界面
15
+ with gr.Blocks() as demo:
16
+ gr.Markdown("# 🤖 AI Chatbot powered by DeepSeek")
17
+
18
+ # 聊天框组件
19
+ chatbot = gr.Chatbot()
20
+ msg = gr.Textbox(label="Type your message:")
21
+ clear = gr.Button("Clear")
22
+
23
+ # 定义响应函数,处理用户输入并更新聊天历史
24
+ def respond(message, chat_history):
25
+ response = chat_with_ai(message)
26
+ chat_history.append((message, response)) # 将对话记录添加到聊天历史
27
+ return "", chat_history
28
+
29
+ # 提交消息并更新聊天记录
30
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
31
+ # 清空聊天记录
32
+ clear.click(lambda: [], None, chatbot)
33
+
34
+ # 启动 Gradio 界面
35
+ demo.launch()