saifisvibin commited on
Commit
8749b48
·
verified ·
1 Parent(s): b4d12d0

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +15 -38
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 (sync for Gradio simplicity, or use utils)
766
- # We need to load model if not loaded
767
- load_model_once()
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=None,
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 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)
 
 
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
+