utkarsh134 commited on
Commit
9ce0951
·
verified ·
1 Parent(s): fa57cb0

Update services/voice_service.py

Browse files
Files changed (1) hide show
  1. services/voice_service.py +268 -277
services/voice_service.py CHANGED
@@ -1,278 +1,269 @@
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)}")
 
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
+ # Constants
13
+ MAX_FILE_SIZE = 25 * 1024 * 1024 # 25MB
14
+ SUPPORTED_FORMATS = {'.wav', '.mp3', '.m4a'}
15
+ MAX_DURATION = 300 # 5 minutes in seconds
16
+
17
+ class AudioAnalyzer:
18
+ def __init__(self):
19
+ self.recognizer = sr.Recognizer()
20
+
21
+ def validate_audio_file(self, file_path: Path) -> None:
22
+ """Validate audio file format and size"""
23
+ if not file_path.suffix.lower() in SUPPORTED_FORMATS:
24
+ raise ValueError(f"Unsupported audio format. Supported formats: {', '.join(SUPPORTED_FORMATS)}")
25
+
26
+ if os.path.getsize(file_path) > MAX_FILE_SIZE:
27
+ raise ValueError(f"File size exceeds maximum limit of {MAX_FILE_SIZE/1024/1024:.1f}MB")
28
+
29
+ def _extract_audio_features(self, audio_data: bytes) -> Tuple[np.ndarray, float]:
30
+ """Extract audio features using librosa"""
31
+ try:
32
+ audio_array, sample_rate = sf.read(io.BytesIO(audio_data))
33
+
34
+ # If stereo, convert to mono
35
+ if len(audio_array.shape) > 1:
36
+ audio_array = np.mean(audio_array, axis=1)
37
+
38
+ # Check duration
39
+ duration = len(audio_array) / sample_rate
40
+ if duration > MAX_DURATION:
41
+ raise ValueError(f"Audio duration exceeds maximum limit of {MAX_DURATION/60:.1f} minutes")
42
+
43
+ return audio_array, sample_rate
44
+ except Exception as e:
45
+ raise Exception(f"Failed to process audio file: {e}")
46
+
47
+ def _transcribe_audio(self, audio_array: np.ndarray, sample_rate: float) -> str:
48
+ """Transcribe audio using speech recognition"""
49
+ with io.BytesIO() as wav_io:
50
+ sf.write(wav_io, audio_array, sample_rate, format='wav')
51
+ wav_io.seek(0)
52
+ with sr.AudioFile(wav_io) as source:
53
+ audio_data = self.recognizer.record(source)
54
+ try:
55
+ return self.recognizer.recognize_google(audio_data)
56
+ except sr.UnknownValueError:
57
+ return "Speech Recognition could not understand the audio"
58
+ except sr.RequestError:
59
+ return "Could not request results from Speech Recognition service"
60
+
61
+ def _calculate_fluency_score(self, audio_array: np.ndarray, sample_rate: float) -> float:
62
+ """Calculate fluency score based on silence intervals and speech rate"""
63
+ # Get onsets for words
64
+ onset_env = librosa.onset.onset_strength(y=audio_array, sr=sample_rate)
65
+ tempo, _ = librosa.beat.beat_track(onset_envelope=onset_env, sr=sample_rate)
66
+
67
+ # Calculate speech rate normalized to a 0-1 scale
68
+ optimal_tempo = 150 # Increased from 120 to be more lenient
69
+ tempo_score = 1 - min(1.0, abs(tempo - optimal_tempo) / optimal_tempo)
70
+
71
+ # Calculate silence ratio with more lenient threshold
72
+ silence_threshold = 0.02 # Reduced from 0.03 to be more lenient
73
+ is_silence = audio_array < silence_threshold
74
+ silence_ratio = np.mean(is_silence)
75
+
76
+ # Combine metrics with adjusted weights
77
+ fluency_score = (tempo_score * 0.8 + (1 - silence_ratio) * 0.2) # Increased weight on tempo
78
+ return min(1.0, max(0.0, fluency_score))
79
+
80
+ def _analyze_tone(self, transcribed_text: str) -> Dict:
81
+ """Analyze tone based on text characteristics"""
82
+ # Analyze based on punctuation and sentence structure
83
+ sentences = [s.strip() for s in transcribed_text.split('.') if s.strip()]
84
+ avg_sentence_length = np.mean([len(s.split()) for s in sentences]) if sentences else 0
85
+
86
+ # Simple tone estimation based on sentence characteristics
87
+ tone_score = min(1.0, avg_sentence_length / 20) # Normalize to 0-1
88
+ return {
89
+ "overall_tone": float(tone_score)
90
+ }
91
+
92
+ def _calculate_confidence_score(self, audio_array: np.ndarray, transcribed_text: str) -> float:
93
+ """Calculate confidence score based on audio characteristics and text"""
94
+ # Audio energy variation with more lenient thresholds
95
+ rmse = librosa.feature.rms(y=audio_array)[0]
96
+ energy_std = np.std(rmse)
97
+ energy_score = min(1.0, energy_std * 20)
98
+
99
+ # Speech rate consistency with adjusted parameters
100
+ onset_env = librosa.onset.onset_strength(y=audio_array)
101
+ onset_std = np.std(onset_env)
102
+ consistency_score = 1 - min(1.0, onset_std / 10) # was 8
103
+
104
+
105
+ # Combine scores with adjusted weights
106
+ confidence_score = (energy_score * 0.7 + consistency_score * 0.3)
107
+ return min(1.0, max(0.0, confidence_score))
108
+
109
+ def _get_grammar_score(self, text: str) -> float:
110
+ """
111
+ Calculate a basic grammar score based on sentence structure
112
+ Note: This is a simplified version. For production, use a proper grammar checker API
113
+ """
114
+ sentences = [s.strip() for s in text.split('.') if s.strip()]
115
+ word_counts = [len(s.split()) for s in sentences]
116
+
117
+ # Analyze sentence length variation
118
+ sentence_length_std = np.std(word_counts) if word_counts else 0
119
+ length_score = 1 - min(1.0, sentence_length_std / 15) # higher tolerance for variance
120
+
121
+ # Count basic punctuation
122
+ punctuation_score = min(1.0, (sum(1 for c in text if c in '.,!?') / len(text)) * 2) # amplify good punctuation
123
+
124
+ return min(1.0, (length_score * 0.7 + punctuation_score * 0.3))
125
+
126
+ def _calculate_wpm(self, audio_duration: float, transcribed_text: str) -> float:
127
+ """Calculate Words Per Minute"""
128
+ word_count = len(transcribed_text.split())
129
+ minutes = audio_duration / 60
130
+ return word_count / minutes if minutes > 0 else 0
131
+
132
+
133
+ def _get_assessment_prompt(self) -> PromptTemplate:
134
+ """Create a prompt template for LLM assessment"""
135
+ 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.
136
+
137
+ Audio Analysis Metrics:
138
+ - Fluency Score: {fluency_score}
139
+ - Grammar Score: {grammar_score}
140
+ - Confidence Score: {confidence_score}
141
+ - Clarity Score: {clarity_score}
142
+ - Pronunciation Score: {pronunciation_score}
143
+ - Words Per Minute: {wpm_rate}
144
+ - Filler Words Count: {filler_words_count}
145
+ - Speaking Duration: {speaking_duration}
146
+ - Number of Pauses: {pauses_count}
147
+
148
+ Transcribed Response:
149
+ "{transcribed_text}"
150
+
151
+ Job Role: {job_role}
152
+
153
+ Please provide a comprehensive assessment including:
154
+ 1. Overall communication effectiveness
155
+ 2. Areas of strength
156
+ 3. Areas for improvement
157
+ 4. Interview performance score (0-100)
158
+ 5. Hiring recommendation (recommend/consider/reject)
159
+
160
+ Always mention:
161
+ 1. Strengths: Start by highlighting what the candidate did well.
162
+ 2. Areas for improvement: Be gentle, offering actionable suggestions.
163
+ 3. Overall impression: Summarize whether the delivery is interview-ready or needs polishing.
164
+
165
+ Be encouraging and avoid sounding robotic or overly critical.
166
+ Format your response as a clear, concise paragraph that covers all these points.
167
+ Focus on being direct and actionable in your assessment.'''
168
+ return PromptTemplate(
169
+ input_variables=[
170
+ "fluency_score", "grammar_score", "confidence_score",
171
+ "clarity_score", "pronunciation_score",
172
+ "wpm_rate", "filler_words_count", "speaking_duration",
173
+ "pauses_count", "transcribed_text", "job_role"
174
+ ],
175
+ template=template
176
+ )
177
+
178
+ def _get_llm_assessment(self, metrics: Dict, job_role: str) -> str:
179
+ """Get LLM assessment of the audio analysis metrics"""
180
+ try:
181
+ llm = get_llm()
182
+ prompt = self._get_assessment_prompt()
183
+
184
+ # Prepare input variables
185
+ input_vars = {
186
+ **{k: metrics[k] for k in [
187
+ "fluency_score", "grammar_score", "confidence_score",
188
+ "clarity_score", "pronunciation_score",
189
+ "wpm_rate", "filler_words_count", "speaking_duration",
190
+ "pauses_count", "transcribed_text"
191
+ ]},
192
+ "job_role": job_role or "General"
193
+ }
194
+
195
+ # Get LLM response
196
+ response = llm.invoke(prompt.format_prompt(**input_vars))
197
+
198
+ # Extract just the content from the response
199
+ response_text = response.content.strip()
200
+ if response_text.startswith("`"):
201
+ response_text = response_text.strip("`")
202
+ if response_text.startswith("json"):
203
+ response_text = response_text[4:]
204
+
205
+ return response_text.strip()
206
+
207
+ except Exception as e:
208
+ print(f"Error getting LLM assessment: {str(e)}")
209
+ # Return a basic assessment based on metrics
210
+ return f"""Basic analysis shows {metrics['speaking_duration']:.1f} seconds of speech with fluency score of {metrics['fluency_score']:.2f}.
211
+ Speech clarity is {metrics['clarity_score']:.2f} and technical accuracy is {metrics['technical_accuracy']:.2f}.
212
+ The candidate shows potential but could provide more technical details.
213
+ Interview score: {int(metrics['confidence_score'] * 100)}/100.
214
+ Recommendation: Consider for further evaluation."""
215
+
216
+ def analyze_audio(self, audio_data: bytes, job_role: str = None) -> Dict:
217
+ """Main method to analyze audio and return a simplified response with transcription and assessment"""
218
+ try:
219
+ # Extract audio features
220
+ audio_array, sample_rate = self._extract_audio_features(audio_data)
221
+ duration = len(audio_array) / sample_rate
222
+
223
+ # Get transcription
224
+ transcribed_text = self._transcribe_audio(audio_array, sample_rate)
225
+
226
+ # Calculate various scores internally
227
+ fluency_score = self._calculate_fluency_score(audio_array, sample_rate)
228
+ grammar_score = self._get_grammar_score(transcribed_text)
229
+ confidence_score = self._calculate_confidence_score(audio_array, transcribed_text)
230
+ tone_analysis = self._analyze_tone(transcribed_text)
231
+ wpm = self._calculate_wpm(duration, transcribed_text)
232
+
233
+ # Calculate clarity score based on audio features with adjusted parameters
234
+ onset_env = librosa.onset.onset_strength(y=audio_array, sr=sample_rate)
235
+ clarity_score = min(1.0, np.mean(onset_env) / 8)
236
+
237
+ # Count pauses with more lenient parameters
238
+ silence_intervals = librosa.effects.split(audio_array, top_db=35)
239
+ pauses_count = len(silence_intervals) - 1
240
+
241
+ # Simplified filler words count with reduced list
242
+ filler_words = ["um", "uh"] # Reduced list to only count significant fillers
243
+ filler_count = sum(transcribed_text.lower().count(word) for word in filler_words)
244
+
245
+ # Create metrics dictionary for LLM assessment
246
+ metrics = {
247
+ "fluency_score": float(fluency_score),
248
+ "grammar_score": float(grammar_score),
249
+ "confidence_score": float(confidence_score),
250
+ "clarity_score": float(clarity_score),
251
+ "pronunciation_score": float(clarity_score * 0.7 + fluency_score * 0.3),
252
+ "transcribed_text": transcribed_text,
253
+ "wpm_rate": float(wpm),
254
+ "filler_words_count": filler_count,
255
+ "speaking_duration": float(duration),
256
+ "pauses_count": pauses_count
257
+ }
258
+
259
+ # Get LLM assessment
260
+ assessment = self._get_llm_assessment(metrics, job_role)
261
+
262
+ # Return simplified response
263
+ return {
264
+ "transcript": transcribed_text,
265
+ "assessment": assessment
266
+ }
267
+ except Exception as e:
268
+ print(f"Error in analyze_audio: {str(e)}")
 
 
 
 
 
 
 
 
 
269
  raise AudioAnalysisError(f"Failed to analyze audio: {str(e)}")