yukee1992 commited on
Commit
5c6748a
Β·
verified Β·
1 Parent(s): 385f56d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -8
app.py CHANGED
@@ -19,6 +19,9 @@ import uuid
19
  import hashlib
20
  from enum import Enum
21
  import random
 
 
 
22
 
23
  # External OCI API URL - YOUR BUCKET SAVING API
24
  OCI_API_BASE_URL = "https://yukee1992-oci-story-book.hf.space"
@@ -831,14 +834,98 @@ async def root():
831
  def get_app():
832
  return app
833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
834
  if __name__ == "__main__":
835
  import uvicorn
836
- print("πŸš€ Starting Complete Storybook Generator...")
837
- print("πŸ“š Features: API (n8n) + UI (Gradio) + Local File Management")
838
- print("πŸ”— OCI API:", OCI_API_BASE_URL)
839
- print("πŸ“ Local storage:", PERSISTENT_IMAGE_DIR)
840
- print("🌐 Web Interface: http://localhost:7860")
841
- print("πŸ”Œ API Endpoints: http://localhost:7860/api/")
842
 
843
- # Launch Gradio interface
844
- gradio_app.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  import hashlib
20
  from enum import Enum
21
  import random
22
+ # Add these imports at the top (after existing imports)
23
+ import threading
24
+ from fastapi.middleware.wsgi import WSGIMiddleware
25
 
26
  # External OCI API URL - YOUR BUCKET SAVING API
27
  OCI_API_BASE_URL = "https://yukee1992-oci-story-book.hf.space"
 
834
  def get_app():
835
  return app
836
 
837
+ # Mount Gradio under a specific path to avoid conflicts
838
+ app.mount("/ui", WSGIMiddleware(gradio_app.server))
839
+
840
+ # Enhanced root endpoint that explains the API structure
841
+ @app.get("/")
842
+ async def root():
843
+ return {
844
+ "message": "Storybook Generator API is running!",
845
+ "api_endpoints": {
846
+ "health_check": "GET /api/health",
847
+ "generate_storybook": "POST /api/generate-storybook",
848
+ "check_job_status": "GET /api/job-status/{job_id}",
849
+ "local_images": "GET /api/local-images"
850
+ },
851
+ "web_interface": "GET /ui",
852
+ "note": "Use API endpoints for programmatic access, /ui for testing interface"
853
+ }
854
+
855
+ # Add a simple test endpoint
856
+ @app.get("/api/test")
857
+ async def test_endpoint():
858
+ return {
859
+ "status": "success",
860
+ "message": "API is working correctly",
861
+ "timestamp": datetime.now().isoformat()
862
+ }
863
+
864
+ # For Hugging Face Spaces deployment
865
+ def get_app():
866
+ return app
867
+
868
  if __name__ == "__main__":
869
  import uvicorn
870
+ import os
871
+
872
+ # Check if we're running on Hugging Face Spaces
873
+ HF_SPACE = os.environ.get('SPACE_ID') is not None
 
 
874
 
875
+ if HF_SPACE:
876
+ print("πŸš€ Running on Hugging Face Spaces - Integrated Mode")
877
+ print("πŸ“š API endpoints available at: /api/*")
878
+ print("🎨 Web interface available at: /ui")
879
+ print("πŸ”Œ Both API and UI running on same port")
880
+
881
+ # On Hugging Face, we need to run only one server
882
+ # Gradio will be mounted at /ui, FastAPI at root
883
+ gradio_app.launch(
884
+ server_name="0.0.0.0",
885
+ server_port=7860,
886
+ share=False,
887
+ show_error=True,
888
+ quiet=False
889
+ )
890
+ else:
891
+ # Local development - run separate servers
892
+ print("πŸš€ Running locally - Separate API and UI servers")
893
+ print("πŸ“š API endpoints: http://localhost:8000/api/*")
894
+ print("🎨 Web interface: http://localhost:7860/ui")
895
+
896
+ def run_fastapi():
897
+ """Run FastAPI on port 8000 for API calls"""
898
+ uvicorn.run(
899
+ app,
900
+ host="0.0.0.0",
901
+ port=8000,
902
+ log_level="info",
903
+ access_log=False
904
+ )
905
+
906
+ def run_gradio():
907
+ """Run Gradio on port 7860 for web interface"""
908
+ gradio_app.launch(
909
+ server_name="0.0.0.0",
910
+ server_port=7860,
911
+ share=False,
912
+ show_error=True,
913
+ quiet=True
914
+ )
915
+
916
+ # Start both servers in separate threads
917
+ api_thread = threading.Thread(target=run_fastapi, daemon=True)
918
+ ui_thread = threading.Thread(target=run_gradio, daemon=True)
919
+
920
+ api_thread.start()
921
+ print("βœ… FastAPI server started on port 8000")
922
+
923
+ ui_thread.start()
924
+ print("βœ… Gradio server started on port 7860")
925
+
926
+ # Keep the main thread alive
927
+ try:
928
+ while True:
929
+ time.sleep(1)
930
+ except KeyboardInterrupt:
931
+ print("πŸ›‘ Shutting down servers...")