File size: 5,241 Bytes
8fba82f
1d744ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fba82f
 
1d744ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()