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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import time
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
import PyPDF2
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
import speech_recognition as sr
|
| 10 |
+
|
| 11 |
+
# Load API key
|
| 12 |
+
load_dotenv()
|
| 13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 14 |
+
|
| 15 |
+
# Initialize session state
|
| 16 |
+
if "generated_questions" not in st.session_state:
|
| 17 |
+
st.session_state["generated_questions"] = []
|
| 18 |
+
if "current_question_index" not in st.session_state:
|
| 19 |
+
st.session_state["current_question_index"] = 0
|
| 20 |
+
if "answers" not in st.session_state:
|
| 21 |
+
st.session_state["answers"] = []
|
| 22 |
+
if "evaluation_feedback" not in st.session_state:
|
| 23 |
+
st.session_state["evaluation_feedback"] = ""
|
| 24 |
+
if "overall_score" not in st.session_state:
|
| 25 |
+
st.session_state["overall_score"] = 0
|
| 26 |
+
if "percentage_score" not in st.session_state:
|
| 27 |
+
st.session_state["percentage_score"] = 0
|
| 28 |
+
if "is_recording" not in st.session_state:
|
| 29 |
+
st.session_state["is_recording"] = False
|
| 30 |
+
if "question_played" not in st.session_state:
|
| 31 |
+
st.session_state["question_played"] = False
|
| 32 |
+
|
| 33 |
+
# Extract text from PDF
|
| 34 |
+
def extract_pdf_text(uploaded_file):
|
| 35 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 36 |
+
text = ''.join(page.extract_text() or "" for page in pdf_reader.pages)
|
| 37 |
+
return text.strip()
|
| 38 |
+
|
| 39 |
+
# Generate conceptual interview questions
|
| 40 |
+
def get_questions(prompt, input_text, num_questions=3):
|
| 41 |
+
model = genai.GenerativeModel('gemini-1.5-pro-latest')
|
| 42 |
+
response = model.generate_content([prompt, input_text])
|
| 43 |
+
questions = [q.strip("* ") for q in response.text.strip().split("\n") if q.strip() and "question" not in q.lower()]
|
| 44 |
+
return questions[:num_questions]
|
| 45 |
+
|
| 46 |
+
# Evaluate answers automatically
|
| 47 |
+
def evaluate_answers():
|
| 48 |
+
model = genai.GenerativeModel('gemini-1.5-pro-latest')
|
| 49 |
+
|
| 50 |
+
prompt = """
|
| 51 |
+
You are an expert interview evaluator. Assess the responses based on:
|
| 52 |
+
- Conceptual Understanding
|
| 53 |
+
- Communication Skills
|
| 54 |
+
- Clarity & Depth of Explanation
|
| 55 |
+
- Use of Real-World Examples
|
| 56 |
+
- Logical Flow
|
| 57 |
+
|
| 58 |
+
Provide an evaluation summary with a score out of 10.
|
| 59 |
+
|
| 60 |
+
Format:
|
| 61 |
+
**Overall Score:** x/10
|
| 62 |
+
**Evaluation Summary:**
|
| 63 |
+
- Concept Understanding: ...
|
| 64 |
+
- Communication: ...
|
| 65 |
+
- Depth of Explanation: ...
|
| 66 |
+
- Examples: ...
|
| 67 |
+
- Logical Flow: ...
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
candidate_responses = "\n\n".join(
|
| 71 |
+
[f"Q: {entry['question']}\nA: {entry['response']}" for entry in st.session_state["answers"]]
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
full_prompt = f"{prompt}\n\nCandidate Responses:\n{candidate_responses}"
|
| 75 |
+
response = model.generate_content(full_prompt)
|
| 76 |
+
|
| 77 |
+
st.session_state["evaluation_feedback"] = response.text.strip()
|
| 78 |
+
|
| 79 |
+
score_match = re.search(r"\*\*Overall Score:\*\* (\d+)/10", response.text)
|
| 80 |
+
if score_match:
|
| 81 |
+
st.session_state["overall_score"] = int(score_match.group(1))
|
| 82 |
+
st.session_state["percentage_score"] = st.session_state["overall_score"] * 10
|
| 83 |
+
else:
|
| 84 |
+
st.session_state["overall_score"] = 0
|
| 85 |
+
st.session_state["percentage_score"] = 0
|
| 86 |
+
|
| 87 |
+
# Function to convert question to speech
|
| 88 |
+
def speak_question(question):
|
| 89 |
+
if not st.session_state["question_played"]:
|
| 90 |
+
tts = gTTS(text=question, lang="en")
|
| 91 |
+
tts.save("question.mp3")
|
| 92 |
+
st.audio("question.mp3", format="audio/mp3", autoplay=True)
|
| 93 |
+
st.session_state["question_played"] = True
|
| 94 |
+
|
| 95 |
+
# Streamlit UI Enhancements
|
| 96 |
+
st.set_page_config(page_title="🔥🎯 GrillMaster", layout="wide")
|
| 97 |
+
st.sidebar.header("🔥🎯 Imarticus GrillMaster")
|
| 98 |
+
|
| 99 |
+
num_questions = st.sidebar.slider("Number of Questions:", 1, 10, 3)
|
| 100 |
+
difficulty_level = st.sidebar.selectbox("Select Difficulty Level:", ["Beginner", "Intermediate", "Advance"])
|
| 101 |
+
section_choice = st.sidebar.radio("Choose Input Type:", ("Resume", "Job Description", "Skills"))
|
| 102 |
+
|
| 103 |
+
input_text = ""
|
| 104 |
+
if section_choice == "Resume":
|
| 105 |
+
uploaded_file = st.sidebar.file_uploader("Upload Resume:", type=["pdf", "txt"])
|
| 106 |
+
if uploaded_file:
|
| 107 |
+
input_text = extract_pdf_text(uploaded_file)
|
| 108 |
+
elif section_choice == "Job Description":
|
| 109 |
+
input_text = st.sidebar.text_area("Paste Job Description:")
|
| 110 |
+
elif section_choice == "Skills":
|
| 111 |
+
input_text = st.sidebar.selectbox("Select a Skill:", ["Python", "SQL", "Machine Learning", "Statistics", "Business Analytics"])
|
| 112 |
+
|
| 113 |
+
if st.sidebar.button("Generate Questions"):
|
| 114 |
+
prompt =f"""
|
| 115 |
+
You are a trainer conducting a beginner-level mock interview.
|
| 116 |
+
Ask {num_questions} direct and fundamental questions related to {input_text}.
|
| 117 |
+
Keep them concise and focused on key concepts, similar to:
|
| 118 |
+
- Difference between DDL and DML
|
| 119 |
+
- Types of Joins in SQL
|
| 120 |
+
- Primary Key vs Foreign Key
|
| 121 |
+
- Loops in Python
|
| 122 |
+
|
| 123 |
+
Ensure the questions are clear, to the point, and suitable for a {difficulty_level} level interview.
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
st.session_state["generated_questions"] = get_questions(prompt, input_text, num_questions)
|
| 127 |
+
st.session_state["current_question_index"] = 0
|
| 128 |
+
st.session_state["answers"] = []
|
| 129 |
+
st.session_state["evaluation_feedback"] = ""
|
| 130 |
+
st.session_state["question_played"] = False
|
| 131 |
+
st.rerun()
|
| 132 |
+
|
| 133 |
+
# Display Current Question
|
| 134 |
+
if st.session_state["generated_questions"]:
|
| 135 |
+
q_index = st.session_state["current_question_index"]
|
| 136 |
+
|
| 137 |
+
if q_index < len(st.session_state["generated_questions"]):
|
| 138 |
+
question = st.session_state["generated_questions"][q_index]
|
| 139 |
+
|
| 140 |
+
#st.subheader(f"Q{q_index + 1}:")
|
| 141 |
+
st.write(f"**{question}**", unsafe_allow_html=True)
|
| 142 |
+
|
| 143 |
+
speak_question(question)
|
| 144 |
+
|
| 145 |
+
recognizer = sr.Recognizer()
|
| 146 |
+
|
| 147 |
+
if st.button("Start Recording"):
|
| 148 |
+
st.session_state["is_recording"] = True
|
| 149 |
+
with sr.Microphone() as source:
|
| 150 |
+
st.write("🎤 Listening... Speak your answer.")
|
| 151 |
+
try:
|
| 152 |
+
audio = recognizer.listen(source, timeout=60)
|
| 153 |
+
response_text = recognizer.recognize_google(audio)
|
| 154 |
+
st.session_state["answers"].append({"question": question, "response": response_text})
|
| 155 |
+
st.session_state["is_recording"] = False
|
| 156 |
+
except sr.UnknownValueError:
|
| 157 |
+
st.write("Could not understand the audio. Try again.")
|
| 158 |
+
|
| 159 |
+
if st.button("Stop Recording"):
|
| 160 |
+
st.session_state["is_recording"] = False
|
| 161 |
+
|
| 162 |
+
if st.button("➡️ Next Question"):
|
| 163 |
+
st.session_state["current_question_index"] += 1
|
| 164 |
+
st.session_state["question_played"] = False
|
| 165 |
+
st.rerun()
|
| 166 |
+
else:
|
| 167 |
+
evaluate_answers()
|
| 168 |
+
|
| 169 |
+
st.subheader("📊 Complete Mock Interview Summary")
|
| 170 |
+
st.write(f"**Overall Score:** {st.session_state['overall_score']} / 10")
|
| 171 |
+
st.write(f"**Percentage Score:** {st.session_state['percentage_score']:.2f}%")
|
| 172 |
+
st.progress(st.session_state["percentage_score"] / 100)
|
| 173 |
+
st.write(st.session_state["evaluation_feedback"], unsafe_allow_html=True)
|