import streamlit as st import json from dotenv import load_dotenv from langchain_core.prompts import ChatPromptTemplate from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.output_parsers import StrOutputParser # ========================================== # Load Environment Variables # ========================================== load_dotenv() # ========================================== # Streamlit Page Config # ========================================== st.set_page_config( page_title="AI Interview Assistant", page_icon="🎯", layout="wide" ) # ========================================== # Custom Styling # ========================================== page_bg = """ """ st.markdown(page_bg, unsafe_allow_html=True) # ========================================== # Title Section # ========================================== st.title("Smart Interview Preparation Assistant") st.subheader("AI-Powered Interview Evaluation System") st.write( "Generate interview questions and receive professional AI feedback instantly." ) # ========================================== # Interview Type # ========================================== role = st.selectbox( "Select Interview Type", ["HR", "Technical", "General"] ) # ========================================== # Gemini Model # ========================================== model = ChatGoogleGenerativeAI( model="gemini-2.5-flash", temperature=0.3 ) parser = StrOutputParser() # ========================================== # Question Prompt # ========================================== question_prompt = ChatPromptTemplate.from_messages([ ( "system", """ You are an expert interviewer. Generate SHORT interview questions only. Rules: - Keep question under 20 words - Beginner friendly - No paragraph questions """ ), ( "human", "Generate one {role} interview question." ) ]) question_chain = question_prompt | model | parser # ========================================== # Generate Question Button # ========================================== if st.button("Generate Interview Question"): try: with st.spinner("Generating question..."): response = question_chain.invoke({ "role": role }) st.session_state.question = response except Exception as e: st.error(f"Error: {e}") # ========================================== # Show Question # ========================================== if "question" in st.session_state: st.subheader("Interview Question") st.success(st.session_state.question) # ========================================== # User Answer # ========================================== answer = st.text_area( "✍️ Write Your Answer", height=200, placeholder="Example: OOP stands for Object-Oriented Programming..." ) # ========================================== # Evaluation Prompt # ========================================== template = """ You are an expert HR interviewer. Evaluate the candidate answer professionally. Return ONLY valid JSON. Expected JSON format: {{ "question": "interview question", "candidate_answer": "user answer", "evaluation": {{ "strengths": [ "strength1", "strength2" ], "weaknesses": [ "weakness1", "weakness2" ] }}, "scores": {{ "technical_knowledge": "8/10", "communication": "7/10", "confidence": "8/10", "overall": "8/10" }}, "final_feedback": "professional feedback" }} """ prompt = ChatPromptTemplate.from_messages([ ("system", template), ("human", """ Question: {question} Answer: {answer} """) ]) evaluation_chain = prompt | model | parser # ========================================== # Evaluate Answer # ========================================== if st.button("Evaluate Answer"): if answer.strip(): with st.spinner("Evaluating answer..."): try: response = evaluation_chain.invoke({ "question": st.session_state.question, "answer": answer }) cleaned_response = ( response .replace("```json", "") .replace("```", "") .strip() ) data = json.loads(cleaned_response) # ========================================== # Display Results # ========================================== st.subheader("Question") st.write(data.get("question", "N/A")) st.subheader("Candidate Answer") st.write(data.get("candidate_answer", "N/A")) # ========================================== # Strengths # ========================================== st.subheader("Strengths") strengths = data.get( "evaluation", {} ).get("strengths", []) for item in strengths: st.write(f"• {item}") # ========================================== # Weaknesses # ========================================== st.subheader("Weaknesses") weaknesses = data.get( "evaluation", {} ).get("weaknesses", []) for item in weaknesses: st.write(f"• {item}") # ========================================== # Scores # ========================================== st.subheader("Performance Scores") scores = data.get("scores", {}) col1, col2 = st.columns(2) with col1: st.metric( "Technical Knowledge", scores.get( "technical_knowledge", "N/A" ) ) st.metric( "Communication", scores.get( "communication", "N/A" ) ) with col2: st.metric( "Confidence", scores.get( "confidence", "N/A" ) ) st.metric( "Overall", scores.get( "overall", "N/A" ) ) # ========================================== # Final Feedback # ========================================== st.subheader("Final Feedback") st.write( data.get( "final_feedback", "N/A" ) ) # ========================================== # Full JSON Output # ========================================== st.subheader("Full JSON Output") st.json(data) except json.JSONDecodeError: st.error("Invalid JSON received from Gemini.") st.code(response) except Exception as e: st.error(f"Error: {e}") else: st.warning("Please enter your answer.") # ========================================== # Footer # ========================================== st.markdown("---") st.write("Built with Streamlit + Gemini AI + LangChain")