Ryan commited on
Commit
1de0df8
·
1 Parent(s): 4973fc0
Files changed (1) hide show
  1. app.py +43 -19
app.py CHANGED
@@ -630,30 +630,54 @@ def create_app():
630
  print("About to process visualization components")
631
  viz_components = process_and_visualize_sentiment_analysis(roberta_results)
632
  print(f"Visualization components generated: {len(viz_components)}")
633
-
634
- # Process the visualization
635
- viz_components = process_and_visualize_sentiment_analysis(roberta_results)
636
 
637
  print("Starting HTML conversion of visualization components")
638
-
639
- # Convert the visualization components to HTML
 
640
  html_content = "<div class='sentiment-visualization'>"
641
 
642
- for component in viz_components:
643
- if isinstance(component, gr.Markdown):
644
- # Access the Markdown content correctly
645
- html_content += f"<div class='markdown-content'>{str(component)}</div>"
646
- elif isinstance(component, gr.Plot):
647
- # For Plot components, add a placeholder message
648
- html_content += "<div class='plot-placeholder'>Sentiment plot will appear here</div>"
649
- elif isinstance(component, gr.HTML):
650
- # Access HTML content correctly
651
- html_content += f"<div class='html-content'>{str(component)}</div>"
652
- else:
653
- # Handle any other component type
654
- html_content += f"<div class='component'>{str(component)}</div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655
 
656
  html_content += "</div>"
 
657
 
658
  # Return updated values
659
  return (
@@ -663,7 +687,7 @@ def create_app():
663
  gr.update(visible=True), # roberta_viz_title (show title)
664
  gr.update(visible=True, value=html_content) # roberta_viz_content (show content)
665
  )
666
-
667
  except Exception as e:
668
  import traceback
669
  error_msg = f"Error in RoBERTa analysis: {str(e)}\n{traceback.format_exc()}"
 
630
  print("About to process visualization components")
631
  viz_components = process_and_visualize_sentiment_analysis(roberta_results)
632
  print(f"Visualization components generated: {len(viz_components)}")
 
 
 
633
 
634
  print("Starting HTML conversion of visualization components")
635
+
636
+ # Convert the visualization components to HTML - OPTIMIZED VERSION
637
+ print("Starting HTML conversion of visualization components")
638
  html_content = "<div class='sentiment-visualization'>"
639
 
640
+ # Use a more efficient and safer approach for component conversion
641
+ for i, component in enumerate(viz_components):
642
+ print(f"Converting component {i+1}/{len(viz_components)}")
643
+
644
+ try:
645
+ if isinstance(component, gr.Markdown):
646
+ # Simple string representation for Markdown
647
+ md_content = str(component).replace('Markdown(value=', '').rstrip(')')
648
+ if md_content.startswith("'") and md_content.endswith("'"):
649
+ md_content = md_content[1:-1]
650
+ html_content += f"<div class='markdown-content'>{md_content}</div>"
651
+
652
+ elif isinstance(component, gr.Plot):
653
+ # Simple placeholder for plots
654
+ html_content += "<div class='plot-placeholder'>Sentiment visualization chart would appear here</div>"
655
+
656
+ elif isinstance(component, gr.HTML):
657
+ # Extract HTML content safely
658
+ if hasattr(component, 'value'):
659
+ html_content += component.value
660
+ else:
661
+ html_str = str(component)
662
+ # Try to extract HTML content from string representation
663
+ if 'value=' in html_str:
664
+ html_value = html_str.split('value=', 1)[1].split(',', 1)[0].strip()
665
+ if html_value.startswith("'") and html_value.endswith("'"):
666
+ html_value = html_value[1:-1]
667
+ html_content += html_value
668
+ else:
669
+ html_content += "<div>HTML component (content unavailable)</div>"
670
+
671
+ else:
672
+ # Generic fallback for any other component type
673
+ html_content += f"<div class='component'>Component: {component.__class__.__name__}</div>"
674
+
675
+ except Exception as e:
676
+ print(f"Error converting component {i+1}: {str(e)}")
677
+ html_content += "<div class='error'>Error rendering component</div>"
678
 
679
  html_content += "</div>"
680
+ print("HTML conversion completed")
681
 
682
  # Return updated values
683
  return (
 
687
  gr.update(visible=True), # roberta_viz_title (show title)
688
  gr.update(visible=True, value=html_content) # roberta_viz_content (show content)
689
  )
690
+
691
  except Exception as e:
692
  import traceback
693
  error_msg = f"Error in RoBERTa analysis: {str(e)}\n{traceback.format_exc()}"