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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -1,15 +1,28 @@
1
  import gradio as gr
2
  from datetime import datetime
 
3
 
4
  # Shared state to store messages
5
  messages = []
6
 
 
 
 
 
 
 
 
 
7
  def chat(message, history):
8
  global messages
9
 
 
 
 
10
  # Add the new message to the shared state
11
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
12
- messages.append([f"User ({timestamp})", message])
 
13
 
14
  # Return the updated chat history
15
  return "", messages
@@ -25,8 +38,8 @@ def get_updates(history):
25
  return history
26
 
27
  # Create the Gradio interface
28
- with gr.Blocks() as demo:
29
- chatbot = gr.Chatbot(label="Chat History")
30
  msg = gr.Textbox(label="Type your message here")
31
  clear = gr.Button("Clear")
32
 
 
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):
12
+ if user_id not in user_colors:
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
 
38
  return history
39
 
40
  # Create the Gradio interface
41
+ 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