Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,7 @@ import os
|
|
| 4 |
import random
|
| 5 |
import whisper
|
| 6 |
|
| 7 |
-
# ========== Gemini API Setup ==========
|
| 8 |
-
|
| 9 |
API_KEYS = [os.getenv("API_KEY_1"), os.getenv("API_KEY_2")]
|
| 10 |
model = None
|
| 11 |
|
|
@@ -14,7 +13,7 @@ def initialize_model():
|
|
| 14 |
for key in API_KEYS:
|
| 15 |
try:
|
| 16 |
genai.configure(api_key=key)
|
| 17 |
-
model = genai.GenerativeModel("gemini-
|
| 18 |
test = model.generate_content("Hello!")
|
| 19 |
if "Hello" in test.text:
|
| 20 |
print(f"[INFO] Using Gemini API key ending with: ...{key[-4:]}")
|
|
@@ -25,19 +24,10 @@ def initialize_model():
|
|
| 25 |
|
| 26 |
initialize_model()
|
| 27 |
|
| 28 |
-
# ========== Whisper
|
| 29 |
whisper_model = whisper.load_model("base")
|
| 30 |
|
| 31 |
-
# ==========
|
| 32 |
-
session = {
|
| 33 |
-
"questions": [],
|
| 34 |
-
"current_index": 0,
|
| 35 |
-
"scores": [],
|
| 36 |
-
"feedbacks": [],
|
| 37 |
-
"transcripts": []
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
# ========== Interview Questions ==========
|
| 41 |
all_questions = {
|
| 42 |
"general": [
|
| 43 |
"Tell me about yourself.",
|
|
@@ -338,7 +328,27 @@ all_questions = {
|
|
| 338 |
]
|
| 339 |
}
|
| 340 |
|
| 341 |
-
# ==========
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
def get_score(question, answer):
|
| 343 |
prompt = f"""
|
| 344 |
You are an interview expert. A user was asked:
|
|
@@ -364,21 +374,15 @@ Format:
|
|
| 364 |
feedback = line.split("**Feedback:**")[1].strip()
|
| 365 |
return score, feedback
|
| 366 |
|
|
|
|
| 367 |
def summarize_feedback(feedbacks):
|
| 368 |
prompt = "Summarize the following feedback into 4-5 improvement tips:\n" + "\n".join(feedbacks)
|
| 369 |
return model.generate_content(prompt).text.strip()
|
| 370 |
|
| 371 |
-
# ==========
|
| 372 |
-
def handle_audio(audio):
|
| 373 |
-
if not audio:
|
| 374 |
-
return "No audio provided"
|
| 375 |
-
result = whisper_model.transcribe(audio)
|
| 376 |
-
return result["text"]
|
| 377 |
-
|
| 378 |
-
# ========== Interview Logic ==========
|
| 379 |
def run_session(audio):
|
| 380 |
if session["current_index"] >= len(session["questions"]):
|
| 381 |
-
return "✅ Interview complete!", gr.update(visible=False)
|
| 382 |
|
| 383 |
question = session["questions"][session["current_index"]]
|
| 384 |
transcript = handle_audio(audio)
|
|
@@ -391,12 +395,21 @@ def run_session(audio):
|
|
| 391 |
|
| 392 |
if session["current_index"] < len(session["questions"]):
|
| 393 |
next_q = session["questions"][session["current_index"]]
|
| 394 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
else:
|
| 396 |
avg_score = sum(session["scores"]) / len(session["scores"])
|
| 397 |
summary = summarize_feedback(session["feedbacks"])
|
| 398 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
|
|
|
| 400 |
def start_session(field):
|
| 401 |
general_qs = random.sample(all_questions["general"], 10)
|
| 402 |
domain_qs = random.sample(all_questions[field], 10)
|
|
@@ -410,18 +423,23 @@ def start_session(field):
|
|
| 410 |
|
| 411 |
# ========== Gradio UI ==========
|
| 412 |
with gr.Blocks() as demo:
|
| 413 |
-
gr.Markdown("## 🎤 AI Mock Interview
|
| 414 |
-
|
| 415 |
with gr.Row():
|
| 416 |
-
field = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
start_btn = gr.Button("Start Interview")
|
| 418 |
|
| 419 |
question_display = gr.Textbox(label="Current Question", interactive=False)
|
| 420 |
audio_input = gr.Audio(type="filepath", label="🎙️ Record Your Answer")
|
|
|
|
| 421 |
submit_btn = gr.Button("Submit Answer", visible=False)
|
| 422 |
-
output = gr.Textbox(label="Feedback", lines=
|
| 423 |
|
| 424 |
start_btn.click(start_session, inputs=field, outputs=[question_display, submit_btn])
|
| 425 |
-
|
|
|
|
| 426 |
|
| 427 |
demo.launch()
|
|
|
|
| 4 |
import random
|
| 5 |
import whisper
|
| 6 |
|
| 7 |
+
# ========== Gemini API Setup (fallback with 2 keys) ==========
|
|
|
|
| 8 |
API_KEYS = [os.getenv("API_KEY_1"), os.getenv("API_KEY_2")]
|
| 9 |
model = None
|
| 10 |
|
|
|
|
| 13 |
for key in API_KEYS:
|
| 14 |
try:
|
| 15 |
genai.configure(api_key=key)
|
| 16 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 17 |
test = model.generate_content("Hello!")
|
| 18 |
if "Hello" in test.text:
|
| 19 |
print(f"[INFO] Using Gemini API key ending with: ...{key[-4:]}")
|
|
|
|
| 24 |
|
| 25 |
initialize_model()
|
| 26 |
|
| 27 |
+
# ========== Load Whisper ==========
|
| 28 |
whisper_model = whisper.load_model("base")
|
| 29 |
|
| 30 |
+
# ========== Questions ==========
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
all_questions = {
|
| 32 |
"general": [
|
| 33 |
"Tell me about yourself.",
|
|
|
|
| 328 |
]
|
| 329 |
}
|
| 330 |
|
| 331 |
+
# ========== Session State ==========
|
| 332 |
+
session = {
|
| 333 |
+
"questions": [],
|
| 334 |
+
"current_index": 0,
|
| 335 |
+
"scores": [],
|
| 336 |
+
"feedbacks": [],
|
| 337 |
+
"transcripts": []
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
# ========== Handle Audio ==========
|
| 341 |
+
def handle_audio(audio):
|
| 342 |
+
if not audio:
|
| 343 |
+
return "No audio provided"
|
| 344 |
+
result = whisper_model.transcribe(audio)
|
| 345 |
+
return result["text"]
|
| 346 |
+
|
| 347 |
+
# Auto-transcription for transcript box
|
| 348 |
+
def transcribe_and_show(audio):
|
| 349 |
+
return handle_audio(audio)
|
| 350 |
+
|
| 351 |
+
# ========== Evaluate Answer ==========
|
| 352 |
def get_score(question, answer):
|
| 353 |
prompt = f"""
|
| 354 |
You are an interview expert. A user was asked:
|
|
|
|
| 374 |
feedback = line.split("**Feedback:**")[1].strip()
|
| 375 |
return score, feedback
|
| 376 |
|
| 377 |
+
# ========== Summary ==========
|
| 378 |
def summarize_feedback(feedbacks):
|
| 379 |
prompt = "Summarize the following feedback into 4-5 improvement tips:\n" + "\n".join(feedbacks)
|
| 380 |
return model.generate_content(prompt).text.strip()
|
| 381 |
|
| 382 |
+
# ========== Submit Answer ==========
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
def run_session(audio):
|
| 384 |
if session["current_index"] >= len(session["questions"]):
|
| 385 |
+
return "✅ Interview complete!", "", gr.update(visible=False)
|
| 386 |
|
| 387 |
question = session["questions"][session["current_index"]]
|
| 388 |
transcript = handle_audio(audio)
|
|
|
|
| 395 |
|
| 396 |
if session["current_index"] < len(session["questions"]):
|
| 397 |
next_q = session["questions"][session["current_index"]]
|
| 398 |
+
return (
|
| 399 |
+
f"🗣️ Answer: {transcript}\n\n🔢 Score: {score}/10\n💬 Feedback: {feedback}\n\n➡️ Next: {next_q}",
|
| 400 |
+
"",
|
| 401 |
+
gr.update(visible=True),
|
| 402 |
+
)
|
| 403 |
else:
|
| 404 |
avg_score = sum(session["scores"]) / len(session["scores"])
|
| 405 |
summary = summarize_feedback(session["feedbacks"])
|
| 406 |
+
return (
|
| 407 |
+
f"✅ Interview Complete!\n\n📊 Avg Score: {avg_score:.2f}/10\n\n📝 Summary:\n{summary}",
|
| 408 |
+
"",
|
| 409 |
+
gr.update(visible=False),
|
| 410 |
+
)
|
| 411 |
|
| 412 |
+
# ========== Start New Session ==========
|
| 413 |
def start_session(field):
|
| 414 |
general_qs = random.sample(all_questions["general"], 10)
|
| 415 |
domain_qs = random.sample(all_questions[field], 10)
|
|
|
|
| 423 |
|
| 424 |
# ========== Gradio UI ==========
|
| 425 |
with gr.Blocks() as demo:
|
| 426 |
+
gr.Markdown("## 🎤 AI Mock Interview (Voice-Based)")
|
|
|
|
| 427 |
with gr.Row():
|
| 428 |
+
field = gr.Dropdown(
|
| 429 |
+
label="Select your domain",
|
| 430 |
+
choices=list(all_questions.keys()),
|
| 431 |
+
value="ml"
|
| 432 |
+
)
|
| 433 |
start_btn = gr.Button("Start Interview")
|
| 434 |
|
| 435 |
question_display = gr.Textbox(label="Current Question", interactive=False)
|
| 436 |
audio_input = gr.Audio(type="filepath", label="🎙️ Record Your Answer")
|
| 437 |
+
transcript_display = gr.Textbox(label="📝 Transcript", interactive=False)
|
| 438 |
submit_btn = gr.Button("Submit Answer", visible=False)
|
| 439 |
+
output = gr.Textbox(label="Feedback", lines=10)
|
| 440 |
|
| 441 |
start_btn.click(start_session, inputs=field, outputs=[question_display, submit_btn])
|
| 442 |
+
audio_input.change(transcribe_and_show, inputs=audio_input, outputs=transcript_display)
|
| 443 |
+
submit_btn.click(run_session, inputs=audio_input, outputs=[output, transcript_display, submit_btn])
|
| 444 |
|
| 445 |
demo.launch()
|