utkarsh134 commited on
Commit
9c0cf44
·
verified ·
1 Parent(s): f0379b3

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +97 -31
api/routes.py CHANGED
@@ -1,31 +1,97 @@
1
- from fastapi import APIRouter, File, UploadFile, Form, HTTPException
2
- import os
3
-
4
- from api.models import ResumeAnalysisResponse
5
- from services.resume_service import analyze_resume_document
6
-
7
- router = APIRouter()
8
-
9
- @router.post("/analyze-resume", response_model=ResumeAnalysisResponse)
10
- async def analyze_resume(
11
- resume_file: UploadFile = File(...),
12
- job_role: str = Form(...)
13
- ):
14
- _, file_extension = os.path.splitext(resume_file.filename)
15
- file_extension = file_extension.lower()
16
-
17
- allowed_extensions = ['.pdf', '.docx']
18
- if file_extension not in allowed_extensions:
19
- raise HTTPException(
20
- status_code=400,
21
- detail=f"Only PDF and DOCX files are supported. Got: {file_extension}"
22
- )
23
-
24
- try:
25
- file_bytes = await resume_file.read()
26
-
27
- result = analyze_resume_document(file_bytes, job_role, file_extension)
28
- return result
29
-
30
- except Exception as e:
31
- raise HTTPException(status_code=500, detail=f"Error processing resume: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, File, UploadFile, Form, HTTPException, Response, status
2
+ import os
3
+ from pydantic import BaseModel
4
+ from typing import Optional, Dict, Any
5
+
6
+ from api.models import ResumeAnalysisResponse
7
+ from services.resume_service import analyze_resume_document
8
+
9
+ router = APIRouter()
10
+
11
+ class StorageStatus(BaseModel):
12
+ stored: bool
13
+ id: Optional[str] = None
14
+ error: Optional[str] = None
15
+
16
+ class ExtendedResumeAnalysisResponse(ResumeAnalysisResponse):
17
+ id: Optional[str] = None
18
+ stored_in_db: bool = False
19
+ db_error: Optional[str] = None
20
+
21
+ @router.post("/analyze-resume", response_model=ExtendedResumeAnalysisResponse)
22
+ async def analyze_resume(
23
+ resume_file: UploadFile = File(...),
24
+ job_role: str = Form(...),
25
+ store_in_db: bool = Form(True)
26
+ ):
27
+ """
28
+ Analyze a resume and extract relevant information based on the job role.
29
+ Optionally store the results in a database.
30
+
31
+ Parameters:
32
+ - resume_file: PDF or DOCX resume file
33
+ - job_role: Target job role for relevance analysis
34
+ - store_in_db: Whether to store results in database (default: True)
35
+
36
+ Returns:
37
+ - Resume analysis with extraction status
38
+ """
39
+ _, file_extension = os.path.splitext(resume_file.filename)
40
+ file_extension = file_extension.lower()
41
+
42
+ allowed_extensions = ['.pdf', '.docx']
43
+ if file_extension not in allowed_extensions:
44
+ raise HTTPException(
45
+ status_code=400,
46
+ detail=f"Only PDF and DOCX files are supported. Got: {file_extension}"
47
+ )
48
+
49
+ try:
50
+ file_bytes = await resume_file.read()
51
+
52
+ # Set environment variable for storage preference
53
+ os.environ["STORE_ANALYSIS_RESULT"] = str(store_in_db).lower()
54
+
55
+ result = await analyze_resume_document(file_bytes, job_role, file_extension)
56
+
57
+ # Return success with storage status
58
+ return result
59
+
60
+ except Exception as e:
61
+ raise HTTPException(status_code=500, detail=f"Error processing resume: {str(e)}")
62
+
63
+ @router.get("/candidate/{candidate_id}", response_model=ExtendedResumeAnalysisResponse)
64
+ async def get_candidate(candidate_id: str):
65
+ """
66
+ Retrieve a previously analyzed candidate by ID.
67
+
68
+ Parameters:
69
+ - candidate_id: Database ID of the candidate
70
+
71
+ Returns:
72
+ - Complete candidate record
73
+ """
74
+ try:
75
+ from services.db_service import get_supabase_client
76
+
77
+ supabase = get_supabase_client()
78
+ response = supabase.table("candidates").select("*").eq("id", candidate_id).execute()
79
+
80
+ if not response.data or len(response.data) == 0:
81
+ raise HTTPException(
82
+ status_code=status.HTTP_404_NOT_FOUND,
83
+ detail=f"Candidate with ID {candidate_id} not found"
84
+ )
85
+
86
+ candidate_data = response.data[0]
87
+ candidate_data["stored_in_db"] = True
88
+
89
+ return candidate_data
90
+
91
+ except Exception as e:
92
+ if isinstance(e, HTTPException):
93
+ raise e
94
+ raise HTTPException(
95
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
96
+ detail=f"Error retrieving candidate: {str(e)}"
97
+ )