Mube commited on
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647from fastapi import FastAPI, UploadFile, Filefrom PIL import Imageimport pytesseractimport fitz # PyMuPDFimport ioimport uuid
app = FastAPI()
@app.get("/")def home(): return { "status": "running", "usage": "POST /extract (image or pdf file)" }
# -------------------------# MAIN DOCUMENT PROCESSOR# -------------------------@app.post("/extract")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() }app.pyAdded+47