| \"\"\" |
| OmniParse AI — FastAPI Backend (HTML UI Ready). |
| Tento soubor slouží jako most mezi CORE logikou v app.py a budoucím HTML/JS frontendem. |
| Všechny business funkce se importují z app.py. |
| \"\"\" |
| |
| import os |
| from fastapi import FastAPI, UploadFile, File, Header, HTTPException, Depends |
| from fastapi.responses import JSONResponse |
| from fastapi.middleware.cors import CORSMiddleware |
| import app as core |
| |
| app = FastAPI(title="OmniParse AI API") |
| |
| # CORS pro HTML/JS frontend |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
| |
| # --- AUTH ENDPOINTY --- |
| |
| @app.post("/api/auth/signup") |
| async def api_signup(data: dict): |
| ok, msg, token = core.signup(data.get("name"), data.get("email"), data.get("password"), data.get("accepted_terms")) |
| if not ok: |
| raise HTTPException(status_code=400, detail=msg) |
| return {"message": msg, "token": token} |
| |
| @app.post("/api/auth/login") |
| async def api_login(data: dict): |
| ok, msg, token = core.login(data.get("email"), data.get("password")) |
| if not ok: |
| raise HTTPException(status_code=401, detail=msg) |
| return {"message": msg, "token": token} |
| |
| # --- BUSINESS ENDPOINTY (vyžadují token nebo API klíč) --- |
| |
| async def get_current_user(authorization: str = Header(None)): |
| if not authorization: |
| raise HTTPException(status_code=401, detail="Missing Authorization header") |
| |
| token = authorization.replace("Bearer ", "") |
| |
| # Zkusíme session token (pro UI) |
| user = core.resolve_session(token) |
| if user: |
| return user |
| |
| # Zkusíme API klíč (pro externí integrace) |
| # Poznámka: v SQLiteDB/SupabaseDB by se musela přidat funkce get_user_by_api_key |
| # Pro demo účely budeme hledat v DB ručně |
| db_user = core.DB.get_user_by_email("demo@omniparse.ai") # Placeholder pro ukázku |
| if db_user and db_user.get("api_key") == token: |
| return db_user |
| |
| raise HTTPException(status_code=401, detail="Invalid token or API key") |
| |
| @app.post("/api/extract") |
| async def api_extract(file: UploadFile = File(...), user: dict = Depends(get_current_user)): |
| # Uložíme dočasně soubor |
| temp_path = f"/tmp/{file.filename}" |
| with open(temp_path, "wb") as buffer: |
| buffer.write(await file.read()) |
| |
| # Zavoláme CORE pipeline |
| result = core.process_invoice_file(user, temp_path, file.filename) |
| |
| if "error" in result: |
| raise HTTPException(status_code=500, detail=result["error"]) |
| |
| return result |
| |
| @app.get("/api/invoices") |
| async def api_get_invoices(user: dict = Depends(get_current_user)): |
| return core.DB.get_invoices(user["id"]) |
| |
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=8000) |
| |