pratyushmaini commited on
Commit
216da8f
·
1 Parent(s): bb36e68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -58
app.py CHANGED
@@ -47,67 +47,49 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, se
47
  print(f"Error calling model API: {e}")
48
  yield f"Sorry, there was an error: {str(e)}"
49
 
50
- # Custom CSS for branding with consistent styling and hiding footer
51
  css = """
52
- body {
53
- background-color: #f0f5fb !important; /* Light pastel blue background */
54
  }
55
-
56
- /* Gold button styling */
57
- .gold-button {
58
- background: white !important;
 
 
59
  color: #333 !important;
60
  border: 2px solid #e6c200 !important;
61
  }
62
- .gold-button:hover {
63
- background: #fff9e6 !important;
64
- box-shadow: 0 2px 5px rgba(230, 194, 0, 0.2) !important;
65
- }
66
-
67
- /* Message styling - no backgrounds */
68
- .message {
69
- background-color: transparent !important;
70
- }
71
- .message p {
72
- background-color: transparent !important;
73
  }
74
- .message-wrap {
75
- background-color: transparent !important;
76
- }
77
-
78
- /* Emoji for messages */
79
- .user .message::before {
80
- content: "👤 ";
81
- font-size: 16px;
82
- margin-right: 5px;
83
  }
84
- .bot .message::before {
85
- content: "🛡️ ";
86
- font-size: 16px;
87
- margin-right: 5px;
88
  }
89
-
90
- /* Hide footer */
91
  footer {
92
  display: none !important;
93
  }
 
 
 
94
  """
95
 
96
-
97
- with gr.Blocks(theme=gr.themes.Default(
98
- primary_hue="blue",
99
- secondary_hue="yellow", # Using yellow instead of gold which isn't available
100
- font=["Crimson Pro", "Palatino", "serif"]
101
- ), css=css) as demo:
102
- # Custom header with branding - removed the hr element
103
  gr.HTML("""
104
- <div class="app-header">
105
- <h1 class="app-title">
106
- <span class="shield">🛡️</span>
107
- <span class="safe">Safe</span>
108
- <span class="lm">Playground</span>
109
  </h1>
110
- <p class="app-subtitle">Responsible AI for everyone</p>
111
  </div>
112
  """)
113
 
