Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
response = requests.post(url, json={"message": message})
|
7 |
-
return response.text
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from datetime import datetime
|
3 |
+
import time
|
4 |
|
5 |
+
# Shared state to store messages
|
6 |
+
messages = []
|
|
|
|
|
7 |
|
8 |
+
def chat(message, history):
|
9 |
+
global messages
|
10 |
+
|
11 |
+
# Add the new message to the shared state
|
12 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
13 |
+
messages.append(f"[{timestamp}] {message}")
|
14 |
+
|
15 |
+
# Return the updated chat history
|
16 |
+
return "", messages
|
17 |
|
18 |
+
def get_updates(history):
|
19 |
+
global messages
|
20 |
+
|
21 |
+
# Check if there are new messages
|
22 |
+
if len(messages) > len(history):
|
23 |
+
return messages
|
24 |
+
|
25 |
+
# If no new messages, return the current history
|
26 |
+
return history
|
27 |
+
|
28 |
+
# Create the Gradio interface
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
chatbot = gr.Chatbot(label="Chat History")
|
31 |
+
msg = gr.Textbox(label="Type your message here")
|
32 |
+
clear = gr.Button("Clear")
|
33 |
+
|
34 |
+
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
35 |
+
clear.click(lambda: [], outputs=[chatbot])
|
36 |
+
|
37 |
+
# Add an update function that runs every second
|
38 |
+
demo.load(get_updates, inputs=chatbot, outputs=chatbot, every=1)
|
39 |
+
|
40 |
+
# Launch the app
|
41 |
+
if __name__ == "__main__":
|
42 |
+
demo.launch()
|