File size: 9,763 Bytes
265d22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cabcf6
265d22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cabcf6
265d22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cabcf6
265d22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3e7f5
 
265d22e
 
 
 
 
 
 
 
0d3e7f5
 
 
 
265d22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3e7f5
 
265d22e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import re
import time
import asyncio
import tempfile
import streamlit as st
import google.generativeai as genai
import edge_tts
import speech_recognition as sr
from dotenv import load_dotenv
import pandas as pd

# βœ… Streamlit page config
st.set_page_config(page_title="GrillMaster", layout="wide")

# Load API key
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

# -----------------------------
# SESSION STATE DEFAULTS
# -----------------------------
defaults = {
    "generated_questions": [],
    "current_question_index": 0,
    "answers": [],
    "evaluations": [],
    "evaluation_feedback": "",
    "overall_score": 0,
    "percentage_score": 0,
    "is_recording": False,
    "question_played": False,
    "selected_domain": "",
    "response_captured": False,
    "timer_start": None,
    "show_intro": False,
    "recorded_text": "",
    "recording_complete": False,
    "recording_started": False,
    "audio_played": False,
    "question_start_time": 0.0,
    "record_phase": "",
    "improvement_suggestions_generated": False,
    "improvement_suggestions": ""
}
for key, value in defaults.items():
    if key not in st.session_state:
        st.session_state[key] = value

# -----------------------------
# QUESTIONS
# -----------------------------
CANDIDATE_QUESTIONS = [
    {"text": "Can you introduce yourself?", "type": "introduction"},
    {"text": "Why do you want to be a part of Analytics domain?", "type": "introduction"},
    {"text": "Can you try to explain any project of yours in detail?", "type": "project"},
    {"text": "Any challenges faced while working on the project?", "type": "project"},
    {"text": "What could be the business impact of the project?", "type": "project"}
]

# -----------------------------
# AUDIO GENERATION
# -----------------------------
async def generate_question_audio(question, voice="en-IE-EmilyNeural"):
    clean_question = re.sub(r'[^A-Za-z0-9.,?! ]+', '', question)
    tts = edge_tts.Communicate(text=clean_question, voice=voice)
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
        await tts.save(tmp_file.name)
        return tmp_file.name

# -----------------------------
# EVALUATION FUNCTION
# -----------------------------
def evaluate_answer(question_text, answer_text, q_type):
    model = genai.GenerativeModel("gemini-2.5-pro")

    if q_type == "introduction":
        prompt = f"""
You are an expert interviewer evaluating a candidate's introduction. Assess the response based on:
- Clarity & Fluency
- Confidence & Professionalism
- Relevance & Structure
- Conciseness
Provide an evaluation summary with a score out of 10.
Candidate Introduction:
{answer_text}
"""
    else:  # project explanation
        prompt = f"""
You are an expert interviewer evaluating a candidate's project explanation. Assess the response based on:
- Technical Understanding
- Communication Clarity
- Problem-Solving & Impact
- Use of Examples
- Logical Flow & Structure
Provide an evaluation summary with a score out of 10.
Candidate Project Explanation:
{answer_text}
"""
    response = model.generate_content(prompt)
    text = response.text.strip()
    score_match = re.search(r"\*\*Overall Score:\*\* (\d+)/10", text)
    score = int(score_match.group(1)) if score_match else 0
    return {"score": score, "feedback": text}

# -----------------------------
# IMPROVEMENT SUGGESTIONS
# -----------------------------
def generate_improvement_suggestions():
    model = genai.GenerativeModel('gemini-2.5-pro')
    if not st.session_state.get("answers"):
        st.session_state.improvement_suggestions = "No answers were recorded to generate improvement suggestions."
        return

    qa_context = []
    for i, entry in enumerate(st.session_state["answers"]):
        qa_context.append(
            f"Question {i+1}: {entry['question']}\nCandidate's Answer {i+1}: {entry.get('response','[No response]')}"
        )
    full_qa_context = "\n\n".join(qa_context)

    prompt = f"""
You are an interview coach. Based on these Q&A:
{full_qa_context}
Provide detailed improvement suggestions for each answer. Be constructive and supportive.
"""
    try:
        st.info("πŸ€– Generating detailed improvement suggestions...")
        response = model.generate_content(prompt)
        st.session_state.improvement_suggestions = response.text.strip()
        st.session_state.improvement_suggestions_generated = True
        st.success("Detailed suggestions generated!")
    except Exception as e:
        st.error(f"Error generating suggestions: {e}")
        st.session_state.improvement_suggestions_generated = False

