lhnjames commited on
Commit
d3dc76e
·
verified ·
1 Parent(s): 1fcb87b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -5,7 +5,7 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
  def respond(message, system_message, max_tokens, temperature, top_p, history):
7
  messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}]
8
-
9
  response1, response2 = "", ""
10
 
11
  for message in client.chat_completion(
@@ -28,42 +28,44 @@ def respond(message, system_message, max_tokens, temperature, top_p, history):
28
  token = message.choices[0].delta.content
29
  response2 += token
30
 
31
- return response1, response2, history + [{"role": "user", "content": message}, {"role": "assistant", "content": response1}, {"role": "assistant", "content": response2}]
 
 
 
 
32
 
33
- def vote(choice, response1, response2, history):
34
- selected_response = response1 if choice == "回答 1" else response2
35
- return history + [("用户选择的回答", selected_response)], f"你选择了:{choice}"
 
36
 
37
  with gr.Blocks() as demo:
38
  gr.Markdown("# 回答选择系统")
39
 
40
- with gr.Row():
41
- with gr.Column():
42
- message = gr.Textbox(label="输入你的问题")
43
- system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
44
- max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
45
- temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
46
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
47
- generate_button = gr.Button("生成回答")
48
 
49
- choice = gr.Radio(["回答 1", "回答 2"], label="选择更喜欢的回答")
50
- submit_button = gr.Button("提交选择")
51
- output = gr.Textbox(label="选择结果")
52
 
53
  history = gr.State([]) # 用于存储对话历史
54
 
55
- chat_history = gr.Chatbot(label="聊天历史")
56
-
57
- generate_button.click(
58
  fn=respond,
59
  inputs=[message, system_message, max_tokens, temperature, top_p, history],
60
- outputs=[chat_history, chat_history, history]
61
  )
62
 
63
- submit_button.click(
 
 
 
64
  fn=vote,
65
- inputs=[choice, chat_history, chat_history, history],
66
- outputs=[chat_history, output]
67
  )
68
 
69
  if __name__ == "__main__":
 
5
 
6
  def respond(message, system_message, max_tokens, temperature, top_p, history):
7
  messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}]
8
+
9
  response1, response2 = "", ""
10
 
11
  for message in client.chat_completion(
 
28
  token = message.choices[0].delta.content
29
  response2 += token
30
 
31
+ history.append(("user", message))
32
+ history.append(("assistant_1", response1))
33
+ history.append(("assistant_2", response2))
34
+
35
+ return history, history
36
 
37
+ def vote(history, choice):
38
+ selected_response = history[-2][1] if choice == "回答 1" else history[-1][1]
39
+ history.append(("user_selected", selected_response))
40
+ return history
41
 
42
  with gr.Blocks() as demo:
43
  gr.Markdown("# 回答选择系统")
44
 
45
+ system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
46
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
47
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
48
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
 
 
 
 
49
 
50
+ chat_history = gr.Chatbot(label="聊天历史")
51
+ message = gr.Textbox(label="输入你的问题")
 
52
 
53
  history = gr.State([]) # 用于存储对话历史
54
 
55
+ submit_button = gr.Button("发送")
56
+ submit_button.click(
 
57
  fn=respond,
58
  inputs=[message, system_message, max_tokens, temperature, top_p, history],
59
+ outputs=[chat_history, history]
60
  )
61
 
62
+ choice = gr.Radio(["回答 1", "回答 2"], label="选择更喜欢的回答")
63
+ vote_button = gr.Button("提交选择")
64
+
65
+ vote_button.click(
66
  fn=vote,
67
+ inputs=[history, choice],
68
+ outputs=[chat_history]
69
  )
70
 
71
  if __name__ == "__main__":