tester
Browse files
app.py
CHANGED
|
@@ -1,84 +1,96 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
|
|
|
|
|
|
| 5 |
theme = gr.themes.Soft(primary_hue="pink", secondary_hue="blue")
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
response = client.chat_completion(
|
| 16 |
messages,
|
| 17 |
max_tokens=1000
|
| 18 |
)
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
name = name or "friend"
|
| 24 |
-
assistant_msg = f"Nice to meet you, {name}!"
|
| 25 |
-
|
| 26 |
-
history = history + [
|
| 27 |
-
{"role": "user", "content": user_msg},
|
| 28 |
-
{"role": "assistant", "content": assistant_msg},
|
| 29 |
-
]
|
| 30 |
-
return history, "" # clear the textbox
|
| 31 |
|
| 32 |
def set_name_and_greet(name: str):
|
| 33 |
name = name or "friend"
|
| 34 |
greeting = [
|
| 35 |
-
{"role": "assistant", "content": f"Hi, {name}! How can I help you?"}
|
| 36 |
]
|
| 37 |
-
return name, greeting
|
| 38 |
|
|
|
|
| 39 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 40 |
name_state = gr.State("") # remembers the chosen name
|
| 41 |
-
|
| 42 |
with gr.Row():
|
|
|
|
| 43 |
with gr.Column(scale=1):
|
| 44 |
gr.Markdown("### Enter your name, then click **Set Name**")
|
| 45 |
-
name_input = gr.Textbox(placeholder="Type your name here…")
|
| 46 |
-
set_btn = gr.Button("Set Name")
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
with gr.Column(scale=2):
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
set_btn.click(
|
| 54 |
fn=set_name_and_greet,
|
| 55 |
inputs=name_input,
|
| 56 |
-
outputs=[name_state, chatbot],
|
| 57 |
show_progress=False,
|
| 58 |
)
|
| 59 |
-
|
|
|
|
| 60 |
user_text.submit(
|
| 61 |
fn=reply,
|
| 62 |
-
inputs=[user_text, chatbot, name_state],
|
| 63 |
-
outputs=[chatbot, user_text],
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
),
|
| 74 |
-
|
| 75 |
-
examples=[
|
| 76 |
-
"What kind of things can we talk about?",
|
| 77 |
-
"I need some advice on how to be a better friend.",
|
| 78 |
-
"Can you tell me a funny joke to cheer me up?",
|
| 79 |
-
"What should I do if I'm feeling a bit lonely today?"
|
| 80 |
-
]
|
| 81 |
-
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch(theme=theme)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize the Hugging Face Inference Client
|
| 5 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 6 |
+
|
| 7 |
+
# Define the custom theme
|
| 8 |
theme = gr.themes.Soft(primary_hue="pink", secondary_hue="blue")
|
| 9 |
+
|
| 10 |
+
def reply(user_msg: str, history: list, name: str):
|
| 11 |
+
name = name or "friend"
|
| 12 |
|
| 13 |
+
# 1. Initialize history as a list if it's empty/None
|
| 14 |
+
if history is None:
|
| 15 |
+
history = []
|
| 16 |
|
| 17 |
+
# 2. Build the message log for the Hugging Face API
|
| 18 |
+
# We include a custom system prompt that subtly reminds the model of their name
|
| 19 |
+
messages = [{"role": "system", "content": f"You are a helpful, friendly AI companion named Mingle. The user's name is {name}."}]
|
| 20 |
+
messages.extend(history)
|
| 21 |
+
messages.append({"role": "user", "content": user_msg})
|
| 22 |
|
| 23 |
+
# 3. Call the Hugging Face API
|
| 24 |
response = client.chat_completion(
|
| 25 |
messages,
|
| 26 |
max_tokens=1000
|
| 27 |
)
|
| 28 |
+
bot_message = response.choices[0].message.content.strip()
|
| 29 |
|
| 30 |
+
# 4. Append the new conversation turn to history to update the UI
|
| 31 |
+
history.append({"role": "user", "content": user_msg})
|
| 32 |
+
history.append({"role": "assistant", "content": bot_message})
|
| 33 |
|
| 34 |
+
return history, "" # returns updated history and clears the text box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def set_name_and_greet(name: str):
|
| 37 |
name = name or "friend"
|
| 38 |
greeting = [
|
| 39 |
+
{"role": "assistant", "content": f"Hi, {name}! I'm Mingle, your AI friend. How can I help you today? 😊"}
|
| 40 |
]
|
| 41 |
+
return name, greeting
|
| 42 |
|
| 43 |
+
# Build the Gradio interface
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("# AI Friend: Mingle\nEver need a friend, Mingle's got you!")
|
| 46 |
+
|
| 47 |
name_state = gr.State("") # remembers the chosen name
|
| 48 |
+
|
| 49 |
with gr.Row():
|
| 50 |
+
# Left column for settings
|
| 51 |
with gr.Column(scale=1):
|
| 52 |
gr.Markdown("### Enter your name, then click **Set Name**")
|
| 53 |
+
name_input = gr.Textbox(placeholder="Type your name here…", label="Your Name")
|
| 54 |
+
set_btn = gr.Button("Set Name", variant="primary")
|
| 55 |
+
|
| 56 |
+
gr.Markdown("---")
|
| 57 |
+
gr.Markdown("### Try asking me:")
|
| 58 |
+
# Use standard buttons for examples since we are customizing the layout via Blocks
|
| 59 |
+
example1 = gr.Button("What kind of things can we talk about?", variant="secondary")
|
| 60 |
+
example2 = gr.Button("I need some advice on how to be a better friend.", variant="secondary")
|
| 61 |
+
example3 = gr.Button("What should I do if I'm feeling a bit lonely today?", variant="secondary")
|
| 62 |
+
|
| 63 |
+
# Right column for the main chat window
|
| 64 |
with gr.Column(scale=2):
|
| 65 |
+
# Removed `type="messages"` to fix your exact TypeError crash
|
| 66 |
+
chatbot = gr.Chatbot(
|
| 67 |
+
label="Chat",
|
| 68 |
+
value=[{"role": "assistant", "content": "Hi there! What's your name, and how are you doing today? 😊"}]
|
| 69 |
+
)
|
| 70 |
+
user_text = gr.Textbox(placeholder="Ask me something…", label="Message Mingle")
|
| 71 |
|
| 72 |
+
# Set name and greet button behavior
|
| 73 |
set_btn.click(
|
| 74 |
fn=set_name_and_greet,
|
| 75 |
inputs=name_input,
|
| 76 |
+
outputs=[name_state, chatbot],
|
| 77 |
show_progress=False,
|
| 78 |
)
|
| 79 |
+
|
| 80 |
+
# Chat submit behavior
|
| 81 |
user_text.submit(
|
| 82 |
fn=reply,
|
| 83 |
+
inputs=[user_text, chatbot, name_state],
|
| 84 |
+
outputs=[chatbot, user_text],
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# Connect example buttons to pass their text into the reply function directly
|
| 88 |
+
for btn in [example1, example2, example3]:
|
| 89 |
+
btn.click(
|
| 90 |
+
fn=reply,
|
| 91 |
+
inputs=[btn, chatbot, name_state],
|
| 92 |
+
outputs=[chatbot, user_text]
|
| 93 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
demo.launch(theme=theme)
|