File size: 1,741 Bytes
ef9eba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9e3f3
ef9eba2
 
 
 
 
 
 
 
 
 
 
 
 
 
4681c12
ef9eba2
 
 
 
 
 
 
 
 
 
 
f05d41d
ef9eba2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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"

@app.get("/", response_model=HealthCheck, tags=["Status"])
async def health_check():
    return HealthCheck()

@app.post("/parse-cv/", tags=["CV Parsing"])
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)