vericudebuget commited on
Commit
cc20725
·
verified ·
1 Parent(s): 04b984b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import gradio as gr
2
  from datetime import datetime
3
  import random
 
4
 
5
  # Shared state to store messages
6
  messages = []
7
 
8
- # Generate a random color for each user
9
  user_colors = {}
10
 
11
  def get_user_color(user_id):
@@ -13,19 +14,20 @@ def get_user_color(user_id):
13
  user_colors[user_id] = f"#{random.randint(0, 0xFFFFFF):06x}"
14
  return user_colors[user_id]
15
 
16
- def chat(message, history):
17
  global messages
18
 
19
- # Generate a unique user ID (in a real app, this would be managed by authentication)
20
- user_id = f"User_{len(user_colors) + 1}"
 
21
 
22
  # Add the new message to the shared state
23
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
24
  user_color = get_user_color(user_id)
25
- messages.append([None, f"<span style='color: {user_color};'>{user_id}</span>: {message}<br><small>{timestamp}</small>"])
26
 
27
- # Return the updated chat history
28
- return "", messages
29
 
30
  def get_updates(history):
31
  global messages
@@ -42,12 +44,13 @@ with gr.Blocks(css="#chatbot {height: 400px; overflow-y: auto;}") as demo:
42
  chatbot = gr.Chatbot(elem_id="chatbot")
43
  msg = gr.Textbox(label="Type your message here")
44
  clear = gr.Button("Clear")
 
45
 
46
- msg.submit(chat, [msg, chatbot], [msg, chatbot])
47
  clear.click(lambda: [], outputs=[chatbot])
48
 
49
  # Add an update function that runs every second
50
- demo.load(get_updates, inputs=chatbot, outputs=chatbot, every=1)
51
 
52
  # Launch the app
53
  if __name__ == "__main__":
 
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 = []
8
 
9
+ # Dictionary to store user colors
10
  user_colors = {}
11
 
12
  def get_user_color(user_id):
 
14
  user_colors[user_id] = f"#{random.randint(0, 0xFFFFFF):06x}"
15
  return user_colors[user_id]
16
 
17
+ def chat(message, history, user_id):
18
  global messages
19
 
20
+ if not user_id:
21
+ # Generate a new user ID if one doesn't exist
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([None, f"<span style='color: {user_color};'>User_{user_id[:4]}</span>: {message}<br><small>{timestamp}</small>"])
28
 
29
+ # Return the updated chat history and user_id
30
+ return "", messages, user_id
31
 
32
  def get_updates(history):
33
  global messages
 
44
  chatbot = gr.Chatbot(elem_id="chatbot")
45
  msg = gr.Textbox(label="Type your message here")
46
  clear = gr.Button("Clear")
47
+ user_id = gr.State(value='') # Add a state component to store the user ID
48
 
49
+ msg.submit(chat, [msg, chatbot, user_id], [msg, chatbot, user_id])
50
  clear.click(lambda: [], outputs=[chatbot])
51
 
52
  # Add an update function that runs every second
53
+ demo.load(get_updates, inputs=chatbot, outputs=chatbot, every=0.05)
54
 
55
  # Launch the app
56
  if __name__ == "__main__":