pavanmutha commited on
Commit
f0493c5
·
verified ·
1 Parent(s): 940e8f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -31,6 +31,7 @@ login(token=hf_token)
31
  model = HfApiModel("mistralai/Mixtral-8x7B-Instruct-v0.1", token=hf_token)
32
 
33
  df_global = None
 
34
 
35
  def clean_data(df):
36
  df = df.dropna(how='all', axis=1).dropna(how='all', axis=0)
@@ -50,6 +51,12 @@ def upload_file(file):
50
  df_global = df
51
  return df.head()
52
 
 
 
 
 
 
 
53
  def format_analysis_report(raw_output, visuals):
54
  try:
55
  if isinstance(raw_output, dict):
@@ -161,7 +168,8 @@ def compare_models():
161
  if df_global is None:
162
  return pd.DataFrame({"Error": ["Please upload and preprocess a dataset first."]}), None
163
 
164
- target = df_global.columns[-1]
 
165
  X = df_global.drop(target, axis=1)
166
  y = df_global[target]
167
 
@@ -212,15 +220,16 @@ def compare_models():
212
 
213
 
214
  # 1. prepare_data should come first
215
- def prepare_data(df, target_column=None):
 
216
  from sklearn.model_selection import train_test_split
217
 
218
  # If no target column is specified, select the first object column or the last column
219
  if target_column is None:
220
- target_column = df.select_dtypes(include=['object']).columns[0] if len(df.select_dtypes(include=['object']).columns) > 0 else df.columns[-1]
221
 
222
- X = df.drop(columns=[target_column])
223
- y = df[target_column]
224
 
225
  return train_test_split(X, y, test_size=0.3, random_state=42)
226
 
@@ -286,7 +295,8 @@ def explainability(_):
286
  import warnings
287
  warnings.filterwarnings("ignore")
288
 
289
- target = df_global.columns[-1]
 
290
  X = df_global.drop(target, axis=1)
291
  y = df_global[target]
292
 
@@ -357,6 +367,11 @@ def explainability(_):
357
 
358
  return shap_path, lime_path
359
 
 
 
 
 
 
360
  with gr.Blocks() as demo:
361
  gr.Markdown("## 📊 AI-Powered Data Analysis with Hyperparameter Optimization")
362
 
@@ -364,7 +379,12 @@ with gr.Blocks() as demo:
364
  with gr.Column():
365
  file_input = gr.File(label="Upload CSV or Excel", type="filepath")
366
  df_output = gr.DataFrame(label="Cleaned Data Preview")
 
 
 
367
  file_input.change(fn=upload_file, inputs=file_input, outputs=df_output)
 
 
368
 
369
  with gr.Column():
370
  insights_output = gr.HTML(label="Insights from SmolAgent")
@@ -391,5 +411,4 @@ with gr.Blocks() as demo:
391
  explain_btn.click(fn=explainability, inputs=[], outputs=[shap_img, lime_img])
392
  compare_btn.click(fn=compare_models, inputs=[], outputs=[compare_output, compare_img])
393
 
394
-
395
- demo.launch(debug=True)
 
31
  model = HfApiModel("mistralai/Mixtral-8x7B-Instruct-v0.1", token=hf_token)
32
 
33
  df_global = None
34
+ target_column_global = None
35
 
36
  def clean_data(df):
37
  df = df.dropna(how='all', axis=1).dropna(how='all', axis=0)
 
51
  df_global = df
52
  return df.head()
53
 
54
+ def set_target_column(col_name):
55
+ global target_column_global
56
+ target_column_global = col_name
57
+ return f"✅ Target column set to: {col_name}"
58
+
59
+
60
  def format_analysis_report(raw_output, visuals):
61
  try:
62
  if isinstance(raw_output, dict):
 
168
  if df_global is None:
169
  return pd.DataFrame({"Error": ["Please upload and preprocess a dataset first."]}), None
170
 
171
+ global target_column_global
172
+ target = target_column_global
173
  X = df_global.drop(target, axis=1)
174
  y = df_global[target]
175
 
 
220
 
221
 
222
  # 1. prepare_data should come first
223
+ def prepare_data(df):
224
+ global target_column_global
225
  from sklearn.model_selection import train_test_split
226
 
227
  # If no target column is specified, select the first object column or the last column
228
  if target_column is None:
229
+ raise ValueError("Target column not set.")
230
 
231
+ X = df.drop(columns=[target_column_global])
232
+ y = df[target_column_global]
233
 
234
  return train_test_split(X, y, test_size=0.3, random_state=42)
235
 
 
295
  import warnings
296
  warnings.filterwarnings("ignore")
297
 
298
+ global target_column_global
299
+ target = target_column_global
300
  X = df_global.drop(target, axis=1)
301
  y = df_global[target]
302
 
 
367
 
368
  return shap_path, lime_path
369
 
370
+ # Define this BEFORE the Gradio app layout
371
+ def update_target_choices():
372
+ global df_global
373
+ return gr.update(choices=df_global.columns.tolist())
374
+
375
  with gr.Blocks() as demo:
376
  gr.Markdown("## 📊 AI-Powered Data Analysis with Hyperparameter Optimization")
377
 
 
379
  with gr.Column():
380
  file_input = gr.File(label="Upload CSV or Excel", type="filepath")
381
  df_output = gr.DataFrame(label="Cleaned Data Preview")
382
+ target_dropdown = gr.Dropdown(label="Select Target Column", choices=[], interactive=True)
383
+ target_status = gr.Textbox(label="Target Column Status", interactive=False)
384
+
385
  file_input.change(fn=upload_file, inputs=file_input, outputs=df_output)
386
+ file_input.change(fn=update_target_choices, inputs=[], outputs=target_dropdown)
387
+ target_dropdown.change(fn=set_target_column, inputs=target_dropdown, outputs=target_status)
388
 
389
  with gr.Column():
390
  insights_output = gr.HTML(label="Insights from SmolAgent")
 
411
  explain_btn.click(fn=explainability, inputs=[], outputs=[shap_img, lime_img])
412
  compare_btn.click(fn=compare_models, inputs=[], outputs=[compare_output, compare_img])
413
 
414
+ demo.launch(debug=True)