File size: 6,031 Bytes
a58d3f3
 
 
 
db4a556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a58d3f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db4a556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a58d3f3
db4a556
 
 
 
 
 
 
 
 
 
 
 
 
a58d3f3
db4a556
 
 
 
 
 
 
 
 
 
a58d3f3
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from openai import OpenAI
import time

# Predefined examples
SETUP_EXAMPLES = [
    ["AI Engineer", "Mid-level"],
    ["Data Scientist", "Senior"],
    ["ML Engineer", "Entry-level"]
]

PRACTICE_EXAMPLES = [
    [
        # Sample Questions
        """Technical Questions:
1. Can you explain the difference between supervised and unsupervised learning?
2. How do you handle imbalanced datasets in machine learning?
3. Can you describe a project where you used deep learning techniques?
4. How do you evaluate the performance of a machine learning model?
5. Can you explain the concept of overfitting and how to prevent it?

Behavioral Questions:
1. Can you describe a time when you had to work on a project with a tight deadline?
2. Tell me about a time when you faced a technical challenge during a project.
3. Can you give an example of teamwork experience?""",
        
        # Sample Answer
        """### 1. **Supervised vs Unsupervised Learning**
Supervised learning uses labeled data to train models, while unsupervised learning finds patterns in unlabeled data...

### 2. **Handling Imbalanced Data**
Techniques include resampling, class weighting, and using appropriate evaluation metrics...

[Rest of the sample answer...]"""
    ]
]

FEEDBACK_EXAMPLE = [
    """**Feedback on Candidate's Answers**
**1. Supervised vs Unsupervised Learning**
Clarity: Clear explanation with good examples...
[Rest of the sample feedback...]"""
]

def generate_questions(api_key, role, experience):
    if not api_key:
        return "[ERROR] Please enter your OpenRouter API key"
    
    client = OpenAI(
        base_url="https://openrouter.ai/api/v1",
        api_key=api_key,
    )
    
    try:
        completion = client.chat.completions.create(
            extra_headers={
                "HTTP-Referer": "http://localhost:7860",
                "X-Title": "AI Mock Interview",
            },
            model="deepseek/deepseek-v3-base:free",
            messages=[
                {
                    "role": "system",
                    "content": "You are a professional interview coach. Generate relevant interview questions.",
                },
                {
                    "role": "user",
                    "content": f"Act as an interviewer for a {role} position requiring {experience} experience. Generate 5 technical questions and 3 behavioral questions. Present them in a numbered list."
                }
            ]
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"[ERROR] API call failed: {str(e)}"

def get_feedback(api_key, questions, answer):
    if not api_key:
        return "[ERROR] Please enter your OpenRouter API key"
    
    client = OpenAI(
        base_url="https://openrouter.ai/api/v1",
        api_key=api_key,
    )
    
    try:
        completion = client.chat.completions.create(
            extra_headers={
                "HTTP-Referer": "http://localhost:7860",
                "X-Title": "AI Mock Interview",
            },
            model="deepseek/deepseek-v3-base:free",
            messages=[
                {
                    "role": "system",
                    "content": "You are an interview coach analyzing candidate responses.",
                },
                {
                    "role": "user",
                    "content": f"Interview questions:\n{questions}\n\nCandidate answer:\n{answer}\n\nProvide constructive feedback focusing on: clarity, relevance, technical accuracy, and suggestions for improvement. Structure your feedback with clear sections."
                }
            ]
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"[ERROR] API call failed: {str(e)}"

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# AI Mock Interview Practice ๐Ÿค–๐Ÿ’ผ")
    
    with gr.Row():
        api_key = gr.Textbox(
            label="OpenRouter API Key",
            placeholder="Enter your API key here",
            type="password"
        )
    
    with gr.Tab("Interview Setup"):
        with gr.Row():
            with gr.Column():
                role = gr.Textbox(label="Desired Job Role")
                experience = gr.Dropdown(
                    label="Experience Level",
                    choices=["Entry-level", "Mid-level", "Senior", "Executive"],
                    value="Mid-level"
                )
                generate_btn = gr.Button("Generate Questions")
                
                # Setup examples
                gr.Examples(
                    examples=SETUP_EXAMPLES,
                    inputs=[role, experience],
                    label="Click any example to load setup:"
                )

    with gr.Tab("Practice Session"):
        with gr.Row():
            with gr.Column():
                questions = gr.Textbox(label="Generated Questions", lines=10, interactive=False)
                answer = gr.Textbox(label="Your Answer", lines=8, placeholder="Type your response here...")
                feedback_btn = gr.Button("Get Feedback")
                
                # Practice examples
                gr.Examples(
                    examples=PRACTICE_EXAMPLES,
                    inputs=[questions, answer],
                    label="Click to load example Q&A:"
                )

    with gr.Tab("Feedback"):
        with gr.Row():
            feedback = gr.Textbox(label="Expert Feedback", lines=12, interactive=False)
            
            # Feedback example
            gr.Examples(
                examples=FEEDBACK_EXAMPLE,
                inputs=[feedback],
                label="Example Feedback:"
            )

    generate_btn.click(
        fn=generate_questions,
        inputs=[api_key, role, experience],
        outputs=questions
    )
    
    feedback_btn.click(
        fn=get_feedback,
        inputs=[api_key, questions, answer],
        outputs=feedback
    )

if __name__ == "__main__":
    demo.launch()