SamanthaStorm commited on
Commit
384087f
·
verified ·
1 Parent(s): 355874d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -83
app.py CHANGED
@@ -71,6 +71,17 @@ def analyze_single_message(message):
71
  "risk_level": "Unknown"
72
  }
73
 
 
 
 
 
 
 
 
 
 
 
 
74
  def format_analysis_results(analysis):
75
  """Format analysis results for display"""
76
  try:
@@ -129,7 +140,6 @@ def format_analysis_results(analysis):
129
  logger.error(f"Error formatting analysis results: {e}")
130
  return f"<p style='color: #1f2937;'>Error formatting results: {str(e)}</p>"
131
 
132
-
133
  def format_summary_results(summary):
134
  """Format summary results for display"""
135
  try:
@@ -299,8 +309,6 @@ def format_summary_results(summary):
299
  logger.error(traceback.format_exc())
300
  return f"<p style='color: #1f2937;'>Error formatting summary: {str(e)}</p>"
301
 
302
-
303
-
304
  def format_safety_plan(safety_plan):
305
  """Format safety plan for display"""
306
  try:
@@ -324,6 +332,81 @@ def format_safety_plan(safety_plan):
324
  logger.error(f"Error formatting safety plan: {e}")
325
  return f"<p style='color: #1f2937;'>Error formatting safety plan: {str(e)}</p>"
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
  def create_interface():
329
  """Create Gradio interface"""
@@ -348,7 +431,8 @@ def create_interface():
348
  summary_output = gr.HTML(
349
  label="Analysis Summary",
350
  value="<p style='color: #1f2937;'>Upload a file and click 'Analyze Chat History' to see results.</p>",
351
- elem_id="summary_output") # Add an element ID for easier debugging
 
352
 
353
  with gr.Row():
354
  with gr.Column():
@@ -367,7 +451,7 @@ def create_interface():
367
  with gr.Row():
368
  safety_plan_output = gr.HTML(
369
  label="Safety Plan",
370
- value="<p>Safety plan will appear here after analysis.</p>"
371
  )
372
 
373
  with gr.Tab("Single Message Analysis"):
@@ -383,7 +467,7 @@ def create_interface():
383
  with gr.Column(scale=2):
384
  message_analysis_output = gr.HTML(
385
  label="Message Analysis",
386
- value="<p>Enter a message and click 'Analyze Message' to see results.</p>"
387
  )
388
 
389
  with gr.Tab("About"):
@@ -412,83 +496,6 @@ def create_interface():
412
  </div>
