vericudebuget commited on
Commit
fe472d7
·
verified ·
1 Parent(s): 5c4b237

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -1,17 +1,42 @@
1
  import gradio as gr
2
- import requests
 
3
 
4
- def send_message(message, ip):
5
- url = f"http://{ip}:8000/receive"
6
- response = requests.post(url, json={"message": message})
7
- return response.text
8
 
9
- iface = gr.Interface(
10
- fn=send_message,
11
- inputs=["text", "text"],
12
- outputs="text",
13
- title="Message Sender",
14
- description="Send messages to a specified public IP."
15
- )
 
 
16
 
17
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()