Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
-
import gradio as gr
|
2 |
from datetime import datetime
|
3 |
import random
|
4 |
import uuid
|
|
|
5 |
|
6 |
# Shared state to store messages
|
7 |
messages = []
|
@@ -9,56 +9,34 @@ messages = []
|
|
9 |
# Dictionary to store user colors
|
10 |
user_colors = {}
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def get_user_color(user_id):
|
13 |
if user_id not in user_colors:
|
14 |
user_colors[user_id] = f"#{random.randint(0, 0xFFFFFF):06x}"
|
15 |
return user_colors[user_id]
|
16 |
|
17 |
-
def chat(
|
18 |
global messages
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
user_id = str(uuid.uuid4())
|
23 |
|
24 |
# Add the new message to the shared state
|
25 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
26 |
user_color = get_user_color(user_id)
|
27 |
-
messages.append(
|
28 |
-
|
29 |
-
# Return the updated chat history and user_id
|
30 |
-
return "", messages, user_id
|
31 |
-
|
32 |
-
def get_updates(history):
|
33 |
-
global messages
|
34 |
-
|
35 |
-
# Check if there are new messages
|
36 |
-
if len(messages) > len(history):
|
37 |
-
return messages
|
38 |
-
|
39 |
-
# If no new messages, return the current history
|
40 |
-
return history
|
41 |
-
|
42 |
-
# Custom CSS to hide the loading animation
|
43 |
-
custom_css = """
|
44 |
-
#chatbot .loading {
|
45 |
-
display: none !important;
|
46 |
-
}
|
47 |
-
"""
|
48 |
-
|
49 |
-
# Create the Gradio interface
|
50 |
-
with gr.Blocks(css=custom_css) as demo:
|
51 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
52 |
-
msg = gr.Textbox(label="Type your message here")
|
53 |
-
clear = gr.Button("Clear")
|
54 |
-
user_id = gr.State(value='') # Add a state component to store the user ID
|
55 |
-
|
56 |
-
msg.submit(chat, [msg, chatbot, user_id], [msg, chatbot, user_id])
|
57 |
-
clear.click(lambda: [], outputs=[chatbot])
|
58 |
|
59 |
-
#
|
60 |
-
|
|
|
61 |
|
62 |
-
#
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
|
|
1 |
from datetime import datetime
|
2 |
import random
|
3 |
import uuid
|
4 |
+
from dataclasses import dataclass
|
5 |
|
6 |
# Shared state to store messages
|
7 |
messages = []
|
|
|
9 |
# Dictionary to store user colors
|
10 |
user_colors = {}
|
11 |
|
12 |
+
@dataclass
|
13 |
+
class Data:
|
14 |
+
user_id: str
|
15 |
+
message: str
|
16 |
+
timestamp: str
|
17 |
+
|
18 |
def get_user_color(user_id):
|
19 |
if user_id not in user_colors:
|
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
|
27 |
+
user_id, message = input_text.split("; ")
|
|
|
28 |
|
29 |
# Add the new message to the shared state
|
30 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
31 |
user_color = get_user_color(user_id)
|
32 |
+
messages.append(Data(user_id=user_id, message=message, timestamp=timestamp))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# Print the updated chat history
|
35 |
+
for msg in messages:
|
36 |
+
print(f"User_{msg.user_id[:4]}: {msg.message} ({msg.timestamp})")
|
37 |
|
38 |
+
# Example usage
|
39 |
if __name__ == "__main__":
|
40 |
+
while True:
|
41 |
+
input_text = input("Enter your message (format: 'id here; text here'): ")
|
42 |
+
chat(input_text)
|