Update app.py
Browse files
app.py
CHANGED
|
@@ -3,187 +3,66 @@ import requests
|
|
| 3 |
import os
|
| 4 |
import random
|
| 5 |
|
| 6 |
-
# =====================================
|
| 7 |
-
# CONFIG
|
| 8 |
-
# =====================================
|
| 9 |
-
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
-
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
questions = {
|
| 24 |
-
"Easy": [
|
| 25 |
-
"What is Machine Learning?",
|
| 26 |
-
"Explain supervised learning.",
|
| 27 |
-
"What is overfitting?"
|
| 28 |
-
],
|
| 29 |
-
"Medium": [
|
| 30 |
-
"Explain bias vs variance tradeoff.",
|
| 31 |
-
"What is gradient descent?",
|
| 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 |
-
|
| 81 |
-
Question:
|
| 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 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 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 |
-
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
{transcribed_text}
|
| 141 |
|
| 142 |
-
|
| 143 |
-
{feedback}
|
| 144 |
-
"""
|
| 145 |
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
# UI
|
| 149 |
-
# =====================================
|
| 150 |
|
| 151 |
with gr.Blocks() as demo:
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
gr.
|
| 155 |
-
|
| 156 |
-
level_dropdown = gr.Dropdown(
|
| 157 |
-
["Easy", "Medium", "Hard"],
|
| 158 |
-
value="Medium",
|
| 159 |
-
label="Select Difficulty"
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
question_output = gr.Textbox(label="Interview Question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
start_interview,
|
| 167 |
-
inputs=level_dropdown,
|
| 168 |
-
outputs=question_output
|
| 169 |
-
)
|
| 170 |
-
|
| 171 |
-
audio_input = gr.Audio(
|
| 172 |
-
type="filepath",
|
| 173 |
-
label="Record Your Answer"
|
| 174 |
-
)
|
| 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,
|
| 185 |
-
inputs=[audio_input, question_output],
|
| 186 |
-
outputs=result_output
|
| 187 |
-
)
|
| 188 |
|
| 189 |
demo.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
import random
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
+
API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
|
|
|
|
| 9 |
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {HF_TOKEN}"
|
| 12 |
}
|
| 13 |
|
| 14 |
+
def generate_question(role):
|
| 15 |
+
prompt = f"Generate one technical interview question for a {role} role."
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
payload = {
|
| 18 |
"inputs": prompt,
|
| 19 |
+
"parameters": {"max_new_tokens": 80}
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
|
| 22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 23 |
+
|
| 24 |
+
if response.status_code != 200:
|
| 25 |
+
return "Error generating question. Check your HF_TOKEN."
|
| 26 |
+
|
| 27 |
+
result = response.json()
|
| 28 |
+
|
| 29 |
+
if isinstance(result, list):
|
| 30 |
+
return result[0]["generated_text"]
|
| 31 |
+
|
| 32 |
+
return "Could not generate question."
|
| 33 |
+
|
| 34 |
+
def evaluate_answer(answer):
|
| 35 |
+
feedback_prompt = f"Evaluate this interview answer and give short feedback:\nAnswer: {answer}"
|
| 36 |
+
|
| 37 |
+
payload = {
|
| 38 |
+
"inputs": feedback_prompt,
|
| 39 |
+
"parameters": {"max_new_tokens": 120}
|
| 40 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 43 |
|
| 44 |
+
if response.status_code != 200:
|
| 45 |
+
return "Error generating feedback."
|
|
|
|
| 46 |
|
| 47 |
+
result = response.json()
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if isinstance(result, list):
|
| 50 |
+
return result[0]["generated_text"]
|
| 51 |
|
| 52 |
+
return "Could not evaluate answer."
|
|
|
|
|
|
|
| 53 |
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# 🤖 Smart Interview Simulator (AI Voice Bot)")
|
| 56 |
+
|
| 57 |
+
role = gr.Textbox(label="Enter Job Role (e.g., Data Scientist)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
question_output = gr.Textbox(label="Interview Question")
|
| 59 |
+
answer_input = gr.Textbox(label="Your Answer")
|
| 60 |
+
feedback_output = gr.Textbox(label="AI Feedback")
|
| 61 |
+
|
| 62 |
+
generate_btn = gr.Button("Generate Question")
|
| 63 |
+
evaluate_btn = gr.Button("Evaluate Answer")
|
| 64 |
|
| 65 |
+
generate_btn.click(generate_question, inputs=role, outputs=question_output)
|
| 66 |
+
evaluate_btn.click(evaluate_answer, inputs=answer_input, outputs=feedback_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
demo.launch()
|