Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -762,15 +762,14 @@ def gradio_process(pdf_file, mode_str):
|
|
| 762 |
include_images = (mode_str != "markdown")
|
| 763 |
include_markdown = (mode_str != "images")
|
| 764 |
|
| 765 |
-
# Process
|
| 766 |
-
#
|
| 767 |
-
|
| 768 |
-
extractor.USE_MULTIPROCESSING = False # Gradio usually runs in thread
|
| 769 |
|
| 770 |
extractor.process_pdf_with_pool(
|
| 771 |
dest_path,
|
| 772 |
output_dir,
|
| 773 |
-
pool=
|
| 774 |
extract_images=include_images,
|
| 775 |
extract_markdown=include_markdown
|
| 776 |
)
|
|
@@ -828,40 +827,18 @@ with gr.Blocks(title="PDF Layout Extractor") as demo:
|
|
| 828 |
|
| 829 |
|
| 830 |
# --------------------------------------------------------------------------------
|
| 831 |
-
# Integrate
|
| 832 |
# --------------------------------------------------------------------------------
|
| 833 |
-
# Gradio
|
| 834 |
-
|
| 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 |
-
#
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 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 |
-
|
| 867 |
-
|
|
|
|
|
|
| 762 |
include_images = (mode_str != "markdown")
|
| 763 |
include_markdown = (mode_str != "images")
|
| 764 |
|
| 765 |
+
# Process using the multiprocessing pool for speed
|
| 766 |
+
# The global pool is already initialized in lifespan
|
| 767 |
+
extractor.USE_MULTIPROCESSING = True
|
|
|
|
| 768 |
|
| 769 |
extractor.process_pdf_with_pool(
|
| 770 |
dest_path,
|
| 771 |
output_dir,
|
| 772 |
+
pool=_pool, # Use the global pool instead of None
|
| 773 |
extract_images=include_images,
|
| 774 |
extract_markdown=include_markdown
|
| 775 |
)
|
|
|
|
| 827 |
|
| 828 |
|
| 829 |
# --------------------------------------------------------------------------------
|
| 830 |
+
# Integrate Gradio with FastAPI
|
| 831 |
# --------------------------------------------------------------------------------
|
| 832 |
+
# Mount Gradio at /gradio path (this ensures static files work correctly)
|
| 833 |
+
app = gr.mount_gradio_app(app, demo, path="/gradio")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 834 |
|
| 835 |
+
# Redirect root to Gradio interface
|
| 836 |
+
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
|
| 837 |
+
async def root_redirect():
|
| 838 |
+
"""Redirect to Gradio interface."""
|
| 839 |
+
return HTMLResponse('<meta http-equiv="refresh" content="0; url=/gradio" />')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 840 |
|
| 841 |
if __name__ == "__main__":
|
| 842 |
+
import uvicorn
|
| 843 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
|
| 844 |
+
|