chenguittiMaroua commited on
Commit
85e9416
·
verified ·
1 Parent(s): 3fbc4f5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -0
main.py CHANGED
@@ -577,5 +577,45 @@ async def rate_limit_exceeded_handler(request: Request, exc: RateLimitExceeded):
577
  content={"detail": "Too many requests. Please try again later."}
578
  )
579
 
 
580
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
581
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
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__":
582
+ import uvicorn
583
+ from fastapi.testclient import TestClient
584
+ from io import BytesIO
585
+ import base64
586
+ from PIL import Image
587
+ import matplotlib.pyplot as plt
588
+
589
+ # 1. Start the app (or connect to a running instance)
590
+ client = TestClient(app)
591
+
592
+ # 2. Test the visualization endpoint
593
+ test_file = "test.xlsx" # Replace with your test file
594
+ test_prompt = "Show me a bar chart of sales by region"
595
+
596
+ # 3. Send request to your own API
597
+ with open(test_file, "rb") as f:
598
+ response = client.post(
599
+ "/visualize/natural",
600
+ files={"file": ("test.xlsx", f, "application/vnd.ms-excel")},
601
+ data={"prompt": test_prompt}
602
+ )
603
+
604
+ # 4. Check if successful
605
+ if response.status_code == 200:
606
+ result = response.json()
607
+ print("Visualization generated successfully!")
608
+
609
+ # 5. Decode and display the image
610
+ image_data = result["image"].split(",")[1] # Remove header
611
+ image_bytes = base64.b64decode(image_data)
612
+ image = Image.open(BytesIO(image_bytes))
613
+
614
+ plt.imshow(image)
615
+ plt.axis("off")
616
+ plt.show()
617
+ else:
618
+ print(f"Error: {response.status_code}\n{response.text}")
619
+
620
+ # 6. Optional: Run the server (if not already running)
621
  uvicorn.run(app, host="0.0.0.0", port=7860)