Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -827,23 +827,41 @@ with gr.Blocks(title="PDF Layout Extractor") as demo:
|
|
| 827 |
)
|
| 828 |
|
| 829 |
|
| 830 |
-
#
|
| 831 |
-
#
|
| 832 |
-
#
|
| 833 |
-
#
|
| 834 |
-
# HF Spaces
|
| 835 |
-
|
| 836 |
-
#
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
#
|
| 840 |
-
#
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 845 |
|
| 846 |
if __name__ == "__main__":
|
| 847 |
-
|
| 848 |
-
|
| 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)
|
|
|