sanayakrishnan's picture
final
4b27fa6 verified
Raw History Blame Contribute Delete
3.18 kB
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
theme = gr.themes.Soft(primary_hue="pink")
def reply(user_msg: str, history: list, name: str):
name = name or "friend"
if history is None:
history = []
messages = [{"role": "system", "content": f"You are a helpful, friendly AI companion named Mingle. The user's name is {name}."}]
messages.extend(history)
messages.append({"role": "user", "content": user_msg})
response = client.chat_completion(
messages,
max_tokens=1000
)
bot_message = response.choices[0].message.content.strip()
history.append({"role": "user", "content": user_msg})
history.append({"role": "assistant", "content": bot_message})
return history, ""
def set_name_and_greet(name: str):
name = name or "friend"
greeting = [
{"role": "assistant", "content": f"Hi, {name}! I'm Mingle, your AI friend. How can I help you today? 😊"}
]
return name, greeting
with gr.Blocks() as demo:
gr.Markdown("# AI Friend: Mingle\nEver need a friend, Mingle's got you!")
name_state = gr.State("")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Enter your name, then click **Set Name**")
name_input = gr.Textbox(placeholder="Type your name here…", label="Your Name")
set_btn = gr.Button("Set Name", variant="primary")
gr.Markdown("---")
gr.Markdown("### Try asking me:")
example1 = gr.Button("What kind of things can we talk about?", variant="secondary")
example2 = gr.Button("I need some advice on how to be a better friend.", variant="secondary")
example3 = gr.Button("What should I do if I'm feeling a bit lonely today?", variant="secondary")
example4 = gr.Button("How do I stop being negative?", variant="secondary")
example5 = gr.Button("I'm feeling sad, make my mood?", variant="secondary")
example6 = gr.Button("what else can we dicuss more about, like friends do?" , variant="secondary")
example7 = gr.Button("Is being a good friend hard?" , variant="secondary")
with gr.Column(scale=2):
chatbot = gr.Chatbot(
label="Chat",
value=[{"role": "assistant", "content": "Hi there! What's your name, and how are you doing today? 😊"}]
)
user_text = gr.Textbox(placeholder="Ask me something…", label="Message Mingle")
set_btn.click(
fn=set_name_and_greet,
inputs=name_input,
outputs=[name_state, chatbot],
show_progress=False,
)
user_text.submit(
fn=reply,
inputs=[user_text, chatbot, name_state],
outputs=[chatbot, user_text],
)
for btn in [example1, example2, example3, example4, example5, example6, example7]:
btn.click(
fn=reply,
inputs=[btn, chatbot, name_state],
outputs=[chatbot, user_text]
)
if __name__ == "__main__":
demo.launch(theme=theme)