File size: 2,002 Bytes
19dc325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException, Depends, UploadFile, File, Form
from typing import Optional
from sqlalchemy.orm import Session
from app.api.deps import get_db
from app.services.similarity_service import SimilarityService
from app.services.jd_parser import JDParser

router = APIRouter()
similarity_service = SimilarityService()

@router.post("/match")
async def match_job_description(
    resume_id: str = Form(...),
    jd_text: Optional[str] = Form(None),
    jd_url: Optional[str] = Form(None),
    jd_file: Optional[UploadFile] = File(None),
    top_k: int = Form(3),
    db: Session = Depends(get_db)
):
    try:
        # Extract JD text
        job_description = ""
        
        if jd_text and jd_text.strip():
            job_description = JDParser.extract_from_text(jd_text)
        elif jd_url and jd_url.strip():
            job_description = JDParser.extract_from_url(jd_url)
        elif jd_file:
            content = await jd_file.read()
            if jd_file.filename.lower().endswith('.pdf'):
                job_description = JDParser.extract_from_pdf(content)
            elif jd_file.filename.lower().endswith(('.png', '.jpg', '.jpeg')):
                 job_description = JDParser.extract_from_image(content)
            else:
                raise HTTPException(status_code=400, detail="Unsupported file type")
        else:
             raise HTTPException(status_code=400, detail="No job description provided (text, url, or file required)")

        if not job_description or not job_description.strip():
             raise HTTPException(status_code=400, detail="Could not extract text from the provided job description input")

        results = similarity_service.find_similar_resumes(
            job_description, 
            k=top_k,
            resume_id=resume_id,
            db=db
        )
        return results
    except HTTPException as he:
        raise he
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))