AI_Interviewer / app.py
TKM03's picture
Update app.py
1d744ac verified
import gradio as gr
import json
import os
from datetime import datetime
from transformers import pipeline
# Initialize speech recognition pipeline
transcriber = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
# Predefined interview questions (can be customized)
default_questions = [
"Tell me about yourself and your background.",
"Why are you interested in this position?",
"What are your key strengths and weaknesses?",
"Describe a challenging situation at work and how you handled it.",
"Where do you see yourself in five years?",
"Why should we hire you for this position?",
"Do you have any questions for us?"
]
def save_responses(candidate_name, responses):
"""Save candidate responses to a JSON file"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{candidate_name.replace(' ', '_')}_{timestamp}.json"
with open(filename, "w") as f:
json.dump(responses, f, indent=4)
return filename
class InterviewSession:
def __init__(self):
self.candidate_name = ""
self.position = ""
self.responses = {}
self.current_question_idx = 0
self.questions = default_questions.copy()
def start_interview(self, candidate_name, position, custom_questions=None):
"""Initialize a new interview session"""
self.candidate_name = candidate_name
self.position = position
self.responses = {
"candidate_name": candidate_name,
"position": position,
"interview_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"answers": {}
}
self.current_question_idx = 0
if custom_questions and custom_questions.strip():
self.questions = custom_questions.strip().split('\n')
else:
self.questions = default_questions.copy()
return self.get_current_question()
def get_current_question(self):
"""Get the current question text"""
if 0 <= self.current_question_idx < len(self.questions):
return self.questions[self.current_question_idx]
return "Interview complete! Click 'Save Interview' to finish."
def process_answer(self, audio_path):
"""Process and save the candidate's answer"""
if self.current_question_idx >= len(self.questions):
return "Interview already completed", None
# Transcribe audio
result = transcriber(audio_path)
transcription = result["text"]
# Save response
question = self.questions[self.current_question_idx]
self.responses["answers"][question] = transcription
# Move to next question
self.current_question_idx += 1
return transcription, self.get_current_question()
def finish_interview(self):
"""Finish the interview and save results"""
if not self.candidate_name:
return "No interview in progress", None
filename = save_responses(self.candidate_name, self.responses)
result = f"Interview saved to {filename}"
# Reset for next interview
self.candidate_name = ""
self.position = ""
self.responses = {}
self.current_question_idx = 0
return result, filename
# Initialize the interview session
session = InterviewSession()
# Create the Gradio interface
with gr.Blocks(title="AI Interviewer") as app:
gr.Markdown("# AI Interviewer")
gr.Markdown("This application conducts interviews, transcribes responses, and saves them for HR review.")
with gr.Tab("Setup Interview"):
candidate_name = gr.Textbox(label="Candidate Name")
position = gr.Textbox(label="Position Applied For")
custom_questions = gr.Textbox(
label="Custom Questions (one per line, leave empty for default questions)",
placeholder="Enter custom questions here, one per line...",
lines=5
)
start_btn = gr.Button("Start Interview")
setup_output = gr.Textbox(label="Status")
start_btn.click(
session.start_interview,
inputs=[candidate_name, position, custom_questions],
outputs=setup_output
)
with gr.Tab("Conduct Interview"):
current_question = gr.Textbox(label="Current Question")
audio_input = gr.Audio(source="microphone", type="filepath", label="Record Your Answer")
transcription = gr.Textbox(label="Transcribed Answer")
next_question = gr.Textbox(label="Next Question")
audio_input.change(
session.process_answer,
inputs=audio_input,
outputs=[transcription, next_question]
)
with gr.Tab("Finish Interview"):
finish_btn = gr.Button("Save Interview")
finish_output = gr.Textbox(label="Result")
download_link = gr.File(label="Download Interview Responses")
finish_btn.click(
session.finish_interview,
inputs=[],
outputs=[finish_output, download_link]
)
# Launch the app
app.launch()