Jenny991 commited on
Commit
007c626
·
1 Parent(s): 226894f

新增聊天機器人

Browse files
Files changed (2) hide show
  1. app.py +20 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
4
+ generator = pipeline(
5
+ "text-generation",
6
+ model="Qwen/Qwen1.5-0.5B-Chat",
7
+ trust_remote_code=True
8
+ )
9
+
10
+ chat_history = []
11
+
12
+ def chat(user_input):
13
+ chat_history.append(f"使用者: {user_input}")
14
+ prompt = "\n".join(chat_history[-6:]) + "\nAI:"
15
+ response = generator(prompt, max_new_tokens=100)[0]["generated_text"]
16
+ reply = response[len(prompt):].strip()
17
+ chat_history.append(f"AI: {reply}")
18
+ return reply
19
+
20
+ gr.ChatInterface(chat).launch()
requirements.txt ADDED
File without changes