utkarsh134 commited on
Commit
bd093cc
·
verified ·
1 Parent(s): 07a4669

Upload 4 files

Browse files
services/audio_error_handling.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ # Configure logging
6
+ LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
7
+ LOG_LEVEL = logging.INFO
8
+ LOG_DIR = Path(__file__).parent.parent / 'logs'
9
+ LOG_DIR.mkdir(exist_ok=True)
10
+
11
+ # File handler
12
+ file_handler = logging.FileHandler(LOG_DIR / 'audio_processing.log')
13
+ file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
14
+
15
+ # Console handler
16
+ console_handler = logging.StreamHandler(sys.stdout)
17
+ console_handler.setFormatter(logging.Formatter(LOG_FORMAT))
18
+
19
+ # Create logger
20
+ logger = logging.getLogger('audio_service')
21
+ logger.setLevel(LOG_LEVEL)
22
+ logger.addHandler(file_handler)
23
+ logger.addHandler(console_handler)
24
+
25
+ class AudioProcessingError(Exception):
26
+ """Base exception for audio processing errors"""
27
+ pass
28
+
29
+ class AudioTranscriptionError(AudioProcessingError):
30
+ """Exception raised for errors during audio transcription"""
31
+ pass
32
+
33
+ class AudioAnalysisError(AudioProcessingError):
34
+ """Exception raised for errors during audio analysis"""
35
+ pass
36
+
37
+ class AudioFormatError(AudioProcessingError):
38
+ """Exception raised for unsupported audio formats"""
39
+ pass
40
+
41
+ def handle_audio_error(func):
42
+ """Decorator for handling audio processing errors"""
43
+ def wrapper(*args, **kwargs):
44
+ try:
45
+ return func(*args, **kwargs)
46
+ except AudioProcessingError as e:
47
+ logger.error(f"Audio processing error in {func.__name__}: {str(e)}")
48
+ raise
49
+ except Exception as e:
50
+ logger.error(f"Unexpected error in {func.__name__}: {str(e)}", exc_info=True)
51
+ raise AudioProcessingError(f"An unexpected error occurred: {str(e)}")
52
+ return wrapper
services/llm_service.py CHANGED
@@ -1,89 +1,108 @@
1
- from langchain_google_genai import ChatGoogleGenerativeAI
2
- from langchain.prompts import PromptTemplate
3
- from config.settings import get_settings
4
-
5
-
6
- def get_llm():
7
- settings = get_settings()
8
- return ChatGoogleGenerativeAI(
9
- model=settings["llm_model"],
10
- temperature=settings["temperature"],
11
- google_api_key=settings["google_api_key"],
12
- )
13
-
14
-
15
- def get_unified_resume_prompt():
16
- return PromptTemplate(
17
- input_variables=["resume_text", "relevant_content", "job_role"],
18
- template='''You are a professional resume analyzer with expertise in technical recruiting.
19
-
20
- Your task is to extract both basic information and relevant work experience from the resume.
21
-
22
- RESUME CONTENT:
23
- {resume_text}
24
-
25
- MOST RELEVANT SECTIONS (semantically similar to job requirements):
26
- {relevant_content}
27
-
28
- TARGET JOB ROLE/DESCRIPTION: {job_role}
29
-
30
- TASK INSTRUCTIONS:
31
- Basic Information:
32
- 1. Extract all personally identifiable information that is explicitly stated in the text
33
- 2. For fields where information is not found, use null
34
- 3. Do not invent or infer information not present in the text
35
- 4. For education, only return the highest level completed (postgraduate > undergraduate > 12th > 10th)
36
- 5. Extract the university/institution name for the highest education qualification
37
- 6. For role, use the most recent/current position mentioned
38
- 7. Return complete values - do not truncate or summarize unnecessarily
39
- 8. Maintain exact formatting/capitalization as shown in the resume
40
- 9. Extract all contact information including email and phone number (just the primary number)
41
- 10. Look for any government ID numbers (PAN, Aadhar, Passport) that might be present
42
-
43
- Experience Analysis:
44
- 1. Create a single concise paragraph summarizing ALL relevant professional experience that directly relates to the job role
45
- 2. Focus only on experiences that explicitly demonstrate use of the exact technologies/tools required
46
- 3. If NO relevant experience is found, return null for relevant_experience
47
- 4. Provide a brief summary analyzing how well the candidate's experience matches the job role
48
-
49
- Skill Scoring:
50
- 1. Identify all technical and professional skills mentioned in the resume
51
- 2. For each skill, count its occurrences:
52
- - If mentioned under projects or experience sections: 5 points per occurrence
53
- - If mentioned outside projects/experience sections: 3 points per occurrence
54
- 3. Calculate total score for each skill by summing up all occurrences
55
- 4. Include both the skill name and its calculated score
56
-
57
- FORMAT YOUR RESPONSE AS A VALID JSON OBJECT WITH THIS EXACT STRUCTURE:
58
- {{
59
- "candidate_name": "full name of the candidate",
60
- "gender": "gender if mentioned",
61
- "date_of_birth": "DOB in YYYY/MM/DD format if mentioned",
62
- "email": "email address if mentioned",
63
- "mobile_number": "primary phone number if mentioned",
64
- "address": "complete physical address if mentioned",
65
- "pan_number": "PAN number if mentioned",
66
- "aadhar_number": "Aadhar number if mentioned",
67
- "passport_number": "Passport number if mentioned",
68
- "highest_education": "most advanced completed education level, interpret from the data and return only 4 of these options : 10th, 12th, Undergraduate, PostGraduate",
69
- "university_name": "name of the university/institution where highest education was completed",
70
- "designation": "current or most recent role/position",
71
- "skills": "comma-separated list of technical and professional skills",
72
- "skill_score": [
73
- {{"skill_name": "name of the skill", "skill_score": numerical_score}},
74
- ...
75
- ],
76
- "total_experience": "total work experience in terms of years i.e., if experience is 6 months return "0.5", divide the number of months found by 12 and return the number as a string",
77
- "region": "geographical region/city of work",
78
- "relevant_experience": "single paragraph summarizing all experience relevant to the job role. If no relevant experience found,reply with a sentence describing they have no relevant experience",
79
- "experience_summary": "brief analysis of how well the experience matches the job role"
80
- }}
81
-
82
- IMPORTANT:
83
- - DO NOT INVENT OR ADD INFORMATION that is not in the resume
84
- - BE EXTREMELY STRICT about experience relevance
85
- - Response must be a valid JSON object
86
- - For sensitive information like Aadhar/PAN, return only if explicitly mentioned in resume
87
- - Skills should be returned as a comma-separated string (e.g. "Java, Spring Boot, Docker, AWS, PostgreSQL")
88
- - Calculate skill scores accurately based on occurrences in projects/experience (5 points) vs elsewhere (3 points)'''
89
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_google_genai import ChatGoogleGenerativeAI
2
+ from langchain.prompts import PromptTemplate
3
+ from config.settings import get_settings
4
+
5
+
6
+ def get_llm():
7
+ settings = get_settings()
8
+ return ChatGoogleGenerativeAI(
9
+ model=settings["llm_model"],
10
+ temperature=settings["temperature"],
11
+ google_api_key=settings["google_api_key"],
12
+ )
13
+
14
+
15
+ def get_unified_resume_prompt():
16
+ return PromptTemplate(
17
+ input_variables=["resume_text", "relevant_content", "job_role"],
18
+ template='''You are a professional resume analyzer with expertise in technical recruiting.
19
+
20
+ Your task is to extract both basic information and relevant work experience from the resume.
21
+
22
+ RESUME CONTENT:
23
+ {resume_text}
24
+
25
+ MOST RELEVANT SECTIONS (semantically similar to job requirements):
26
+ {relevant_content}
27
+
28
+ TARGET JOB ROLE/DESCRIPTION: {job_role}
29
+
30
+ TASK INSTRUCTIONS:
31
+ Basic Information:
32
+ 1. Extract all personally identifiable information that is explicitly stated in the text
33
+ 2. For fields where information is not found, use null
34
+ 3. Do not invent or infer information not present in the text
35
+ 4. For education, only return the highest level completed (postgraduate > undergraduate > 12th > 10th)
36
+ 5. Extract the university/institution name for the highest education qualification
37
+ 6. For role, use the most recent/current position mentioned
38
+ 7. Return complete values - do not truncate or summarize unnecessarily
39
+ 8. Maintain exact formatting/capitalization as shown in the resume
40
+ 9. Extract all contact information including email and phone number (just the primary number)
41
+ 10. Look for any government ID numbers (PAN, Aadhar, Passport) that might be present
42
+
43
+ Experience Analysis:
44
+ 1. Create a single concise paragraph summarizing ALL relevant professional experience that directly relates to the job role
45
+ 2. Focus only on experiences that explicitly demonstrate use of the exact technologies/tools required
46
+ 3. If NO relevant experience is found, return null for relevant_experience
47
+ 4. Provide a brief summary analyzing how well the candidate's experience matches the job role
48
+
49
+ Skill Scoring:
50
+ 1. Identify all technical and professional skills mentioned in the resume which are relevant to the job role provided
51
+ 2. For each skill, count its occurrences:
52
+ - If mentioned under projects or experience sections: 5 points per occurrence
53
+ - If mentioned outside projects/experience sections: 3 points per occurrence
54
+ 3. Calculate total score for each skill by summing up all occurrences
55
+ 4. Include both the skill name and its calculated score
56
+
57
+ HR Questions Generation:
58
+ 1. Generate as many relevant interview questions based on the candidate's experience and the job role
59
+ 2. For each question, provide the expected answer based on information found in the resume
60
+ 3. Focus questions on:
61
+ - Technical skills and their practical application
62
+ - Project experiences and challenges
63
+ - Role-specific competencies
64
+ - Career transitions or gaps if any
65
+ 4. Questions should be specific to this candidate's background
66
+ 5. Answers should strictly reflect information available in the resume
67
+
68
+ FORMAT YOUR RESPONSE AS A VALID JSON OBJECT WITH THIS EXACT STRUCTURE:
69
+ {{
70
+ "candidate_name": "full name of the candidate",
71
+ "gender": "gender if mentioned",
72
+ "date_of_birth": "DOB in YYYY/MM/DD format if mentioned",
73
+ "email": "email address if mentioned",
74
+ "mobile_number": "primary phone number if mentioned",
75
+ "address": "complete physical address if mentioned",
76
+ "pan_number": "PAN number if mentioned",
77
+ "aadhar_number": "Aadhar number if mentioned",
78
+ "passport_number": "Passport number if mentioned",
79
+ "highest_education": "most advanced completed education level, interpret from the data and return only 4 of these options : 10th, 12th, Undergraduate, PostGraduate",
80
+ "university_name": "name of the university/institution where highest education was completed",
81
+ "designation": "current or most recent role/position",
82
+ "skills": "comma-separated list of technical and professional skills",
83
+ "skill_score": [
84
+ {{"skill_name": "name of the skill", "skill_score": numerical_score}},
85
+ ...
86
+ ],
87
+ "total_experience": "total work experience in terms of years i.e., if experience is 6 months return "0.5", divide the number of months found by 12 and return the number as a string",
88
+ "region": "geographical region/city of work",
89
+ "relevant_experience": "single paragraph summarizing all experience relevant to the job role. If no relevant experience found,reply with a sentence describing they have no relevant experience",
90
+ "experience_summary": "brief analysis of how well the experience matches the job role",
91
+ "hr_questions": [
92
+ {{
93
+ "question": "relevant interview question based on resume content and job role",
94
+ "answer": "expected answer based on the resume content"
95
+ }},
96
+ ...
97
+ ]
98
+ }}
99
+
100
+ IMPORTANT:
101
+ - DO NOT INVENT OR ADD INFORMATION that is not in the resume
102
+ - BE EXTREMELY STRICT about experience relevance
103
+ - Response must be a valid JSON object
104
+ - For sensitive information like Aadhar/PAN, return only if explicitly mentioned in resume
105
+ - Skills should be returned as a comma-separated string (e.g. "Java, Spring Boot, Docker, AWS, PostgreSQL")
106
+ - Calculate skill scores accurately based on occurrences in projects/experience (5 points) vs elsewhere (3 points)
107
+ - HR questions should be relevant to both the candidate's experience and the target job role'''
108
+ )
services/resume_service.py CHANGED
@@ -1,145 +1,147 @@
1
- import json
2
- from typing import Dict
3
- from datetime import datetime
4
-
5
- from core.doc_processor import (
6
- extract_text_from_bytes,
7
- extract_text_from_docx,
8
- parse_pdf_date
9
- )
10
- from core.text_processor import chunk_text
11
- from core.vector_store import create_or_load_vectorstore, similarity_search
12
- from services.llm_service import get_llm, get_unified_resume_prompt
13
- from services.db_service import store_candidate_data
14
-
15
-
16
- async def analyze_resume_document(
17
- file_bytes: bytes, job_role: str, file_extension: str
18
- ) -> Dict:
19
- if file_extension.lower() == ".pdf":
20
- resume_text, mod_date = extract_text_from_bytes(file_bytes)
21
- file_modified_date = parse_pdf_date(mod_date) if mod_date else datetime.now()
22
- elif file_extension.lower() == ".docx":
23
- resume_text, mod_date = extract_text_from_docx(file_bytes)
24
- file_modified_date = mod_date if mod_date else datetime.now()
25
- else:
26
- raise ValueError(f"Unsupported file format: {file_extension}")
27
-
28
- if resume_text.startswith("Error"):
29
- raise ValueError(resume_text)
30
-
31
- chunks = chunk_text(resume_text)
32
- vector_store = create_or_load_vectorstore(chunks)
33
-
34
- analysis_result = analyze_resume_with_llm(vector_store, resume_text, job_role, file_modified_date)
35
-
36
- try:
37
- db_record = await store_candidate_data(analysis_result)
38
- analysis_result["id"] = db_record.get("id")
39
- analysis_result["stored_in_db"] = True
40
- except Exception as e:
41
- print(f"Error storing candidate data: {str(e)}")
42
- analysis_result["stored_in_db"] = False
43
- analysis_result["db_error"] = str(e)
44
-
45
- return analysis_result
46
-
47
-
48
- def analyze_resume_with_llm(vectorstore, resume_text: str, job_role: str, file_modified_date: datetime) -> Dict:
49
- try:
50
- retrieved_docs = similarity_search(vectorstore, job_role)
51
- relevant_content = "\n".join([doc.page_content for doc in retrieved_docs])
52
-
53
- unified_prompt = get_unified_resume_prompt()
54
- llm = get_llm()
55
- chain = unified_prompt | llm
56
-
57
- response = chain.invoke(
58
- {
59
- "resume_text": resume_text,
60
- "relevant_content": relevant_content,
61
- "job_role": job_role,
62
- }
63
- )
64
-
65
- response_text = response.content.strip()
66
- if response_text.startswith("`"):
67
- response_text = response_text.strip("`")
68
- if response_text.startswith("json"):
69
- response_text = response_text[4:]
70
-
71
- parsed_response = json.loads(response_text.strip())
72
-
73
- default_response = {
74
- "candidate_name": "",
75
- "gender": None,
76
- "date_of_birth": None,
77
- "email": None,
78
- "mobile_number": None,
79
- "address": None,
80
- "pan_number": None,
81
- "aadhar_number": None,
82
- "passport_number": None,
83
- "highest_education": None,
84
- "university_name": None,
85
- "designation": None,
86
- "skills": "",
87
- "skill_score": [], # New field for skill scoring
88
- "total_experience": None,
89
- "region": None,
90
- "relevant_experience": None,
91
- "experience_summary": "No relevant experience analysis available",
92
- }
93
-
94
- result = {**default_response, **parsed_response}
95
-
96
- result["file_modified_date"] = file_modified_date
97
-
98
- return result
99
-
100
- except json.JSONDecodeError as e:
101
- print(f"JSON Parse Error: {str(e)}")
102
- return {
103
- "candidate_name": "",
104
- "gender": None,
105
- "date_of_birth": None,
106
- "email": None,
107
- "mobile_number": None,
108
- "address": None,
109
- "pan_number": None,
110
- "aadhar_number": None,
111
- "passport_number": None,
112
- "highest_education": None,
113
- "university_name": None,
114
- "designation": None,
115
- "skills": "",
116
- "skill_score": [], # New field for skill scoring
117
- "total_experience": None,
118
- "region": None,
119
- "relevant_experience": None,
120
- "experience_summary": "Could not parse resume details from response",
121
- "file_modified_date": file_modified_date
122
- }
123
- except Exception as e:
124
- print(f"Unexpected Error: {str(e)}")
125
- return {
126
- "candidate_name": "",
127
- "gender": None,
128
- "date_of_birth": None,
129
- "email": None,
130
- "mobile_number": None,
131
- "address": None,
132
- "pan_number": None,
133
- "aadhar_number": None,
134
- "passport_number": None,
135
- "highest_education": None,
136
- "university_name": None,
137
- "designation": None,
138
- "skills": "",
139
- "skill_score": [], # New field for skill scoring
140
- "total_experience": None,
141
- "region": None,
142
- "relevant_experience": None,
143
- "experience_summary": f"Error analyzing resume: {str(e)}",
144
- "file_modified_date": file_modified_date
 
 
145
  }
 
1
+ import json
2
+ from typing import Dict
3
+ from datetime import datetime
4
+
5
+ from core.doc_processor import (
6
+ extract_text_from_bytes,
7
+ extract_text_from_docx,
8
+ parse_pdf_date
9
+ )
10
+ from core.text_processor import chunk_text
11
+ from core.vector_store import create_or_load_vectorstore, similarity_search
12
+ from services.llm_service import get_llm, get_unified_resume_prompt
13
+ from services.db_service import store_candidate_data
14
+
15
+
16
+ async def analyze_resume_document(
17
+ file_bytes: bytes, job_role: str, file_extension: str
18
+ ) -> Dict:
19
+ if file_extension.lower() == ".pdf":
20
+ resume_text, mod_date = extract_text_from_bytes(file_bytes)
21
+ file_modified_date = parse_pdf_date(mod_date) if mod_date else datetime.now()
22
+ elif file_extension.lower() == ".docx":
23
+ resume_text, mod_date = extract_text_from_docx(file_bytes)
24
+ file_modified_date = mod_date if mod_date else datetime.now()
25
+ else:
26
+ raise ValueError(f"Unsupported file format: {file_extension}")
27
+
28
+ if resume_text.startswith("Error"):
29
+ raise ValueError(resume_text)
30
+
31
+ chunks = chunk_text(resume_text)
32
+ vector_store = create_or_load_vectorstore(chunks)
33
+
34
+ analysis_result = analyze_resume_with_llm(vector_store, resume_text, job_role, file_modified_date)
35
+
36
+ try:
37
+ db_record = await store_candidate_data(analysis_result)
38
+ analysis_result["id"] = db_record.get("id")
39
+ analysis_result["stored_in_db"] = True
40
+ except Exception as e:
41
+ print(f"Error storing candidate data: {str(e)}")
42
+ analysis_result["stored_in_db"] = False
43
+ analysis_result["db_error"] = str(e)
44
+
45
+ return analysis_result
46
+
47
+
48
+ def analyze_resume_with_llm(vectorstore, resume_text: str, job_role: str, file_modified_date: datetime) -> Dict:
49
+ try:
50
+ retrieved_docs = similarity_search(vectorstore, job_role)
51
+ relevant_content = "\n".join([doc.page_content for doc in retrieved_docs])
52
+
53
+ unified_prompt = get_unified_resume_prompt()
54
+ llm = get_llm()
55
+ chain = unified_prompt | llm
56
+
57
+ response = chain.invoke(
58
+ {
59
+ "resume_text": resume_text,
60
+ "relevant_content": relevant_content,
61
+ "job_role": job_role,
62
+ }
63
+ )
64
+
65
+ response_text = response.content.strip()
66
+ if response_text.startswith("`"):
67
+ response_text = response_text.strip("`")
68
+ if response_text.startswith("json"):
69
+ response_text = response_text[4:]
70
+
71
+ parsed_response = json.loads(response_text.strip())
72
+
73
+ default_response = {
74
+ "candidate_name": "",
75
+ "gender": None,
76
+ "date_of_birth": None,
77
+ "email": None,
78
+ "mobile_number": None,
79
+ "address": None,
80
+ "pan_number": None,
81
+ "aadhar_number": None,
82
+ "passport_number": None,
83
+ "highest_education": None,
84
+ "university_name": None,
85
+ "designation": None,
86
+ "skills": "",
87
+ "skill_score": [],
88
+ "total_experience": None,
89
+ "region": None,
90
+ "relevant_experience": None,
91
+ "experience_summary": "No relevant experience analysis available",
92
+ "hr_questions": []
93
+ }
94
+
95
+ result = {**default_response, **parsed_response}
96
+ result["file_modified_date"] = file_modified_date
97
+
98
+ return result
99
+
100
+ except json.JSONDecodeError as e:
101
+ print(f"JSON Parse Error: {str(e)}")
102
+ return {
103
+ "candidate_name": "",
104
+ "gender": None,
105
+ "date_of_birth": None,
106
+ "email": None,
107
+ "mobile_number": None,
108
+ "address": None,
109
+ "pan_number": None,
110
+ "aadhar_number": None,
111
+ "passport_number": None,
112
+ "highest_education": None,
113
+ "university_name": None,
114
+ "designation": None,
115
+ "skills": "",
116
+ "skill_score": [],
117
+ "total_experience": None,
118
+ "region": None,
119
+ "relevant_experience": None,
120
+ "experience_summary": "Could not parse resume details from response",
121
+ "hr_questions": [],
122
+ "file_modified_date": file_modified_date
123
+ }
124
+ except Exception as e:
125
+ print(f"Unexpected Error: {str(e)}")
126
+ return {
127
+ "candidate_name": "",
128
+ "gender": None,
129
+ "date_of_birth": None,
130
+ "email": None,
131
+ "mobile_number": None,
132
+ "address": None,
133
+ "pan_number": None,
134
+ "aadhar_number": None,
135
+ "passport_number": None,
136
+ "highest_education": None,
137
+ "university_name": None,
138
+ "designation": None,
139
+ "skills": "",
140
+ "skill_score": [],
141
+ "total_experience": None,
142
+ "region": None,
143
+ "relevant_experience": None,
144
+ "experience_summary": f"Error analyzing resume: {str(e)}",
145
+ "hr_questions": [],
146
+ "file_modified_date": file_modified_date
147
  }
services/voice_service.py ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+ from pathlib import Path
4
+ from typing import Dict, Tuple
5
+ import librosa
6
+ import numpy as np
7
+ import speech_recognition as sr
8
+ import soundfile as sf
9
+ from .llm_service import get_llm
10
+ from langchain.prompts import PromptTemplate
11
+
12
+ from .audio_error_handling import (
13
+ handle_audio_error, AudioProcessingError, AudioFormatError,
14
+ AudioAnalysisError, logger
15
+ )
16
+
17
+ # Constants
18
+ MAX_FILE_SIZE = 25 * 1024 * 1024 # 25MB
19
+ SUPPORTED_FORMATS = {'.wav', '.mp3', '.m4a'}
20
+ MAX_DURATION = 300 # 5 minutes in seconds
21
+
22
+ class AudioAnalyzer:
23
+ def __init__(self):
24
+ self.recognizer = sr.Recognizer()
25
+
26
+ @handle_audio_error
27
+ def validate_audio_file(self, file_path: Path) -> None:
28
+ """Validate audio file format and size"""
29
+ if not file_path.suffix.lower() in SUPPORTED_FORMATS:
30
+ raise AudioFormatError(f"Unsupported audio format. Supported formats: {', '.join(SUPPORTED_FORMATS)}")
31
+
32
+ if os.path.getsize(file_path) > MAX_FILE_SIZE:
33
+ raise AudioFormatError(f"File size exceeds maximum limit of {MAX_FILE_SIZE/1024/1024:.1f}MB")
34
+
35
+ @handle_audio_error
36
+ def _extract_audio_features(self, audio_data: bytes) -> Tuple[np.ndarray, float]:
37
+ """Extract audio features using librosa with error handling"""
38
+ try:
39
+ audio_array, sample_rate = sf.read(io.BytesIO(audio_data))
40
+
41
+ # If stereo, convert to mono
42
+ if len(audio_array.shape) > 1:
43
+ audio_array = np.mean(audio_array, axis=1)
44
+
45
+ # Check duration
46
+ duration = len(audio_array) / sample_rate
47
+ if duration > MAX_DURATION:
48
+ raise AudioFormatError(f"Audio duration exceeds maximum limit of {MAX_DURATION/60:.1f} minutes")
49
+
50
+ return audio_array, sample_rate
51
+ except Exception as e:
52
+ logger.error(f"Error extracting audio features: {e}")
53
+ raise AudioProcessingError("Failed to process audio file")
54
+
55
+ def _transcribe_audio(self, audio_array: np.ndarray, sample_rate: float) -> str:
56
+ """Transcribe audio using speech recognition"""
57
+ # Convert numpy array to audio file format recognized by speech_recognition
58
+ with io.BytesIO() as wav_io:
59
+ sf.write(wav_io, audio_array, sample_rate, format='wav')
60
+ wav_io.seek(0)
61
+ with sr.AudioFile(wav_io) as source:
62
+ audio_data = self.recognizer.record(source)
63
+ try:
64
+ return self.recognizer.recognize_google(audio_data)
65
+ except sr.UnknownValueError:
66
+ return "Speech Recognition could not understand the audio"
67
+ except sr.RequestError:
68
+ return "Could not request results from Speech Recognition service"
69
+
70
+ def _calculate_fluency_score(self, audio_array: np.ndarray, sample_rate: float) -> float:
71
+ """Calculate fluency score based on silence intervals and speech rate"""
72
+ # Get onsets for words
73
+ onset_env = librosa.onset.onset_strength(y=audio_array, sr=sample_rate)
74
+ tempo, _ = librosa.beat.beat_track(onset_envelope=onset_env, sr=sample_rate)
75
+
76
+ # Calculate speech rate normalized to a 0-1 scale
77
+ optimal_tempo = 150 # Increased from 120 to be more lenient
78
+ tempo_score = 1 - min(1.0, abs(tempo - optimal_tempo) / optimal_tempo)
79
+
80
+ # Calculate silence ratio with more lenient threshold
81
+ silence_threshold = 0.02 # Reduced from 0.03 to be more lenient
82
+ is_silence = audio_array < silence_threshold
83
+ silence_ratio = np.mean(is_silence)
84
+
85
+ # Combine metrics with adjusted weights
86
+ fluency_score = (tempo_score * 0.8 + (1 - silence_ratio) * 0.2) # Increased weight on tempo
87
+ return min(1.0, max(0.0, fluency_score))
88
+
89
+ def _analyze_tone(self, transcribed_text: str) -> Dict:
90
+ """Analyze tone based on text characteristics"""
91
+ # Analyze based on punctuation and sentence structure
92
+ sentences = [s.strip() for s in transcribed_text.split('.') if s.strip()]
93
+ avg_sentence_length = np.mean([len(s.split()) for s in sentences]) if sentences else 0
94
+
95
+ # Simple tone estimation based on sentence characteristics
96
+ tone_score = min(1.0, avg_sentence_length / 20) # Normalize to 0-1
97
+ return {
98
+ "overall_tone": float(tone_score)
99
+ }
100
+
101
+ def _calculate_confidence_score(self, audio_array: np.ndarray, transcribed_text: str) -> float:
102
+ """Calculate confidence score based on audio characteristics and text"""
103
+ # Audio energy variation with more lenient thresholds
104
+ rmse = librosa.feature.rms(y=audio_array)[0]
105
+ energy_std = np.std(rmse)
106
+ energy_score = min(1.0, energy_std * 20)
107
+
108
+ # Speech rate consistency with adjusted parameters
109
+ onset_env = librosa.onset.onset_strength(y=audio_array)
110
+ onset_std = np.std(onset_env)
111
+ consistency_score = 1 - min(1.0, onset_std / 10) # was 8
112
+
113
+
114
+ # Combine scores with adjusted weights
115
+ confidence_score = (energy_score * 0.7 + consistency_score * 0.3)
116
+ return min(1.0, max(0.0, confidence_score))
117
+
118
+ def _get_grammar_score(self, text: str) -> float:
119
+ """
120
+ Calculate a basic grammar score based on sentence structure
121
+ Note: This is a simplified version. For production, use a proper grammar checker API
122
+ """
123
+ sentences = [s.strip() for s in text.split('.') if s.strip()]
124
+ word_counts = [len(s.split()) for s in sentences]
125
+
126
+ # Analyze sentence length variation
127
+ sentence_length_std = np.std(word_counts) if word_counts else 0
128
+ length_score = 1 - min(1.0, sentence_length_std / 15) # higher tolerance for variance
129
+
130
+ # Count basic punctuation
131
+ punctuation_score = min(1.0, (sum(1 for c in text if c in '.,!?') / len(text)) * 2) # amplify good punctuation
132
+
133
+ return min(1.0, (length_score * 0.7 + punctuation_score * 0.3))
134
+
135
+ def _calculate_wpm(self, audio_duration: float, transcribed_text: str) -> float:
136
+ """Calculate Words Per Minute"""
137
+ word_count = len(transcribed_text.split())
138
+ minutes = audio_duration / 60
139
+ return word_count / minutes if minutes > 0 else 0
140
+
141
+
142
+ def _get_assessment_prompt(self) -> PromptTemplate:
143
+ """Create a prompt template for LLM assessment"""
144
+ template = '''You are an interview evaluation assistant. Based on the given metrics—fluency, confidence, grammar, tone, and pauses—generate a brief, constructive assessment of the candidate's spoken response. Use a tone that is supportive and human-like, suitable for hiring and recruitment purposes.
145
+
146
+ Audio Analysis Metrics:
147
+ - Fluency Score: {fluency_score}
148
+ - Grammar Score: {grammar_score}
149
+ - Confidence Score: {confidence_score}
150
+ - Clarity Score: {clarity_score}
151
+ - Pronunciation Score: {pronunciation_score}
152
+ - Words Per Minute: {wpm_rate}
153
+ - Filler Words Count: {filler_words_count}
154
+ - Speaking Duration: {speaking_duration}
155
+ - Number of Pauses: {pauses_count}
156
+
157
+ Transcribed Response:
158
+ "{transcribed_text}"
159
+
160
+ Job Role: {job_role}
161
+
162
+ Please provide a comprehensive assessment including:
163
+ 1. Overall communication effectiveness
164
+ 2. Areas of strength
165
+ 3. Areas for improvement
166
+ 4. Interview performance score (0-100)
167
+ 5. Hiring recommendation (recommend/consider/reject)
168
+
169
+ Always mention:
170
+ 1. Strengths: Start by highlighting what the candidate did well.
171
+ 2. Areas for improvement: Be gentle, offering actionable suggestions.
172
+ 3. Overall impression: Summarize whether the delivery is interview-ready or needs polishing.
173
+
174
+ Be encouraging and avoid sounding robotic or overly critical.
175
+ Format your response as a clear, concise paragraph that covers all these points.
176
+ Focus on being direct and actionable in your assessment.'''
177
+ return PromptTemplate(
178
+ input_variables=[
179
+ "fluency_score", "grammar_score", "confidence_score",
180
+ "clarity_score", "pronunciation_score",
181
+ "wpm_rate", "filler_words_count", "speaking_duration",
182
+ "pauses_count", "transcribed_text", "job_role"
183
+ ],
184
+ template=template
185
+ )
186
+
187
+ def _get_llm_assessment(self, metrics: Dict, job_role: str) -> str:
188
+ """Get LLM assessment of the audio analysis metrics"""
189
+ try:
190
+ llm = get_llm()
191
+ prompt = self._get_assessment_prompt()
192
+
193
+ # Prepare input variables
194
+ input_vars = {
195
+ **{k: metrics[k] for k in [
196
+ "fluency_score", "grammar_score", "confidence_score",
197
+ "clarity_score", "pronunciation_score",
198
+ "wpm_rate", "filler_words_count", "speaking_duration",
199
+ "pauses_count", "transcribed_text"
200
+ ]},
201
+ "job_role": job_role or "General"
202
+ }
203
+
204
+ # Get LLM response
205
+ response = llm.invoke(prompt.format_prompt(**input_vars))
206
+
207
+ # Extract just the content from the response
208
+ response_text = response.content.strip()
209
+ if response_text.startswith("`"):
210
+ response_text = response_text.strip("`")
211
+ if response_text.startswith("json"):
212
+ response_text = response_text[4:]
213
+
214
+ return response_text.strip()
215
+
216
+ except Exception as e:
217
+ print(f"Error getting LLM assessment: {str(e)}")
218
+ # Return a basic assessment based on metrics
219
+ return f"""Basic analysis shows {metrics['speaking_duration']:.1f} seconds of speech with fluency score of {metrics['fluency_score']:.2f}.
220
+ Speech clarity is {metrics['clarity_score']:.2f} and technical accuracy is {metrics['technical_accuracy']:.2f}.
221
+ The candidate shows potential but could provide more technical details.
222
+ Interview score: {int(metrics['confidence_score'] * 100)}/100.
223
+ Recommendation: Consider for further evaluation."""
224
+
225
+ def analyze_audio(self, audio_data: bytes, job_role: str = None) -> Dict:
226
+ """Main method to analyze audio and return a simplified response with transcription and assessment"""
227
+ try:
228
+ # Extract audio features
229
+ audio_array, sample_rate = self._extract_audio_features(audio_data)
230
+ duration = len(audio_array) / sample_rate
231
+
232
+ # Get transcription
233
+ transcribed_text = self._transcribe_audio(audio_array, sample_rate)
234
+
235
+ # Calculate various scores internally
236
+ fluency_score = self._calculate_fluency_score(audio_array, sample_rate)
237
+ grammar_score = self._get_grammar_score(transcribed_text)
238
+ confidence_score = self._calculate_confidence_score(audio_array, transcribed_text)
239
+ tone_analysis = self._analyze_tone(transcribed_text)
240
+ wpm = self._calculate_wpm(duration, transcribed_text)
241
+
242
+ # Calculate clarity score based on audio features with adjusted parameters
243
+ onset_env = librosa.onset.onset_strength(y=audio_array, sr=sample_rate)
244
+ clarity_score = min(1.0, np.mean(onset_env) / 8)
245
+
246
+ # Count pauses with more lenient parameters
247
+ silence_intervals = librosa.effects.split(audio_array, top_db=35)
248
+ pauses_count = len(silence_intervals) - 1
249
+
250
+ # Simplified filler words count with reduced list
251
+ filler_words = ["um", "uh"] # Reduced list to only count significant fillers
252
+ filler_count = sum(transcribed_text.lower().count(word) for word in filler_words)
253
+
254
+ # Create metrics dictionary for LLM assessment
255
+ metrics = {
256
+ "fluency_score": float(fluency_score),
257
+ "grammar_score": float(grammar_score),
258
+ "confidence_score": float(confidence_score),
259
+ "clarity_score": float(clarity_score),
260
+ "pronunciation_score": float(clarity_score * 0.7 + fluency_score * 0.3),
261
+ "transcribed_text": transcribed_text,
262
+ "wpm_rate": float(wpm),
263
+ "filler_words_count": filler_count,
264
+ "speaking_duration": float(duration),
265
+ "pauses_count": pauses_count
266
+ }
267
+
268
+ # Get LLM assessment
269
+ assessment = self._get_llm_assessment(metrics, job_role)
270
+
271
+ # Return simplified response
272
+ return {
273
+ "transcript": transcribed_text,
274
+ "assessment": assessment
275
+ }
276
+ except Exception as e:
277
+ print(f"Error in analyze_audio: {str(e)}")
278
+ raise AudioAnalysisError(f"Failed to analyze audio: {str(e)}")