saifisvibin commited on
Commit
b4d12d0
·
verified ·
1 Parent(s): 4c26add

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -18
app.py CHANGED
@@ -827,23 +827,41 @@ with gr.Blocks(title="PDF Layout Extractor") as demo:
827
  )
828
 
829
 
830
- # Mount Gradio to FastAPI
831
- # We mount it at the root "/" to replace the old index.html,
832
- # OR we can mount it at "/gradio" and keep the old UI.
833
- # The user said "switch to gradio", implying replacement or primary usage.
834
- # HF Spaces expects the app at root.
835
- # But we have specific API endpoints.
836
- # Let's mount Gradio at "/" and move the custom API docs to "/docs" (already there).
837
- # Wait, if we mount at "/", it might shadow "/api" if not careful?
838
- # No, FastAPI routing takes precedence if defined before mounting, usually.
839
- # But `gr.mount_gradio_app` documentation says it mounts a sub-app.
840
- # If path="", it mounts at root.
841
- # Let's mount at "/" but ensure API routes work.
842
- # Actually, `mount_gradio_app` handles this gracefully if we pass the `app`.
843
-
844
- app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845
 
846
  if __name__ == "__main__":
847
- import uvicorn
848
- # Disable reload in production (HF Spaces) to prevent EADDRINUSE and loop issues
849
- uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
 
827
  )
828
 
829
 
830
+ # --------------------------------------------------------------------------------
831
+ # Integrate FastAPI routes into Gradio's app
832
+ # --------------------------------------------------------------------------------
833
+ # Gradio creates a FastAPI app internally. We add our API routes to it.
834
+ # This approach works better on HF Spaces than mounting Gradio on our FastAPI app.
835
+
836
+ # Access Gradio's internal FastAPI app
837
+ fastapi_app = demo.fastapi_app
838
+
839
+ # Add our existing routes to Gradio's FastAPI instance
840
+ # We need to re-register all our routes on this app
841
+ fastapi_app.add_middleware(
842
+ CORSMiddleware,
843
+ allow_origins=["*"],
844
+ allow_credentials=True,
845
+ allow_methods=["*"],
846
+ allow_headers=["*"],
847
+ )
848
+
849
+ # Mount static output directory
850
+ fastapi_app.mount("/output", StaticFiles(directory="output"), name="output")
851
+
852
+ # Re-register all API routes
853
+ fastapi_app.get("/api/docs", response_class=HTMLResponse, tags=["UI"], include_in_schema=False)(api_docs_redirect)
854
+ fastapi_app.get("/api/device-info", response_model=DeviceInfo, tags=["System"])(device_info_endpoint)
855
+ fastapi_app.post("/api/upload", response_model=TaskStartResponse, tags=["Processing"])(upload_files)
856
+ fastapi_app.get("/api/progress/{task_id}", response_model=ProgressResponse, tags=["Processing"])(get_progress)
857
+ fastapi_app.get("/api/pdf-list", response_model=PDFListResponse, tags=["Retrieval"])(pdf_list)
858
+ fastapi_app.get("/api/pdf-details/{pdf_stem}", tags=["Retrieval"])(pdf_details)
859
+ fastapi_app.post("/api/predict", tags=["Legacy"])(predict)
860
+ fastapi_app.post("/api/delete", tags=["Processing"])(delete_pdf)
861
+
862
+ # For HF Spaces: expose the Gradio demo as 'app'
863
+ app = demo
864
 
865
  if __name__ == "__main__":
866
+ # For local development
867
+ demo.launch(server_name="0.0.0.0", server_port=7860)