File size: 1,091 Bytes
5c4b237
fe472d7
 
5c4b237
fe472d7
 
5c4b237
fe472d7
 
 
 
 
 
 
 
 
5c4b237
fe472d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from datetime import datetime
import time

# Shared state to store messages
messages = []

def chat(message, history):
    global messages
    
    # Add the new message to the shared state
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    messages.append(f"[{timestamp}] {message}")
    
    # Return the updated chat history
    return "", messages

def get_updates(history):
    global messages
    
    # Check if there are new messages
    if len(messages) > len(history):
        return messages
    
    # If no new messages, return the current history
    return history

# Create the Gradio interface
with gr.Blocks() as demo:
    chatbot = gr.Chatbot(label="Chat History")
    msg = gr.Textbox(label="Type your message here")
    clear = gr.Button("Clear")

    msg.submit(chat, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: [], outputs=[chatbot])
    
    # Add an update function that runs every second
    demo.load(get_updates, inputs=chatbot, outputs=chatbot, every=1)

# Launch the app
if __name__ == "__main__":
    demo.launch()