Rioo26 commited on
Commit
141633b
·
verified ·
1 Parent(s): 145701e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -29
app.py CHANGED
@@ -3,38 +3,11 @@ from transformers import pipeline
3
 
4
  pipe = pipeline("text-generation", model="X-D-Lab/MindChat-Qwen2-0_5B")
5
 
6
- # 初始化对话历史
7
- history = []
8
 
9
- # 设置 prompt 模板
10
- def format_prompt(history, user_input):
11
- prompt = ""
12
- for i, (user, assistant) in enumerate(history):
13
- prompt += f"[Round {i+1}]\n问:{user}\n答:{assistant}\n"
14
- prompt += f"[Round {len(history)+1}]\n问:{user_input}\n答:"
15
- return prompt
16
-
17
- # 响应函数
18
  def respond(message):
19
- global history
20
- prompt = format_prompt(history, message)
21
- result = pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]["generated_text"]
22
-
23
- # 提取模型回答部分(去掉提示部分)
24
- if "答:" in result:
25
- answer = result.split("答:")[-1].strip()
26
- else:
27
- answer = result.strip()
28
-
29
- # 更新历史
30
- history.append((message, answer))
31
- return answer
32
 
33
- # 重置对话
34
- def reset():
35
- global history
36
- history = []
37
- return "已重置对话历史。"
38
 
39
  # 创建界面
40
  gr.Interface(
 
3
 
4
  pipe = pipeline("text-generation", model="X-D-Lab/MindChat-Qwen2-0_5B")
5
 
 
 
6
 
 
 
 
 
 
 
 
 
 
7
  def respond(message):
8
+ result = pipe(message, max_new_tokens=100)
9
+ return result[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
10
 
 
 
 
 
 
11
 
12
  # 创建界面
13
  gr.Interface(