import os import gradio as gr import whisper import re import datetime import pandas as pd import google.generativeai as genai # ✅ Configure Gemini API genai.configure(api_key="AIzaSyAeNCjKJVCT0gmQRAPq4NltXkc-1zELH28") model = genai.GenerativeModel("gemini-1.5-flash-latest") asr_model = whisper.load_model("base") # ✅ Global session state session = { "username": "", "company": "", "role": "", "questions": [], "index": 0, "feedback": [], "interview_done": False } # ✅ Dummy public data with full analysis public_data = pd.DataFrame([ {"Username": "Flipkart employee", "Company": "amazon", "Role": "services", "Date": "2025-05-27", "Tone (%)": 60, "Vocabulary (%)": 50, "Grammar (%)": 40, "Technical (%)": 30}, {"Username": "NIELT", "Company": "Dmart", "Role": "Marketing and Sales Funnel", "Date": "2025-05-22", "Tone (%)": 74, "Vocabulary (%)": 70, "Grammar (%)": 76, "Technical (%)": 69}, {"Username": "AIML Saddi", "Company": "Nykaa", "Role": "Product Manager", "Date": "2025-05-27", "Tone (%)": 86, "Vocabulary (%)": 79, "Grammar (%)": 81, "Technical (%)": 88}, {"Username": "AIML Deepanshu", "Company": "Unilever", "Role": "Brand Manager", "Date": "2025-05-27", "Tone (%)": 80, "Vocabulary (%)": 78, "Grammar (%)": 83, "Technical (%)": 82}, {"Username": "Rahul Ropar", "Company": "Godrej", "Role": "B2B Sales", "Date": "2025-05-23", "Tone (%)": 72, "Vocabulary (%)": 75, "Grammar (%)": 69, "Technical (%)": 65}, {"Username": "Vidit789", "Company": "Amazon", "Role": "Cloud Security Analyst", "Date": "2025-05-23", "Tone (%)": 90, "Vocabulary (%)": 82, "Grammar (%)": 87, "Technical (%)": 91}, {"Username": "DSP Dev", "Company": "Tesla", "Role": "Autopilot QA", "Date": "2025-05-22", "Tone (%)": 84, "Vocabulary (%)": 77, "Grammar (%)": 81, "Technical (%)": 79} ]) # ✅ Start interview def start_interview(username, company, role): if not username or not company or not role: return "❌ Please fill all fields first." session.update({ "username": username, "company": company, "role": role, "questions": [], "index": 0, "feedback": [], "interview_done": False }) greeting = ( "Hello, I am Mrinankush Dutta.\n\n" "Welcome to your AI Mock interview session.\n\n" "This Mock interview provides tailored support for interviewees in real time.\n\n" "Just focus on being yourself. We will handle the rest." ) return greeting # ✅ Generate interview questions def generate_questions(): if not session["company"] or not session["role"]: return "❌ Please complete setup first." prompt = f"Generate 13 internship interview questions (8 technical and 5 behavioral) for the role of {session['role']} at {session['company']}. Number them 1 to 13." response = model.generate_content(prompt) session["questions"] = re.findall(r"^\d+\.\s+.*", response.text.strip(), re.MULTILINE) return next_question() # ✅ Show next question def next_question(): if session["index"] >= len(session["questions"]): session["interview_done"] = True return "Interview complete. Click 'Finish' to view feedback." return session["questions"][session["index"]] # ✅ Record and process answer def process_answer(audio): if audio is None: return "❌ Please record your answer." result = asr_model.transcribe(audio) transcript = result["text"] q = session["questions"][session["index"]] session["index"] += 1 prompt = ( f"Interview Question: {q}\n" f"Answer: {transcript}\n\n" "Evaluate the following in percentage:\n" "Tone, Grammar, Vocabulary, and Technical correctness.\n" "Respond in this format:\n" "Tone: %\nGrammar: %\nVocabulary: %\nTechnical: %" ) feedback = model.generate_content(prompt).text.strip() session["feedback"].append({"question": q, "answer": transcript, "feedback": feedback}) return "✅ Answer recorded." # ✅ Compile full feedback summary def feedback_summary(): session["interview_done"] = True summary = "" for i, entry in enumerate(session["feedback"], 1): summary += f"Q{i}: {entry['question']}\nAnswer: {entry['answer']}\n{entry['feedback']}\n{'-'*40}\n" return summary # ✅ Public sharing after confirmation def make_public(share_decision): if not session["interview_done"]: return "❌ Complete interview first." if share_decision.lower() != "yes": return "✅ Interview ended. Your data was not shared publicly." last_feedback = session["feedback"][-1]["feedback"] tone = re.search(r"Tone:\s*(\d+)%", last_feedback) grammar = re.search(r"Grammar:\s*(\d+)%", last_feedback) vocab = re.search(r"Vocabulary:\s*(\d+)%", last_feedback) tech = re.search(r"Technical:\s*(\d+)%", last_feedback) today = datetime.date.today().strftime("%Y-%m-%d") new_row = { "Username": session["username"], "Company": session["company"], "Role": session["role"], "Date": today, "Tone (%)": int(tone.group(1)) if tone else 70, "Vocabulary (%)": int(vocab.group(1)) if vocab else 70, "Grammar (%)": int(grammar.group(1)) if grammar else 70, "Technical (%)": int(tech.group(1)) if tech else 70 } global public_data public_data.loc[len(public_data)] = new_row return "✅ Your analysis has been shared publicly." # ✅ View public profiles as DataFrame def show_public(): if not session["interview_done"]: return public_data.iloc[0:0] # Empty table if not finished return public_data # ✅ Gradio UI with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# 🧠 AI MOCK INTERVIEW BUDDY") uname = gr.Textbox(label="👤 Unique Username") comp = gr.Textbox(label="🏢 Company") role = gr.Textbox(label="🎯 Role") start_btn = gr.Button("🚀 Start Interview") greet = gr.Textbox(label="Greeting") start_btn.click(start_interview, inputs=[uname, comp, role], outputs=greet) gen_btn = gr.Button("📋 Generate Questions") qbox = gr.Textbox(label="Current Question") gen_btn.click(generate_questions, outputs=qbox) record = gr.Audio(sources=["microphone"], type="filepath", label="🎤 Record Answer") submit = gr.Button("✅ Submit") result = gr.Textbox(label="Status") submit.click(process_answer, inputs=record, outputs=result) next_btn = gr.Button("➡ Next Question") next_btn.click(next_question, outputs=qbox) final = gr.Button("📊 End Interview") fb = gr.Textbox(label="📄 Final Feedback", lines=15) final.click(feedback_summary, outputs=fb) confirm = gr.Textbox(label="Do you want to share publicly? (Yes/No)") share_btn = gr.Button("☑ Share Final Results") status = gr.Textbox(label="Sharing Status") share_btn.click(make_public, inputs=confirm, outputs=status) view = gr.Button("📣 View Public Profiles") public_table = gr.Dataframe(label="🧠 Public Summary Table", interactive=False) view.click(show_public, outputs=public_table) # ✅ Fixed launch block if __name__ == "__main__": demo.launch()