# -----------------------------
# START PAGE: Candidate Intro Button
# -----------------------------
if not st.session_state["show_intro"]:
    st.title("πŸ”₯🎯 Welcome to GrillMaster Mock Interview")
    st.markdown("Click the button below to start the Candidate Introduction + Project mock interview:")
    if st.button("🎀 Candidate Intro + Project"):
        st.session_state.update({
            "show_intro": True,
            "selected_domain": "Candidate Intro + Project",
            "current_question_index": 0,
            "answers": [],
            "evaluations": [],
            "question_played": False
        })
        st.rerun()

# -----------------------------
# CANDIDATE INTRO WORKFLOW
# -----------------------------
if st.session_state.get("show_intro"):
    st.header("🎯 Candidate Introduction + Project")

    q_index = st.session_state["current_question_index"]
    if q_index < len(CANDIDATE_QUESTIONS):
        question = CANDIDATE_QUESTIONS[q_index]
        st.subheader(f"Q{q_index+1}: {question['text']}")

        # Generate audio
        if not st.session_state["question_played"]:
            audio_file = asyncio.run(generate_question_audio(question["text"]))
            st.audio(audio_file, format="audio/mp3")
            st.session_state["question_played"] = True

        # Record answer
        audio_data = st.audio_input("🎀 Record your answer here")
        if audio_data:
            audio_bytes = audio_data.read()
            with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
                f.write(audio_bytes)
                wav_path = f.name
            recognizer = sr.Recognizer()
            with sr.AudioFile(wav_path) as source:
                recorded_audio = recognizer.record(source)
                try:
                    response_text = recognizer.recognize_google(recorded_audio)
                    st.session_state["current_response"] = response_text
                    st.success("βœ… Answer recorded. You can re-record or move to next question.")
                except sr.UnknownValueError:
                    st.error("⚠️ Could not understand audio.")

        # Buttons
        col1, col2 = st.columns(2)
        with col1:
            if st.button("πŸ”„ Re-record Answer"):
                st.session_state.pop("current_response", None)
                st.session_state["question_played"] = False
                st.experimental_rerun()
        with col2:
            if "current_response" in st.session_state and st.button("➑️ Next Question"):
                st.session_state["answers"].append({
                    "question": question["text"],
                    "response": st.session_state.pop("current_response"),
                    "type": question["type"]
                })
                st.session_state["question_played"] = False
                st.session_state["current_question_index"] += 1
                st.rerun()

    else:
        # Evaluate all answers
        if not st.session_state["evaluations"]:
            for ans in st.session_state["answers"]:
                ev = evaluate_answer(ans["question"], ans["response"], ans["type"])
                st.session_state["evaluations"].append(ev)

        st.subheader("πŸ“Š Mock Interview Completed")
        total_score = sum([ev["score"] for ev in st.session_state["evaluations"]])
        overall_score = round(total_score / len(st.session_state["evaluations"]), 2)
        st.write(f"**Overall Average Score:** {overall_score}/10")
        st.progress(overall_score / 10)

        # Show answers & feedback
        for i, ans in enumerate(st.session_state["answers"]):
            ev = st.session_state["evaluations"][i]
            st.write(f"**Q{i+1}: {ans['question']}**")
            st.write(f"**A:** {ans['response']}")
            st.write(f"**Score:** {ev['score']}/10")
            st.write(ev["feedback"])
            st.write("---")

        # Improvement suggestions
        if st.button("πŸ’‘ Generate Improvement Suggestions"):
            generate_improvement_suggestions()
            st.rerun()
        if st.session_state.get("improvement_suggestions_generated"):
            with st.expander("πŸ” Improvement Suggestions", expanded=True):
                st.markdown(st.session_state["improvement_suggestions"])

        # Download summary
        def prepare_summary():
            text = "# GrillMaster Candidate Intro Summary\n\n"
            for i, ans in enumerate(st.session_state["answers"]):
                ev = st.session_state["evaluations"][i]
                text += f"**Q{i+1}: {ans['question']}**\n**A:** {ans['response']}\n**Score:** {ev['score']}/10\n\n"
            if st.session_state.get("improvement_suggestions_generated"):
                text += "## Improvement Suggestions:\n" + st.session_state["improvement_suggestions"]
            return text.encode("utf-8")

        st.download_button("πŸ’Ύ Download Summary", data=prepare_summary(),
                           file_name=f"GrillMaster_Summary_{time.strftime('%Y%m%d_%H%M')}.md",
                           mime="text/markdown")