import gradio as gr import requests import os import re # We use Mistral because DialoGPT is for chatting, while Mistral follows instructions API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" HF_TOKEN = os.getenv("HF_TOKEN") headers = {"Authorization": f"Bearer {HF_TOKEN}"} def query_llm(prompt): """Helper function to send prompts to the Hugging Face API.""" payload = { "inputs": f"[INST] {prompt} [/INST]", "parameters": {"max_new_tokens": 250, "temperature": 0.7} } try: response = requests.post(API_URL, headers=headers, json=payload) result = response.json() if isinstance(result, list) and "generated_text" in result[0]: # Clean the output to remove the prompt markers full_text = result[0]["generated_text"] return full_text.split("[/INST]")[-1].strip() return "The AI is still warming up. Please wait 30 seconds and try again." except Exception as e: return f"Error: Could not connect to AI (Check your HF_TOKEN). {str(e)}" # ---------------------------- # AI Logic Functions # ---------------------------- def generate_question(role, difficulty, resume_text): if not role: return "Please enter a Job Role first!" prompt = f"Act as a professional recruiter. Generate one {difficulty} level technical interview question for a {role} role." if resume_text.strip(): prompt += f" Based on this resume context: {resume_text}" return query_llm(prompt) def evaluate_answer(question, answer): if not answer or len(answer) < 5: return "Please provide a more detailed answer for evaluation.", "N/A" prompt = f""" Interviewer Question: {question} Candidate Answer: {answer} Task: Critically evaluate this answer. 1. Give constructive feedback. 2. Provide a score out of 10. Format your response with the score clearly at the end as 'Final Score: X/10'. """ feedback = query_llm(prompt) # Extract score using regex (looks for X/10) score_match = re.search(r"(\d+/10)", feedback) score = score_match.group(1) if score_match else "Score not generated" return feedback, score # ---------------------------- # Gradio UI Design # ---------------------------- with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# 🤖 Smart Interview Simulator") gr.Markdown("Practice your interview skills with AI-generated questions and real-time feedback.") with gr.Row(): with gr.Column(): role_input = gr.Textbox(label="Target Job Role", placeholder="e.g., Python Developer") diff_input = gr.Dropdown(["Easy", "Medium", "Hard"], label="Level", value="Medium") resume_input = gr.Textbox(label="Resume Summary (Optional)", lines=3) gen_btn = gr.Button("Generate Interview Question", variant="primary") with gr.Column(): question_box = gr.Textbox(label="AI Question", lines=5, interactive=False) gr.Markdown("---") with gr.Row(): with gr.Column(): gr.Markdown("### Your Response") # Note: For voice input to work, Gradio handles the file, # but you would need a transcription model (like Whisper) to convert audio to text. # For now, we will focus on the text input. ans_input = gr.Textbox(label="Type your answer here", lines=5) eval_btn = gr.Button("Submit for Evaluation", variant="secondary") with gr.Column(): gr.Markdown("### Results") feedback_box = gr.Textbox(label="AI Feedback", lines=5) score_box = gr.Label(label="Final Score") # Button actions gen_btn.click( fn=generate_question, inputs=[role_input, diff_input, resume_input], outputs=question_box ) eval_btn.click( fn=evaluate_answer, inputs=[question_box, ans_input], outputs=[feedback_box, score_box] ) if __name__ == "__main__": demo.launch()