-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathai_processor.py
More file actions
379 lines (318 loc) · 15.1 KB
/
ai_processor.py
File metadata and controls
379 lines (318 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import google.generativeai as genai
import json
import re
from textblob import TextBlob
import nltk
from nltk.tokenize import word_tokenize
from config import Config
# Download NLTK data
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
class AIProcessor:
def __init__(self):
if not Config.GEMINI_API_KEY:
raise ValueError("GEMINI_API_KEY not set in environment variables")
genai.configure(api_key=Config.GEMINI_API_KEY)
self.model = genai.GenerativeModel(Config.GEMINI_MODEL)
def extract_text_from_resume(self, resume_text):
"""Extract key information from resume text"""
prompt = f"""
Extract the following information from this resume text:
{resume_text[:2000]}
Provide as JSON with these keys:
- name: person's name (if available)
- skills: list of technical skills
- experience_years: total years of experience (as float)
- education: list of educational qualifications
- projects: list of key projects
- certifications: list of certifications
Return only JSON, no additional text.
"""
try:
response = self.model.generate_content(prompt)
response_text = response.text.strip()
# Extract JSON from response
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
if json_match:
return json.loads(json_match.group())
return {}
except Exception as e:
print(f"Error extracting resume text: {e}")
return {}
def generate_questions(self, resume_data, job_description, domain, experience_level, count=10):
"""Generate interview questions based on resume and JD"""
prompt = f"""
You are an expert technical interviewer. Generate {count} interview questions for a {experience_level} level {domain} position.
Resume Information:
{json.dumps(resume_data, indent=2)}
Job Description:
{job_description[:1000]}
Generate a mix of questions:
1. 3-4 Technical questions specific to {domain}
2. 2-3 Behavioral questions (use STAR method)
3. 2-3 Situational/Scenario-based questions
4. 1-2 Advanced/Problem-solving questions
For each question, provide:
- question_text: The actual question
- question_type: "technical", "behavioral", "situational", or "advanced"
- difficulty: "easy", "medium", or "hard"
- category: e.g., "Python", "System Design", "Teamwork"
- time_allocated: Time in seconds (120 for easy, 180 for medium, 240 for hard)
Return as a JSON list of questions.
"""
try:
response = self.model.generate_content(prompt)
response_text = response.text.strip()
# Extract JSON from response
json_match = re.search(r'\[.*\]', response_text, re.DOTALL)
if json_match:
questions = json.loads(json_match.group())
return questions[:count]
# Fallback to some default questions
return self._get_default_questions(domain, experience_level, count)
except Exception as e:
print(f"Error generating questions: {e}")
return self._get_default_questions(domain, experience_level, count)
def _get_default_questions(self, domain, experience_level, count):
"""Provide default questions if AI fails"""
default_questions = [
{
"question_text": f"Tell me about your experience with {domain}.",
"question_type": "behavioral",
"difficulty": "easy",
"category": domain,
"time_allocated": 120
},
{
"question_text": "Describe a challenging project you worked on and how you overcame obstacles.",
"question_type": "behavioral",
"difficulty": "medium",
"category": "Project Management",
"time_allocated": 180
},
{
"question_text": "What are your strengths and weaknesses?",
"question_type": "behavioral",
"difficulty": "easy",
"category": "Self Assessment",
"time_allocated": 120
}
]
return default_questions[:count]
def analyze_answer(self, question, answer, transcript):
"""Analyze candidate's answer"""
filler_words = ['um', 'uh', 'ah', 'er', 'like', 'you know', 'so', 'well']
filler_count = sum(transcript.lower().count(word) for word in filler_words)
# Calculate sentiment using TextBlob
blob = TextBlob(transcript)
sentiment_score = blob.sentiment.polarity # -1 to 1
# Generate AI feedback
prompt = f"""
Analyze this interview answer and provide personalized feedback:
Question: {question}
Candidate's Answer: {answer}
First, analyze the candidate's actual answer and identify:
1. What they did well in their response
2. What specific aspects could be improved
3. What key points or examples they mentioned
Then provide detailed analysis as JSON with these keys:
- grammar_score: 0-10 score for grammar and sentence structure
- relevance_score: 0-10 score for relevance to question
- star_score: 0-10 score for STAR method usage (Situation, Task, Action, Result)
- detailed_feedback: Specific, actionable feedback based on their actual answer
- suggested_better_answer: A personalized improvement of their answer that builds on what they said, not a generic response. Make it specific to their content and context.
- confidence_indicator: "low", "medium", or "high" based on answer quality
For the suggested_better_answer, DO NOT provide a generic template. Instead:
- Take their actual answer as a starting point
- Improve their structure using STAR method
- Add relevant details they might have missed
- Keep their core message but make it more professional and complete
- Make it sound natural, not robotic
Also evaluate if the candidate needs a cross-question because:
1. Answer is too short (< 30 words)
2. Answer is vague or unclear
3. Answer shows lack of depth
If cross-question is needed, provide:
- needs_cross_question: true
- cross_question: A follow-up question to probe deeper
Return only JSON, no additional text.
"""
try:
response = self.model.generate_content(prompt)
response_text = response.text.strip()
# Extract JSON from response
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
if json_match:
analysis = json.loads(json_match.group())
# Calculate confidence score (0-10)
confidence_score = (
analysis.get('relevance_score', 5) * 0.3 +
analysis.get('star_score', 5) * 0.3 +
(1 + sentiment_score) * 5 * 0.2 + # Convert -1 to 1 into 0-10
max(0, 10 - (filler_count * 0.5)) * 0.2 # Penalize filler words
)
return {
'grammar_score': analysis.get('grammar_score', 5),
'relevance_score': analysis.get('relevance_score', 5),
'star_score': analysis.get('star_score', 5),
'confidence_score': min(10, max(0, confidence_score)),
'filler_words_count': filler_count,
'feedback': analysis.get('detailed_feedback', 'No specific feedback available.'),
'suggested_answer': analysis.get('suggested_better_answer', ''),
'needs_cross_question': analysis.get('needs_cross_question', False),
'cross_question': analysis.get('cross_question', '') if analysis.get('needs_cross_question') else ''
}
except Exception as e:
print(f"Error analyzing answer: {e}")
# Fallback analysis
return {
'grammar_score': 6,
'relevance_score': 6,
'star_score': 5,
'confidence_score': 6,
'filler_words_count': filler_count,
'feedback': 'Basic analysis only. AI service unavailable.',
'suggested_answer': 'Try to provide more specific examples and structure your answer using the STAR method.',
'needs_cross_question': len(answer.split()) < 30,
'cross_question': 'Could you elaborate more on that point?' if len(answer.split()) < 30 else ''
}
def generate_cross_question(self, question, answer):
"""Generate a cross-question when answer is insufficient"""
prompt = f"""
Based on this question and insufficient answer, generate a probing follow-up question:
Original Question: {question}
Candidate's Answer: {answer}
The answer was too short/vague. Generate ONE follow-up question that will:
1. Probe deeper into the topic
2. Ask for specific examples
3. Challenge the candidate constructively
Return only the question text.
"""
try:
response = self.model.generate_content(prompt)
return response.text.strip()
except:
return "Could you provide a more detailed example or elaborate on that point?"
def evaluate_code(self, problem_statement, user_code, language='python'):
"""Evaluate submitted code"""
prompt = f"""
Evaluate this coding solution:
Problem: {problem_statement}
Language: {language}
Code:
{user_code}
Provide evaluation as JSON with:
- logic_score: 0-10 for logical correctness
- efficiency_score: 0-10 for time/space efficiency
- clarity_score: 0-10 for code readability and structure
- test_cases_passed: estimated test cases passed (0-5)
- total_test_cases: 5 (assumed)
- detailed_feedback: Specific feedback on improvements
- suggested_improvements: How to improve the code
- time_complexity: Estimated time complexity
- space_complexity: Estimated space complexity
Return only JSON.
"""
try:
response = self.model.generate_content(prompt)
response_text = response.text.strip()
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
if json_match:
evaluation = json.loads(json_match.group())
return evaluation
except Exception as e:
print(f"Error evaluating code: {e}")
# Fallback evaluation
return {
'logic_score': 6,
'efficiency_score': 6,
'clarity_score': 6,
'test_cases_passed': 3,
'total_test_cases': 5,
'detailed_feedback': 'Basic evaluation only. AI service unavailable.',
'suggested_improvements': 'Add more comments and handle edge cases.',
'time_complexity': 'O(n)',
'space_complexity': 'O(1)'
}
def generate_problem_statement(self, domain, difficulty='medium'):
"""Generate a coding problem statement"""
prompt = f"""
Generate a {difficulty} level coding problem for {domain} domain.
The problem should be solvable in 10-15 minutes and test:
1. Basic programming logic
2. Problem-solving approach
3. Clean code practices
Provide as JSON with:
- problem_statement: Clear description of the problem
- example_input: Example input
- example_output: Expected output for example
- constraints: Any constraints (time/space)
- hints: 1-2 hints for solving
Return only JSON.
"""
try:
response = self.model.generate_content(prompt)
response_text = response.text.strip()
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
if json_match:
return json.loads(json_match.group())
except:
pass
# Default problem
return {
'problem_statement': 'Write a function to find the maximum element in a list.',
'example_input': '[1, 5, 3, 9, 2]',
'example_output': '9',
'constraints': 'Time complexity should be O(n)',
'hints': ['Iterate through the list while keeping track of maximum']
}
def generate_final_report(self, session_data, answers_data, coding_data):
"""Generate final performance report"""
prompt = f"""
Generate a comprehensive interview performance report.
Interview Session Details:
- Domain: {session_data.get('domain')}
- Experience Level: {session_data.get('experience_level')}
Performance Analysis:
{json.dumps(answers_data, indent=2)}
Coding Test Results:
{json.dumps(coding_data, indent=2) if coding_data else 'No coding test'}
Provide a detailed report as JSON with:
- overall_score: 0-100 overall performance
- strengths: list of 3-5 strengths
- weaknesses: list of 3-5 areas to improve
- communication_score: 0-10 for communication skills
- technical_score: 0-10 for technical knowledge
- confidence_score: 0-10 for confidence level
- improvement_plan: 5-7 specific actionable recommendations
- final_verdict: "Strong Candidate", "Needs Improvement", or "Not Ready"
- detailed_analysis: Paragraph summarizing performance
Return only JSON.
"""
try:
response = self.model.generate_content(prompt)
response_text = response.text.strip()
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
if json_match:
return json.loads(json_match.group())
except Exception as e:
print(f"Error generating report: {e}")
# Fallback report
return {
'overall_score': 70,
'strengths': ['Basic technical knowledge', 'Clear communication'],
'weaknesses': ['Need more examples', 'Improve STAR method usage'],
'communication_score': 7,
'technical_score': 6,
'confidence_score': 6,
'improvement_plan': [
'Practice more behavioral questions',
'Use STAR method consistently',
'Reduce filler words',
'Prepare specific examples'
],
'final_verdict': 'Needs Improvement',
'detailed_analysis': 'Basic performance with room for improvement.'
}