pavanmutha commited on
Commit
48c9843
·
verified ·
1 Parent(s): 8f39adc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -26,8 +26,15 @@ model = HfApiModel("mistralai/Mixtral-8x7B-Instruct-v0.1", token=hf_token)
26
 
27
  def format_analysis_report(raw_output, visuals):
28
  try:
29
- analysis_dict = raw_output if isinstance(raw_output, dict) else ast.literal_eval(str(raw_output))
30
-
 
 
 
 
 
 
 
31
  report = f"""
32
  <div style="font-family: Arial, sans-serif; padding: 20px; color: #333;">
33
  <h1 style="color: #2B547E; border-bottom: 2px solid #2B547E; padding-bottom: 10px;">📊 Data Analysis Report</h1>
@@ -43,7 +50,7 @@ def format_analysis_report(raw_output, visuals):
43
  """
44
  return report, visuals
45
  except Exception as e:
46
- print(f"Error formatting analysis report: {e}")
47
  return str(raw_output), visuals
48
 
49
  def format_observations(observations):
@@ -91,8 +98,22 @@ def analyze_data(csv_file, additional_notes=""):
91
  1. Basic statistics and data quality checks
92
  2. 3 insightful analytical questions about relationships in the data
93
  3. Visualization of key patterns and correlations
94
- 4. Actionable real-world insights derived from findings
95
- Generate publication-quality visualizations and save to './figures/'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  """, additional_args={"additional_notes": additional_notes, "source_file": csv_file})
97
 
98
  execution_time = time.time() - start_time
@@ -154,7 +175,7 @@ def tune_hyperparameters(csv_file, n_trials: int):
154
  shap_fig_path = "./figures/shap_summary.png"
155
  plt.savefig(shap_fig_path)
156
  wandb.log({"shap_summary": wandb.Image(shap_fig_path)})
157
- plt.clf() #Clear figure to avoid plot overlap.
158
 
159
  lime_explainer = lime.lime_tabular.LimeTabularExplainer(X_train.values, feature_names=X_train.columns, class_names=['target'], mode='classification')
160
  lime_explanation = lime_explainer.explain_instance(X_test.iloc[0].values, model.predict_proba)
@@ -162,7 +183,7 @@ def tune_hyperparameters(csv_file, n_trials: int):
162
  lime_fig_path = "./figures/lime_explanation.png"
163
  lime_fig.savefig(lime_fig_path)
164
  wandb.log({"lime_explanation": wandb.Image(lime_fig_path)})
165
- plt.clf() #Clear figure to avoid plot overlap.
166
 
167
  return f"Best Hyperparameters: {best_params}<br>Accuracy: {accuracy}<br>Precision: {precision}<br>Recall: {recall}<br>F1-score: {f1}"
168
 
 
26
 
27
  def format_analysis_report(raw_output, visuals):
28
  try:
29
+ if isinstance(raw_output, dict):
30
+ analysis_dict = raw_output
31
+ else:
32
+ try:
33
+ analysis_dict = ast.literal_eval(str(raw_output))
34
+ except (SyntaxError, ValueError) as e:
35
+ print(f"Error parsing CodeAgent output: {e}")
36
+ return str(raw_output), visuals # Return raw output as string
37
+
38
  report = f"""
39
  <div style="font-family: Arial, sans-serif; padding: 20px; color: #333;">
40
  <h1 style="color: #2B547E; border-bottom: 2px solid #2B547E; padding-bottom: 10px;">📊 Data Analysis Report</h1>
 
50
  """
51
  return report, visuals
52
  except Exception as e:
53
+ print(f"Error in format_analysis_report: {e}")
54
  return str(raw_output), visuals
55
 
56
  def format_observations(observations):
 
98
  1. Basic statistics and data quality checks
99
  2. 3 insightful analytical questions about relationships in the data
100
  3. Visualization of key patterns and correlations
101
+ 4. Actionable real-world insights derived from findings.
102
+ Generate publication-quality visualizations and save to './figures/'.
103
+ Return the analysis results as a python dictionary that can be parsed by ast.literal_eval().
104
+ The dictionary should have the following structure:
105
+ {
106
+ 'observations': {
107
+ 'observation_1_key': 'observation_1_value',
108
+ 'observation_2_key': 'observation_2_value',
109
+ ...
110
+ },
111
+ 'insights': {
112
+ 'insight_1_key': 'insight_1_value',
113
+ 'insight_2_key': 'insight_2_value',
114
+ ...
115
+ }
116
+ }
117
  """, additional_args={"additional_notes": additional_notes, "source_file": csv_file})
118
 
119
  execution_time = time.time() - start_time
 
175
  shap_fig_path = "./figures/shap_summary.png"
176
  plt.savefig(shap_fig_path)
177
  wandb.log({"shap_summary": wandb.Image(shap_fig_path)})
178
+ plt.clf()
179
 
180
  lime_explainer = lime.lime_tabular.LimeTabularExplainer(X_train.values, feature_names=X_train.columns, class_names=['target'], mode='classification')
181
  lime_explanation = lime_explainer.explain_instance(X_test.iloc[0].values, model.predict_proba)
 
183
  lime_fig_path = "./figures/lime_explanation.png"
184
  lime_fig.savefig(lime_fig_path)
185
  wandb.log({"lime_explanation": wandb.Image(lime_fig_path)})
186
+ plt.clf()
187
 
188
  return f"Best Hyperparameters: {best_params}<br>Accuracy: {accuracy}<br>Precision: {precision}<br>Recall: {recall}<br>F1-score: {f1}"
189