新增聊天機器人
Browse files- app.py +20 -0
- 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
|