elfsong
commited on
Commit
·
41ad8f7
1
Parent(s):
1e1cd96
feat: Refactor chatbot functionality to support multiple models and improve user interaction with a new input system.
Browse files
app.py
CHANGED
|
@@ -2,77 +2,101 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
MODELS = [
|
| 5 |
-
"
|
| 6 |
-
"
|
| 7 |
]
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
hf_token: gr.OAuthToken,
|
| 18 |
-
):
|
| 19 |
-
"""
|
| 20 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 21 |
-
"""
|
| 22 |
-
client = InferenceClient(token=hf_token.token, model=model_name)
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
temperature=temperature,
|
| 37 |
-
top_p=top_p,
|
| 38 |
-
):
|
| 39 |
-
choices = message.choices
|
| 40 |
-
token = ""
|
| 41 |
-
if len(choices) and choices[0].delta.content:
|
| 42 |
-
token = choices[0].delta.content
|
| 43 |
|
| 44 |
-
|
| 45 |
-
yield response
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
""
|
| 51 |
-
chatbot = gr.ChatInterface(
|
| 52 |
-
respond,
|
| 53 |
-
type="messages",
|
| 54 |
-
additional_inputs=[
|
| 55 |
-
gr.Dropdown(
|
| 56 |
-
MODELS, label="Select Model", value=MODELS[0], interactive=True
|
| 57 |
-
),
|
| 58 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 59 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 60 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 61 |
-
gr.Slider(
|
| 62 |
-
minimum=0.1,
|
| 63 |
-
maximum=1.0,
|
| 64 |
-
value=0.95,
|
| 65 |
-
step=0.05,
|
| 66 |
-
label="Top-p (nucleus sampling)",
|
| 67 |
-
),
|
| 68 |
-
],
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
|
|
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
-
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
MODELS = [
|
| 5 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
| 6 |
+
"meta-llama/Llama-3.1-8B-Instruct",
|
| 7 |
]
|
| 8 |
|
| 9 |
+
def user(user_message, history_a, history_b):
|
| 10 |
+
if not user_message:
|
| 11 |
+
return "", history_a, history_b
|
| 12 |
+
|
| 13 |
+
user_msg = {"role": "user", "content": user_message}
|
| 14 |
+
|
| 15 |
+
return "", history_a + [user_msg], history_b + [user_msg]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def bot(history, model_name, system_message, max_tokens, temperature, top_p, oauth_token: gr.OAuthToken | None):
|
| 18 |
+
if not history:
|
| 19 |
+
yield history
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
if oauth_token is None:
|
| 23 |
+
history.append({"role": "assistant", "content": "⚠️ Please login via the Sidebar."})
|
| 24 |
+
yield history
|
| 25 |
+
return
|
| 26 |
|
| 27 |
+
client = InferenceClient(token=oauth_token.token, model=model_name)
|
| 28 |
+
|
| 29 |
+
# Prepend system message for the API call
|
| 30 |
+
messages = [{"role": "system", "content": system_message}] + history
|
| 31 |
+
|
| 32 |
+
# Initialize assistant message
|
| 33 |
+
history.append({"role": "assistant", "content": ""})
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
stream = client.chat_completion(
|
| 37 |
+
messages,
|
| 38 |
+
max_tokens=max_tokens,
|
| 39 |
+
stream=True,
|
| 40 |
+
temperature=temperature,
|
| 41 |
+
top_p=top_p,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
response_text = ""
|
| 45 |
+
for chunk in stream:
|
| 46 |
+
token_content = chunk.choices[0].delta.content or ""
|
| 47 |
+
response_text += token_content
|
| 48 |
+
history[-1]["content"] = response_text
|
| 49 |
+
yield history
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
history[-1]["content"] = f"**Error:** {str(e)}"
|
| 53 |
+
yield history
|
| 54 |
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
with gr.Sidebar():
|
| 57 |
+
gr.Markdown("## Configuration")
|
| 58 |
+
gr.LoginButton() # 'scopes' argument removed in Gradio 6
|
| 59 |
+
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
|
| 60 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
| 61 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 62 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
gr.Markdown("# Chatbot Arena (Gradio 6.x)")
|
|
|
|
| 65 |
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
model_a = gr.Dropdown(MODELS, label="Model A", value=MODELS[0])
|
| 69 |
+
chatbot_a = gr.Chatbot(label="Model A Output") # 'type' argument removed
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
model_b = gr.Dropdown(MODELS, label="Model B", value=MODELS[1])
|
| 73 |
+
chatbot_b = gr.Chatbot(label="Model B Output")
|
| 74 |
|
| 75 |
+
with gr.Row():
|
| 76 |
+
msg = gr.Textbox(scale=4, label="Input Message", autofocus=True)
|
| 77 |
+
clear_btn = gr.Button("Clear Chat", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Event handlers
|
| 80 |
+
submit_event = msg.submit(
|
| 81 |
+
user,
|
| 82 |
+
[msg, chatbot_a, chatbot_b],
|
| 83 |
+
[msg, chatbot_a, chatbot_b],
|
| 84 |
+
queue=False
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
submit_event.then(
|
| 88 |
+
bot,
|
| 89 |
+
[chatbot_a, model_a, system_message, max_tokens, temperature, top_p],
|
| 90 |
+
[chatbot_a]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
submit_event.then(
|
| 94 |
+
bot,
|
| 95 |
+
[chatbot_b, model_b, system_message, max_tokens, temperature, top_p],
|
| 96 |
+
[chatbot_b]
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
clear_btn.click(lambda: ([], []), None, [chatbot_a, chatbot_b])
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch(debug=True)
|