Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -78,26 +78,24 @@ class SyntheticDataGenerator:
|
|
78 |
|
79 |
def get_quality_report_file(self) -> Optional[str]:
|
80 |
"""
|
81 |
-
Build/export the quality report and return a file path for
|
82 |
-
Uses /tmp
|
83 |
"""
|
84 |
if not self.generator:
|
85 |
return None
|
86 |
try:
|
87 |
rep = self.generator.reports(display=False)
|
88 |
|
89 |
-
#
|
90 |
if isinstance(rep, str) and rep.endswith(".zip") and os.path.exists(rep):
|
91 |
return rep
|
92 |
|
93 |
-
# If object exposes a path-like attribute
|
94 |
for attr in ("archive_path", "zip_path", "path", "file_path"):
|
95 |
if hasattr(rep, attr):
|
96 |
p = getattr(rep, attr)
|
97 |
if isinstance(p, str) and os.path.exists(p):
|
98 |
return p
|
99 |
|
100 |
-
# Try saving/exporting
|
101 |
os.makedirs("/tmp", exist_ok=True)
|
102 |
target_zip = "/tmp/quality_report.zip"
|
103 |
if hasattr(rep, "save"):
|
@@ -115,7 +113,6 @@ class SyntheticDataGenerator:
|
|
115 |
except Exception:
|
116 |
pass
|
117 |
|
118 |
-
# Fallback: stringify into TXT
|
119 |
target_txt = "/tmp/quality_report.txt"
|
120 |
with open(target_txt, "w", encoding="utf-8") as f:
|
121 |
f.write(str(rep))
|
@@ -140,9 +137,9 @@ Memory Usage Estimate:
|
|
140 |
""".strip()
|
141 |
|
142 |
|
143 |
-
# App state
|
144 |
generator = SyntheticDataGenerator()
|
145 |
-
_last_synth_df: Optional[pd.DataFrame] = None
|
146 |
|
147 |
|
148 |
# ---- Gradio wrappers ----
|
@@ -177,8 +174,8 @@ def generate_data(size: int) -> Tuple[Optional[pd.DataFrame], str]:
|
|
177 |
return None, f"Error: {message}"
|
178 |
|
179 |
|
180 |
-
def
|
181 |
-
"""
|
182 |
global _last_synth_df
|
183 |
if _last_synth_df is None or _last_synth_df.empty:
|
184 |
return None
|
@@ -269,9 +266,11 @@ def create_interface():
|
|
269 |
with gr.Column():
|
270 |
train_status = gr.Textbox(label="Training Status", interactive=False)
|
271 |
|
|
|
272 |
with gr.Row():
|
273 |
-
|
274 |
-
|
|
|
275 |
|
276 |
with gr.Tab("Generate Data"):
|
277 |
gr.Markdown("### Generate synthetic data from your trained model")
|
@@ -283,9 +282,12 @@ def create_interface():
|
|
283 |
gen_status = gr.Textbox(label="Generation Status", interactive=False)
|
284 |
|
285 |
synthetic_data = gr.Dataframe(label="Synthetic Data", interactive=False)
|
|
|
|
|
286 |
with gr.Row():
|
287 |
-
|
288 |
-
|
|
|
289 |
comparison_plot = gr.Plot(label="Data Comparison")
|
290 |
|
291 |
# ---- Events ----
|
@@ -297,16 +299,38 @@ def create_interface():
|
|
297 |
outputs=[train_status],
|
298 |
)
|
299 |
|
300 |
-
#
|
301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
302 |
|
|
|
303 |
generate_btn.click(generate_data, inputs=[gen_size], outputs=[synthetic_data, gen_status])
|
304 |
|
305 |
# Build comparison plot when both datasets are available
|
306 |
synthetic_data.change(create_comparison_plot, inputs=[uploaded_data, synthetic_data], outputs=[comparison_plot])
|
307 |
|
308 |
-
# CSV download:
|
309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
310 |
|
311 |
# File upload handler
|
312 |
def process_uploaded_file(file):
|
|
|
78 |
|
79 |
def get_quality_report_file(self) -> Optional[str]:
|
80 |
"""
|
81 |
+
Build/export the quality report and return a file path for download.
|
82 |
+
Uses /tmp on Spaces; tries ZIP first, then TXT fallback.
|
83 |
"""
|
84 |
if not self.generator:
|
85 |
return None
|
86 |
try:
|
87 |
rep = self.generator.reports(display=False)
|
88 |
|
89 |
+
# Path already?
|
90 |
if isinstance(rep, str) and rep.endswith(".zip") and os.path.exists(rep):
|
91 |
return rep
|
92 |
|
|
|
93 |
for attr in ("archive_path", "zip_path", "path", "file_path"):
|
94 |
if hasattr(rep, attr):
|
95 |
p = getattr(rep, attr)
|
96 |
if isinstance(p, str) and os.path.exists(p):
|
97 |
return p
|
98 |
|
|
|
99 |
os.makedirs("/tmp", exist_ok=True)
|
100 |
target_zip = "/tmp/quality_report.zip"
|
101 |
if hasattr(rep, "save"):
|
|
|
113 |
except Exception:
|
114 |
pass
|
115 |
|
|
|
116 |
target_txt = "/tmp/quality_report.txt"
|
117 |
with open(target_txt, "w", encoding="utf-8") as f:
|
118 |
f.write(str(rep))
|
|
|
137 |
""".strip()
|
138 |
|
139 |
|
140 |
+
# --- App state ---
|
141 |
generator = SyntheticDataGenerator()
|
142 |
+
_last_synth_df: Optional[pd.DataFrame] = None
|
143 |
|
144 |
|
145 |
# ---- Gradio wrappers ----
|
|
|
174 |
return None, f"Error: {message}"
|
175 |
|
176 |
|
177 |
+
def download_csv_prepare() -> Optional[str]:
|
178 |
+
"""Return a path to the latest synthetic CSV; used as output to gr.File."""
|
179 |
global _last_synth_df
|
180 |
if _last_synth_df is None or _last_synth_df.empty:
|
181 |
return None
|
|
|
266 |
with gr.Column():
|
267 |
train_status = gr.Textbox(label="Training Status", interactive=False)
|
268 |
|
269 |
+
# --- Quality report: button + hidden File with download link ---
|
270 |
with gr.Row():
|
271 |
+
get_report_btn = gr.Button("Get Quality Report", variant="secondary")
|
272 |
+
with gr.Group(visible=False) as report_group:
|
273 |
+
report_file = gr.File(label="Quality Report", interactive=False)
|
274 |
|
275 |
with gr.Tab("Generate Data"):
|
276 |
gr.Markdown("### Generate synthetic data from your trained model")
|
|
|
282 |
gen_status = gr.Textbox(label="Generation Status", interactive=False)
|
283 |
|
284 |
synthetic_data = gr.Dataframe(label="Synthetic Data", interactive=False)
|
285 |
+
|
286 |
+
# --- CSV download: button + hidden File with download link ---
|
287 |
with gr.Row():
|
288 |
+
csv_download_btn = gr.Button("Download CSV", variant="secondary")
|
289 |
+
with gr.Group(visible=False) as csv_group:
|
290 |
+
csv_file = gr.File(label="Synthetic CSV", interactive=False)
|
291 |
comparison_plot = gr.Plot(label="Data Comparison")
|
292 |
|
293 |
# ---- Events ----
|
|
|
299 |
outputs=[train_status],
|
300 |
)
|
301 |
|
302 |
+
# Quality report: on click, return filepath to gr.File and reveal its group
|
303 |
+
def _prepare_report_for_download():
|
304 |
+
path = generator.get_quality_report_file()
|
305 |
+
if path:
|
306 |
+
return path, gr.update(visible=True)
|
307 |
+
else:
|
308 |
+
# keep group hidden and clear file if nothing
|
309 |
+
return None, gr.update(visible=False)
|
310 |
+
|
311 |
+
get_report_btn.click(
|
312 |
+
_prepare_report_for_download,
|
313 |
+
outputs=[report_file, report_group],
|
314 |
+
)
|
315 |
|
316 |
+
# Generate data
|
317 |
generate_btn.click(generate_data, inputs=[gen_size], outputs=[synthetic_data, gen_status])
|
318 |
|
319 |
# Build comparison plot when both datasets are available
|
320 |
synthetic_data.change(create_comparison_plot, inputs=[uploaded_data, synthetic_data], outputs=[comparison_plot])
|
321 |
|
322 |
+
# CSV download: write CSV and reveal the file link
|
323 |
+
def _prepare_csv_for_download():
|
324 |
+
path = download_csv_prepare()
|
325 |
+
if path:
|
326 |
+
return path, gr.update(visible=True)
|
327 |
+
else:
|
328 |
+
return None, gr.update(visible=False)
|
329 |
+
|
330 |
+
csv_download_btn.click(
|
331 |
+
_prepare_csv_for_download,
|
332 |
+
outputs=[csv_file, csv_group],
|
333 |
+
)
|
334 |
|
335 |
# File upload handler
|
336 |
def process_uploaded_file(file):
|