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