@@ -147,9 +129,7 @@ with gr.Blocks(theme=gr.themes.Default(
147
  chatbot = gr.Chatbot(
148
  label="Conversation",
149
  show_label=True,
150
- # Removed avatar_images which may not be supported in some Gradio versions
151
- elem_id="chatbot",
152
- height=500
153
  )
154
  with gr.Row():
155
  user_input = gr.Textbox(
@@ -161,14 +141,11 @@ with gr.Blocks(theme=gr.themes.Default(
161
  send_button = gr.Button(
162
  "Send",
163
  scale=1,
164
- variant="primary"
165
  )
166
 
167
  with gr.Row():
168
- clear_button = gr.Button("Clear Chat", variant="secondary", elem_classes=["gold-button"])
169
-
170
- # No footer - removed
171
- gr.HTML('<div style="height: 20px;"></div>')
172
 
173
  # Fix 1: Correct event handling for the chatbot interface
174
  def user(user_message, history):
@@ -176,10 +153,13 @@ with gr.Blocks(theme=gr.themes.Default(
176
  return "", history + [[user_message, None]]
177
 
178
  def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
179
- # Get the last user message from history (with error checking)
180
  if not history or len(history) == 0:
181
  return history
 
 
182
  user_message = history[-1][0]
 
183
  # Call respond function with the message
184
  response_generator = respond(
185
  user_message,
@@ -190,12 +170,13 @@ with gr.Blocks(theme=gr.themes.Default(
190
  top_p,
191
  selected_model
192
  )
 
193
  # Update history as responses come in
194
  for response in response_generator:
195
  history[-1][1] = response
196
  yield history
197
 
198
- # Wire up the event chain - use queue=True for the bot responses
199
  user_input.submit(
200
  user,
201
  [user_input, chatbot],
@@ -220,12 +201,11 @@ with gr.Blocks(theme=gr.themes.Default(
220
  queue=True
221
  )
222
 
223
- # Clear the chat history - use a proper function instead of lambda
224
  def clear_history():
225
  return []
226
 
227
  clear_button.click(clear_history, None, chatbot, queue=False)
228
 
229
  if __name__ == "__main__":
230
- # Simple launch without parameters that might not be supported
231
  demo.launch()
 
47
  print(f"Error calling model API: {e}")
48
  yield f"Sorry, there was an error: {str(e)}"
49
 
50
+ # Custom CSS for styling
51
  css = """
52
+ body {
53
+ background-color: #f0f5fb; /* Light pastel blue background */
54
  }
55
+ .gradio-container {
56
+ margin: 0 auto;
57
+ }
58
+ /* Simple styling for buttons */
59
+ #send-btn {
60
+ background-color: white !important;
61
  color: #333 !important;
62
  border: 2px solid #e6c200 !important;
63
  }
64
+ #send-btn:hover {
65
+ background-color: #fff9e6 !important;
 
 
 
 
 
 
 
 
 
66
  }
67
+ #clear-btn {
68
+ background-color: white !important;
69
+ color: #333 !important;
70
+ border: 2px solid #e6c200 !important;
 
 
 
 
 
71
  }
72
+ #clear-btn:hover {
73
+ background-color: #fff9e6 !important;
 
 
74
  }
 
 
75
  footer {
76
  display: none !important;
77
  }
78
+ .footer {
79
+ display: none !important;
80
+ }
81
  """
82
 
83
+ with gr.Blocks(css=css) as demo:
84
+ # Custom header with branding
 
 
 
 
 
85
  gr.HTML("""
86
+ <div style="background: linear-gradient(135deg, #4a90e2, #75c6ef); padding: 15px; border-radius: 16px 16px 0 0; color: white; border-bottom: 3px solid #e6c200;">
87
+ <h1 style="font-size: 24px; font-weight: 600; margin: 0; display: flex; align-items: center; font-family: 'Palatino', serif;">
88
+ <span style="margin-right: 10px; font-size: 22px;">🛡️</span>
89
+ <span style="font-weight: 700; margin-right: 5px;">Safe</span>
90
+ <span style="font-weight: 400; letter-spacing: 1px;">Playground</span>
91
  </h1>
92
+ <p style="font-size: 14px; opacity: 0.9; margin-top: 5px;">Responsible AI for everyone</p>
93
  </div>
94
  """)
95
 
 
129
  chatbot = gr.Chatbot(
130
  label="Conversation",
131
  show_label=True,
132
+ height=400
 
 
133
  )
134
  with gr.Row():
135
  user_input = gr.Textbox(
 
141
  send_button = gr.Button(
142
  "Send",
143
  scale=1,
144
+ elem_id="send-btn"
145
  )
146
 
147
  with gr.Row():
148
+ clear_button = gr.Button("Clear Chat", elem_id="clear-btn")
 
 
 
149
 
150
  # Fix 1: Correct event handling for the chatbot interface
151
  def user(user_message, history):
 
153
  return "", history + [[user_message, None]]
154
 
155
  def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
156
+ # Ensure there's history
157
  if not history or len(history) == 0:
158
  return history
159
+
160
+ # Get the last user message from history
161
  user_message = history[-1][0]
162
+
163
  # Call respond function with the message
164
  response_generator = respond(
165
  user_message,
 
170
  top_p,
171
  selected_model
172
  )
173
+
174
  # Update history as responses come in
175
  for response in response_generator:
176
  history[-1][1] = response
177
  yield history
178
 
179
+ # Wire up the event chain
180
  user_input.submit(
181
  user,
182
  [user_input, chatbot],
 
201
  queue=True
202
  )
203
 
204
+ # Clear the chat history - using a proper function
205
  def clear_history():
206
  return []
207
 
208
  clear_button.click(clear_history, None, chatbot, queue=False)
209
 
210
  if __name__ == "__main__":
 
211
  demo.launch()