Experiments / app.py
credent007's picture
Update app.py
c477faa verified
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import HTMLResponse
import shutil
from inference import process_document
# from json_handling import process_whole_doc
from model_loader import get_model
app = FastAPI()
@app.on_event("startup")
def startup_event():
print("Starting application...")
print("Loading model before API becomes ready...")
get_model()
print("Startup complete. API is ready.")
@app.get("/", response_class=HTMLResponse)
def root():
return """
<html>
<head>
<title>Qwen Extraction API</title>
</head>
<body>
<h2>✅ Qwen Extraction API Running</h2>
<p>Use POST /extract to upload file</p>
<a href="/docs">Go to API Docs</a>
</body>
</html>
"""
@app.post("/extract")
async def extract(file: UploadFile = File(...)):
file_path = f"/tmp/{file.filename}"
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
result = process_document(file_path)
# result = process_whole_doc(file_path)
# return {"result": result}
return result