Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
from google import generativeai as genai
|
| 4 |
+
|
| 5 |
+
# Configure Gemini
|
| 6 |
+
genai.configure(api_key="YOUR_API_KEY_HERE")
|
| 7 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 8 |
+
|
| 9 |
+
# Evaluation logic
|
| 10 |
+
def getscore(question, answer):
|
| 11 |
+
info = f"""
|
| 12 |
+
You are an interview expert but be a little free not toooo strict. A user was asked the following interview question:
|
| 13 |
+
|
| 14 |
+
Question: {question}
|
| 15 |
+
|
| 16 |
+
Their answer was: "{answer}"
|
| 17 |
+
|
| 18 |
+
Please do the following:
|
| 19 |
+
1. Rate the answer from 1 to 10 based on relevance, clarity, and depth.
|
| 20 |
+
2. Provide detailed feedback on how the answer can be improved in a single line.
|
| 21 |
+
Respond in the format:
|
| 22 |
+
1. **Rating:** <score>/10
|
| 23 |
+
2. **Feedback:** <your feedback here>
|
| 24 |
+
"""
|
| 25 |
+
response = model.generate_content(info)
|
| 26 |
+
text = response.text.strip()
|
| 27 |
+
lines = text.split("\n")
|
| 28 |
+
score = -1
|
| 29 |
+
feedback = "Feedback not found"
|
| 30 |
+
for line in lines:
|
| 31 |
+
if "**Rating:**" in line:
|
| 32 |
+
try:
|
| 33 |
+
score = int(line.split("**Rating:**")[1].split("/")[0].strip())
|
| 34 |
+
except:
|
| 35 |
+
score = -1
|
| 36 |
+
elif "**Feedback:**" in line:
|
| 37 |
+
feedback = line.split("**Feedback:**")[1].strip()
|
| 38 |
+
return [score, feedback]
|
| 39 |
+
|
| 40 |
+
def finalsummary(feedback_list):
|
| 41 |
+
prompt = f"""
|
| 42 |
+
You are given a list of feedback. Summarize it and give 5–6 final suggestions for improvement.
|
| 43 |
+
feedback={feedback_list}
|
| 44 |
+
"""
|
| 45 |
+
response = model.generate_content(prompt)
|
| 46 |
+
return response.text
|
| 47 |
+
|
| 48 |
+
# Question bank (shortened example)
|
| 49 |
+
imp_questions_map = {
|
| 50 |
+
"ml": [
|
| 51 |
+
"What is supervised learning? Give an example.",
|
| 52 |
+
"What is the difference between classification and regression?",
|
| 53 |
+
"How does linear regression work?",
|
| 54 |
+
"What are decision trees and how do they work?",
|
| 55 |
+
"What is overfitting vs underfitting?"
|
| 56 |
+
],
|
| 57 |
+
"general": [
|
| 58 |
+
"Tell me about yourself.",
|
| 59 |
+
"What are your strengths and weaknesses?",
|
| 60 |
+
"How do you handle stress or pressure?",
|
| 61 |
+
"Describe a time when you worked in a team.",
|
| 62 |
+
"Why should we hire you?"
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
# App state
|
| 67 |
+
session = {
|
| 68 |
+
"questions": [],
|
| 69 |
+
"current_index": 0,
|
| 70 |
+
"answers": [],
|
| 71 |
+
"scores": [],
|
| 72 |
+
"feedbacks": []
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# Main logic handler
|
| 76 |
+
def start_session(field):
|
| 77 |
+
session["questions"] = random.sample(imp_questions_map.get(field, []), 5) + random.sample(imp_questions_map["general"], 3)
|
| 78 |
+
session["current_index"] = 0
|
| 79 |
+
session["answers"] = []
|
| 80 |
+
session["scores"] = []
|
| 81 |
+
session["feedbacks"] = []
|
| 82 |
+
return session["questions"][0]
|
| 83 |
+
|
| 84 |
+
def submit_answer(audio):
|
| 85 |
+
import whisper
|
| 86 |
+
model = whisper.load_model("base")
|
| 87 |
+
audio_path = audio
|
| 88 |
+
result = model.transcribe(audio_path)
|
| 89 |
+
answer = result["text"]
|
| 90 |
+
|
| 91 |
+
# Evaluate
|
| 92 |
+
question = session["questions"][session["current_index"]]
|
| 93 |
+
score, fb = getscore(question, answer)
|
| 94 |
+
session["answers"].append(answer)
|
| 95 |
+
session["scores"].append(score)
|
| 96 |
+
session["feedbacks"].append(fb)
|
| 97 |
+
|
| 98 |
+
session["current_index"] += 1
|
| 99 |
+
|
| 100 |
+
if session["current_index"] < len(session["questions"]):
|
| 101 |
+
next_q = session["questions"][session["current_index"]]
|
| 102 |
+
return next_q, gr.update(visible=True), gr.update(visible=False), ""
|
| 103 |
+
else:
|
| 104 |
+
# Done - show summary
|
| 105 |
+
total_score = sum(session["scores"]) / len(session["scores"])
|
| 106 |
+
summary = finalsummary(session["feedbacks"])
|
| 107 |
+
return "Interview completed!", gr.update(visible=False), gr.update(visible=True), f"Average Score: {total_score:.2f}/10\n\nSummary:\n{summary}"
|
| 108 |
+
|
| 109 |
+
def show_detailed_feedback():
|
| 110 |
+
detailed = ""
|
| 111 |
+
for i, q in enumerate(session["questions"]):
|
| 112 |
+
detailed += f"**Q{i+1}: {q}**\nAnswer: {session['answers'][i]}\nScore: {session['scores'][i]}/10\nFeedback: {session['feedbacks'][i]}\n\n"
|
| 113 |
+
return detailed
|
| 114 |
+
|
| 115 |
+
# Gradio UI
|
| 116 |
+
with gr.Blocks() as demo:
|
| 117 |
+
gr.Markdown("## AI Mock Interview Evaluator")
|
| 118 |
+
|
| 119 |
+
field_input = gr.Dropdown(
|
| 120 |
+
choices=list(imp_questions_map.keys()),
|
| 121 |
+
label="Select your field"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
start_btn = gr.Button("Start Interview")
|
| 125 |
+
question_output = gr.Textbox(label="Question", interactive=False)
|
| 126 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Record your answer")
|
| 127 |
+
submit_btn = gr.Button("Submit Answer")
|
| 128 |
+
|
| 129 |
+
final_output = gr.Textbox(label="Final Score and Summary", visible=False)
|
| 130 |
+
detailed_btn = gr.Button("Show Detailed Feedback", visible=False)
|
| 131 |
+
detailed_output = gr.Markdown()
|
| 132 |
+
|
| 133 |
+
start_btn.click(start_session, inputs=field_input, outputs=question_output)
|
| 134 |
+
submit_btn.click(submit_answer, inputs=audio_input,
|
| 135 |
+
outputs=[question_output, audio_input, final_output, detailed_output])
|
| 136 |
+
detailed_btn.click(show_detailed_feedback, outputs=detailed_output)
|
| 137 |
+
|
| 138 |
+
demo.launch()
|