Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- api/models.py +49 -28
- api/routes.py +123 -78
api/models.py
CHANGED
|
@@ -1,28 +1,49 @@
|
|
| 1 |
-
from pydantic import BaseModel
|
| 2 |
-
from typing import List, Optional, Union
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
|
| 5 |
-
class SkillScore(BaseModel):
|
| 6 |
-
skill_name: str
|
| 7 |
-
skill_score: int
|
| 8 |
-
|
| 9 |
-
class
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from typing import List, Optional, Union
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
class SkillScore(BaseModel):
|
| 6 |
+
skill_name: str
|
| 7 |
+
skill_score: int
|
| 8 |
+
|
| 9 |
+
class HRQuestion(BaseModel):
|
| 10 |
+
question: str
|
| 11 |
+
answer: str
|
| 12 |
+
|
| 13 |
+
class ResumeAnalysisResponse(BaseModel):
|
| 14 |
+
candidate_name: str = ""
|
| 15 |
+
gender: Optional[str] = None
|
| 16 |
+
date_of_birth: Optional[str] = None
|
| 17 |
+
email: Optional[str] = None
|
| 18 |
+
mobile_number: Optional[str] = None
|
| 19 |
+
address: Optional[str] = None
|
| 20 |
+
pan_number: Optional[str] = None
|
| 21 |
+
aadhar_number: Optional[str] = None
|
| 22 |
+
passport_number: Optional[str] = None
|
| 23 |
+
highest_education: Optional[str] = None
|
| 24 |
+
university_name: Optional[str] = None
|
| 25 |
+
designation: Optional[str] = None
|
| 26 |
+
skills: Optional[str] = ""
|
| 27 |
+
skill_score: Optional[List[SkillScore]] = []
|
| 28 |
+
total_experience: Optional[Union[str, float]] = None
|
| 29 |
+
region: Optional[str] = None
|
| 30 |
+
relevant_experience: Optional[str] = None
|
| 31 |
+
experience_summary: Optional[str] = None
|
| 32 |
+
file_modified_date: Optional[datetime] = None
|
| 33 |
+
hr_questions: Optional[List[HRQuestion]] = []
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class LLMAssessment(BaseModel):
|
| 37 |
+
"""Model for LLM assessment results"""
|
| 38 |
+
overall_assessment: str
|
| 39 |
+
communication_effectiveness: str
|
| 40 |
+
technical_competency: str
|
| 41 |
+
strengths: List[str]
|
| 42 |
+
improvement_areas: List[str]
|
| 43 |
+
interview_score: int
|
| 44 |
+
hiring_recommendation: str
|
| 45 |
+
|
| 46 |
+
class AudioAnalysisResponse(BaseModel):
|
| 47 |
+
"""Response model for audio analysis - simplified to only show transcript and assessment"""
|
| 48 |
+
transcript: str
|
| 49 |
+
assessment: str
|
api/routes.py
CHANGED
|
@@ -1,79 +1,124 @@
|
|
| 1 |
-
from fastapi import APIRouter, File, UploadFile, Form, HTTPException, Response, status
|
| 2 |
-
import os
|
| 3 |
-
import urllib.parse
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from typing import Optional, Dict, Any
|
| 6 |
-
|
| 7 |
-
from api.models import ResumeAnalysisResponse
|
| 8 |
-
from services.resume_service import analyze_resume_document
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
class
|
| 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 |
)
|
|
|
|
| 1 |
+
from fastapi import APIRouter, File, UploadFile, Form, HTTPException, Response, status
|
| 2 |
+
import os
|
| 3 |
+
import urllib.parse
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import Optional, Dict, Any
|
| 6 |
+
|
| 7 |
+
from api.models import ResumeAnalysisResponse
|
| 8 |
+
from services.resume_service import analyze_resume_document
|
| 9 |
+
from api.models import AudioAnalysisResponse
|
| 10 |
+
from services.voice_service import AudioAnalyzer
|
| 11 |
+
|
| 12 |
+
router = APIRouter()
|
| 13 |
+
|
| 14 |
+
# Initialize services
|
| 15 |
+
audio_analyzer = AudioAnalyzer()
|
| 16 |
+
|
| 17 |
+
class StorageStatus(BaseModel):
|
| 18 |
+
stored: bool
|
| 19 |
+
id: Optional[str] = None
|
| 20 |
+
error: Optional[str] = None
|
| 21 |
+
|
| 22 |
+
class ExtendedResumeAnalysisResponse(ResumeAnalysisResponse):
|
| 23 |
+
id: Optional[str] = None
|
| 24 |
+
stored_in_db: bool = False
|
| 25 |
+
db_error: Optional[str] = None
|
| 26 |
+
|
| 27 |
+
@router.post("/analyze-resume", response_model=ExtendedResumeAnalysisResponse)
|
| 28 |
+
async def analyze_resume(
|
| 29 |
+
resume_file: UploadFile = File(...),
|
| 30 |
+
job_role: str = Form(...)
|
| 31 |
+
):
|
| 32 |
+
|
| 33 |
+
filename = urllib.parse.unquote(resume_file.filename)
|
| 34 |
+
_, file_extension = os.path.splitext(filename)
|
| 35 |
+
file_extension = file_extension.lower()
|
| 36 |
+
|
| 37 |
+
allowed_extensions = ['.pdf', '.docx']
|
| 38 |
+
if file_extension not in allowed_extensions:
|
| 39 |
+
raise HTTPException(
|
| 40 |
+
status_code=400,
|
| 41 |
+
detail=f"Only PDF and DOCX files are supported. Got: {file_extension}"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
file_bytes = await resume_file.read()
|
| 46 |
+
|
| 47 |
+
os.environ["STORE_ANALYSIS_RESULT"] = "true"
|
| 48 |
+
|
| 49 |
+
result = await analyze_resume_document(file_bytes, job_role, file_extension)
|
| 50 |
+
|
| 51 |
+
return result
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
raise HTTPException(status_code=500, detail=f"Error processing resume: {str(e)}")
|
| 55 |
+
|
| 56 |
+
@router.get("/candidate/{candidate_id}", response_model=ExtendedResumeAnalysisResponse)
|
| 57 |
+
async def get_candidate(candidate_id: str):
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
from services.db_service import get_supabase_client
|
| 61 |
+
|
| 62 |
+
clean_id = urllib.parse.unquote(candidate_id).strip('"')
|
| 63 |
+
|
| 64 |
+
supabase = get_supabase_client()
|
| 65 |
+
response = supabase.table("candidates").select("*").eq("id", clean_id).execute()
|
| 66 |
+
|
| 67 |
+
if not response.data or len(response.data) == 0:
|
| 68 |
+
raise HTTPException(
|
| 69 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 70 |
+
detail=f"Candidate with ID {clean_id} not found"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
candidate_data = response.data[0]
|
| 74 |
+
candidate_data["stored_in_db"] = True
|
| 75 |
+
|
| 76 |
+
return candidate_data
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
if isinstance(e, HTTPException):
|
| 80 |
+
raise e
|
| 81 |
+
raise HTTPException(
|
| 82 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 83 |
+
detail=f"Error retrieving candidate: {str(e)}"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
@router.post("/analyze/audio", response_model=AudioAnalysisResponse)
|
| 87 |
+
async def analyze_audio_recording(
|
| 88 |
+
file: UploadFile = File(...),
|
| 89 |
+
job_role: Optional[str] = Form(None)
|
| 90 |
+
):
|
| 91 |
+
"""
|
| 92 |
+
Analyze an audio recording of a candidate's introduction.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
file (UploadFile): The audio file to analyze. Supported formats: WAV, MP3, M4A
|
| 96 |
+
job_role (str, optional): The job role the candidate is applying for. Used for role-specific analysis.
|
| 97 |
+
|
| 98 |
+
Returns:
|
| 99 |
+
AudioAnalysisResponse: Comprehensive analysis of the audio recording including fluency,
|
| 100 |
+
grammar, confidence scores, and other metrics.
|
| 101 |
+
"""
|
| 102 |
+
try:
|
| 103 |
+
# Validate file type
|
| 104 |
+
allowed_extensions = {'.wav', '.mp3', '.m4a'}
|
| 105 |
+
file_ext = '.' + file.filename.split('.')[-1].lower()
|
| 106 |
+
if file_ext not in allowed_extensions:
|
| 107 |
+
raise HTTPException(
|
| 108 |
+
status_code=400,
|
| 109 |
+
detail=f"Invalid file type. Supported formats: {', '.join(allowed_extensions)}"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Read file content
|
| 113 |
+
file_content = await file.read()
|
| 114 |
+
|
| 115 |
+
# Analyze the audio with job role
|
| 116 |
+
analysis_result = audio_analyzer.analyze_audio(file_content, job_role=job_role)
|
| 117 |
+
|
| 118 |
+
return AudioAnalysisResponse(**analysis_result)
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
raise HTTPException(
|
| 122 |
+
status_code=500,
|
| 123 |
+
detail=f"Error analyzing audio: {str(e)}"
|
| 124 |
)
|