File size: 3,762 Bytes
a58d3f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fb67bf
 
 
 
 
 
 
 
a58d3f3
2fb67bf
 
 
 
a58d3f3
2fb67bf
 
 
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
import gradio as gr
from openai import OpenAI
import time

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"):
        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 Interview Questions")
        
    with gr.Tab("Practice Session"):
        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")
        
    with gr.Tab("Feedback"):
        feedback = gr.Textbox(label="Expert Feedback", lines=12, interactive=False)
    
    # Event handlers
    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()