import gradio as gr import requests import os import random # ===================================== # CONFIG # ===================================== HF_TOKEN = os.getenv("HF_TOKEN") WHISPER_API = "https://api-inference.huggingface.co/models/openai/whisper-base" LLM_API = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2" headers = { "Authorization": f"Bearer {HF_TOKEN}" } # ===================================== # 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.", "What is attention mechanism?", "Explain transformer architecture." ] } # ===================================== # Generate Question # ===================================== def start_interview(level): return random.choice(questions[level]) # ===================================== # Speech-to-Text using API # ===================================== def speech_to_text(audio_path): try: with open(audio_path, "rb") as f: audio_bytes = f.read() response = requests.post( WHISPER_API, headers=headers, data=audio_bytes ) if response.status_code == 200: return response.json().get("text", "Could not transcribe audio.") else: return "Speech recognition API error." except Exception as e: return f"Speech recognition failed: {str(e)}" # ===================================== # LLM Evaluation # ===================================== def evaluate_with_llm(question, answer): prompt = f""" You are a strict technical interviewer. Question: {question} Candidate Answer: {answer} Evaluate and give: 1. Technical Accuracy (0-10) 2. Clarity (0-10) 3. Depth (0-10) 4. Overall Score (0-10) 5. Improvement Suggestions Be concise and structured. """ payload = { "inputs": prompt, "parameters": { "max_new_tokens": 300, "temperature": 0.7 } } try: response = requests.post( LLM_API, headers=headers, json=payload ) if response.status_code == 200: return response.json()[0]["generated_text"] else: return "LLM evaluation API error." except Exception as e: return f"Evaluation failed: {str(e)}" # ===================================== # Main Evaluation Function # ===================================== def evaluate_answer(audio, question): if audio is None: return "Please record your answer." if question.strip() == "": return "Please click 'Start Interview' first." transcribed_text = speech_to_text(audio) feedback = evaluate_with_llm(question, transcribed_text) return f""" 📝 Transcribed Answer: {transcribed_text} 📊 Evaluation: {feedback} """ # ===================================== # UI # ===================================== with gr.Blocks() as demo: gr.Markdown("# 🎤 Smart Interview Simulator") gr.Markdown("Voice-Based AI Mock Interview System") 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", lines=15 ) submit_button.click( evaluate_answer, inputs=[audio_input, question_output], outputs=result_output ) demo.launch()