elinstallation commited on
Commit
0a35893
·
verified ·
1 Parent(s): b514635

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -2
app.py CHANGED
@@ -197,10 +197,22 @@ with gr.Blocks(
197
  color_accent_soft='*secondary_200',
198
  color_accent_soft_dark='*secondary_800',
199
  button_primary_background_fill='*secondary_100',
200
- button_primary_background_fill_dark='*secondary_600'
 
 
 
 
201
  )
202
 
203
  ) as demo:
 
 
 
 
 
 
 
 
204
  gr.ChatInterface(
205
  fn=respond,
206
  title="Accessible Intelligence Hub",
@@ -210,7 +222,46 @@ with gr.Blocks(
210
  with gr.Row(scale=1):
211
  chatbot_topic=gr.CheckboxGroup(["Academia", "Extracurriculars", "Time Management", "Financial Aid"], label="What would you like advice about?")
212
 
213
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  #launching chatbot
215
  demo.launch()
216
 
 
197
  color_accent_soft='*secondary_200',
198
  color_accent_soft_dark='*secondary_800',
199
  button_primary_background_fill='*secondary_100',
200
+ button_primary_background_fill_dark='*secondary_600',
201
+ button_primary_background_fill_dark='*secondary_800',
202
+ button_primary_text_color='white',
203
+ button_primary_border_color='*secondary_700',
204
+ button_primary_border_color_dark='*secondary_900'
205
  )
206
 
207
  ) as demo:
208
+ with gr.Row():
209
+ save_button = gr.Button("💾 Save Chat History",
210
+ variant="primary",
211
+ size="sm")
212
+ download_button = gr.File(interactive=False,
213
+ visible=False,
214
+ elem_classes=["download-btn"])
215
+
216
  gr.ChatInterface(
217
  fn=respond,
218
  title="Accessible Intelligence Hub",
 
222
  with gr.Row(scale=1):
223
  chatbot_topic=gr.CheckboxGroup(["Academia", "Extracurriculars", "Time Management", "Financial Aid"], label="What would you like advice about?")
224
 
225
+ demo.css = """
226
+ .download-btn {
227
+ min-width: 200px !important;
228
+ }
229
+ .download-btn .gr-button {
230
+ background: var(--button-primary-background-fill) !important;
231
+ color: var(--button-primary-text-color) !important;
232
+ }
233
+ """
234
+
235
+ def save_chat_history(history, username):
236
+ if not username:
237
+ username = "anonymous"
238
+
239
+ timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
240
+ filename = f"chat_history_{username}_{timestamp}.txt"
241
+
242
+ with open(filename, "w", encoding="utf-8") as f:
243
+ f.write(f"Chat History for {username} - {timestamp}\n\n")
244
+ for exchange in history:
245
+ if isinstance(exchange, tuple) and len(exchange) == 2:
246
+ user_msg, bot_msg = exchange
247
+ f.write(f"User: {user_msg}\n")
248
+ f.write(f"Bot: {bot_msg}\n\n")
249
+ elif isinstance(exchange, dict):
250
+ # Handle dictionary format if needed
251
+ role = exchange.get("role", "unknown")
252
+ content = exchange.get("content", "")
253
+ f.write(f"{role.capitalize()}: {content}\n\n")
254
+
255
+ return filename
256
+
257
+ save_button.click(
258
+ fn=save_chat_history,
259
+ inputs=[chat_interface.chatbot],
260
+ outputs=download_button
261
+ ).then(
262
+ lambda: gr.update(visible=True),
263
+ outputs=download_button
264
+ )
265
  #launching chatbot
266
  demo.launch()
267