Download app.py from Muktaryy/document: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/Muktaryy/document/resolve/main/app.py
- Command line
-
hf download hf://spaces/Muktaryy/document/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Muktaryy/document/resolve/main/app.py
1.03 kB
| from fastapi import FastAPI, UploadFile, File | |
| from PIL import Image | |
| import pytesseract | |
| import fitz # PyMuPDF | |
| import io | |
| import uuid | |
| app = FastAPI() | |
| def home(): | |
| return { | |
| "status": "running", | |
| "usage": "POST /extract (image or pdf file)" | |
| } | |
| # ------------------------- | |
| # MAIN DOCUMENT PROCESSOR | |
| # ------------------------- | |
| async def extract(file: UploadFile = File(...)): | |
| content = await file.read() | |
| filename = file.filename.lower() | |
| text = "" | |
| # ------------------------- | |
| # PDF HANDLING | |
| # ------------------------- | |
| if filename.endswith(".pdf"): | |
| doc = fitz.open(stream=content, filetype="pdf") | |
| for page in doc: | |
| text += page.get_text() | |
| # ------------------------- | |
| # IMAGE OCR HANDLING | |
| # ------------------------- | |
| else: | |
| image = Image.open(io.BytesIO(content)) | |
| text = pytesseract.image_to_string(image) | |
| return { | |
| "filename": file.filename, | |
| "text": text.strip() | |
| } |