Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
import random
|
| 6 |
|
| 7 |
-
# ==============================
|
| 8 |
# CONFIG
|
| 9 |
-
# ==============================
|
| 10 |
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
headers = {
|
| 15 |
"Authorization": f"Bearer {HF_TOKEN}"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
# ==============================
|
| 19 |
-
# Load Whisper (Lightweight)
|
| 20 |
-
# ==============================
|
| 21 |
-
|
| 22 |
-
asr = pipeline(
|
| 23 |
-
"automatic-speech-recognition",
|
| 24 |
-
model="openai/whisper-base"
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# ==============================
|
| 28 |
# Question Bank
|
| 29 |
-
# ==============================
|
| 30 |
|
| 31 |
questions = {
|
| 32 |
"Easy": [
|
|
@@ -40,54 +32,49 @@ questions = {
|
|
| 40 |
"Difference between CNN and RNN?"
|
| 41 |
],
|
| 42 |
"Hard": [
|
| 43 |
-
"Explain backpropagation
|
| 44 |
"What is attention mechanism?",
|
| 45 |
-
"Explain
|
| 46 |
]
|
| 47 |
}
|
| 48 |
|
| 49 |
-
# ==============================
|
| 50 |
# Generate Question
|
| 51 |
-
# ==============================
|
| 52 |
|
| 53 |
def start_interview(level):
|
| 54 |
return random.choice(questions[level])
|
| 55 |
|
| 56 |
-
# ==============================
|
| 57 |
-
#
|
| 58 |
-
# ==============================
|
| 59 |
|
| 60 |
-
def
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
return
|
| 74 |
-
else:
|
| 75 |
-
return "Error contacting LLM API."
|
| 76 |
|
| 77 |
-
# ==============================
|
| 78 |
-
# Evaluate Answer
|
| 79 |
-
# ==============================
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
return "Please record your answer."
|
| 85 |
|
| 86 |
-
# Speech to Text
|
| 87 |
-
result = asr(audio)
|
| 88 |
-
user_answer = result["text"]
|
| 89 |
-
|
| 90 |
-
# Prompt Engineering
|
| 91 |
prompt = f"""
|
| 92 |
You are a strict technical interviewer.
|
| 93 |
|
|
@@ -95,37 +82,76 @@ Question:
|
|
| 95 |
{question}
|
| 96 |
|
| 97 |
Candidate Answer:
|
| 98 |
-
{
|
| 99 |
|
| 100 |
Evaluate and give:
|
| 101 |
|
| 102 |
-
1. Technical Accuracy
|
| 103 |
-
2. Clarity
|
| 104 |
-
3. Depth
|
| 105 |
4. Overall Score (0-10)
|
| 106 |
-
5. Improvement Suggestions
|
| 107 |
|
| 108 |
Be concise and structured.
|
| 109 |
"""
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
return f"""
|
| 114 |
📝 Transcribed Answer:
|
| 115 |
-
{
|
| 116 |
|
| 117 |
📊 Evaluation:
|
| 118 |
{feedback}
|
| 119 |
"""
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
# UI
|
| 123 |
-
# ==============================
|
| 124 |
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
|
| 127 |
-
gr.Markdown("# 🎤 Smart Interview Simulator
|
| 128 |
-
gr.Markdown("
|
| 129 |
|
| 130 |
level_dropdown = gr.Dropdown(
|
| 131 |
["Easy", "Medium", "Hard"],
|
|
@@ -136,7 +162,11 @@ with gr.Blocks() as demo:
|
|
| 136 |
question_output = gr.Textbox(label="Interview Question")
|
| 137 |
|
| 138 |
start_button = gr.Button("Start Interview")
|
| 139 |
-
start_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
audio_input = gr.Audio(
|
| 142 |
type="filepath",
|
|
@@ -145,7 +175,10 @@ with gr.Blocks() as demo:
|
|
| 145 |
|
| 146 |
submit_button = gr.Button("Submit Answer")
|
| 147 |
|
| 148 |
-
result_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
submit_button.click(
|
| 151 |
evaluate_answer,
|
|
@@ -153,4 +186,4 @@ with gr.Blocks() as demo:
|
|
| 153 |
outputs=result_output
|
| 154 |
)
|
| 155 |
|
| 156 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import random
|
| 5 |
|
| 6 |
+
# =====================================
|
| 7 |
# CONFIG
|
| 8 |
+
# =====================================
|
| 9 |
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
WHISPER_API = "https://api-inference.huggingface.co/models/openai/whisper-base"
|
| 13 |
+
LLM_API = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 14 |
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {HF_TOKEN}"
|
| 17 |
}
|
| 18 |
|
| 19 |
+
# =====================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Question Bank
|
| 21 |
+
# =====================================
|
| 22 |
|
| 23 |
questions = {
|
| 24 |
"Easy": [
|
|
|
|
| 32 |
"Difference between CNN and RNN?"
|
| 33 |
],
|
| 34 |
"Hard": [
|
| 35 |
+
"Explain backpropagation.",
|
| 36 |
"What is attention mechanism?",
|
| 37 |
+
"Explain transformer architecture."
|
| 38 |
]
|
| 39 |
}
|
| 40 |
|
| 41 |
+
# =====================================
|
| 42 |
# Generate Question
|
| 43 |
+
# =====================================
|
| 44 |
|
| 45 |
def start_interview(level):
|
| 46 |
return random.choice(questions[level])
|
| 47 |
|
| 48 |
+
# =====================================
|
| 49 |
+
# Speech-to-Text using API
|
| 50 |
+
# =====================================
|
| 51 |
|
| 52 |
+
def speech_to_text(audio_path):
|
| 53 |
+
try:
|
| 54 |
+
with open(audio_path, "rb") as f:
|
| 55 |
+
audio_bytes = f.read()
|
| 56 |
|
| 57 |
+
response = requests.post(
|
| 58 |
+
WHISPER_API,
|
| 59 |
+
headers=headers,
|
| 60 |
+
data=audio_bytes
|
| 61 |
+
)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
if response.status_code == 200:
|
| 64 |
+
return response.json().get("text", "Could not transcribe audio.")
|
| 65 |
+
else:
|
| 66 |
+
return "Speech recognition API error."
|
| 67 |
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Speech recognition failed: {str(e)}"
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# =====================================
|
| 73 |
+
# LLM Evaluation
|
| 74 |
+
# =====================================
|
| 75 |
|
| 76 |
+
def evaluate_with_llm(question, answer):
|
|
|
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
prompt = f"""
|
| 79 |
You are a strict technical interviewer.
|
| 80 |
|
|
|
|
| 82 |
{question}
|
| 83 |
|
| 84 |
Candidate Answer:
|
| 85 |
+
{answer}
|
| 86 |
|
| 87 |
Evaluate and give:
|
| 88 |
|
| 89 |
+
1. Technical Accuracy (0-10)
|
| 90 |
+
2. Clarity (0-10)
|
| 91 |
+
3. Depth (0-10)
|
| 92 |
4. Overall Score (0-10)
|
| 93 |
+
5. Improvement Suggestions
|
| 94 |
|
| 95 |
Be concise and structured.
|
| 96 |
"""
|
| 97 |
|
| 98 |
+
payload = {
|
| 99 |
+
"inputs": prompt,
|
| 100 |
+
"parameters": {
|
| 101 |
+
"max_new_tokens": 300,
|
| 102 |
+
"temperature": 0.7
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
response = requests.post(
|
| 108 |
+
LLM_API,
|
| 109 |
+
headers=headers,
|
| 110 |
+
json=payload
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if response.status_code == 200:
|
| 114 |
+
return response.json()[0]["generated_text"]
|
| 115 |
+
else:
|
| 116 |
+
return "LLM evaluation API error."
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"Evaluation failed: {str(e)}"
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# =====================================
|
| 123 |
+
# Main Evaluation Function
|
| 124 |
+
# =====================================
|
| 125 |
+
|
| 126 |
+
def evaluate_answer(audio, question):
|
| 127 |
+
|
| 128 |
+
if audio is None:
|
| 129 |
+
return "Please record your answer."
|
| 130 |
+
|
| 131 |
+
if question.strip() == "":
|
| 132 |
+
return "Please click 'Start Interview' first."
|
| 133 |
+
|
| 134 |
+
transcribed_text = speech_to_text(audio)
|
| 135 |
+
|
| 136 |
+
feedback = evaluate_with_llm(question, transcribed_text)
|
| 137 |
|
| 138 |
return f"""
|
| 139 |
📝 Transcribed Answer:
|
| 140 |
+
{transcribed_text}
|
| 141 |
|
| 142 |
📊 Evaluation:
|
| 143 |
{feedback}
|
| 144 |
"""
|
| 145 |
|
| 146 |
+
|
| 147 |
+
# =====================================
|
| 148 |
# UI
|
| 149 |
+
# =====================================
|
| 150 |
|
| 151 |
with gr.Blocks() as demo:
|
| 152 |
|
| 153 |
+
gr.Markdown("# 🎤 Smart Interview Simulator")
|
| 154 |
+
gr.Markdown("Voice-Based AI Mock Interview System")
|
| 155 |
|
| 156 |
level_dropdown = gr.Dropdown(
|
| 157 |
["Easy", "Medium", "Hard"],
|
|
|
|
| 162 |
question_output = gr.Textbox(label="Interview Question")
|
| 163 |
|
| 164 |
start_button = gr.Button("Start Interview")
|
| 165 |
+
start_button.click(
|
| 166 |
+
start_interview,
|
| 167 |
+
inputs=level_dropdown,
|
| 168 |
+
outputs=question_output
|
| 169 |
+
)
|
| 170 |
|
| 171 |
audio_input = gr.Audio(
|
| 172 |
type="filepath",
|
|
|
|
| 175 |
|
| 176 |
submit_button = gr.Button("Submit Answer")
|
| 177 |
|
| 178 |
+
result_output = gr.Textbox(
|
| 179 |
+
label="Evaluation Feedback",
|
| 180 |
+
lines=15
|
| 181 |
+
)
|
| 182 |
|
| 183 |
submit_button.click(
|
| 184 |
evaluate_answer,
|
|
|
|
| 186 |
outputs=result_output
|
| 187 |
)
|
| 188 |
|
| 189 |
+
demo.launch()
|