Spaces:
Paused
Paused
Update mainapp.py
Browse files- mainapp.py +39 -15
mainapp.py
CHANGED
|
@@ -1,43 +1,67 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
import asyncio
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
-
# Initialize once
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
-
@app.get("/")
|
| 15 |
-
def home ():
|
| 16 |
-
return JSONResponse(content={'message':'home page'})
|
| 17 |
@app.post("/ocr-llm")
|
| 18 |
async def ocr_llm_endpoint(file: UploadFile = File(...)):
|
|
|
|
| 19 |
if not file.filename.lower().endswith((".pdf", ".png", ".jpg", ".jpeg")):
|
| 20 |
raise HTTPException(status_code=400, detail="File must be PDF or image")
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
results = []
|
| 24 |
|
|
|
|
| 25 |
if file.filename.lower().endswith(".pdf"):
|
| 26 |
file_bytes = await file.read()
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
-
# Single image file
|
| 35 |
image_bytes = await file.read()
|
| 36 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 37 |
-
llm_results=await call_llm(image,"")
|
| 38 |
-
results.append({"page": 1, "llm": llm_results})
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
import asyncio
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
+
from llm import call_llm
|
| 8 |
+
|
| 9 |
app = FastAPI()
|
|
|
|
| 10 |
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def home():
|
| 13 |
+
return {"message": "home page"}
|
| 14 |
|
| 15 |
|
| 16 |
+
# 🔥 Process single image
|
| 17 |
+
async def process_image(image: Image.Image, page_num: int):
|
| 18 |
+
prompt = """
|
| 19 |
+
Extract all text and return ONLY valid JSON structured.
|
| 20 |
+
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
result = await call_llm(image, prompt)
|
| 24 |
+
|
| 25 |
+
return {
|
| 26 |
+
"page": page_num,
|
| 27 |
+
"llm": result
|
| 28 |
+
}
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
@app.post("/ocr-llm")
|
| 32 |
async def ocr_llm_endpoint(file: UploadFile = File(...)):
|
| 33 |
+
|
| 34 |
if not file.filename.lower().endswith((".pdf", ".png", ".jpg", ".jpeg")):
|
| 35 |
raise HTTPException(status_code=400, detail="File must be PDF or image")
|
| 36 |
+
|
| 37 |
try:
|
| 38 |
results = []
|
| 39 |
|
| 40 |
+
# ✅ HANDLE PDF
|
| 41 |
if file.filename.lower().endswith(".pdf"):
|
| 42 |
file_bytes = await file.read()
|
| 43 |
+
doc = fitz.open(stream=file_bytes, filetype="pdf")
|
| 44 |
+
|
| 45 |
+
tasks = []
|
| 46 |
|
| 47 |
+
for i, page in enumerate(doc):
|
| 48 |
+
pix = page.get_pixmap()
|
| 49 |
+
img_bytes = pix.tobytes("png")
|
| 50 |
+
image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 51 |
|
| 52 |
+
tasks.append(process_image(image, i + 1))
|
| 53 |
+
|
| 54 |
+
results = await asyncio.gather(*tasks)
|
| 55 |
+
|
| 56 |
+
# ✅ HANDLE IMAGE
|
| 57 |
else:
|
|
|
|
| 58 |
image_bytes = await file.read()
|
| 59 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
result = await process_image(image, 1)
|
| 62 |
+
results.append(result)
|
| 63 |
+
|
| 64 |
+
return {"results": results}
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
raise HTTPException(status_code=500, detail=str(e))
|