Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import json
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 5 |
+
from paddleocr import PaddleOCRVL
|
| 6 |
+
|
| 7 |
+
# Initialize FastAPI
|
| 8 |
+
app = FastAPI(title="Document Ingestion API", description="PaddleOCR-VL Markdown/JSON extraction")
|
| 9 |
+
|
| 10 |
+
# Load the model once globally when the server starts
|
| 11 |
+
print("Initializing PaddleOCR-VL Pipeline...")
|
| 12 |
+
pipeline = PaddleOCRVL()
|
| 13 |
+
print("Pipeline ready!")
|
| 14 |
+
|
| 15 |
+
@app.post("/ingest")
|
| 16 |
+
async def ingest_document(file: UploadFile = File(...)):
|
| 17 |
+
if not file.filename:
|
| 18 |
+
raise HTTPException(status_code=400, detail="No file provided")
|
| 19 |
+
|
| 20 |
+
temp_dir = "temp_workspace"
|
| 21 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 22 |
+
file_path = os.path.join(temp_dir, file.filename)
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Save the uploaded file temporarily
|
| 26 |
+
with open(file_path, "wb") as buffer:
|
| 27 |
+
shutil.copyfileobj(file.file, buffer)
|
| 28 |
+
|
| 29 |
+
# Run the VLM prediction
|
| 30 |
+
output = pipeline.predict(file_path)
|
| 31 |
+
|
| 32 |
+
parsed_pages = []
|
| 33 |
+
|
| 34 |
+
for page_num, res in enumerate(output):
|
| 35 |
+
md_path = os.path.join(temp_dir, f"page_{page_num + 1}.md")
|
| 36 |
+
json_path = os.path.join(temp_dir, f"page_{page_num + 1}.json")
|
| 37 |
+
|
| 38 |
+
# Save using the model's native methods
|
| 39 |
+
res.save_to_markdown(save_path=md_path)
|
| 40 |
+
res.save_to_json(save_path=json_path)
|
| 41 |
+
|
| 42 |
+
# Read the contents back to send in the HTTP response
|
| 43 |
+
with open(md_path, "r", encoding="utf-8") as f:
|
| 44 |
+
md_content = f.read()
|
| 45 |
+
|
| 46 |
+
with open(json_path, "r", encoding="utf-8") as f:
|
| 47 |
+
json_content = json.load(f)
|
| 48 |
+
|
| 49 |
+
parsed_pages.append({
|
| 50 |
+
"page": page_num + 1,
|
| 51 |
+
"markdown": md_content,
|
| 52 |
+
"json_data": json_content
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"status": "success",
|
| 57 |
+
"filename": file.filename,
|
| 58 |
+
"total_pages": len(parsed_pages),
|
| 59 |
+
"data": parsed_pages
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 64 |
+
|
| 65 |
+
finally:
|
| 66 |
+
# Clean up the temporary files so the server doesn't run out of storage
|
| 67 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 68 |
+
|
| 69 |
+
# Hugging Face Spaces routes health checks to the root
|
| 70 |
+
@app.get("/")
|
| 71 |
+
def health_check():
|
| 72 |
+
return {"status": "active", "model": "PaddleOCR-VL"}
|