| import gradio as gr |
| from transformers import pipeline |
| import requests |
| import os |
| import random |
|
|
| |
| |
| |
|
|
| HF_TOKEN = os.getenv("HF_TOKEN") |
| API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2" |
|
|
| headers = { |
| "Authorization": f"Bearer {HF_TOKEN}" |
| } |
|
|
| |
| |
| |
|
|
| asr = pipeline( |
| "automatic-speech-recognition", |
| model="openai/whisper-base" |
| ) |
|
|
| |
| |
| |
|
|
| questions = { |
| "Easy": [ |
| "What is Machine Learning?", |
| "Explain supervised learning.", |
| "What is overfitting?" |
| ], |
| "Medium": [ |
| "Explain bias vs variance tradeoff.", |
| "What is gradient descent?", |
| "Difference between CNN and RNN?" |
| ], |
| "Hard": [ |
| "Explain backpropagation mathematically.", |
| "What is attention mechanism?", |
| "Explain transformers architecture." |
| ] |
| } |
|
|
| |
| |
| |
|
|
| def start_interview(level): |
| return random.choice(questions[level]) |
|
|
| |
| |
| |
|
|
| def query_llm(prompt): |
|
|
| payload = { |
| "inputs": prompt, |
| "parameters": { |
| "max_new_tokens": 300, |
| "temperature": 0.7 |
| } |
| } |
|
|
| response = requests.post(API_URL, headers=headers, json=payload) |
|
|
| if response.status_code == 200: |
| return response.json()[0]["generated_text"] |
| else: |
| return "Error contacting LLM API." |
|
|
| |
| |
| |
|
|
| def evaluate_answer(audio, question): |
|
|
| if audio is None: |
| return "Please record your answer." |
|
|
| |
| result = asr(audio) |
| user_answer = result["text"] |
|
|
| |
| prompt = f""" |
| You are a strict technical interviewer. |
| |
| Question: |
| {question} |
| |
| Candidate Answer: |
| {user_answer} |
| |
| Evaluate and give: |
| |
| 1. Technical Accuracy Score (0-10) |
| 2. Clarity Score (0-10) |
| 3. Depth Score (0-10) |
| 4. Overall Score (0-10) |
| 5. Improvement Suggestions (short and clear) |
| |
| Be concise and structured. |
| """ |
|
|
| feedback = query_llm(prompt) |
|
|
| return f""" |
| π Transcribed Answer: |
| {user_answer} |
| |
| π Evaluation: |
| {feedback} |
| """ |
|
|
| |
| |
| |
|
|
| with gr.Blocks() as demo: |
|
|
| gr.Markdown("# π€ Smart Interview Simulator (AI Voice Bot)") |
| gr.Markdown("Select difficulty β Answer using voice β Get AI feedback") |
|
|
| level_dropdown = gr.Dropdown( |
| ["Easy", "Medium", "Hard"], |
| value="Medium", |
| label="Select Difficulty" |
| ) |
|
|
| question_output = gr.Textbox(label="Interview Question") |
|
|
| start_button = gr.Button("Start Interview") |
| start_button.click(start_interview, inputs=level_dropdown, outputs=question_output) |
|
|
| audio_input = gr.Audio( |
| type="filepath", |
| label="Record Your Answer" |
| ) |
|
|
| submit_button = gr.Button("Submit Answer") |
|
|
| result_output = gr.Textbox(label="Evaluation Feedback") |
|
|
| submit_button.click( |
| evaluate_answer, |
| inputs=[audio_input, question_output], |
| outputs=result_output |
| ) |
|
|
| demo.launch() |
|
|