import gradio as gr from transformers import pipeline import requests import os import random # ============================== # CONFIG # ============================== 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}" } # ============================== # Load Whisper (Lightweight) # ============================== asr = pipeline( "automatic-speech-recognition", model="openai/whisper-base" ) # ============================== # Question Bank # ============================== 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." ] } # ============================== # Generate Question # ============================== def start_interview(level): return random.choice(questions[level]) # ============================== # Call LLM via API # ============================== 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." # ============================== # Evaluate Answer # ============================== def evaluate_answer(audio, question): if audio is None: return "Please record your answer." # Speech to Text result = asr(audio) user_answer = result["text"] # Prompt Engineering 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} """ # ============================== # UI # ============================== 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()