File size: 1,121 Bytes
cd2ce2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)