Priti0210 commited on
Commit
90b6e19
Β·
1 Parent(s): 944c441

video added

Browse files
Files changed (1) hide show
  1. app.py +106 -30
app.py CHANGED
@@ -71,21 +71,10 @@ def process_data(file_obj, model_col: str, train_col: str, data_source: str) ->
71
  if not file_obj:
72
  return "Error: No file uploaded.", None, None, None, None, None, None
73
 
74
- file_path = file_obj.name
75
- if file_path.endswith(".csv"):
76
- df = pd.read_csv(file_path)
77
- elif file_path.endswith(".json"):
78
- df = pd.read_json(file_path)
79
- else:
80
- return (
81
- "Error: Invalid file type. Please upload a CSV or JSON file.",
82
- None,
83
- None,
84
- None,
85
- None,
86
- None,
87
- None,
88
- )
89
 
90
  if model_col not in df.columns or train_col not in df.columns:
91
  return (
@@ -113,7 +102,7 @@ def process_data(file_obj, model_col: str, train_col: str, data_source: str) ->
113
  evaluation_markdown = summary + details + explanation
114
 
115
  return (
116
- None, # No error
117
  render_pipeline_graph(data_source),
118
  df.head().to_markdown(index=False, numalign="left", stralign="left"),
119
  evaluation_markdown,
@@ -121,16 +110,25 @@ def process_data(file_obj, model_col: str, train_col: str, data_source: str) ->
121
  ttr_train,
122
  similarity,
123
  )
124
-
125
  except Exception as e:
126
  return f"An error occurred: {str(e)}", None, None, None, None, None, None
127
 
128
 
129
- def update_dropdowns(file_obj) -> Tuple[list, list, str]:
130
- """Updates dropdown choices based on the uploaded file."""
 
 
 
 
131
  if not file_obj:
132
- return [], [], "No file uploaded."
 
 
 
 
 
133
 
 
134
  try:
135
  file_name = getattr(file_obj, "name", "")
136
  if file_name.endswith(".csv"):
@@ -138,14 +136,45 @@ def update_dropdowns(file_obj) -> Tuple[list, list, str]:
138
  elif file_name.endswith(".json"):
139
  df = pd.read_json(file_obj)
140
  else:
141
- return [], [], "Invalid file type. Only .csv and .json are supported."
 
 
 
 
142
 
 
143
  columns = df.columns.tolist()
144
  preview = df.head().to_markdown(index=False, numalign="left", stralign="left")
145
- return columns, columns, preview # For two dropdowns + preview
 
 
 
 
 
146
 
147
  except Exception as e:
148
- return [], [], f"Error reading file: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
 
151
  def main_interface():
@@ -178,9 +207,22 @@ def main_interface():
178
 
179
  with gr.Accordion("πŸ“š Research Reference", open=False):
180
  gr.HTML(render_core_reference())
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  gr.Markdown("## 1. Pipeline Simulation")
183
- data_source, description = render_pipeline()
 
184
  gr.HTML(description)
185
  pipeline_output = gr.Image(type="filepath", label="Pipeline Graph")
186
  warning_output = gr.HTML()
@@ -190,11 +232,18 @@ def main_interface():
190
  data_source.change(
191
  fn=render_pipeline_graph, inputs=data_source, outputs=pipeline_output
192
  )
 
 
 
 
 
193
 
194
  gr.Markdown("## 2. Upload CSV or JSON File")
195
  file_input = gr.File(
196
  file_types=[".csv", ".json"], label="Upload a CSV or JSON file"
197
  )
 
 
198
  gr.Markdown(
199
  """
200
  πŸ“ **Note:**
@@ -206,12 +255,17 @@ def main_interface():
206
 
207
  with gr.Row():
208
  model_col_input = gr.Dropdown(
209
- choices=[], label="Select column for model output"
 
 
 
210
  )
211
  train_col_input = gr.Dropdown(
212
- choices=[], label="Select column for future training data"
 
 
 
213
  )
214
-
215
  file_preview = gr.Markdown(label="πŸ“„ File Preview")
216
 
217
  output_markdown = gr.Markdown(label="πŸ” Evaluation Summary")
@@ -224,10 +278,15 @@ def main_interface():
224
  ttr_train_metric = gr.Number(label="Lexical Diversity (Training Set)")
225
  similarity_metric = gr.Number(label="Semantic Similarity (Cosine)")
226
 
 
 
 
 
 
227
  file_input.change(
228
- update_dropdowns,
229
- inputs=file_input,
230
- outputs=[model_col_input, train_col_input, file_preview],
231
  )
232
 
233
  def process_and_generate(
@@ -260,6 +319,23 @@ def main_interface():
260
  ttr_train_metric,
261
  similarity_metric,
262
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  for input_component in inputs:
264
  input_component.change(
265
  fn=process_and_generate, inputs=inputs, outputs=outputs
 
71
  if not file_obj:
72
  return "Error: No file uploaded.", None, None, None, None, None, None
73
 
74
+ global uploaded_df
75
+ df = uploaded_df.get("data")
76
+ if df is None:
77
+ return "Error: File not yet processed.", None, None, None, None, None, None
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  if model_col not in df.columns or train_col not in df.columns:
80
  return (
 
102
  evaluation_markdown = summary + details + explanation
103
 
104
  return (
105
+ None,
106
  render_pipeline_graph(data_source),
107
  df.head().to_markdown(index=False, numalign="left", stralign="left"),
108
  evaluation_markdown,
 
110
  ttr_train,
111
  similarity,
112
  )
 
113
  except Exception as e:
114
  return f"An error occurred: {str(e)}", None, None, None, None, None, None
115
 
116
 
117
+ # Store uploaded DataFrame globally for later access
118
+ uploaded_df = {}
119
+
120
+
121
+ def update_dropdowns(file_obj) -> Tuple[gr.Dropdown, gr.Dropdown, str]:
122
+ global uploaded_df
123
  if not file_obj:
124
+ uploaded_df["data"] = None # Clear cached file
125
+ return (
126
+ gr.update(choices=[], value=None),
127
+ gr.update(choices=[], value=None),
128
+ "No file uploaded.",
129
+ )
130
 
131
+ # Read the file and extract columns
132
  try:
133
  file_name = getattr(file_obj, "name", "")
134
  if file_name.endswith(".csv"):
 
136
  elif file_name.endswith(".json"):
137
  df = pd.read_json(file_obj)
138
  else:
139
+ return (
140
+ gr.update(choices=[], value=None),
141
+ gr.update(choices=[], value=None),
142
+ "Invalid file type.",
143
+ )
144
 
145
+ uploaded_df["data"] = df
146
  columns = df.columns.tolist()
147
  preview = df.head().to_markdown(index=False, numalign="left", stralign="left")
148
+
149
+ return (
150
+ gr.update(choices=columns, value=None),
151
+ gr.update(choices=columns, value=None),
152
+ preview,
153
+ )
154
 
155
  except Exception as e:
156
+ return (
157
+ gr.update(choices=[], value=None),
158
+ gr.update(choices=[], value=None),
159
+ f"Error reading file: {e}",
160
+ )
161
+
162
+
163
+ def clear_all_fields():
164
+ global uploaded_df
165
+ uploaded_df.clear() # Clear stored DataFrame
166
+ return (
167
+ None, # file_input
168
+ gr.update(choices=[], value=None), # model_col_input
169
+ gr.update(choices=[], value=None), # train_col_input
170
+ "", # file_preview
171
+ "", # output_markdown
172
+ "", # warning_output
173
+ None, # ttr_output_metric
174
+ None, # ttr_train_metric
175
+ None, # similarity_metric
176
+ render_pipeline_graph("Synthetic Generated Data"), # pipeline_output default
177
+ )
178
 
179
 
180
  def main_interface():
 
207
 
208
  with gr.Accordion("πŸ“š Research Reference", open=False):
209
  gr.HTML(render_core_reference())
210
+ gr.HTML(
211
+ """
212
+ <div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 20px;">
213
+ <h3 style="text-align: center;">πŸ“½οΈ How to Use MADGuard AI Explorer</h3>
214
+ <iframe width="720" height="405"
215
+ src="https://www.youtube.com/embed/qjMwvaBXQeY"
216
+ title="MADGuard AI Tutorial" frameborder="0"
217
+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
218
+ allowfullscreen></iframe>
219
+ </div>
220
+ """
221
+ )
222
 
223
  gr.Markdown("## 1. Pipeline Simulation")
224
+ data_source, description = render_pipeline(default="Synthetic Generated Data")
225
+
226
  gr.HTML(description)
227
  pipeline_output = gr.Image(type="filepath", label="Pipeline Graph")
228
  warning_output = gr.HTML()
 
232
  data_source.change(
233
  fn=render_pipeline_graph, inputs=data_source, outputs=pipeline_output
234
  )
235
+ interface.load(
236
+ fn=render_pipeline_graph,
237
+ inputs=[data_source],
238
+ outputs=[pipeline_output],
239
+ )
240
 
241
  gr.Markdown("## 2. Upload CSV or JSON File")
242
  file_input = gr.File(
243
  file_types=[".csv", ".json"], label="Upload a CSV or JSON file"
244
  )
245
+ clear_btn = gr.Button("🧹 Clear All")
246
+
247
  gr.Markdown(
248
  """
249
  πŸ“ **Note:**
 
255
 
256
  with gr.Row():
257
  model_col_input = gr.Dropdown(
258
+ choices=[],
259
+ value=None,
260
+ label="Select column for model output",
261
+ interactive=True,
262
  )
263
  train_col_input = gr.Dropdown(
264
+ choices=[],
265
+ value=None,
266
+ label="Select column for future training data",
267
+ interactive=True,
268
  )
 
269
  file_preview = gr.Markdown(label="πŸ“„ File Preview")
270
 
271
  output_markdown = gr.Markdown(label="πŸ” Evaluation Summary")
 
278
  ttr_train_metric = gr.Number(label="Lexical Diversity (Training Set)")
279
  similarity_metric = gr.Number(label="Semantic Similarity (Cosine)")
280
 
281
+ def handle_file_upload(file_obj, data_source_val):
282
+ dropdowns = update_dropdowns(file_obj)
283
+ graph = render_pipeline_graph(data_source_val)
284
+ return *dropdowns, graph
285
+
286
  file_input.change(
287
+ fn=handle_file_upload,
288
+ inputs=[file_input, data_source],
289
+ outputs=[model_col_input, train_col_input, file_preview, pipeline_output],
290
  )
291
 
292
  def process_and_generate(
 
319
  ttr_train_metric,
320
  similarity_metric,
321
  ]
322
+ clear_btn.click(
323
+ fn=clear_all_fields,
324
+ inputs=[],
325
+ outputs=[
326
+ file_input,
327
+ model_col_input,
328
+ train_col_input,
329
+ file_preview,
330
+ output_markdown,
331
+ warning_output,
332
+ ttr_output_metric,
333
+ ttr_train_metric,
334
+ similarity_metric,
335
+ pipeline_output,
336
+ ],
337
+ )
338
+
339
  for input_component in inputs:
340
  input_component.change(
341
  fn=process_and_generate, inputs=inputs, outputs=outputs