Mehak-Mazhar commited on
Commit
3c1c33b
·
verified ·
1 Parent(s): 66c203c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -27
app.py CHANGED
@@ -13,47 +13,43 @@ from sklearn.pipeline import Pipeline
13
  from sklearn.linear_model import LogisticRegression
14
  from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_auc_score
15
  import gradio as gr
16
-
17
-
18
  def load_csv(file_obj):
19
- try:
20
- # Case 1: File path string (Gradio sometimes gives this)
21
- if isinstance(file_obj, str):
 
 
22
  try:
23
- return pd.read_csv(file_obj), None
24
- except Exception as e_csv:
25
- try:
26
- return pd.read_excel(file_obj), None
27
- except Exception as e_xls:
28
- return None, f"Failed to read file from path. CSV error: {e_csv} / Excel error: {e_xls}"
29
-
30
- # Case 2: File-like object
31
- if hasattr(file_obj, "read"):
 
32
  file_obj.seek(0)
33
  try:
34
- return pd.read_csv(file_obj), None
35
- except Exception as e_csv:
36
- file_obj.seek(0)
37
- try:
38
- return pd.read_excel(file_obj), None
39
- except Exception as e_xls:
40
- return None, f"Failed to read file object. CSV error: {e_csv} / Excel error: {e_xls}"
41
-
42
- return None, "Unsupported file type."
43
 
44
- except Exception as e:
45
- return None, f"Unexpected error: {e}"
46
 
47
 
48
- # Step 1: upload file -> return column choices
49
  def on_upload(file):
50
  if file is None:
51
  return gr.Dropdown.update(choices=[]), "No file uploaded", None
 
52
  df, err = load_csv(file)
53
  if err:
54
  return gr.Dropdown.update(choices=[]), f"Error: {err}", None
 
55
  cols = df.columns.tolist()
56
- return gr.Dropdown.update(choices=cols, value=cols[-1] if len(cols)>0 else None), f"Loaded {len(df)} rows, {len(cols)} columns", df
 
57
 
58
 
59
  # Helper: build preprocessing + model pipeline
 
13
  from sklearn.linear_model import LogisticRegression
14
  from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_auc_score
15
  import gradio as gr
 
 
16
  def load_csv(file_obj):
17
+ # Case: path string
18
+ if isinstance(file_obj, str):
19
+ try:
20
+ return pd.read_csv(file_obj), None
21
+ except Exception as e_csv:
22
  try:
23
+ return pd.read_excel(file_obj), None
24
+ except Exception as e_xls:
25
+ return None, f"Failed to read file from path. CSV error: {e_csv} / Excel error: {e_xls}"
26
+
27
+ # Case: file-like object
28
+ if hasattr(file_obj, "read"):
29
+ file_obj.seek(0)
30
+ try:
31
+ return pd.read_csv(file_obj), None
32
+ except Exception as e_csv:
33
  file_obj.seek(0)
34
  try:
35
+ return pd.read_excel(file_obj), None
36
+ except Exception as e_xls:
37
+ return None, f"Failed to read file object. CSV error: {e_csv} / Excel error: {e_xls}"
 
 
 
 
 
 
38
 
39
+ return None, "Unsupported file type."
 
40
 
41
 
 
42
  def on_upload(file):
43
  if file is None:
44
  return gr.Dropdown.update(choices=[]), "No file uploaded", None
45
+
46
  df, err = load_csv(file)
47
  if err:
48
  return gr.Dropdown.update(choices=[]), f"Error: {err}", None
49
+
50
  cols = df.columns.tolist()
51
+ default_target = cols[-1] if cols else None
52
+ return gr.Dropdown.update(choices=cols, value=default_target), f"Loaded {len(df)} rows, {len(cols)} columns", df
53
 
54
 
55
  # Helper: build preprocessing + model pipeline