pavanmutha commited on
Commit
c553e84
·
verified ·
1 Parent(s): b66d286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -2
app.py CHANGED
@@ -18,6 +18,61 @@ model = HfApiModel(
18
  token=hf_token # Pass token explicitly
19
  )
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def analyze_data(csv_file, additional_notes=""):
22
  # Clear previous figures
23
  if os.path.exists('./figures'):
@@ -56,7 +111,8 @@ def analyze_data(csv_file, additional_notes=""):
56
  visuals = [os.path.join('./figures', f) for f in os.listdir('./figures')
57
  if f.endswith(('.png', '.jpg', '.jpeg'))]
58
 
59
- return analysis_result, visuals
 
60
 
61
  # Create Gradio interface
62
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -69,7 +125,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
69
  analyze_btn = gr.Button("Analyze", variant="primary")
70
 
71
  with gr.Column():
72
- analysis_output = gr.Textbox(label="Analysis Report", interactive=False)
73
  gallery = gr.Gallery(label="Data Visualizations", columns=2)
74
 
75
  analyze_btn.click(
 
18
  token=hf_token # Pass token explicitly
19
  )
20
 
21
+ # Add this formatting function
22
+ def format_analysis_report(raw_output, visuals):
23
+ try:
24
+ # Convert string output to dictionary
25
+ analysis_dict = ast.literal_eval(str(raw_output))
26
+
27
+ report = f"""
28
+ <div style="font-family: Arial, sans-serif; padding: 20px; color: #333;">
29
+ <h1 style="color: #2B547E; border-bottom: 2px solid #2B547E; padding-bottom: 10px;">📊 Data Analysis Report</h1>
30
+
31
+ <div style="margin-top: 25px; background: #f8f9fa; padding: 20px; border-radius: 8px;">
32
+ <h2 style="color: #2B547E;">🔍 Key Observations</h2>
33
+ {format_observations(analysis_dict.get('observations', {}))}
34
+ </div>
35
+
36
+ <div style="margin-top: 30px;">
37
+ <h2 style="color: #2B547E;">💡 Insights & Visualizations</h2>
38
+ {format_insights(analysis_dict.get('insights', {}), visuals)}
39
+ </div>
40
+ </div>
41
+ """
42
+ return report, visuals
43
+ except:
44
+ return raw_output, visuals
45
+
46
+ def format_observations(observations):
47
+ items = []
48
+ for key, value in observations.items():
49
+ if 'proportions' in key:
50
+ items.append(f"""
51
+ <div style="margin: 15px 0; padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
52
+ <h3 style="margin: 0 0 10px 0; color: #4A708B;">{key.replace('_', ' ').title()}</h3>
53
+ <pre style="margin: 0; padding: 10px; background: #f8f9fa; border-radius: 4px;">{value}</pre>
54
+ </div>
55
+ """)
56
+ return '\n'.join(items)
57
+
58
+ def format_insights(insights, visuals):
59
+ items = []
60
+ for idx, (key, insight) in enumerate(insights.items()):
61
+ img_tag = ""
62
+ if idx < len(visuals):
63
+ img_tag = f'<img src="/file={visuals[idx]}" style="max-width: 100%; height: auto; margin-top: 10px; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">'
64
+
65
+ items.append(f"""
66
+ <div style="margin: 20px 0; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
67
+ <div style="display: flex; align-items: center; gap: 10px;">
68
+ <div style="background: #2B547E; color: white; width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center;">{idx+1}</div>
69
+ <p style="margin: 0; font-size: 16px;">{insight}</p>
70
+ </div>
71
+ {img_tag}
72
+ </div>
73
+ """)
74
+ return '\n'.join(items)
75
+
76
  def analyze_data(csv_file, additional_notes=""):
77
  # Clear previous figures
78
  if os.path.exists('./figures'):
 
111
  visuals = [os.path.join('./figures', f) for f in os.listdir('./figures')
112
  if f.endswith(('.png', '.jpg', '.jpeg'))]
113
 
114
+
115
+ return format_analysis_report(analysis_result, visuals)
116
 
117
  # Create Gradio interface
118
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
125
  analyze_btn = gr.Button("Analyze", variant="primary")
126
 
127
  with gr.Column():
128
+ analysis_output = gr.Markdown("### Analysis results will appear here...")
129
  gallery = gr.Gallery(label="Data Visualizations", columns=2)
130
 
131
  analyze_btn.click(