akhaliq's picture
akhaliq HF Staff
Upload app.py with huggingface_hub
cd2ce2c verified
Raw
History Blame Contribute Delete
1.12 kB
import gradio as gr
import time
def chat(message, history):
"""
Simple echo chatbot that appends a timestamp.
Replace with your own LLM or logic.
"""
bot_message = f"Echo: {message} ({time.strftime('%X')})"
history = history or []
history.append((message, bot_message))
return history, "" # empty string clears the textbox
with gr.Blocks() as demo:
gr.Markdown("# Gradio Chatbot Demo")
chatbot = gr.Chatbot(label="Conversation")
msg = gr.Textbox(label="Type a message…", lines=1)
def user(user_message, history):
history = history or []
history.append((user_message, "")) # user side message
return history, "" # empty textbox
def bot(history):
user_message = history[-1][0] # last user message
bot_message = chat(user_message, history)[0][-1][1] # reuse chat fn
history[-1][1] = bot_message
return history
# chain events
msg.submit(user, [msg, chatbot], [chatbot, msg], queue=False).then(
bot, chatbot, chatbot
)
demo.queue().launch(server_name="0.0.0.0", server_port=7860)