shukdevdatta123's picture
Update app.py
2fb67bf verified
raw
history blame
3.76 kB
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()