Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -410,26 +410,27 @@ async def visualize_with_code(
|
|
410 |
filters: Optional[str] = Form(None)
|
411 |
):
|
412 |
try:
|
413 |
-
# Validate file
|
414 |
file_ext, content = await process_uploaded_file(file)
|
|
|
415 |
if file_ext not in {"xlsx", "xls"}:
|
416 |
-
raise HTTPException(400, "
|
417 |
-
|
418 |
-
# Read Excel file
|
419 |
df = pd.read_excel(io.BytesIO(content))
|
420 |
-
|
421 |
-
|
422 |
-
|
|
|
|
|
|
|
423 |
if filters:
|
424 |
try:
|
425 |
-
|
426 |
-
if not isinstance(
|
427 |
-
|
428 |
-
except:
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
vis_request = VisualizationRequest(
|
433 |
chart_type=chart_type,
|
434 |
x_column=x_column,
|
435 |
y_column=y_column,
|
@@ -438,41 +439,17 @@ async def visualize_with_code(
|
|
438 |
x_label=x_label,
|
439 |
y_label=y_label,
|
440 |
style=style,
|
441 |
-
filters=
|
442 |
)
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
# Execute the code to generate the plot
|
448 |
-
plt.figure()
|
449 |
-
local_vars = {}
|
450 |
-
exec(visualization_code, globals(), local_vars)
|
451 |
-
|
452 |
-
# Save the plot to a temporary file
|
453 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|
454 |
-
plt.savefig(tmpfile.name, format='png', dpi=300)
|
455 |
-
plt.close()
|
456 |
-
|
457 |
-
# Read the image back as bytes
|
458 |
-
with open(tmpfile.name, "rb") as f:
|
459 |
-
image_bytes = f.read()
|
460 |
-
|
461 |
-
# Encode image as base64
|
462 |
-
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
463 |
-
|
464 |
-
return {
|
465 |
-
"status": "success",
|
466 |
-
"image": f"data:image/png;base64,{image_base64}",
|
467 |
-
"code": visualization_code,
|
468 |
-
"data_preview": df.head().to_dict(orient='records')
|
469 |
-
}
|
470 |
-
|
471 |
except HTTPException:
|
472 |
raise
|
473 |
except Exception as e:
|
474 |
-
logger.error(f"Visualization failed: {str(e)}
|
475 |
-
raise HTTPException(500,
|
476 |
|
477 |
@app.post("/visualize/natural")
|
478 |
@limiter.limit("5/minute")
|
|
|
410 |
filters: Optional[str] = Form(None)
|
411 |
):
|
412 |
try:
|
|
|
413 |
file_ext, content = await process_uploaded_file(file)
|
414 |
+
|
415 |
if file_ext not in {"xlsx", "xls"}:
|
416 |
+
raise HTTPException(400, "Visualization is only supported for Excel files")
|
417 |
+
|
|
|
418 |
df = pd.read_excel(io.BytesIO(content))
|
419 |
+
|
420 |
+
if df.empty:
|
421 |
+
raise HTTPException(400, "The uploaded Excel file is empty")
|
422 |
+
|
423 |
+
# Convert filters from string to dictionary safely
|
424 |
+
filters_dict = None
|
425 |
if filters:
|
426 |
try:
|
427 |
+
filters_dict = ast.literal_eval(filters)
|
428 |
+
if not isinstance(filters_dict, dict):
|
429 |
+
raise ValueError()
|
430 |
+
except Exception:
|
431 |
+
raise HTTPException(400, "Invalid format for filters. Must be a valid dictionary string.")
|
432 |
+
|
433 |
+
viz_request = VisualizationRequest(
|
|
|
434 |
chart_type=chart_type,
|
435 |
x_column=x_column,
|
436 |
y_column=y_column,
|
|
|
439 |
x_label=x_label,
|
440 |
y_label=y_label,
|
441 |
style=style,
|
442 |
+
filters=filters_dict
|
443 |
)
|
444 |
+
|
445 |
+
code = generate_visualization_code(df, viz_request)
|
446 |
+
return {"code": code}
|
447 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
448 |
except HTTPException:
|
449 |
raise
|
450 |
except Exception as e:
|
451 |
+
logger.error(f"Visualization code generation failed: {str(e)}")
|
452 |
+
raise HTTPException(500, f"Visualization code generation failed: {str(e)}")
|
453 |
|
454 |
@app.post("/visualize/natural")
|
455 |
@limiter.limit("5/minute")
|