Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -576,6 +576,47 @@ async def rate_limit_exceeded_handler(request: Request, exc: RateLimitExceeded):
|
|
576 |
status_code=429,
|
577 |
content={"detail": "Too many requests. Please try again later."}
|
578 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
579 |
|
580 |
# ===== ADD THIS AT THE BOTTOM OF main.py =====
|
581 |
if __name__ == "__main__":
|
|
|
576 |
status_code=429,
|
577 |
content={"detail": "Too many requests. Please try again later."}
|
578 |
)
|
579 |
+
import gradio as gr
|
580 |
+
|
581 |
+
# Gradio interface for visualization
|
582 |
+
def gradio_visualize(file, prompt, style="seaborn-v0_8"):
|
583 |
+
# Call your existing FastAPI endpoint
|
584 |
+
with open(file.name, "rb") as f:
|
585 |
+
response = client.post(
|
586 |
+
"/visualize/natural",
|
587 |
+
files={"file": f},
|
588 |
+
data={"prompt": prompt, "style": style}
|
589 |
+
)
|
590 |
+
result = response.json()
|
591 |
+
|
592 |
+
# Return both image and code
|
593 |
+
return (
|
594 |
+
result["image"], # Base64 image
|
595 |
+
f"```python\n{result['code']}\n```" # Code with Markdown formatting
|
596 |
+
)
|
597 |
+
|
598 |
+
# Create Gradio interface
|
599 |
+
visualization_interface = gr.Interface(
|
600 |
+
fn=gradio_visualize,
|
601 |
+
inputs=[
|
602 |
+
gr.File(label="Upload Excel File", type="filepath"),
|
603 |
+
gr.Textbox(label="Visualization Prompt", placeholder="e.g., 'Show sales by region'"),
|
604 |
+
gr.Dropdown(label="Style", choices=plt.style.available, value="seaborn-v0_8")
|
605 |
+
],
|
606 |
+
outputs=[
|
607 |
+
gr.Image(label="Generated Visualization"), # Auto-handles base64
|
608 |
+
gr.Markdown(label="Generated Code") # Renders code with syntax highlighting
|
609 |
+
],
|
610 |
+
title="📊 Data Visualizer",
|
611 |
+
description="Upload an Excel file and describe the visualization you want"
|
612 |
+
)
|
613 |
+
|
614 |
+
# Mount Gradio to your FastAPI app
|
615 |
+
app = gr.mount_gradio_app(app, visualization_interface, path="/gradio")
|
616 |
+
|
617 |
+
|
618 |
+
|
619 |
+
|
620 |
|
621 |
# ===== ADD THIS AT THE BOTTOM OF main.py =====
|
622 |
if __name__ == "__main__":
|