SamCChauhan commited on
Commit
3169ed9
·
unverified ·
1 Parent(s): b88acf5

Enhance transcript download functionality

Browse files

Added error handling and notification for saving chat history.

Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -267,16 +267,31 @@ def main_page():
267
  ui.separator().classes('my-4')
268
 
269
  def download_transcript():
270
- if not chat_history: return
 
 
 
271
  transcript_text = "DACODEX MENTOR SESSION\n" + "="*30 + "\n\n"
272
  for msg in chat_history:
273
  prefix = "STUDENT" if msg["role"] == "user" else "MENTOR"
274
  transcript_text += f"{prefix}:\n{msg['raw_text']}\n\n"
275
 
276
- # Create file bytes for download
277
  filename = f"DACodeX_Transcript_{datetime.datetime.now().strftime('%Y%m%d_%H%M')}.txt"
278
- file_bytes = transcript_text.encode('utf-8')
279
- ui.download(file_bytes, filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
  ui.button("Download Text File", on_click=download_transcript).classes('w-full mt-2 start-btn text-white')
282
 
 
267
  ui.separator().classes('my-4')
268
 
269
  def download_transcript():
270
+ if not chat_history:
271
+ ui.notify("No chat history to save.", type="warning")
272
+ return
273
+
274
  transcript_text = "DACODEX MENTOR SESSION\n" + "="*30 + "\n\n"
275
  for msg in chat_history:
276
  prefix = "STUDENT" if msg["role"] == "user" else "MENTOR"
277
  transcript_text += f"{prefix}:\n{msg['raw_text']}\n\n"
278
 
 
279
  filename = f"DACodeX_Transcript_{datetime.datetime.now().strftime('%Y%m%d_%H%M')}.txt"
280
+
281
+ try:
282
+ # Reliably save locally by targeting the user's Downloads folder
283
+ downloads_path = os.path.join(os.path.expanduser('~'), 'Downloads')
284
+ if not os.path.exists(downloads_path):
285
+ downloads_path = os.getcwd() # Fallback
286
+
287
+ full_path = os.path.join(downloads_path, filename)
288
+
289
+ with open(full_path, "w", encoding="utf-8") as f:
290
+ f.write(transcript_text)
291
+
292
+ ui.notify(f"Transcript saved to: {full_path}", type='positive')
293
+ except Exception as e:
294
+ ui.notify(f"Failed to save transcript: {str(e)}", color='negative')
295
 
296
  ui.button("Download Text File", on_click=download_transcript).classes('w-full mt-2 start-btn text-white')
297