Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
def vote(
|
| 34 |
-
selected_response =
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
gr.Markdown("# 回答选择系统")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
output = gr.Textbox(label="选择结果")
|
| 52 |
|
| 53 |
history = gr.State([]) # 用于存储对话历史
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
generate_button.click(
|
| 58 |
fn=respond,
|
| 59 |
inputs=[message, system_message, max_tokens, temperature, top_p, history],
|
| 60 |
-
outputs=[chat_history,
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
fn=vote,
|
| 65 |
-
inputs=[
|
| 66 |
-
outputs=[chat_history
|
| 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__":
|