Spaces:
Sleeping
Sleeping
File size: 1,535 Bytes
4df21d7 6418181 86e568e 6418181 c35b7da 6418181 0e1ebd8 6418181 c35b7da 6418181 c35b7da 0e1ebd8 86e568e c35b7da 0e1ebd8 86e568e c35b7da 138f585 86e568e 138f585 6418181 86e568e 0e1ebd8 6418181 138f585 c35b7da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import gradio as gr
def reply(user_msg: str, history: list, name: str):
name = name or "friend"
assistant_msg = f"Nice to meet you, {name}! You said: {user_msg}"
history = history + [
{"role": "user", "content": user_msg},
{"role": "assistant", "content": assistant_msg},
]
return history, "" # clear the textbox
def set_name_and_greet(name: str):
name = name or "friend"
greeting = [
{"role": "assistant", "content": f"Hi, {name}! How can I help you?"}
]
return name, greeting # matches the two outputs
with gr.Blocks() as demo:
name_state = gr.State("") # remembers the chosen name
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…")
set_btn = gr.Button("Set Name")
gr.Markdown("_After the greeting appears, start chatting →_")
with gr.Column(scale=2):
chatbot = gr.Chatbot(label="Chat", type="messages")
user_text = gr.Textbox(placeholder="Ask me something…")
set_btn.click(
fn=set_name_and_greet,
inputs=name_input,
outputs=[name_state, chatbot], # update state & chatbot history
show_progress=False,
)
user_text.submit(
fn=reply,
inputs=[user_text, chatbot, name_state], # latest msg, history, name
outputs=[chatbot, user_text], # update history & clear input
)
demo.launch() |