File size: 2,386 Bytes
6da2b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import logging
import tempfile
import uuid
from langtrace_python_sdk import inject_additional_attributes

from fastapi import FastAPI, UploadFile, File, HTTPException

from fastapi.middleware.cors import CORSMiddleware

from src.services.cv_service import parse_cv
from langtrace_python_sdk import langtrace

langtrace.init(api_key=os.getenv("LANGTRACE_API_KEY"))

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="CV Parser API",
    description="parsing de CV agentique",
    version="2.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

ALLOWED_ORIGINS = os.getenv("CORS_ORIGINS", "http://localhost:8000").split(",")

app.add_middleware(
    CORSMiddleware,
    allow_origins=ALLOWED_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:
        session_id = str(uuid.uuid4())
        attributes = {
            "session.id": session_id,
            "user_id": session_id
        }

        async def _traced_parse():
            return await parse_cv(tmp_path)

        result = await inject_additional_attributes(
            _traced_parse,
            attributes
        )
    except Exception as e:
        logger.error(f"Error processing CV: {str(e)}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Internal Server Error: {str(e)}")
    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", 8001)) 
    uvicorn.run(app, host="0.0.0.0", port=port)