Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from datetime import datetime
|
2 |
import random
|
3 |
import uuid
|
@@ -20,7 +21,7 @@ def get_user_color(user_id):
|
|
20 |
user_colors[user_id] = f"#{random.randint(0, 0xFFFFFF):06x}"
|
21 |
return user_colors[user_id]
|
22 |
|
23 |
-
def chat(input_text):
|
24 |
global messages
|
25 |
|
26 |
# Parse the input to get user_id and message
|
@@ -31,12 +32,32 @@ def chat(input_text):
|
|
31 |
user_color = get_user_color(user_id)
|
32 |
messages.append(Data(user_id=user_id, message=message, timestamp=timestamp))
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if __name__ == "__main__":
|
40 |
-
|
41 |
-
input_text = input("Enter your message (format: 'id here; text here'): ")
|
42 |
-
chat(input_text)
|
|
|
1 |
+
import gradio as gr
|
2 |
from datetime import datetime
|
3 |
import random
|
4 |
import uuid
|
|
|
21 |
user_colors[user_id] = f"#{random.randint(0, 0xFFFFFF):06x}"
|
22 |
return user_colors[user_id]
|
23 |
|
24 |
+
def chat(input_text, history):
|
25 |
global messages
|
26 |
|
27 |
# Parse the input to get user_id and message
|
|
|
32 |
user_color = get_user_color(user_id)
|
33 |
messages.append(Data(user_id=user_id, message=message, timestamp=timestamp))
|
34 |
|
35 |
+
# Update the chat history
|
36 |
+
history.append(f"User_{user_id[:4]}: {message} ({timestamp})")
|
37 |
+
return "", history
|
38 |
|
39 |
+
def get_updates(history):
|
40 |
+
global messages
|
41 |
+
|
42 |
+
# Check if there are new messages
|
43 |
+
if len(messages) > len(history):
|
44 |
+
return [f"User_{msg.user_id[:4]}: {msg.message} ({msg.timestamp})" for msg in messages]
|
45 |
+
|
46 |
+
# If no new messages, return the current history
|
47 |
+
return history
|
48 |
+
|
49 |
+
# Create the Gradio interface
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
chatbot = gr.Chatbot()
|
52 |
+
msg = gr.Textbox(label="Type your message here (format: 'id here; text here')")
|
53 |
+
clear = gr.Button("Clear")
|
54 |
+
|
55 |
+
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
56 |
+
clear.click(lambda: [], outputs=[chatbot])
|
57 |
+
|
58 |
+
# Add an update function that runs every 0.2 seconds
|
59 |
+
demo.load(get_updates, inputs=chatbot, outputs=chatbot, every=0.2)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
if __name__ == "__main__":
|
63 |
+
demo.launch()
|
|
|
|