Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,12 @@ from PIL import Image
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
from typing import List
|
| 12 |
|
| 13 |
-
# For simplicity, we directly use the fast functions.
|
| 14 |
-
# The `image_processor.py` file now contains the optimized versions.
|
| 15 |
from image_processor import enhance_image_fast, extract_text_from_image_fast, process_pdf_in_parallel
|
| 16 |
|
| 17 |
app = FastAPI(
|
| 18 |
title="High-Speed OCR API",
|
| 19 |
description="An API to extract text from images and PDFs, optimized for speed.",
|
| 20 |
-
version="
|
| 21 |
)
|
| 22 |
|
| 23 |
# CORS Middleware to allow requests from any origin
|
|
@@ -51,7 +49,6 @@ def read_root():
|
|
| 51 |
|
| 52 |
@app.post("/ocr-image", response_model=ImageOCRResponse, tags=["OCR"])
|
| 53 |
async def ocr_image_endpoint(file: UploadFile = File(...)):
|
| 54 |
-
"""Accepts an image, enhances it, and returns the extracted text."""
|
| 55 |
if not file.content_type.startswith("image/"):
|
| 56 |
raise HTTPException(status_code=400, detail="File must be an image.")
|
| 57 |
try:
|
|
@@ -61,11 +58,10 @@ async def ocr_image_endpoint(file: UploadFile = File(...)):
|
|
| 61 |
text = extract_text_from_image_fast(enhanced_image)
|
| 62 |
return {"filename": file.filename, "text": text}
|
| 63 |
except Exception as e:
|
| 64 |
-
raise HTTPException(status_code=500, detail=f"Error processing image OCR: {e}")
|
| 65 |
|
| 66 |
@app.post("/ocr-pdf", response_model=PDFOCRResponse, tags=["OCR"])
|
| 67 |
async def ocr_pdf_endpoint(file: UploadFile = File(...)):
|
| 68 |
-
"""Accepts a PDF, extracts text from each page in parallel, and returns structured results."""
|
| 69 |
if file.content_type != "application/pdf":
|
| 70 |
raise HTTPException(status_code=400, detail="File must be a PDF.")
|
| 71 |
try:
|
|
@@ -77,10 +73,7 @@ async def ocr_pdf_endpoint(file: UploadFile = File(...)):
|
|
| 77 |
"results": results
|
| 78 |
}
|
| 79 |
except Exception as e:
|
| 80 |
-
raise HTTPException(status_code=500, detail=f"Error processing PDF: {e}")
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
# Hugging Face Spaces expects the app to run on port 7860
|
| 85 |
-
port = int(os.environ.get("PORT", 7860))
|
| 86 |
-
uvicorn.run("app:app", host="0.0.0.0", port=port, reload=True)
|
|
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
from typing import List
|
| 12 |
|
|
|
|
|
|
|
| 13 |
from image_processor import enhance_image_fast, extract_text_from_image_fast, process_pdf_in_parallel
|
| 14 |
|
| 15 |
app = FastAPI(
|
| 16 |
title="High-Speed OCR API",
|
| 17 |
description="An API to extract text from images and PDFs, optimized for speed.",
|
| 18 |
+
version="5.0.0-hf-final"
|
| 19 |
)
|
| 20 |
|
| 21 |
# CORS Middleware to allow requests from any origin
|
|
|
|
| 49 |
|
| 50 |
@app.post("/ocr-image", response_model=ImageOCRResponse, tags=["OCR"])
|
| 51 |
async def ocr_image_endpoint(file: UploadFile = File(...)):
|
|
|
|
| 52 |
if not file.content_type.startswith("image/"):
|
| 53 |
raise HTTPException(status_code=400, detail="File must be an image.")
|
| 54 |
try:
|
|
|
|
| 58 |
text = extract_text_from_image_fast(enhanced_image)
|
| 59 |
return {"filename": file.filename, "text": text}
|
| 60 |
except Exception as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=f"Error processing image OCR: {str(e)}")
|
| 62 |
|
| 63 |
@app.post("/ocr-pdf", response_model=PDFOCRResponse, tags=["OCR"])
|
| 64 |
async def ocr_pdf_endpoint(file: UploadFile = File(...)):
|
|
|
|
| 65 |
if file.content_type != "application/pdf":
|
| 66 |
raise HTTPException(status_code=400, detail="File must be a PDF.")
|
| 67 |
try:
|
|
|
|
| 73 |
"results": results
|
| 74 |
}
|
| 75 |
except Exception as e:
|
| 76 |
+
raise HTTPException(status_code=500, detail=f"Error processing PDF: {str(e)}")
|
| 77 |
|
| 78 |
+
# The `if __name__ == "__main__":` block has been completely removed.
|
| 79 |
+
# The platform will import the `app` object and run it.
|
|
|
|
|
|
|
|
|