Spaces:
Running
Running
import os | |
import sys | |
cur_dir = os.getcwd() | |
parent_dir = os.path.realpath(os.path.join(os.path.dirname(cur_dir))) | |
if parent_dir not in sys.path: | |
sys.path.append(parent_dir) | |
sys.path.append(cur_dir) | |
sys.path.insert(1, ".") | |
from fastapi import FastAPI, UploadFile, File | |
from extraction import process_document | |
from fastapi.responses import JSONResponse | |
from app.config import * | |
import tempfile | |
import shutil | |
import json | |
import uvicorn | |
app = FastAPI( | |
title="PDF Processor API", | |
description="Upload a PDF and get structured document data", | |
version="1.0.0", | |
docs_url="/docs", | |
redoc_url="/redoc", | |
openapi_url="/openapi.json", | |
root_path="/spaces/mussie1212/melhiq_ocr" | |
) | |
def root(): | |
return {"status": "ok", "routes": [str(r.path) for r in app.routes]} | |
async def process_pdf(file: UploadFile = File(...)): | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp: | |
shutil.copyfileobj(file.file, tmp) | |
tmp_path = tmp.name | |
result = process_document(tmp_path, api_key=ConfigStaticFiles.gemini_api_key) | |
if isinstance(result, str): | |
result = json.loads(result) | |
return JSONResponse(content=result) |