Update process_interview.py
Browse files- process_interview.py +219 -660
process_interview.py
CHANGED
|
@@ -19,16 +19,15 @@ from typing import Dict, List, Tuple
|
|
| 19 |
import logging
|
| 20 |
import tempfile
|
| 21 |
from reportlab.lib.pagesizes import letter
|
| 22 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, Image
|
| 23 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 24 |
from reportlab.lib.units import inch
|
| 25 |
from reportlab.lib import colors
|
| 26 |
import matplotlib.pyplot as plt
|
| 27 |
import matplotlib
|
| 28 |
matplotlib.use('Agg')
|
| 29 |
-
from reportlab.platypus import Image
|
| 30 |
import io
|
| 31 |
-
from transformers import AutoTokenizer, AutoModel
|
| 32 |
import spacy
|
| 33 |
import google.generativeai as genai
|
| 34 |
import joblib
|
|
@@ -37,91 +36,75 @@ from concurrent.futures import ThreadPoolExecutor
|
|
| 37 |
# Setup logging
|
| 38 |
logging.basicConfig(level=logging.INFO)
|
| 39 |
logger = logging.getLogger(__name__)
|
| 40 |
-
logging.getLogger("
|
| 41 |
-
logging.getLogger("nemo").setLevel(logging.ERROR)
|
| 42 |
|
| 43 |
# Configuration
|
| 44 |
-
AUDIO_DIR = "./uploads"
|
| 45 |
OUTPUT_DIR = "./processed_audio"
|
| 46 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 47 |
|
| 48 |
# API Keys
|
| 49 |
-
PINECONE_KEY = os.getenv("PINECONE_KEY")
|
| 50 |
-
ASSEMBLYAI_KEY = os.getenv("ASSEMBLYAI_KEY")
|
| 51 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def download_audio_from_url(url: str) -> str:
|
| 54 |
-
|
|
|
|
| 55 |
try:
|
| 56 |
temp_dir = tempfile.gettempdir()
|
| 57 |
temp_path = os.path.join(temp_dir, f"{uuid.uuid4()}.tmp_audio")
|
| 58 |
logger.info(f"Downloading audio from {url} to {temp_path}")
|
| 59 |
-
with requests.get(url, stream=True) as r:
|
| 60 |
r.raise_for_status()
|
| 61 |
with open(temp_path, 'wb') as f:
|
| 62 |
for chunk in r.iter_content(chunk_size=8192):
|
| 63 |
f.write(chunk)
|
| 64 |
return temp_path
|
| 65 |
except Exception as e:
|
| 66 |
-
logger.error(f"Failed to download audio from URL {url}: {e}")
|
| 67 |
raise
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
def initialize_services():
|
| 73 |
-
# Pinecone
|
| 74 |
-
pc = Pinecone(api_key=PINECONE_KEY)
|
| 75 |
-
index_name = "interview-speaker-embeddings"
|
| 76 |
-
if index_name not in pc.list_indexes().names():
|
| 77 |
-
pc.create_index(
|
| 78 |
-
name=index_name,
|
| 79 |
-
dimension=192,
|
| 80 |
-
metric="cosine",
|
| 81 |
-
spec=ServerlessSpec(cloud="aws", region="us-east-1")
|
| 82 |
-
)
|
| 83 |
-
index = pc.Index(index_name)
|
| 84 |
-
|
| 85 |
-
# حذف أي بيانات قديمة (اختياري)
|
| 86 |
try:
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
-
logger.
|
| 90 |
-
|
| 91 |
-
# Gemini
|
| 92 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 93 |
-
gemini_model = genai.GenerativeModel('gemini-1.5-flash')
|
| 94 |
-
|
| 95 |
-
return index, gemini_model
|
| 96 |
-
index, gemini_model = initialize_services()
|
| 97 |
|
| 98 |
-
|
| 99 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 100 |
logger.info(f"Using device: {device}")
|
| 101 |
|
| 102 |
-
# Load ML models
|
| 103 |
def load_models():
|
| 104 |
-
speaker_model = EncDecSpeakerLabelModel.from_pretrained("nvidia/speakerverification_en_titanet_large"
|
| 105 |
speaker_model.eval()
|
| 106 |
-
|
| 107 |
nlp = spacy.load("en_core_web_sm")
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
llm_model = AutoModel.from_pretrained("distilbert-base-uncased").to(device)
|
| 111 |
-
llm_model.eval()
|
| 112 |
-
|
| 113 |
-
return speaker_model, nlp, tokenizer, llm_model
|
| 114 |
|
| 115 |
-
speaker_model, nlp
|
| 116 |
|
| 117 |
-
# Audio processing functions
|
| 118 |
def convert_to_wav(audio_path: str, output_dir: str = OUTPUT_DIR) -> str:
|
|
|
|
| 119 |
try:
|
| 120 |
audio = AudioSegment.from_file(audio_path)
|
| 121 |
-
if audio.channels > 1:
|
| 122 |
-
audio = audio.set_channels(1)
|
| 123 |
audio = audio.set_frame_rate(16000)
|
| 124 |
-
|
| 125 |
wav_file = os.path.join(output_dir, f"{uuid.uuid4()}.wav")
|
| 126 |
audio.export(wav_file, format="wav")
|
| 127 |
return wav_file
|
|
@@ -130,18 +113,18 @@ def convert_to_wav(audio_path: str, output_dir: str = OUTPUT_DIR) -> str:
|
|
| 130 |
raise
|
| 131 |
|
| 132 |
def extract_prosodic_features(audio_path: str, start_ms: int, end_ms: int) -> Dict:
|
|
|
|
| 133 |
try:
|
| 134 |
audio = AudioSegment.from_file(audio_path)
|
| 135 |
segment = audio[start_ms:end_ms]
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
pitches = librosa.piptrack(y=y, sr=sr)
|
| 141 |
pitches = pitches[pitches > 0]
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
'duration': (end_ms - start_ms) / 1000,
|
| 145 |
'mean_pitch': float(np.mean(pitches)) if len(pitches) > 0 else 0.0,
|
| 146 |
'min_pitch': float(np.min(pitches)) if len(pitches) > 0 else 0.0,
|
| 147 |
'max_pitch': float(np.max(pitches)) if len(pitches) > 0 else 0.0,
|
|
@@ -151,391 +134,116 @@ def extract_prosodic_features(audio_path: str, start_ms: int, end_ms: int) -> Di
|
|
| 151 |
'intensityMax': float(np.max(librosa.feature.rms(y=y)[0])),
|
| 152 |
'intensitySD': float(np.std(librosa.feature.rms(y=y)[0])),
|
| 153 |
}
|
| 154 |
-
|
| 155 |
-
os.remove(temp_path)
|
| 156 |
-
return features
|
| 157 |
except Exception as e:
|
| 158 |
logger.error(f"Feature extraction failed: {str(e)}")
|
| 159 |
-
return {
|
| 160 |
-
|
| 161 |
-
'mean_pitch': 0.0,
|
| 162 |
-
'min_pitch': 0.0,
|
| 163 |
-
'max_pitch': 0.0,
|
| 164 |
-
'pitch_sd': 0.0,
|
| 165 |
-
'intensityMean': 0.0,
|
| 166 |
-
'intensityMin': 0.0,
|
| 167 |
-
'intensityMax': 0.0,
|
| 168 |
-
'intensitySD': 0.0,
|
| 169 |
-
}
|
| 170 |
|
| 171 |
-
# Transcription
|
| 172 |
def transcribe(audio_path: str) -> Dict:
|
|
|
|
| 173 |
try:
|
| 174 |
-
# Upload audio
|
| 175 |
with open(audio_path, 'rb') as f:
|
| 176 |
-
upload_response = requests.post(
|
| 177 |
-
"https://api.assemblyai.com/v2/upload",
|
| 178 |
-
headers={"authorization": ASSEMBLYAI_KEY},
|
| 179 |
-
data=f
|
| 180 |
-
)
|
| 181 |
audio_url = upload_response.json()['upload_url']
|
| 182 |
-
|
| 183 |
-
# Start transcription
|
| 184 |
-
transcript_response = requests.post(
|
| 185 |
-
"https://api.assemblyai.com/v2/transcript",
|
| 186 |
-
headers={"authorization": ASSEMBLYAI_KEY},
|
| 187 |
-
json={
|
| 188 |
-
"audio_url": audio_url,
|
| 189 |
-
"speaker_labels": True,
|
| 190 |
-
"filter_profanity": True
|
| 191 |
-
}
|
| 192 |
-
)
|
| 193 |
transcript_id = transcript_response.json()['id']
|
| 194 |
-
|
| 195 |
-
# Poll for results
|
| 196 |
while True:
|
| 197 |
-
result = requests.get(
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
).json()
|
| 201 |
-
|
| 202 |
-
if result['status'] == 'completed':
|
| 203 |
-
return result
|
| 204 |
-
elif result['status'] == 'error':
|
| 205 |
-
raise Exception(result['error'])
|
| 206 |
-
|
| 207 |
time.sleep(5)
|
| 208 |
except Exception as e:
|
| 209 |
logger.error(f"Transcription failed: {str(e)}")
|
| 210 |
raise
|
| 211 |
|
| 212 |
-
|
| 213 |
-
#
|
| 214 |
-
def process_utterance(utterance, full_audio, wav_file):
|
| 215 |
try:
|
| 216 |
-
|
| 217 |
-
start = utterance['start']
|
| 218 |
-
end = utterance['end']
|
| 219 |
segment = full_audio[start:end]
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
query_result = index.query(
|
| 229 |
-
vector=embedding.cpu().numpy().tolist(),
|
| 230 |
-
top_k=1,
|
| 231 |
-
include_metadata=True
|
| 232 |
-
)
|
| 233 |
-
|
| 234 |
-
# Identify speaker
|
| 235 |
-
if query_result['matches'] and query_result['matches'][0]['score'] > 0.5: # تخفيض العتبة
|
| 236 |
speaker_id = query_result['matches'][0]['id']
|
| 237 |
speaker_name = query_result['matches'][0]['metadata']['speaker_name']
|
| 238 |
else:
|
| 239 |
-
speaker_id = f"
|
| 240 |
-
speaker_name = f"Speaker_{speaker_id[-4:]}"
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
# Cleanup
|
| 245 |
-
os.remove(temp_path)
|
| 246 |
-
|
| 247 |
-
return {
|
| 248 |
-
**utterance,
|
| 249 |
-
'speaker': speaker_name,
|
| 250 |
-
'speaker_id': speaker_id,
|
| 251 |
-
'embedding': embedding.cpu().numpy().tolist()
|
| 252 |
-
}
|
| 253 |
except Exception as e:
|
| 254 |
logger.error(f"Utterance processing failed: {str(e)}")
|
| 255 |
-
return {
|
| 256 |
-
**utterance,
|
| 257 |
-
'speaker': 'Unknown',
|
| 258 |
-
'speaker_id': 'unknown',
|
| 259 |
-
'embedding': None
|
| 260 |
-
}
|
| 261 |
-
|
| 262 |
|
| 263 |
def identify_speakers(transcript: Dict, wav_file: str) -> List[Dict]:
|
|
|
|
| 264 |
try:
|
| 265 |
full_audio = AudioSegment.from_wav(wav_file)
|
| 266 |
-
utterances = transcript
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
with ThreadPoolExecutor(max_workers=4) as executor:
|
| 270 |
-
futures = [
|
| 271 |
-
executor.submit(process_utterance, utterance, full_audio, wav_file)
|
| 272 |
-
for utterance in utterances
|
| 273 |
-
]
|
| 274 |
results = [f.result() for f in futures]
|
| 275 |
-
|
| 276 |
return results
|
| 277 |
except Exception as e:
|
| 278 |
logger.error(f"Speaker identification failed: {str(e)}")
|
| 279 |
raise
|
| 280 |
-
# Role classification
|
| 281 |
-
def train_role_classifier(utterances: List[Dict]):
|
| 282 |
-
try:
|
| 283 |
-
# تحليل المحتوى للتمييز بين الأسئلة (المحاور) والإجابات (المتحدث)
|
| 284 |
-
texts = [u['text'] for u in utterances]
|
| 285 |
-
vectorizer = TfidfVectorizer(max_features=500, ngram_range=(1, 2))
|
| 286 |
-
X_text = vectorizer.fit_transform(texts)
|
| 287 |
-
|
| 288 |
-
features = []
|
| 289 |
-
labels = []
|
| 290 |
-
|
| 291 |
-
for i, utterance in enumerate(utterances):
|
| 292 |
-
# Prosodic features
|
| 293 |
-
prosodic = utterance['prosodic_features']
|
| 294 |
-
feat = [
|
| 295 |
-
prosodic['duration'],
|
| 296 |
-
prosodic['mean_pitch'],
|
| 297 |
-
prosodic['min_pitch'],
|
| 298 |
-
prosodic['max_pitch'],
|
| 299 |
-
prosodic['pitch_sd'],
|
| 300 |
-
prosodic['intensityMean'],
|
| 301 |
-
prosodic['intensityMin'],
|
| 302 |
-
prosodic['intensityMax'],
|
| 303 |
-
prosodic['intensitySD'],
|
| 304 |
-
]
|
| 305 |
-
|
| 306 |
-
# Text features
|
| 307 |
-
feat.extend(X_text[i].toarray()[0].tolist())
|
| 308 |
-
|
| 309 |
-
# Linguistic features
|
| 310 |
-
doc = nlp(utterance['text'])
|
| 311 |
-
is_question = int(utterance['text'].endswith('?'))
|
| 312 |
-
question_words = len(re.findall(r'\b(why|how|what|when|where|who|which)\b', utterance['text'].lower()))
|
| 313 |
-
feat.extend([
|
| 314 |
-
is_question,
|
| 315 |
-
question_words,
|
| 316 |
-
len(utterance['text'].split()),
|
| 317 |
-
sum(1 for token in doc if token.pos_ == 'VERB'),
|
| 318 |
-
sum(1 for token in doc if token.pos_ == 'NOUN')
|
| 319 |
-
])
|
| 320 |
-
|
| 321 |
-
features.append(feat)
|
| 322 |
-
# التصنيف بناءً على كون النص سؤالاً (محاور) أو لا (متحدث)
|
| 323 |
-
labels.append(0 if is_question or question_words > 0 else 1)
|
| 324 |
-
|
| 325 |
-
# Train classifier
|
| 326 |
-
scaler = StandardScaler()
|
| 327 |
-
X = scaler.fit_transform(features)
|
| 328 |
-
|
| 329 |
-
clf = RandomForestClassifier(
|
| 330 |
-
n_estimators=150,
|
| 331 |
-
max_depth=10,
|
| 332 |
-
random_state=42,
|
| 333 |
-
class_weight='balanced'
|
| 334 |
-
)
|
| 335 |
-
clf.fit(X, labels)
|
| 336 |
-
|
| 337 |
-
# Save models
|
| 338 |
-
joblib.dump(clf, os.path.join(OUTPUT_DIR, 'role_classifier.pkl'))
|
| 339 |
-
joblib.dump(vectorizer, os.path.join(OUTPUT_DIR, 'text_vectorizer.pkl'))
|
| 340 |
-
joblib.dump(scaler, os.path.join(OUTPUT_DIR, 'feature_scaler.pkl'))
|
| 341 |
-
|
| 342 |
-
return clf, vectorizer, scaler
|
| 343 |
-
except Exception as e:
|
| 344 |
-
logger.error(f"Classifier training failed: {str(e)}")
|
| 345 |
-
raise
|
| 346 |
|
| 347 |
-
def classify_roles(utterances: List[Dict]
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
for i, utterance in enumerate(utterances):
|
| 355 |
-
# Prosodic features
|
| 356 |
-
prosodic = utterance['prosodic_features']
|
| 357 |
-
feat = [
|
| 358 |
-
prosodic['duration'],
|
| 359 |
-
prosodic['mean_pitch'],
|
| 360 |
-
prosodic['min_pitch'],
|
| 361 |
-
prosodic['max_pitch'],
|
| 362 |
-
prosodic['pitch_sd'],
|
| 363 |
-
prosodic['intensityMean'],
|
| 364 |
-
prosodic['intensityMin'],
|
| 365 |
-
prosodic['intensityMax'],
|
| 366 |
-
prosodic['intensitySD'],
|
| 367 |
-
]
|
| 368 |
-
|
| 369 |
-
# Text features
|
| 370 |
-
feat.extend(X_text[i].toarray()[0].tolist())
|
| 371 |
-
|
| 372 |
-
# Linguistic features
|
| 373 |
-
doc = nlp(utterance['text'])
|
| 374 |
-
feat.extend([
|
| 375 |
-
int(utterance['text'].endswith('?')),
|
| 376 |
-
len(re.findall(r'\b(why|how|what|when|where|who|which)\b', utterance['text'].lower())),
|
| 377 |
-
len(utterance['text'].split()),
|
| 378 |
-
sum(1 for token in doc if token.pos_ == 'VERB'),
|
| 379 |
-
sum(1 for token in doc if token.pos_ == 'NOUN')
|
| 380 |
-
])
|
| 381 |
-
|
| 382 |
-
# Predict
|
| 383 |
-
X = scaler.transform([feat])
|
| 384 |
-
role = 'Interviewer' if clf.predict(X)[0] == 0 else 'Interviewee'
|
| 385 |
-
|
| 386 |
-
results.append({**utterance, 'role': role})
|
| 387 |
-
|
| 388 |
-
return results
|
| 389 |
-
except Exception as e:
|
| 390 |
-
logger.error(f"Role classification failed: {str(e)}")
|
| 391 |
-
raise
|
| 392 |
|
| 393 |
-
# Voice analysis for interviewee
|
| 394 |
def analyze_interviewee_voice(audio_path: str, utterances: List[Dict]) -> Dict:
|
|
|
|
| 395 |
try:
|
| 396 |
-
# Load full audio
|
| 397 |
y, sr = librosa.load(audio_path, sr=16000)
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
if not
|
| 402 |
-
return {'error': 'No interviewee utterances found'}
|
| 403 |
-
|
| 404 |
-
# Extract all interviewee segments
|
| 405 |
-
segments = []
|
| 406 |
-
for u in interviewee_utterances:
|
| 407 |
-
start = int(u['start'] * sr / 1000)
|
| 408 |
-
end = int(u['end'] * sr / 1000)
|
| 409 |
-
segments.append(y[start:end])
|
| 410 |
-
|
| 411 |
-
# Combine all segments
|
| 412 |
-
combined_audio = np.concatenate(segments)
|
| 413 |
-
|
| 414 |
-
# Speaking rate analysis
|
| 415 |
total_duration = sum(u['prosodic_features']['duration'] for u in interviewee_utterances)
|
| 416 |
total_words = sum(len(u['text'].split()) for u in interviewee_utterances)
|
| 417 |
speaking_rate = total_words / total_duration if total_duration > 0 else 0
|
| 418 |
-
|
| 419 |
-
# Filler words analysis
|
| 420 |
filler_words = ['um', 'uh', 'like', 'you know', 'so', 'i mean']
|
| 421 |
-
filler_count = sum(
|
| 422 |
-
sum(u['text'].lower().count(fw) for fw in filler_words)
|
| 423 |
-
for u in interviewee_utterances
|
| 424 |
-
)
|
| 425 |
filler_ratio = filler_count / total_words if total_words > 0 else 0
|
| 426 |
-
|
| 427 |
-
# Repetition analysis
|
| 428 |
-
all_words = ' '.join(u['text'].lower() for u in interviewee_utterances).split()
|
| 429 |
-
word_counts = {}
|
| 430 |
-
for i in range(len(all_words) - 1):
|
| 431 |
-
bigram = (all_words[i], all_words[i+1])
|
| 432 |
-
word_counts[bigram] = word_counts.get(bigram, 0) + 1
|
| 433 |
-
repetition_score = sum(1 for count in word_counts.values() if count > 1) / len(word_counts) if word_counts else 0
|
| 434 |
-
|
| 435 |
-
# Pitch analysis (anxiety)
|
| 436 |
-
pitches = []
|
| 437 |
for segment in segments:
|
| 438 |
-
|
|
|
|
| 439 |
pitches.extend(f0[voiced_flag])
|
| 440 |
-
|
| 441 |
-
pitch_mean = np.mean(pitches) if len(pitches) > 0 else 0
|
| 442 |
-
|
| 443 |
-
jitter = np.mean(np.abs(np.diff(pitches))) / pitch_mean if len(pitches) > 1 and pitch_mean > 0 else 0
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
for segment in segments:
|
| 448 |
-
rms = librosa.feature.rms(y=segment)[0]
|
| 449 |
-
intensities.extend(rms)
|
| 450 |
-
|
| 451 |
-
intensity_mean = np.mean(intensities) if intensities else 0
|
| 452 |
-
intensity_std = np.std(intensities) if intensities else 0
|
| 453 |
-
shimmer = np.mean(np.abs(np.diff(intensities))) / intensity_mean if len(intensities) > 1 and intensity_mean > 0 else 0
|
| 454 |
-
|
| 455 |
-
# Composite scores
|
| 456 |
-
anxiety_score = 0.6 * (pitch_std / pitch_mean) + 0.4 * (jitter + shimmer) if pitch_mean > 0 else 0
|
| 457 |
-
confidence_score = 0.7 * (1 / (1 + intensity_std)) + 0.3 * (1 / (1 + filler_ratio))
|
| 458 |
-
hesitation_score = filler_ratio + repetition_score
|
| 459 |
-
|
| 460 |
-
# Interpretation
|
| 461 |
-
anxiety_level = 'high' if anxiety_score > 0.15 else 'moderate' if anxiety_score > 0.07 else 'low'
|
| 462 |
-
confidence_level = 'high' if confidence_score > 0.7 else 'moderate' if confidence_score > 0.5 else 'low'
|
| 463 |
-
fluency_level = 'fluent' if (filler_ratio < 0.05 and repetition_score < 0.1) else 'moderate' if (filler_ratio < 0.1 and repetition_score < 0.2) else 'disfluent'
|
| 464 |
-
|
| 465 |
return {
|
| 466 |
-
'speaking_rate':
|
| 467 |
-
'
|
| 468 |
-
'repetition_score': float(round(repetition_score, 4)),
|
| 469 |
-
'pitch_analysis': {
|
| 470 |
-
'mean': float(round(pitch_mean, 2)),
|
| 471 |
-
'std_dev':float(round(pitch_std, 2)),
|
| 472 |
-
'jitter': float(round(jitter, 4))
|
| 473 |
-
},
|
| 474 |
-
'intensity_analysis': {
|
| 475 |
-
'mean': float(round(intensity_mean, 2)),
|
| 476 |
-
'std_dev': float(round(intensity_std, 2)),
|
| 477 |
-
'shimmer': float(round(shimmer, 4))
|
| 478 |
-
},
|
| 479 |
-
'composite_scores': {
|
| 480 |
-
'anxiety': float(round(anxiety_score, 4)),
|
| 481 |
-
'confidence': float(round(confidence_score, 4)),
|
| 482 |
-
'hesitation': float(round(hesitation_score, 4))
|
| 483 |
-
},
|
| 484 |
'interpretation': {
|
| 485 |
-
'anxiety_level':
|
| 486 |
-
'confidence_level':
|
| 487 |
-
'fluency_level':
|
| 488 |
}
|
| 489 |
}
|
| 490 |
except Exception as e:
|
| 491 |
logger.error(f"Voice analysis failed: {str(e)}")
|
| 492 |
return {'error': str(e)}
|
| 493 |
|
| 494 |
-
|
| 495 |
-
def generate_voice_interpretation(analysis: Dict) -> str:
|
| 496 |
-
if 'error' in analysis:
|
| 497 |
-
return "Voice analysis unavailable due to processing limitations."
|
| 498 |
-
interpretation_lines = [
|
| 499 |
-
"Vocal Performance Profile:",
|
| 500 |
-
f"- Speaking Rate: {analysis['speaking_rate']} words/sec - Benchmark: 2.0-3.0 wps for clear, professional delivery",
|
| 501 |
-
f"- Filler Word Frequency: {analysis['filler_ratio'] * 100:.1f}% - Measures non-content words (e.g., 'um', 'like')",
|
| 502 |
-
f"- Repetition Index: {analysis['repetition_score']:.3f} - Frequency of repeated phrases or ideas",
|
| 503 |
-
f"- Anxiety Indicator: {analysis['interpretation']['anxiety_level']} (Score: {analysis['composite_scores']['anxiety']:.3f}) - Derived from pitch variation and vocal stability",
|
| 504 |
-
f"- Confidence Indicator: {analysis['interpretation']['confidence_level']} (Score: {analysis['composite_scores']['confidence']:.3f}) - Reflects vocal strength and consistency",
|
| 505 |
-
f"- Fluency Rating: {analysis['interpretation']['fluency_level']} - Assesses speech flow and coherence",
|
| 506 |
-
"",
|
| 507 |
-
"HR Performance Insights:",
|
| 508 |
-
"- Rapid speech (>3.0 wps) may signal enthusiasm but risks clarity; slower, deliberate pacing enhances professionalism.",
|
| 509 |
-
"- Elevated filler word use reduces perceived polish and can distract from key messages.",
|
| 510 |
-
"- High anxiety scores suggest interview pressure; training can build resilience.",
|
| 511 |
-
"- Strong confidence indicators align with leadership presence and effective communication.",
|
| 512 |
-
"- Fluent speech enhances engagement, critical for client-facing or team roles."
|
| 513 |
-
]
|
| 514 |
-
return "\n".join(interpretation_lines)
|
| 515 |
-
|
| 516 |
-
def generate_anxiety_confidence_chart(composite_scores: Dict, chart_path_or_buffer):
|
| 517 |
-
try:
|
| 518 |
-
labels = ['Anxiety', 'Confidence']
|
| 519 |
-
scores = [composite_scores.get('anxiety', 0), composite_scores.get('confidence', 0)]
|
| 520 |
-
fig, ax = plt.subplots(figsize=(5, 3))
|
| 521 |
-
bars = ax.bar(labels, scores, color=['#FF6B6B', '#4ECDC4'], edgecolor='black', width=0.6)
|
| 522 |
-
ax.set_ylabel('Score (Normalized)', fontsize=12)
|
| 523 |
-
ax.set_title('Vocal Dynamics: Anxiety vs. Confidence', fontsize=14, pad=15)
|
| 524 |
-
ax.set_ylim(0, 1.2)
|
| 525 |
-
for bar in bars:
|
| 526 |
-
height = bar.get_height()
|
| 527 |
-
ax.text(bar.get_x() + bar.get_width()/2, height + 0.05, f"{height:.2f}",
|
| 528 |
-
ha='center', color='black', fontweight='bold', fontsize=11)
|
| 529 |
-
ax.grid(True, axis='y', linestyle='--', alpha=0.7)
|
| 530 |
-
plt.tight_layout()
|
| 531 |
-
plt.savefig(chart_path_or_buffer, format='png', bbox_inches='tight', dpi=200)
|
| 532 |
-
plt.close(fig)
|
| 533 |
-
except Exception as e:
|
| 534 |
-
logger.error(f"Error generating chart: {str(e)}")
|
| 535 |
-
|
| 536 |
def calculate_acceptance_probability(analysis_data: Dict) -> float:
|
|
|
|
| 537 |
voice = analysis_data.get('voice_analysis', {})
|
| 538 |
-
if 'error' in voice: return
|
| 539 |
w_confidence, w_anxiety, w_fluency, w_speaking_rate, w_filler_repetition, w_content_strengths = 0.35, -0.25, 0.2, 0.15, -0.15, 0.25
|
| 540 |
confidence_score = voice.get('composite_scores', {}).get('confidence', 0.0)
|
| 541 |
anxiety_score = voice.get('composite_scores', {}).get('anxiety', 0.0)
|
|
@@ -553,48 +261,54 @@ def calculate_acceptance_probability(analysis_data: Dict) -> float:
|
|
| 553 |
content_strength_val = 0.85 if analysis_data.get('text_analysis', {}).get('total_duration', 0) > 60 else 0.4
|
| 554 |
raw_score = (confidence_score * w_confidence + (1 - anxiety_score) * abs(w_anxiety) + fluency_val * w_fluency + speaking_rate_score * w_speaking_rate + filler_repetition_score * abs(w_filler_repetition) + content_strength_val * w_content_strengths)
|
| 555 |
max_possible_score = (w_confidence + abs(w_anxiety) + w_fluency + w_speaking_rate + abs(w_filler_repetition) + w_content_strengths)
|
| 556 |
-
if max_possible_score
|
| 557 |
-
normalized_score = raw_score / max_possible_score
|
| 558 |
acceptance_probability = max(0.0, min(1.0, normalized_score))
|
| 559 |
return float(f"{acceptance_probability * 100:.2f}")
|
| 560 |
|
| 561 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
try:
|
| 563 |
voice = analysis_data.get('voice_analysis', {})
|
| 564 |
-
voice_interpretation =
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
prompt = f"""
|
| 575 |
-
You are
|
| 576 |
-
|
| 577 |
-
**
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
**
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
**
|
| 587 |
-
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
**
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
-
|
| 596 |
-
- Target: Communication Effectiveness, Response Depth, Professional Impact.
|
| 597 |
-
- Suggest clear next steps for hiring managers (e.g., advance, train, assess).
|
| 598 |
"""
|
| 599 |
response = gemini_model.generate_content(prompt)
|
| 600 |
return response.text
|
|
@@ -602,278 +316,123 @@ def generate_report(analysis_data: Dict) -> str:
|
|
| 602 |
logger.error(f"Report generation failed: {str(e)}")
|
| 603 |
return f"Error generating report: {str(e)}"
|
| 604 |
|
|
|
|
| 605 |
def create_pdf_report(analysis_data: Dict, output_path: str, gemini_report_text: str):
|
| 606 |
try:
|
| 607 |
doc = SimpleDocTemplate(output_path, pagesize=letter,
|
| 608 |
-
rightMargin=0.
|
| 609 |
-
topMargin=
|
| 610 |
styles = getSampleStyleSheet()
|
| 611 |
-
h1 = ParagraphStyle(name='Heading1', fontSize=
|
| 612 |
-
h2 = ParagraphStyle(name='Heading2', fontSize=
|
| 613 |
-
|
| 614 |
-
body_text = ParagraphStyle(name='BodyText', parent=styles['Normal'], fontSize=10, leading=14, spaceAfter=10, fontName='Helvetica')
|
| 615 |
-
bullet_style = ParagraphStyle(name='Bullet', parent=body_text, leftIndent=25, bulletIndent=12, fontName='Helvetica')
|
| 616 |
|
| 617 |
story = []
|
| 618 |
-
|
| 619 |
def header_footer(canvas, doc):
|
| 620 |
canvas.saveState()
|
| 621 |
canvas.setFont('Helvetica', 9)
|
| 622 |
-
canvas.setFillColor(colors.
|
| 623 |
-
canvas.drawString(doc.leftMargin, 0.5 * inch, f"Page {doc.page} | EvalBot
|
| 624 |
-
canvas.setStrokeColor(colors.HexColor('#2E5A87'))
|
| 625 |
-
canvas.setLineWidth(1.2)
|
| 626 |
-
canvas.line(doc.leftMargin, doc.height + 0.9*inch, doc.width + doc.leftMargin, doc.height + 0.9*inch)
|
| 627 |
-
canvas.setFont('Helvetica-Bold', 11)
|
| 628 |
-
canvas.drawString(doc.leftMargin, doc.height + 0.95*inch, "Candidate Interview Analysis")
|
| 629 |
-
canvas.setFillColor(colors.HexColor('#666666'))
|
| 630 |
-
canvas.drawRightString(doc.width + doc.leftMargin, doc.height + 0.95*inch, time.strftime('%B %d, %Y'))
|
| 631 |
canvas.restoreState()
|
| 632 |
|
| 633 |
-
#
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
story.append(Paragraph("<b>HR Verdict:</b> Moderate potential, requires additional assessment and skill-building.", body_text))
|
| 649 |
else:
|
| 650 |
-
|
| 651 |
-
story.append(Spacer(1, 0.4 * inch))
|
| 652 |
-
table_data = [
|
| 653 |
-
['Key Metrics', 'Value'],
|
| 654 |
-
['Interview Length', f"{analysis_data['text_analysis']['total_duration']:.2f} seconds"],
|
| 655 |
-
['Speaker Turns', f"{analysis_data['text_analysis']['speaker_turns']}"],
|
| 656 |
-
['Participants', ', '.join(analysis_data['speakers'])]
|
| 657 |
-
]
|
| 658 |
-
table = Table(table_data, colWidths=[2.5*inch, 4*inch])
|
| 659 |
-
table.setStyle(TableStyle([
|
| 660 |
-
('BACKGROUND', (0,0), (-1,0), colors.HexColor('#2E5A87')),
|
| 661 |
-
('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke),
|
| 662 |
-
('ALIGN', (0,0), (-1,-1), 'LEFT'),
|
| 663 |
-
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
|
| 664 |
-
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
| 665 |
-
('FONTSIZE', (0, 0), (-1, -1), 10),
|
| 666 |
-
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
| 667 |
-
('TOPPADDING', (0, 0), (-1, 0), 12),
|
| 668 |
-
('BACKGROUND', (0, 1), (-1, -1), colors.HexColor('#F5F7FA')),
|
| 669 |
-
('GRID', (0,0), (-1,-1), 1, colors.HexColor('#DDE4EB'))
|
| 670 |
-
]))
|
| 671 |
-
story.append(table)
|
| 672 |
-
story.append(Spacer(1, 0.5 * inch))
|
| 673 |
-
story.append(Paragraph("Prepared by: EvalBot - AI-Powered HR Analysis System", body_text))
|
| 674 |
-
story.append(PageBreak())
|
| 675 |
-
|
| 676 |
-
# Detailed Analysis
|
| 677 |
-
story.append(Paragraph("Detailed Candidate Profile", h1))
|
| 678 |
-
|
| 679 |
-
story.append(Paragraph("1. Communication & Vocal Dynamics", h2))
|
| 680 |
-
voice_analysis = analysis_data.get('voice_analysis', {})
|
| 681 |
-
if voice_analysis and 'error' not in voice_analysis:
|
| 682 |
-
table_data = [
|
| 683 |
-
['Metric', 'Value', 'HR Insight'],
|
| 684 |
-
['Speaking Rate', f"{voice_analysis.get('speaking_rate', 0):.2f} words/sec", 'Benchmark: 2.0-3.0 wps; affects clarity, poise'],
|
| 685 |
-
['Filler Word Frequency', f"{voice_analysis.get('filler_ratio', 0) * 100:.1f}%", 'Excess use impacts polish, credibility'],
|
| 686 |
-
['Anxiety Indicator', voice_analysis.get('interpretation', {}).get('anxiety_level', 'N/A'), f"Score: {voice_analysis.get('composite_scores', {}).get('anxiety', 0):.3f}; shows stress response"],
|
| 687 |
-
['Confidence Indicator', voice_analysis.get('interpretation', {}).get('confidence_level', 'N/A'), f"Score: {voice_analysis.get('composite_scores', {}).get('confidence', 0):.3f}; reflects vocal strength"],
|
| 688 |
-
['Fluency Rating', voice_analysis.get('interpretation', {}).get('fluency_level', 'N/A'), 'Drives engagement, message impact']
|
| 689 |
-
]
|
| 690 |
-
table = Table(table_data, colWidths=[1.9*inch, 1.3*inch, 3.3*inch])
|
| 691 |
-
table.setStyle(TableStyle([
|
| 692 |
-
('BACKGROUND', (0,0), (-1,0), colors.HexColor('#2E5A87')),
|
| 693 |
-
('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke),
|
| 694 |
-
('ALIGN', (0,0), (-1,-1), 'LEFT'),
|
| 695 |
-
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
|
| 696 |
-
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
| 697 |
-
('FONTSIZE', (0, 0), (-1, -1), 9),
|
| 698 |
-
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
| 699 |
-
('TOPPADDING', (0, 0), (-1, 0), 12),
|
| 700 |
-
('BACKGROUND', (0, 1), (-1, -1), colors.HexColor('#F5F7FA')),
|
| 701 |
-
('GRID', (0,0), (-1,-1), 1, colors.HexColor('#DDE4EB'))
|
| 702 |
-
]))
|
| 703 |
-
story.append(table)
|
| 704 |
-
story.append(Spacer(1, 0.3 * inch))
|
| 705 |
-
chart_buffer = io.BytesIO()
|
| 706 |
-
generate_anxiety_confidence_chart(voice_analysis.get('composite_scores', {}), chart_buffer)
|
| 707 |
-
chart_buffer.seek(0)
|
| 708 |
-
img = Image(chart_buffer, width=5*inch, height=3*inch)
|
| 709 |
-
img.hAlign = 'CENTER'
|
| 710 |
-
story.append(img)
|
| 711 |
-
else:
|
| 712 |
-
story.append(Paragraph("Vocal analysis unavailable due to processing constraints.", body_text))
|
| 713 |
-
story.append(Spacer(1, 0.4 * inch))
|
| 714 |
-
|
| 715 |
-
# Parse Gemini Report
|
| 716 |
-
sections = {}
|
| 717 |
-
section_titles = ["Executive Summary", "Communication and Vocal Dynamics",
|
| 718 |
-
"Competency and Content Evaluation",
|
| 719 |
-
"Role Fit and Growth Potential", "Strategic HR Recommendations"]
|
| 720 |
-
for title in section_titles:
|
| 721 |
-
sections[title] = []
|
| 722 |
-
report_parts = re.split(r'(\s*\*\*\s*\d\.\s*.*?\s*\*\*)', gemini_report_text)
|
| 723 |
-
current_section = None
|
| 724 |
-
for part in report_parts:
|
| 725 |
-
if not part.strip(): continue
|
| 726 |
-
is_heading = False
|
| 727 |
-
for title in section_titles:
|
| 728 |
-
if title.lower() in part.lower():
|
| 729 |
-
current_section = title
|
| 730 |
-
is_heading = True
|
| 731 |
-
break
|
| 732 |
-
if not is_heading and current_section:
|
| 733 |
-
sections[current_section].append(part.strip())
|
| 734 |
-
|
| 735 |
-
# Executive Summary
|
| 736 |
-
story.append(Paragraph("2. Executive Summary", h2))
|
| 737 |
-
if sections['Executive Summary']:
|
| 738 |
-
for line in sections['Executive Summary']:
|
| 739 |
-
if line.startswith(('-', '•', '*')):
|
| 740 |
-
story.append(Paragraph(line.lstrip('-•* ').strip(), bullet_style))
|
| 741 |
-
else:
|
| 742 |
-
story.append(Paragraph(line, body_text))
|
| 743 |
-
else:
|
| 744 |
-
story.append(Paragraph("Executive summary unavailable.", body_text))
|
| 745 |
-
story.append(Spacer(1, 0.4 * inch))
|
| 746 |
-
|
| 747 |
-
# Competency and Content
|
| 748 |
-
story.append(Paragraph("3. Competency & Content Evaluation", h2))
|
| 749 |
-
if sections['Competency and Content Evaluation']:
|
| 750 |
-
story.append(Paragraph("Strengths", h3))
|
| 751 |
-
strengths_found = False
|
| 752 |
-
for line in sections['Competency and Content Evaluation']:
|
| 753 |
-
if 'strength' in line.lower() or any(k in line.lower() for k in ['leadership', 'problem-solving', 'communication', 'adaptability']):
|
| 754 |
-
story.append(Paragraph(line.lstrip('-•* ').strip(), bullet_style))
|
| 755 |
-
strengths_found = True
|
| 756 |
-
if not strengths_found:
|
| 757 |
-
story.append(Paragraph("No specific strengths identified.", body_text))
|
| 758 |
-
story.append(Spacer(1, 0.2 * inch))
|
| 759 |
-
story.append(Paragraph("Growth Areas", h3))
|
| 760 |
-
growth_found = False
|
| 761 |
-
for line in sections['Competency and Content Evaluation']:
|
| 762 |
-
if 'improve' in line.lower() or 'weak' in line.lower() or 'challenge' in line.lower():
|
| 763 |
-
story.append(Paragraph(line.lstrip('-•* ').strip(), bullet_style))
|
| 764 |
-
growth_found = True
|
| 765 |
-
if not growth_found:
|
| 766 |
-
story.append(Paragraph("No specific growth areas identified.", body_text))
|
| 767 |
-
else:
|
| 768 |
-
story.append(Paragraph("Competency and content evaluation unavailable.", body_text))
|
| 769 |
-
story.append(PageBreak())
|
| 770 |
-
|
| 771 |
-
# Role Fit
|
| 772 |
-
story.append(Paragraph("4. Role Fit & Growth Potential", h2))
|
| 773 |
-
if sections['Role Fit and Growth Potential']:
|
| 774 |
-
for line in sections['Role Fit and Growth Potential']:
|
| 775 |
-
if line.startswith(('-', '•', '*')):
|
| 776 |
-
story.append(Paragraph(line.lstrip('-•* ').strip(), bullet_style))
|
| 777 |
-
else:
|
| 778 |
-
story.append(Paragraph(line, body_text))
|
| 779 |
-
else:
|
| 780 |
-
story.append(Paragraph("Role fit and potential analysis unavailable.", body_text))
|
| 781 |
-
story.append(Spacer(1, 0.4 * inch))
|
| 782 |
-
|
| 783 |
-
# HR Recommendations
|
| 784 |
-
story.append(Paragraph("5. Strategic HR Recommendations", h2))
|
| 785 |
-
if sections['Strategic HR Recommendations']:
|
| 786 |
-
story.append(Paragraph("Development Priorities", h3))
|
| 787 |
-
dev_found = False
|
| 788 |
-
for line in sections['Strategic HR Recommendations']:
|
| 789 |
-
if any(k in line.lower() for k in ['communication', 'clarity', 'depth', 'presence', 'improve']):
|
| 790 |
-
story.append(Paragraph(line.lstrip('-•* ').strip(), bullet_style))
|
| 791 |
-
dev_found = True
|
| 792 |
-
if not dev_found:
|
| 793 |
-
story.append(Paragraph("No development priorities specified.", body_text))
|
| 794 |
-
story.append(Spacer(1, 0.2 * inch))
|
| 795 |
-
story.append(Paragraph("Next Steps for Hiring Managers", h3))
|
| 796 |
-
steps_found = False
|
| 797 |
-
for line in sections['Strategic HR Recommendations']:
|
| 798 |
-
if any(k in line.lower() for k in ['advance', 'train', 'assess', 'next step']):
|
| 799 |
-
story.append(Paragraph(line.lstrip('-•* ').strip(), bullet_style))
|
| 800 |
-
steps_found = True
|
| 801 |
-
if not steps_found:
|
| 802 |
-
story.append(Paragraph("No specific next steps provided.", body_text))
|
| 803 |
-
else:
|
| 804 |
-
story.append(Paragraph("Strategic recommendations unavailable.", body_text))
|
| 805 |
-
story.append(Spacer(1, 0.3 * inch))
|
| 806 |
-
story.append(Paragraph("This report delivers a comprehensive, data-driven evaluation to guide hiring decisions and candidate development.", body_text))
|
| 807 |
|
| 808 |
doc.build(story, onFirstPage=header_footer, onLaterPages=header_footer)
|
| 809 |
return True
|
| 810 |
except Exception as e:
|
| 811 |
-
logger.error(f"
|
| 812 |
return False
|
| 813 |
|
| 814 |
-
def convert_to_serializable(obj):
|
| 815 |
-
"""Convert numpy data types to Python native types for JSON serialization"""
|
| 816 |
-
if isinstance(obj, np.generic):
|
| 817 |
-
return obj.item()
|
| 818 |
-
elif isinstance(obj, dict):
|
| 819 |
-
return {key: convert_to_serializable(value) for key, value in obj.items()}
|
| 820 |
-
elif isinstance(obj, list):
|
| 821 |
-
return [convert_to_serializable(item) for item in obj]
|
| 822 |
-
elif isinstance(obj, np.ndarray):
|
| 823 |
-
return obj.tolist()
|
| 824 |
-
return obj
|
| 825 |
|
| 826 |
-
|
|
|
|
| 827 |
local_audio_path = None
|
| 828 |
wav_file = None
|
| 829 |
is_downloaded = False
|
| 830 |
try:
|
| 831 |
-
logger.info(f"Starting processing for {
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
local_audio_path = audio_path_or_url
|
| 837 |
wav_file = convert_to_wav(local_audio_path)
|
| 838 |
transcript = transcribe(wav_file)
|
| 839 |
-
|
| 840 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
utterances_with_speakers = identify_speakers(transcript, wav_file)
|
| 842 |
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
clf, vectorizer, scaler = train_role_classifier(utterances_with_speakers)
|
| 849 |
-
classified_utterances = classify_roles(utterances_with_speakers, clf, vectorizer, scaler)
|
| 850 |
voice_analysis = analyze_interviewee_voice(wav_file, classified_utterances)
|
|
|
|
|
|
|
| 851 |
analysis_data = {
|
|
|
|
| 852 |
'transcript': classified_utterances,
|
| 853 |
-
'speakers': list(set(u['speaker'] for u in classified_utterances)),
|
| 854 |
'voice_analysis': voice_analysis,
|
| 855 |
'text_analysis': {
|
| 856 |
-
'total_duration': sum(u
|
| 857 |
'speaker_turns': len(classified_utterances)
|
| 858 |
}
|
| 859 |
}
|
|
|
|
| 860 |
analysis_data['acceptance_probability'] = calculate_acceptance_probability(analysis_data)
|
| 861 |
-
gemini_report_text = generate_report(analysis_data)
|
|
|
|
| 862 |
base_name = str(uuid.uuid4())
|
| 863 |
-
|
|
|
|
| 864 |
json_path = os.path.join(OUTPUT_DIR, f"{base_name}_analysis.json")
|
| 865 |
-
|
|
|
|
|
|
|
| 866 |
with open(json_path, 'w') as f:
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
logger.info(f"Processing completed for {
|
| 870 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 871 |
except Exception as e:
|
| 872 |
-
logger.error(f"Processing failed for {
|
| 873 |
raise
|
|
|
|
| 874 |
finally:
|
| 875 |
if wav_file and os.path.exists(wav_file):
|
| 876 |
-
os.remove(wav_file)
|
|
|
|
| 877 |
if is_downloaded and local_audio_path and os.path.exists(local_audio_path):
|
| 878 |
-
|
| 879 |
-
|
|
|
|
|
|
|
|
|
| 19 |
import logging
|
| 20 |
import tempfile
|
| 21 |
from reportlab.lib.pagesizes import letter
|
| 22 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, Image, HRFlowable
|
| 23 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 24 |
from reportlab.lib.units import inch
|
| 25 |
from reportlab.lib import colors
|
| 26 |
import matplotlib.pyplot as plt
|
| 27 |
import matplotlib
|
| 28 |
matplotlib.use('Agg')
|
|
|
|
| 29 |
import io
|
| 30 |
+
from transformers import AutoTokenizer, AutoModel, pipeline
|
| 31 |
import spacy
|
| 32 |
import google.generativeai as genai
|
| 33 |
import joblib
|
|
|
|
| 36 |
# Setup logging
|
| 37 |
logging.basicConfig(level=logging.INFO)
|
| 38 |
logger = logging.getLogger(__name__)
|
| 39 |
+
logging.getLogger("nemo_logger").setLevel(logging.WARNING)
|
|
|
|
| 40 |
|
| 41 |
# Configuration
|
|
|
|
| 42 |
OUTPUT_DIR = "./processed_audio"
|
| 43 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 44 |
|
| 45 |
# API Keys
|
| 46 |
+
PINECONE_KEY = os.getenv("PINECONE_KEY", "your-pinecone-key")
|
| 47 |
+
ASSEMBLYAI_KEY = os.getenv("ASSEMBLYAI_KEY", "your-assemblyai-key")
|
| 48 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "your-gemini-key")
|
| 49 |
+
|
| 50 |
+
def validate_url(url: str) -> bool:
|
| 51 |
+
try:
|
| 52 |
+
response = requests.head(url, timeout=5)
|
| 53 |
+
return response.status_code == 200
|
| 54 |
+
except requests.RequestException as e:
|
| 55 |
+
logger.error(f"URL validation failed for {url}: {str(e)}")
|
| 56 |
+
return False
|
| 57 |
|
| 58 |
def download_audio_from_url(url: str) -> str:
|
| 59 |
+
if not validate_url(url):
|
| 60 |
+
raise ValueError(f"Audio file not found or inaccessible at {url}")
|
| 61 |
try:
|
| 62 |
temp_dir = tempfile.gettempdir()
|
| 63 |
temp_path = os.path.join(temp_dir, f"{uuid.uuid4()}.tmp_audio")
|
| 64 |
logger.info(f"Downloading audio from {url} to {temp_path}")
|
| 65 |
+
with requests.get(url, stream=True, timeout=10) as r:
|
| 66 |
r.raise_for_status()
|
| 67 |
with open(temp_path, 'wb') as f:
|
| 68 |
for chunk in r.iter_content(chunk_size=8192):
|
| 69 |
f.write(chunk)
|
| 70 |
return temp_path
|
| 71 |
except Exception as e:
|
| 72 |
+
logger.error(f"Failed to download audio from URL {url}: {str(e)}")
|
| 73 |
raise
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
def initialize_services():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
+
pc = Pinecone(api_key=PINECONE_KEY)
|
| 78 |
+
index_name = "interview-speaker-embeddings"
|
| 79 |
+
if index_name not in pc.list_indexes().names():
|
| 80 |
+
pc.create_index(name=index_name, dimension=192, metric="cosine", spec=ServerlessSpec(cloud="aws", region="us-east-1"))
|
| 81 |
+
index = pc.Index(index_name)
|
| 82 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 83 |
+
gemini_model = genai.GenerativeModel('gemini-1.5-flash')
|
| 84 |
+
return index, gemini_model
|
| 85 |
except Exception as e:
|
| 86 |
+
logger.error(f"Error initializing services: {str(e)}")
|
| 87 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
index, gemini_model = initialize_services()
|
| 90 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 91 |
logger.info(f"Using device: {device}")
|
| 92 |
|
|
|
|
| 93 |
def load_models():
|
| 94 |
+
speaker_model = EncDecSpeakerLabelModel.from_pretrained("nvidia/speakerverification_en_titanet_large", map_location=device)
|
| 95 |
speaker_model.eval()
|
|
|
|
| 96 |
nlp = spacy.load("en_core_web_sm")
|
| 97 |
+
# Removed unused models for clarity
|
| 98 |
+
return speaker_model, nlp
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
speaker_model, nlp = load_models()
|
| 101 |
|
|
|
|
| 102 |
def convert_to_wav(audio_path: str, output_dir: str = OUTPUT_DIR) -> str:
|
| 103 |
+
# This function is unchanged from your version
|
| 104 |
try:
|
| 105 |
audio = AudioSegment.from_file(audio_path)
|
| 106 |
+
if audio.channels > 1: audio = audio.set_channels(1)
|
|
|
|
| 107 |
audio = audio.set_frame_rate(16000)
|
|
|
|
| 108 |
wav_file = os.path.join(output_dir, f"{uuid.uuid4()}.wav")
|
| 109 |
audio.export(wav_file, format="wav")
|
| 110 |
return wav_file
|
|
|
|
| 113 |
raise
|
| 114 |
|
| 115 |
def extract_prosodic_features(audio_path: str, start_ms: int, end_ms: int) -> Dict:
|
| 116 |
+
# This function is unchanged from your version
|
| 117 |
try:
|
| 118 |
audio = AudioSegment.from_file(audio_path)
|
| 119 |
segment = audio[start_ms:end_ms]
|
| 120 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 121 |
+
segment.export(tmp.name, format="wav")
|
| 122 |
+
y, sr = librosa.load(tmp.name, sr=16000)
|
| 123 |
+
os.remove(tmp.name)
|
| 124 |
+
pitches, _ = librosa.piptrack(y=y, sr=sr)
|
| 125 |
pitches = pitches[pitches > 0]
|
| 126 |
+
return {
|
| 127 |
+
'duration': (end_ms - start_ms) / 1000.0,
|
|
|
|
| 128 |
'mean_pitch': float(np.mean(pitches)) if len(pitches) > 0 else 0.0,
|
| 129 |
'min_pitch': float(np.min(pitches)) if len(pitches) > 0 else 0.0,
|
| 130 |
'max_pitch': float(np.max(pitches)) if len(pitches) > 0 else 0.0,
|
|
|
|
| 134 |
'intensityMax': float(np.max(librosa.feature.rms(y=y)[0])),
|
| 135 |
'intensitySD': float(np.std(librosa.feature.rms(y=y)[0])),
|
| 136 |
}
|
|
|
|
|
|
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
logger.error(f"Feature extraction failed: {str(e)}")
|
| 139 |
+
return {}
|
| 140 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
|
|
|
| 142 |
def transcribe(audio_path: str) -> Dict:
|
| 143 |
+
# This function is unchanged from your version
|
| 144 |
try:
|
|
|
|
| 145 |
with open(audio_path, 'rb') as f:
|
| 146 |
+
upload_response = requests.post("https://api.assemblyai.com/v2/upload", headers={"authorization": ASSEMBLYAI_KEY}, data=f)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
audio_url = upload_response.json()['upload_url']
|
| 148 |
+
transcript_response = requests.post("https://api.assemblyai.com/v2/transcript", headers={"authorization": ASSEMBLYAI_KEY}, json={"audio_url": audio_url, "speaker_labels": True, "filter_profanity": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
transcript_id = transcript_response.json()['id']
|
|
|
|
|
|
|
| 150 |
while True:
|
| 151 |
+
result = requests.get(f"https://api.assemblyai.com/v2/transcript/{transcript_id}", headers={"authorization": ASSEMBLYAI_KEY}).json()
|
| 152 |
+
if result['status'] == 'completed': return result
|
| 153 |
+
elif result['status'] == 'error': raise Exception(f"AssemblyAI Error: {result.get('error')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
time.sleep(5)
|
| 155 |
except Exception as e:
|
| 156 |
logger.error(f"Transcription failed: {str(e)}")
|
| 157 |
raise
|
| 158 |
|
| 159 |
+
def process_utterance(utterance: Dict, full_audio: AudioSegment) -> Dict:
|
| 160 |
+
# This function is unchanged from your version
|
|
|
|
| 161 |
try:
|
| 162 |
+
start, end = utterance['start'], utterance['end']
|
|
|
|
|
|
|
| 163 |
segment = full_audio[start:end]
|
| 164 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 165 |
+
segment.export(tmp.name, format="wav")
|
| 166 |
+
with torch.no_grad():
|
| 167 |
+
embedding = speaker_model.get_embedding(tmp.name).cpu().numpy()
|
| 168 |
+
os.remove(tmp.name)
|
| 169 |
+
embedding_list = embedding.flatten().tolist()
|
| 170 |
+
query_result = index.query(vector=embedding_list, top_k=1, include_metadata=True)
|
| 171 |
+
if query_result['matches'] and query_result['matches'][0]['score'] > 0.75:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
speaker_id = query_result['matches'][0]['id']
|
| 173 |
speaker_name = query_result['matches'][0]['metadata']['speaker_name']
|
| 174 |
else:
|
| 175 |
+
speaker_id = f"speaker_{uuid.uuid4().hex[:6]}"
|
| 176 |
+
speaker_name = f"Speaker_{speaker_id[-4:].upper()}"
|
| 177 |
+
index.upsert([(speaker_id, embedding_list, {"speaker_name": speaker_name})])
|
| 178 |
+
return {**utterance, 'speaker': speaker_name, 'speaker_id': speaker_id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
except Exception as e:
|
| 180 |
logger.error(f"Utterance processing failed: {str(e)}")
|
| 181 |
+
return {**utterance, 'speaker': 'Unknown', 'speaker_id': 'unknown'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
def identify_speakers(transcript: Dict, wav_file: str) -> List[Dict]:
|
| 184 |
+
# This function is unchanged from your version
|
| 185 |
try:
|
| 186 |
full_audio = AudioSegment.from_wav(wav_file)
|
| 187 |
+
utterances = transcript.get('utterances', [])
|
| 188 |
+
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 189 |
+
futures = [executor.submit(process_utterance, u, full_audio) for u in utterances]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
results = [f.result() for f in futures]
|
|
|
|
| 191 |
return results
|
| 192 |
except Exception as e:
|
| 193 |
logger.error(f"Speaker identification failed: {str(e)}")
|
| 194 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
def classify_roles(utterances: List[Dict]) -> List[Dict]:
|
| 197 |
+
# Using simple alternating logic as per your decision to pause on training a custom model
|
| 198 |
+
results = []
|
| 199 |
+
for i, utterance in enumerate(utterances):
|
| 200 |
+
utterance['role'] = 'Interviewer' if i % 2 == 0 else 'Interviewee'
|
| 201 |
+
results.append(utterance)
|
| 202 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
|
|
|
| 204 |
def analyze_interviewee_voice(audio_path: str, utterances: List[Dict]) -> Dict:
|
| 205 |
+
# This function is unchanged from your version
|
| 206 |
try:
|
|
|
|
| 207 |
y, sr = librosa.load(audio_path, sr=16000)
|
| 208 |
+
interviewee_utterances = [u for u in utterances if u.get('role') == 'Interviewee']
|
| 209 |
+
if not interviewee_utterances: return {'error': 'No interviewee utterances found'}
|
| 210 |
+
segments = [y[int(u['start']*sr/1000):int(u['end']*sr/1000)] for u in interviewee_utterances if u['end'] > u['start']]
|
| 211 |
+
if not segments: return {'error': 'No valid audio segments found'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
total_duration = sum(u['prosodic_features']['duration'] for u in interviewee_utterances)
|
| 213 |
total_words = sum(len(u['text'].split()) for u in interviewee_utterances)
|
| 214 |
speaking_rate = total_words / total_duration if total_duration > 0 else 0
|
|
|
|
|
|
|
| 215 |
filler_words = ['um', 'uh', 'like', 'you know', 'so', 'i mean']
|
| 216 |
+
filler_count = sum(sum(u['text'].lower().count(fw) for fw in filler_words) for u in interviewee_utterances)
|
|
|
|
|
|
|
|
|
|
| 217 |
filler_ratio = filler_count / total_words if total_words > 0 else 0
|
| 218 |
+
pitches, intensities = [], []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
for segment in segments:
|
| 220 |
+
if len(segment) == 0: continue
|
| 221 |
+
f0, voiced_flag, _ = librosa.pyin(segment, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'), sr=sr)
|
| 222 |
pitches.extend(f0[voiced_flag])
|
| 223 |
+
intensities.extend(librosa.feature.rms(y=segment)[0])
|
| 224 |
+
pitch_mean = float(np.mean(pitches)) if len(pitches) > 0 else 0.0
|
| 225 |
+
intensity_std = float(np.std(intensities)) if len(intensities) > 0 else 0.0
|
| 226 |
+
jitter = float(np.mean(np.abs(np.diff(pitches))) / pitch_mean) if len(pitches) > 1 and pitch_mean > 0 else 0.0
|
| 227 |
+
shimmer = float(np.mean(np.abs(np.diff(intensities))) / np.mean(intensities)) if len(intensities) > 1 and np.mean(intensities) > 0 else 0.0
|
| 228 |
+
anxiety_score = 0.6 * (np.std(pitches)/pitch_mean if pitch_mean > 0 else 0) + 0.4 * (jitter + shimmer)
|
| 229 |
+
confidence_score = 0.7 * (1/(1+intensity_std)) + 0.3 * (1-filler_ratio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
return {
|
| 231 |
+
'speaking_rate': round(speaking_rate, 2), 'filler_ratio': round(filler_ratio, 3),
|
| 232 |
+
'composite_scores': {'anxiety': round(anxiety_score, 3), 'confidence': round(confidence_score, 3)},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
'interpretation': {
|
| 234 |
+
'anxiety_level': 'High' if anxiety_score > 0.15 else 'Moderate' if anxiety_score > 0.07 else 'Low',
|
| 235 |
+
'confidence_level': 'High' if confidence_score > 0.75 else 'Moderate' if confidence_score > 0.5 else 'Low',
|
| 236 |
+
'fluency_level': 'Fluent' if filler_ratio < 0.05 else 'Moderate'
|
| 237 |
}
|
| 238 |
}
|
| 239 |
except Exception as e:
|
| 240 |
logger.error(f"Voice analysis failed: {str(e)}")
|
| 241 |
return {'error': str(e)}
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
def calculate_acceptance_probability(analysis_data: Dict) -> float:
|
| 244 |
+
# This is your custom, detailed function
|
| 245 |
voice = analysis_data.get('voice_analysis', {})
|
| 246 |
+
if 'error' in voice: return 50.0
|
| 247 |
w_confidence, w_anxiety, w_fluency, w_speaking_rate, w_filler_repetition, w_content_strengths = 0.35, -0.25, 0.2, 0.15, -0.15, 0.25
|
| 248 |
confidence_score = voice.get('composite_scores', {}).get('confidence', 0.0)
|
| 249 |
anxiety_score = voice.get('composite_scores', {}).get('anxiety', 0.0)
|
|
|
|
| 261 |
content_strength_val = 0.85 if analysis_data.get('text_analysis', {}).get('total_duration', 0) > 60 else 0.4
|
| 262 |
raw_score = (confidence_score * w_confidence + (1 - anxiety_score) * abs(w_anxiety) + fluency_val * w_fluency + speaking_rate_score * w_speaking_rate + filler_repetition_score * abs(w_filler_repetition) + content_strength_val * w_content_strengths)
|
| 263 |
max_possible_score = (w_confidence + abs(w_anxiety) + w_fluency + w_speaking_rate + abs(w_filler_repetition) + w_content_strengths)
|
| 264 |
+
normalized_score = raw_score / max_possible_score if max_possible_score > 0 else 0.5
|
|
|
|
| 265 |
acceptance_probability = max(0.0, min(1.0, normalized_score))
|
| 266 |
return float(f"{acceptance_probability * 100:.2f}")
|
| 267 |
|
| 268 |
+
def convert_to_serializable(obj):
|
| 269 |
+
# This function is unchanged
|
| 270 |
+
if isinstance(obj, np.generic): return obj.item()
|
| 271 |
+
if isinstance(obj, dict): return {k: convert_to_serializable(v) for k, v in obj.items()}
|
| 272 |
+
if isinstance(obj, list): return [convert_to_serializable(i) for i in obj]
|
| 273 |
+
if isinstance(obj, np.ndarray): return obj.tolist()
|
| 274 |
+
return obj
|
| 275 |
+
|
| 276 |
+
# --- NEW: HR Persona Report Generation ---
|
| 277 |
+
def generate_report(analysis_data: Dict, user_id: str) -> str:
|
| 278 |
try:
|
| 279 |
voice = analysis_data.get('voice_analysis', {})
|
| 280 |
+
voice_interpretation = "Voice analysis data was not available."
|
| 281 |
+
if voice and 'error' not in voice:
|
| 282 |
+
voice_interpretation = (
|
| 283 |
+
f"The candidate's voice profile indicates a '{voice.get('interpretation', {}).get('confidence_level', 'N/A').upper()}' confidence level "
|
| 284 |
+
f"and a '{voice.get('interpretation', {}).get('anxiety_level', 'N/A').upper()}' anxiety level. "
|
| 285 |
+
f"Fluency was rated as '{voice.get('interpretation', {}).get('fluency_level', 'N/A').upper()}'."
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
prob = analysis_data.get('acceptance_probability')
|
| 289 |
+
|
| 290 |
prompt = f"""
|
| 291 |
+
**Persona:** You are a Senior HR Partner writing a candidate evaluation memo for the hiring manager.
|
| 292 |
+
**Task:** Write a professional, objective, and concise evaluation based on the data below.
|
| 293 |
+
**Tone:** Analytical and formal.
|
| 294 |
+
|
| 295 |
+
**CANDIDATE EVALUATION MEMORANDUM**
|
| 296 |
+
**CONFIDENTIAL**
|
| 297 |
+
|
| 298 |
+
**Candidate ID:** {user_id}
|
| 299 |
+
**Analysis Date:** {time.strftime('%Y-%m-%d')}
|
| 300 |
+
**Estimated Suitability Score:** {prob:.2f}%
|
| 301 |
+
|
| 302 |
+
**1. Overall Recommendation:**
|
| 303 |
+
Provide a clear, one-sentence recommendation (e.g., "Highly recommend proceeding to the final round," "Recommend with reservations," or "Do not recommend at this time."). Briefly justify the recommendation.
|
| 304 |
+
|
| 305 |
+
**2. Communication & Presentation Style:**
|
| 306 |
+
- Evaluate the candidate's communication style based on vocal delivery (confidence, clarity, potential nervousness).
|
| 307 |
+
- **Data for Analysis:** {voice_interpretation}
|
| 308 |
+
|
| 309 |
+
**3. Actionable Next Steps:**
|
| 310 |
+
- Suggest specific questions or topics for the next interviewer to focus on.
|
| 311 |
+
- If not recommending, provide a concise, constructive reason.
|
|
|
|
|
|
|
| 312 |
"""
|
| 313 |
response = gemini_model.generate_content(prompt)
|
| 314 |
return response.text
|
|
|
|
| 316 |
logger.error(f"Report generation failed: {str(e)}")
|
| 317 |
return f"Error generating report: {str(e)}"
|
| 318 |
|
| 319 |
+
# --- NEW: Polished PDF Creation ---
|
| 320 |
def create_pdf_report(analysis_data: Dict, output_path: str, gemini_report_text: str):
|
| 321 |
try:
|
| 322 |
doc = SimpleDocTemplate(output_path, pagesize=letter,
|
| 323 |
+
rightMargin=0.75*inch, leftMargin=0.75*inch,
|
| 324 |
+
topMargin=1.2*inch, bottomMargin=1*inch)
|
| 325 |
styles = getSampleStyleSheet()
|
| 326 |
+
h1 = ParagraphStyle(name='Heading1', fontSize=18, leading=22, spaceAfter=12, alignment=1, textColor=colors.HexColor('#00205B'), fontName='Helvetica-Bold')
|
| 327 |
+
h2 = ParagraphStyle(name='Heading2', fontSize=14, leading=18, spaceBefore=12, spaceAfter=8, textColor=colors.HexColor('#003366'), fontName='Helvetica-Bold')
|
| 328 |
+
body_text = ParagraphStyle(name='BodyText', parent=styles['Normal'], fontSize=10, leading=14, spaceAfter=6, fontName='Helvetica')
|
|
|
|
|
|
|
| 329 |
|
| 330 |
story = []
|
|
|
|
| 331 |
def header_footer(canvas, doc):
|
| 332 |
canvas.saveState()
|
| 333 |
canvas.setFont('Helvetica', 9)
|
| 334 |
+
canvas.setFillColor(colors.grey)
|
| 335 |
+
canvas.drawString(doc.leftMargin, 0.5 * inch, f"Page {doc.page} | EvalBot Confidential Report")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
canvas.restoreState()
|
| 337 |
|
| 338 |
+
# Simple renderer for markdown-like text from Gemini
|
| 339 |
+
# It converts **bold** to <b>bold</b> and newlines to <br/>
|
| 340 |
+
formatted_text = gemini_report_text.replace('\n', '<br/>')
|
| 341 |
+
formatted_text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', formatted_text)
|
| 342 |
+
|
| 343 |
+
lines = formatted_text.split('<br/>')
|
| 344 |
+
for line in lines:
|
| 345 |
+
line = line.strip()
|
| 346 |
+
if not line:
|
| 347 |
+
story.append(Spacer(1, 8))
|
| 348 |
+
continue
|
| 349 |
+
|
| 350 |
+
# Use heading style for lines that look like headings (bolded and short)
|
| 351 |
+
if line.startswith('<b>') and len(line) < 100:
|
| 352 |
+
story.append(Paragraph(line, h2))
|
|
|
|
| 353 |
else:
|
| 354 |
+
story.append(Paragraph(line, body_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
doc.build(story, onFirstPage=header_footer, onLaterPages=header_footer)
|
| 357 |
return True
|
| 358 |
except Exception as e:
|
| 359 |
+
logger.error(f"PDF creation failed: {str(e)}", exc_info=True)
|
| 360 |
return False
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
+
# --- MAIN ORCHESTRATOR FUNCTION ---
|
| 364 |
+
def process_interview(audio_url: str, user_id: str) -> Dict:
|
| 365 |
local_audio_path = None
|
| 366 |
wav_file = None
|
| 367 |
is_downloaded = False
|
| 368 |
try:
|
| 369 |
+
logger.info(f"Starting processing for user '{user_id}' URL: {audio_url}")
|
| 370 |
+
|
| 371 |
+
local_audio_path = download_audio_from_url(audio_url)
|
| 372 |
+
is_downloaded = True
|
| 373 |
+
|
|
|
|
| 374 |
wav_file = convert_to_wav(local_audio_path)
|
| 375 |
transcript = transcribe(wav_file)
|
| 376 |
+
|
| 377 |
+
if 'utterances' not in transcript or not transcript['utterances']:
|
| 378 |
+
raise ValueError("Transcription returned no utterances.")
|
| 379 |
+
|
| 380 |
+
for u in transcript['utterances']:
|
| 381 |
+
u['prosodic_features'] = extract_prosodic_features(wav_file, u['start'], u['end'])
|
| 382 |
+
|
| 383 |
utterances_with_speakers = identify_speakers(transcript, wav_file)
|
| 384 |
|
| 385 |
+
# Using alternating role classification as decided
|
| 386 |
+
for i, u in enumerate(utterances_with_speakers):
|
| 387 |
+
u['role'] = 'Interviewer' if i % 2 == 0 else 'Interviewee'
|
| 388 |
+
classified_utterances = utterances_with_speakers
|
| 389 |
+
|
|
|
|
|
|
|
| 390 |
voice_analysis = analyze_interviewee_voice(wav_file, classified_utterances)
|
| 391 |
+
# We removed the separate content analysis and integrated it into the Gemini prompt
|
| 392 |
+
|
| 393 |
analysis_data = {
|
| 394 |
+
'user_id': user_id,
|
| 395 |
'transcript': classified_utterances,
|
| 396 |
+
'speakers': list(set(u['speaker'] for u in classified_utterances if u['speaker'] != 'Unknown')),
|
| 397 |
'voice_analysis': voice_analysis,
|
| 398 |
'text_analysis': {
|
| 399 |
+
'total_duration': sum(u.get('prosodic_features',{}).get('duration',0) for u in classified_utterances),
|
| 400 |
'speaker_turns': len(classified_utterances)
|
| 401 |
}
|
| 402 |
}
|
| 403 |
+
|
| 404 |
analysis_data['acceptance_probability'] = calculate_acceptance_probability(analysis_data)
|
| 405 |
+
gemini_report_text = generate_report(analysis_data, user_id)
|
| 406 |
+
|
| 407 |
base_name = str(uuid.uuid4())
|
| 408 |
+
# We will now generate only one professional PDF report
|
| 409 |
+
company_pdf_path = os.path.join(OUTPUT_DIR, f"{base_name}_company_report.pdf")
|
| 410 |
json_path = os.path.join(OUTPUT_DIR, f"{base_name}_analysis.json")
|
| 411 |
+
|
| 412 |
+
create_pdf_report(analysis_data, company_pdf_path, gemini_report_text)
|
| 413 |
+
|
| 414 |
with open(json_path, 'w') as f:
|
| 415 |
+
json.dump(convert_to_serializable(analysis_data), f, indent=2)
|
| 416 |
+
|
| 417 |
+
logger.info(f"Processing completed for {audio_url}")
|
| 418 |
+
|
| 419 |
+
return {
|
| 420 |
+
'company_pdf_path': company_pdf_path,
|
| 421 |
+
'json_path': json_path,
|
| 422 |
+
'pdf_filename': os.path.basename(company_pdf_path),
|
| 423 |
+
'json_filename': os.path.basename(json_path)
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
except Exception as e:
|
| 427 |
+
logger.error(f"Processing failed for {audio_url}: {str(e)}", exc_info=True)
|
| 428 |
raise
|
| 429 |
+
|
| 430 |
finally:
|
| 431 |
if wav_file and os.path.exists(wav_file):
|
| 432 |
+
try: os.remove(wav_file)
|
| 433 |
+
except Exception as e: logger.error(f"Failed to clean up wav file {wav_file}: {str(e)}")
|
| 434 |
if is_downloaded and local_audio_path and os.path.exists(local_audio_path):
|
| 435 |
+
try:
|
| 436 |
+
os.remove(local_audio_path)
|
| 437 |
+
logger.info(f"Cleaned up temporary file: {local_audio_path}")
|
| 438 |
+
except Exception as e: logger.error(f"Failed to clean up local audio file {local_audio_path}: {str(e)}")
|