413
  """)
414
 
415
- def handle_file_analysis(file_path):
416
- if not file_path:
417
- return (
418
- "<p style='color: #1f2937;'>Please upload a file first.</p>",
419
- None, None, None, None,
420
- "<p style='color: #1f2937;'>Please upload a file first.</p>"
421
- )
422
-
423
- try:
424
- # Analyze file
425
- results_df, summary = analyze_file(file_path)
426
-
427
- if "error" in summary:
428
- return (
429
- f"<p style='color: #1f2937;'>Error: {summary['error']}</p>",
430
- None, None, None, None,
431
- "<p style='color: #1f2937;'>Error analyzing file.</p>"
432
- )
433
-
434
- # Format summary
435
- summary_html = format_summary_results(summary)
436
-
437
- # Generate plots
438
- timeline = generate_timeline_chart(results_df)
439
- pattern_freq = generate_pattern_frequency_chart(results_df)
440
- sender_comp = generate_sender_comparison_chart(results_df)
441
- time_heatmap = generate_time_of_day_heatmap(results_df)
442
-
443
- # Format safety plan
444
- safety_plan_html = format_safety_plan(summary['safety_plan'])
445
-
446
- # Debug output
447
- logger.info(f"Summary HTML length: {len(summary_html)}")
448
- logger.info(f"Generated {len(results_df)} results")
449
- logger.info(f"Timeline chart generated: {timeline is not None}")
450
- logger.info(f"Pattern frequency chart generated: {pattern_freq is not None}")
451
- logger.info(f"Sender comparison chart generated: {sender_comp is not None}")
452
- logger.info(f"Time heatmap generated: {time_heatmap is not None}")
453
-
454
- return (
455
- summary_html,
456
- timeline,
457
- pattern_freq,
458
- sender_comp,
459
- time_heatmap,
460
- safety_plan_html
461
- )
462
- except Exception as e:
463
- logger.error(f"Error in handle_file_analysis: {e}")
464
- logger.error(traceback.format_exc())
465
- return (
466
- f"<p style='color: #1f2937;'>Error analyzing file: {str(e)}</p>",
467
- None, None, None, None,
468
- "<p style='color: #1f2937;'>Error analyzing file.</p>"
469
- )
470
-
471
-
472
- def handle_message_analysis(message):
473
- if not message.strip():
474
- return "<p>Please enter a message first.</p>"
475
-
476
- try:
477
- # Analyze message
478
- analysis = analyze_single_message(message)
479
-
480
- if "error" in analysis:
481
- return f"<p>Error: {analysis['error']}</p>"
482
-
483
- # Format analysis
484
- html = format_analysis_results(analysis)
485
-
486
- return html
487
- except Exception as e:
488
- logger.error(f"Error in handle_message_analysis: {e}")
489
- logger.error(traceback.format_exc())
490
- return f"<p>Error analyzing message: {str(e)}</p>"
491
-
492
  # Connect event handlers
493
  analyze_button.click(
494
  handle_file_analysis,
@@ -514,3 +521,4 @@ def create_interface():
514
  if __name__ == "__main__":
515
  demo = create_interface()
516
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
71
  "risk_level": "Unknown"
72
  }
73
 
74
+ def get_risk_color(risk_level):
75
+ """Get color for risk level"""
76
+ colors = {
77
+ "Critical": "#ef4444",
78
+ "High": "#f97316",
79
+ "Moderate": "#f59e0b",
80
+ "Low": "#10b981",
81
+ "Unknown": "#6b7280"
82
+ }
83
+ return colors.get(risk_level, "#6b7280")
84
+
85
  def format_analysis_results(analysis):
86
  """Format analysis results for display"""
87
  try:
 
140
  logger.error(f"Error formatting analysis results: {e}")
141
  return f"<p style='color: #1f2937;'>Error formatting results: {str(e)}</p>"
142
 
 
143
  def format_summary_results(summary):
144
  """Format summary results for display"""
145
  try:
 
309
  logger.error(traceback.format_exc())
310
  return f"<p style='color: #1f2937;'>Error formatting summary: {str(e)}</p>"
311
 
 
 
312
  def format_safety_plan(safety_plan):
313
  """Format safety plan for display"""
314
  try:
 
332
  logger.error(f"Error formatting safety plan: {e}")
333
  return f"<p style='color: #1f2937;'>Error formatting safety plan: {str(e)}</p>"
334
 
335
+ def handle_file_analysis(file_path):
336
+ if not file_path:
337
+ return (
338
+ "<p style='color: #1f2937;'>Please upload a file first.</p>",
339
+ None, None, None, None,
340
+ "<p style='color: #1f2937;'>Please upload a file first.</p>"
341
+ )
342
+
343
+ try:
344
+ # Analyze file
345
+ results_df, summary = analyze_file(file_path)
346
+
347
+ if "error" in summary:
348
+ return (
349
+ f"<p style='color: #1f2937;'>Error: {summary['error']}</p>",
350
+ None, None, None, None,
351
+ "<p style='color: #1f2937;'>Error analyzing file.</p>"
352
+ )
353
+
354
+ # Format summary
355
+ summary_html = format_summary_results(summary)
356
+
357
+ # Generate plots
358
+ timeline = generate_timeline_chart(results_df)
359
+ pattern_freq = generate_pattern_frequency_chart(results_df)
360
+ sender_comp = generate_sender_comparison_chart(results_df)
361
+ time_heatmap = generate_time_of_day_heatmap(results_df)
362
+
363
+ # Format safety plan
364
+ safety_plan_html = format_safety_plan(summary['safety_plan'])
365
+
366
+ # Debug output
367
+ logger.info(f"Summary HTML length: {len(summary_html)}")
368
+ logger.info(f"Generated {len(results_df)} results")
369
+ logger.info(f"Timeline chart generated: {timeline is not None}")
370
+ logger.info(f"Pattern frequency chart generated: {pattern_freq is not None}")
371
+ logger.info(f"Sender comparison chart generated: {sender_comp is not None}")
372
+ logger.info(f"Time heatmap generated: {time_heatmap is not None}")
373
+
374
+ return (
375
+ summary_html,
376
+ timeline,
377
+ pattern_freq,
378
+ sender_comp,
379
+ time_heatmap,
380
+ safety_plan_html
381
+ )
382
+ except Exception as e:
383
+ logger.error(f"Error in handle_file_analysis: {e}")
384
+ logger.error(traceback.format_exc())
385
+ return (
386
+ f"<p style='color: #1f2937;'>Error analyzing file: {str(e)}</p>",
387
+ None, None, None, None,
388
+ "<p style='color: #1f2937;'>Error analyzing file.</p>"
389
+ )
390
+
391
+ def handle_message_analysis(message):
392
+ if not message.strip():
393
+ return "<p style='color: #1f2937;'>Please enter a message first.</p>"
394
+
395
+ try:
396
+ # Analyze message
397
+ analysis = analyze_single_message(message)
398
+
399
+ if "error" in analysis:
400
+ return f"<p style='color: #1f2937;'>Error: {analysis['error']}</p>"
401
+
402
+ # Format analysis
403
+ html = format_analysis_results(analysis)
404
+
405
+ return html
406
+ except Exception as e:
407
+ logger.error(f"Error in handle_message_analysis: {e}")
408
+ logger.error(traceback.format_exc())
409
+ return f"<p style='color: #1f2937;'>Error analyzing message: {str(e)}</p>"
410
 
411
  def create_interface():
412
  """Create Gradio interface"""
 
431
  summary_output = gr.HTML(
432
  label="Analysis Summary",
433
  value="<p style='color: #1f2937;'>Upload a file and click 'Analyze Chat History' to see results.</p>",
434
+ elem_id="summary_output"
435
+ )
436
 
437
  with gr.Row():
438
  with gr.Column():
 
451
  with gr.Row():
452
  safety_plan_output = gr.HTML(
453
  label="Safety Plan",
454
+ value="<p style='color: #1f2937;'>Safety plan will appear here after analysis.</p>"
455
  )
456
 
457
  with gr.Tab("Single Message Analysis"):
 
467
  with gr.Column(scale=2):
468
  message_analysis_output = gr.HTML(
469
  label="Message Analysis",
470
+ value="<p style='color: #1f2937;'>Enter a message and click 'Analyze Message' to see results.</p>"
471
  )
472
 
473
  with gr.Tab("About"):
 
496
  </div>
497
  """)
498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
  # Connect event handlers
500
  analyze_button.click(
501
  handle_file_analysis,
 
521
  if __name__ == "__main__":
522
  demo = create_interface()
523
  demo.launch(server_name="0.0.0.0", server_port=7860)
524
+