Spaces:
Sleeping
Sleeping
| import os | |
| import logging | |
| import tempfile | |
| from fastapi import FastAPI, UploadFile, File, HTTPException, Query | |
| from fastapi.concurrency import run_in_threadpool | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from src.services.cv_service import parse_cv | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI( | |
| title="CV Parser API", | |
| description="API for parsing CVs.", | |
| version="1.0.0", | |
| docs_url="/docs", | |
| redoc_url="/redoc" | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| from pydantic import BaseModel | |
| class HealthCheck(BaseModel): | |
| status: str = "ok" | |
| async def health_check(): | |
| return HealthCheck() | |
| async def parse_cv_endpoint( | |
| file: UploadFile = File(...) | |
| ): | |
| """ | |
| Parses a CV file (PDF) and returns the parsed data. | |
| """ | |
| if file.content_type != "application/pdf": | |
| raise HTTPException(status_code=400, detail="PDF file required") | |
| contents = await file.read() | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp: | |
| tmp.write(contents) | |
| tmp_path = tmp.name | |
| try: | |
| result = await parse_cv(tmp_path) | |
| finally: | |
| if os.path.exists(tmp_path): | |
| os.remove(tmp_path) | |
| if not result: | |
| raise HTTPException(status_code=500, detail="Failed to extract data from CV.") | |
| return result | |
| if __name__ == "__main__": | |
| import uvicorn | |
| port = int(os.getenv("PORT", 7860)) # Use PORT environment variable, default to 8001 | |
| uvicorn.run(app, host="0.0.0.0", port=port) | |