Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
def chat(message, history):
|
| 5 |
+
"""
|
| 6 |
+
Simple echo chatbot that appends a timestamp.
|
| 7 |
+
Replace with your own LLM or logic.
|
| 8 |
+
"""
|
| 9 |
+
bot_message = f"Echo: {message} ({time.strftime('%X')})"
|
| 10 |
+
history = history or []
|
| 11 |
+
history.append((message, bot_message))
|
| 12 |
+
return history, "" # empty string clears the textbox
|
| 13 |
+
|
| 14 |
+
with gr.Blocks() as demo:
|
| 15 |
+
gr.Markdown("# Gradio Chatbot Demo")
|
| 16 |
+
|
| 17 |
+
chatbot = gr.Chatbot(label="Conversation")
|
| 18 |
+
msg = gr.Textbox(label="Type a message…", lines=1)
|
| 19 |
+
|
| 20 |
+
def user(user_message, history):
|
| 21 |
+
history = history or []
|
| 22 |
+
history.append((user_message, "")) # user side message
|
| 23 |
+
return history, "" # empty textbox
|
| 24 |
+
|
| 25 |
+
def bot(history):
|
| 26 |
+
user_message = history[-1][0] # last user message
|
| 27 |
+
bot_message = chat(user_message, history)[0][-1][1] # reuse chat fn
|
| 28 |
+
history[-1][1] = bot_message
|
| 29 |
+
return history
|
| 30 |
+
|
| 31 |
+
# chain events
|
| 32 |
+
msg.submit(user, [msg, chatbot], [chatbot, msg], queue=False).then(
|
| 33 |
+
bot, chatbot, chatbot
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|