-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
371 lines (300 loc) · 11.8 KB
/
app.py
File metadata and controls
371 lines (300 loc) · 11.8 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
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
from flask_session import Session
import os
import json
from datetime import datetime
from werkzeug.utils import secure_filename
import PyPDF2
from config import Config
from database import init_db, save_interview_session, save_question, save_answer, save_coding_test
from ai_processor import AIProcessor
from code_sandbox import CodeSandbox
from report_generator import ReportGenerator
# Initialize Flask app
app = Flask(__name__)
app.config.from_object(Config)
# Initialize session
Session(app)
# Initialize database
init_db()
# Initialize AI processor
ai_processor = AIProcessor()
# Create upload directories
os.makedirs('uploads/resumes', exist_ok=True)
os.makedirs('uploads/job_descriptions', exist_ok=True)
def allowed_file(filename):
"""Check if file extension is allowed"""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in Config.ALLOWED_EXTENSIONS
def extract_text_from_pdf(filepath):
"""Extract text from PDF file"""
try:
with open(filepath, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
except Exception as e:
print(f"Error extracting PDF text: {e}")
return ""
@app.route('/')
def index():
"""Home page"""
return render_template('index.html')
@app.route('/mic-test')
def mic_test():
"""Microphone test page"""
return render_template('mic_test.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload():
"""Upload resume and job description"""
if request.method == 'POST':
# Handle file uploads
resume_file = request.files.get('resume')
jd_file = request.files.get('job_description')
jd_text = request.form.get('job_description_text', '')
resume_text = ""
jd_final_text = ""
# Process resume
if resume_file and allowed_file(resume_file.filename):
filename = secure_filename(resume_file.filename)
filepath = os.path.join('uploads/resumes', filename)
resume_file.save(filepath)
# Extract text from resume
if filename.endswith('.pdf'):
resume_text = extract_text_from_pdf(filepath)
else:
with open(filepath, 'r', encoding='utf-8') as f:
resume_text = f.read()
# Process job description
if jd_file and allowed_file(jd_file.filename):
filename = secure_filename(jd_file.filename)
filepath = os.path.join('uploads/job_descriptions', filename)
jd_file.save(filepath)
if filename.endswith('.pdf'):
jd_final_text = extract_text_from_pdf(filepath)
else:
with open(filepath, 'r', encoding='utf-8') as f:
jd_final_text = f.read()
else:
jd_final_text = jd_text
# Store in session
session['resume_text'] = resume_text
session['job_description'] = jd_final_text
return redirect(url_for('setup_interview'))
return render_template('upload.html')
@app.route('/setup', methods=['GET', 'POST'])
def setup_interview():
"""Set up interview parameters"""
if request.method == 'POST':
domain = request.form.get('domain')
experience_level = request.form.get('experience_level')
# Store in session
session['domain'] = domain
session['experience_level'] = experience_level
return redirect(url_for('start_interview'))
return render_template('setup.html')
@app.route('/start-interview')
def start_interview():
"""Start interview session"""
# Extract resume data
resume_text = session.get('resume_text', '')
resume_data = ai_processor.extract_text_from_resume(resume_text)
# Generate questions
questions = ai_processor.generate_questions(
resume_data=resume_data,
job_description=session.get('job_description', ''),
domain=session.get('domain', 'Software Engineering'),
experience_level=session.get('experience_level', 'Entry'),
count=8
)
# Save session to database
session_data = {
'user_id': 1, # Default user for demo
'domain': session.get('domain'),
'experience_level': session.get('experience_level'),
'resume_text': session.get('resume_text', ''),
'job_description': session.get('job_description', '')
}
session_id = save_interview_session(session_data)
session['session_id'] = session_id
# Save questions
question_ids = []
for q in questions:
q_id = save_question(session_id, q)
question_ids.append(q_id)
q['id'] = q_id
session['questions'] = questions
session['current_question_index'] = 0
session['answers'] = []
return render_template('interview.html',
questions=questions,
current_index=0,
session_id=session_id)
@app.route('/api/next-question', methods=['POST'])
def next_question():
"""Get next question"""
current_index = session.get('current_question_index', 0)
questions = session.get('questions', [])
if current_index >= len(questions):
return jsonify({
'status': 'completed',
'message': 'All questions completed'
})
question = questions[current_index]
# Update session index for next call
session['current_question_index'] = current_index + 1
return jsonify({
'status': 'success',
'question': question,
'current_index': current_index,
'total_questions': len(questions)
})
@app.route('/api/analyze-answer', methods=['POST'])
def analyze_answer():
"""Analyze candidate's answer"""
data = request.json
question_id = data.get('question_id')
answer_text = data.get('answer_text', '')
transcript = data.get('transcript', '')
duration = data.get('duration', 0)
# Get current question
current_index = session.get('current_question_index', 0)
questions = session.get('questions', [])
if current_index < len(questions):
current_question = questions[current_index]
# Analyze answer
analysis = ai_processor.analyze_answer(
question=current_question['question_text'],
answer=answer_text,
transcript=transcript
)
# Save answer
answer_data = {
'question_id': question_id,
'session_id': session.get('session_id'),
'answer_text': answer_text,
'transcript': transcript,
'duration': duration,
'grammar_score': analysis['grammar_score'],
'relevance_score': analysis['relevance_score'],
'confidence_score': analysis['confidence_score'],
'star_score': analysis['star_score'],
'filler_words_count': analysis['filler_words_count'],
'feedback': analysis['feedback'],
'cross_question_asked': analysis['needs_cross_question']
}
answer_id = save_answer(answer_data)
# Store in session
answer_data['id'] = answer_id
answer_data['question_text'] = current_question['question_text']
session['answers'].append(answer_data)
# Update question index
session['current_question_index'] = current_index + 1
response = {
'status': 'success',
'analysis': analysis,
'next_question_available': (current_index + 1) < len(questions)
}
if analysis['needs_cross_question']:
response['cross_question'] = analysis['cross_question']
return jsonify(response)
return jsonify({'status': 'error', 'message': 'No more questions'})
@app.route('/coding-test')
def coding_test():
"""Coding test page"""
# Generate coding problem
domain = session.get('domain', 'Software Engineering')
problem = ai_processor.generate_problem_statement(domain)
session['coding_problem'] = problem
return render_template('coding.html', problem=problem)
@app.route('/api/evaluate-code', methods=['POST'])
def evaluate_code():
"""Evaluate submitted code"""
data = request.json
user_code = data.get('code', '')
time_taken = data.get('time_taken', 0)
problem = session.get('coding_problem', {})
# Basic safety check
if not CodeSandbox.is_code_safe(user_code):
return jsonify({
'status': 'error',
'message': 'Code contains potentially unsafe operations'
})
# Execute code
output, error, success = CodeSandbox.execute_python_code(user_code)
# AI evaluation
evaluation = ai_processor.evaluate_code(
problem_statement=problem.get('problem_statement', ''),
user_code=user_code
)
# Save coding test
test_data = {
'session_id': session.get('session_id'),
'problem_statement': problem.get('problem_statement', ''),
'language': 'python',
'user_code': user_code,
'test_cases_passed': evaluation.get('test_cases_passed', 0),
'total_test_cases': evaluation.get('total_test_cases', 5),
'efficiency_score': evaluation.get('efficiency_score', 0),
'clarity_score': evaluation.get('clarity_score', 0),
'logic_score': evaluation.get('logic_score', 0),
'feedback': evaluation.get('detailed_feedback', ''),
'time_taken': time_taken
}
test_id = save_coding_test(test_data)
session['coding_test'] = test_data
return jsonify({
'status': 'success',
'evaluation': evaluation,
'execution': {
'output': output,
'error': error,
'success': success
}
})
@app.route('/feedback')
def feedback():
"""Show feedback page"""
answers = session.get('answers', [])
# Calculate averages
if answers:
avg_scores = {
'grammar': sum(a.get('grammar_score', 0) for a in answers) / len(answers),
'relevance': sum(a.get('relevance_score', 0) for a in answers) / len(answers),
'confidence': sum(a.get('confidence_score', 0) for a in answers) / len(answers),
'star': sum(a.get('star_score', 0) for a in answers) / len(answers)
}
else:
avg_scores = {'grammar': 0, 'relevance': 0, 'confidence': 0, 'star': 0}
return render_template('feedback.html',
answers=answers,
avg_scores=avg_scores)
@app.route('/generate-report')
def generate_report():
"""Generate and display final report"""
# Get session data
session_data = {
'domain': session.get('domain', 'Software Engineering'),
'experience_level': session.get('experience_level', 'Entry Level')
}
# Get answers data
answers_data = session.get('answers', [])
# Get coding test data
coding_data = session.get('coding_test', {})
# Generate final report using AI
report = ai_processor.generate_final_report(session_data, answers_data, coding_data)
# Store report in session for potential download
session['final_report'] = report
return render_template('report.html',
report=report,
answers=answers_data,
coding_test=coding_data if coding_data else None)
@app.route('/api/speech-status', methods=['POST'])
def speech_status():
"""Update speech recognition status"""
data = request.json
session['speech_active'] = data.get('active', False)
return jsonify({'status': 'success'})
if __name__ == '__main__':
app.run(debug=True, port=5000)