Viper51 commited on
Commit
6301e7e
·
verified ·
1 Parent(s): 78fc224

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +164 -47
src/streamlit_app.py CHANGED
@@ -1,16 +1,45 @@
1
  import streamlit as st
2
- from PyPDF2 import PdfReader
3
- import google.generativeai as genai
4
- from langchain_google_genai import ChatGoogleGenerativeAI
5
- from langchain_core.prompts import PromptTemplate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from pydantic import BaseModel, Field
7
  from typing import Optional
8
- from gtts import gTTS
9
- import speech_recognition as sr
 
 
 
 
 
 
 
 
 
 
10
  import os
11
  import io
12
  import tempfile
13
- from streamlit_mic_recorder import mic_recorder # Key component for browser audio
 
 
 
 
 
14
 
15
  # --- Configuration & Secrets ---
16
 
@@ -54,6 +83,10 @@ def get_models(_llm_model):
54
  def read_resume(uploaded_file):
55
  """Reads a PDF file uploaded via Streamlit."""
56
  try:
 
 
 
 
57
  reader = PdfReader(uploaded_file)
58
  text = ""
59
  for page in reader.pages:
@@ -65,18 +98,52 @@ def read_resume(uploaded_file):
65
 
66
  def generate_questions_from_resume(resume_text, model):
67
  """Generates interview questions from resume text."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  parse_resume_prompt_template = PromptTemplate(
69
  template="""Generate 4-8 interview questions about the Experience and Projects section from this given text of from a resume.
70
  Try to cover all projects and experience. Generate some conceptual questions too. Don't generate unnecessary questions.
71
  Resume:\n{text}""",
72
  input_variables=['text']
73
  )
74
- generate_question_from_resume_chain = parse_resume_prompt_template | model
75
- output = generate_question_from_resume_chain.invoke({'text': resume_text})
76
- return output.questions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  def get_introduction(model):
79
  """Gets the AI's intro and first question."""
 
 
 
 
80
  introduction_prompt = PromptTemplate(template="""Introduce yourself to the user telling the user that you are a AI agent. And ask the user to give introduction""")
81
  intro_chain = introduction_prompt | model
82
  output = intro_chain.invoke({})
@@ -84,14 +151,31 @@ def get_introduction(model):
84
 
85
  def ask_followup(user_intro, model):
86
  """Asks a followup to the user's intro."""
 
 
 
87
  intro_followup = PromptTemplate(template="""The user has given the following introduction of himself/herself. Ask a followup about his intro to make the user comfortable. Intro given by the user: {intro}""",
88
  input_variables=['intro'])
89
  followup_chain = intro_followup | model
90
  output = followup_chain.invoke({'intro': user_intro})
91
- return output.followup
92
 
93
  def evaluate_answer(question, answer, model):
94
  """Evaluates the user's answer."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  evaluate_answer_prompt = PromptTemplate(template="""You are given a question and an answer. Evaluate the answer honestly on the question out of 100.
96
  Also generate a very short review on the answer telling the candidate about his answer. If he is wrong but close to the correct answer, give subtle hints.
97
  If a good followup question can be asked generate it but only if it is a genuine question.\nQuestion: {question}\n\n Answer: {answer}""",
@@ -109,14 +193,20 @@ def text_to_speech_and_display(text, autoplay=True):
109
 
110
  try:
111
  # Display the caption
 
 
112
  st.session_state.chat_history.append(f"**Interviewer:** {text}")
113
-
114
- # Generate audio
 
 
 
 
115
  tts = gTTS(text=text, lang='en', slow=False)
116
  audio_fp = io.BytesIO()
117
  tts.write_to_fp(audio_fp)
118
  audio_fp.seek(0)
119
-
120
  # Display audio player
121
  st.audio(audio_fp, format='audio/mp3', autoplay=autoplay)
122
 
@@ -128,24 +218,27 @@ def speech_to_text(audio_bytes):
128
  if not audio_bytes:
129
  return "No audio recorded."
130
 
 
 
 
 
131
  r = sr.Recognizer()
132
-
133
  # Need to save bytes to a temporary WAV file
134
- # because recognizer.recognize_google requires a file path or AudioData
135
  try:
136
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
137
  temp_wav.write(audio_bytes)
138
  temp_wav_path = temp_wav.name
139
-
140
- # Use the temp file
141
  with sr.AudioFile(temp_wav_path) as source:
142
  audio_data = r.record(source)
143
-
144
- # Recognize speech
145
  text = r.recognize_google(audio_data)
 
 
146
  st.session_state.chat_history.append(f"**You:** {text}")
147
  return text
148
-
149
  except sr.UnknownValueError:
150
  st.warning("Could not understand audio.")
151
  return None
@@ -156,9 +249,11 @@ def speech_to_text(audio_bytes):
156
  st.error(f"Error processing audio: {e}")
157
  return None
158
  finally:
159
- # Clean up the temp file
160
  if 'temp_wav_path' in locals() and os.path.exists(temp_wav_path):
161
- os.remove(temp_wav_path)
 
 
 
162
 
163
  # --- Main Streamlit App ---
164
 
@@ -166,20 +261,26 @@ st.set_page_config(page_title="AI Interviewer", layout="wide")
166
  st.title("Interviewer.AI")
167
 
168
  # Initialize LLM and models
169
- # First, load the key from the environment variable
170
- try:
171
- GOOGLE_API_KEY = os.environ["GOOGLE_API_KEY"]
172
- genai.configure(api_key=GOOGLE_API_KEY)
173
- except KeyError:
174
- st.error("GOOGLE_API_KEY not found in Hugging Face secrets. Please go to your Space's 'Settings' tab and add it.", icon="🚨")
175
- st.stop()
176
- except Exception as e:
177
- st.error(f"Error configuring Google API: {e}", icon="🚨")
178
- st.stop()
179
-
180
- # Now, pass the key to the cached function
181
- llm = get_llm(GOOGLE_API_KEY)
182
- gen_q_model, intro_model, eval_model = get_models(llm)
 
 
 
 
 
 
183
 
184
  # --- Session State Initialization ---
185
  # This is crucial for making the app work step-by-step
@@ -217,12 +318,12 @@ if st.session_state.stage == 'start':
217
  else:
218
  # 2. Get AI Introduction
219
  intro_output = get_introduction(intro_model)
220
- st.session_state.current_question = intro_output.question
221
 
222
  # 3. Move to next stage and display intro
223
  st.session_state.stage = 'awaiting_intro'
224
- text_to_speech_and_display(intro_output.intro)
225
- text_to_speech_and_display(intro_output.question)
226
  st.rerun() # Rerun to update the UI
227
 
228
  # --- Main Interview Area (Stages > 0) ---
@@ -230,19 +331,23 @@ if st.session_state.stage != 'start':
230
 
231
  # --- Chat History Display ---
232
  st.subheader("Interview Transcript")
233
- chat_container = st.container(height=300, border=True)
234
  with chat_container:
235
  for entry in st.session_state.chat_history:
236
  st.markdown(entry)
237
-
238
- st.divider()
 
 
 
 
239
 
240
  # --- Audio Recorder ---
241
  # This component returns audio bytes when the user stops recording
242
  st.write("Your turn to speak:")
243
  audio_bytes = mic_recorder(
244
- start_prompt="Start Recording",
245
- stop_prompt="Stop Recording",
246
  key='recorder'
247
  )
248
 
@@ -252,9 +357,21 @@ if st.session_state.stage != 'start':
252
  st.rerun()
253
 
254
  # --- Process Recorded Audio ---
255
- if audio_bytes:
 
 
 
 
 
 
 
 
 
 
 
 
256
  with st.spinner("Transcribing your answer..."):
257
- user_text = speech_to_text(audio_bytes['bytes'])
258
 
259
  if user_text:
260
  # --- STAGE 1: Process User's Introduction ---
 
1
  import streamlit as st
2
+ try:
3
+ from PyPDF2 import PdfReader
4
+ except Exception:
5
+ PdfReader = None
6
+
7
+ # Optional AI SDKs - guarded imports so the app can still run without them
8
+ try:
9
+ import google.generativeai as genai
10
+ except Exception:
11
+ genai = None
12
+
13
+ try:
14
+ from langchain_google_genai import ChatGoogleGenerativeAI
15
+ from langchain_core.prompts import PromptTemplate
16
+ except Exception:
17
+ ChatGoogleGenerativeAI = None
18
+ PromptTemplate = None
19
+
20
  from pydantic import BaseModel, Field
21
  from typing import Optional
22
+
23
+ # Optional TTS / speech libs
24
+ try:
25
+ from gtts import gTTS
26
+ except Exception:
27
+ gTTS = None
28
+
29
+ try:
30
+ import speech_recognition as sr
31
+ except Exception:
32
+ sr = None
33
+
34
  import os
35
  import io
36
  import tempfile
37
+ try:
38
+ from streamlit_mic_recorder import mic_recorder # Key component for browser audio
39
+ except Exception:
40
+ # Fallback dummy recorder function that always returns None
41
+ def mic_recorder(*args, **kwargs):
42
+ return None
43
 
44
  # --- Configuration & Secrets ---
45
 
 
83
  def read_resume(uploaded_file):
84
  """Reads a PDF file uploaded via Streamlit."""
85
  try:
86
+ if PdfReader is None:
87
+ st.warning("PyPDF2 is not installed; resume text extraction disabled.")
88
+ return None
89
+ # PdfReader accepts a file-like object
90
  reader = PdfReader(uploaded_file)
91
  text = ""
92
  for page in reader.pages:
 
98
 
99
  def generate_questions_from_resume(resume_text, model):
100
  """Generates interview questions from resume text."""
101
+ # If LangChain PromptTemplate or LLM wrapper is not available, return simple heuristic questions
102
+ if PromptTemplate is None or model is None:
103
+ # Simple fallback: create questions from lines with 'Project'/'Experience' keywords
104
+ lines = resume_text.splitlines()
105
+ candidates = [l.strip() for l in lines if l and ('project' in l.lower() or 'experience' in l.lower())]
106
+ questions = []
107
+ for c in candidates:
108
+ if len(questions) >= 6:
109
+ break
110
+ questions.append(f"Tell me more about: {c}")
111
+ if not questions:
112
+ questions = ["Tell me about your most significant project.", "Describe a challenging bug you fixed.", "How do you design for scalability?", "Which technologies are you most comfortable with?"]
113
+ return questions
114
+
115
  parse_resume_prompt_template = PromptTemplate(
116
  template="""Generate 4-8 interview questions about the Experience and Projects section from this given text of from a resume.
117
  Try to cover all projects and experience. Generate some conceptual questions too. Don't generate unnecessary questions.
118
  Resume:\n{text}""",
119
  input_variables=['text']
120
  )
121
+ # Use the LangChain pipeline if available
122
+ try:
123
+ generate_question_from_resume_chain = parse_resume_prompt_template | model
124
+ output = generate_question_from_resume_chain.invoke({'text': resume_text})
125
+ # attempt to coerce into a list
126
+ return getattr(output, 'questions', output)
127
+ except Exception as e:
128
+ st.warning(f"LLM question generation failed, using fallback: {e}")
129
+ # fallback similar to above
130
+ lines = resume_text.splitlines()
131
+ candidates = [l.strip() for l in lines if l and ('project' in l.lower() or 'experience' in l.lower())]
132
+ questions = []
133
+ for c in candidates:
134
+ if len(questions) >= 6:
135
+ break
136
+ questions.append(f"Tell me more about: {c}")
137
+ if not questions:
138
+ questions = ["Tell me about your most significant project.", "Describe a challenging bug you fixed.", "How do you design for scalability?", "Which technologies are you most comfortable with?"]
139
+ return questions
140
 
141
  def get_introduction(model):
142
  """Gets the AI's intro and first question."""
143
+ if PromptTemplate is None or model is None:
144
+ # Return a simple dict-like fallback
145
+ return type('O', (), {'intro': "Hello, I'm Interviewer.AI. Please introduce yourself.", 'question': "Can you briefly introduce yourself?"})()
146
+
147
  introduction_prompt = PromptTemplate(template="""Introduce yourself to the user telling the user that you are a AI agent. And ask the user to give introduction""")
148
  intro_chain = introduction_prompt | model
149
  output = intro_chain.invoke({})
 
151
 
152
  def ask_followup(user_intro, model):
153
  """Asks a followup to the user's intro."""
154
+ if PromptTemplate is None or model is None:
155
+ return "Thanks — could you tell me one achievement you're most proud of?"
156
+
157
  intro_followup = PromptTemplate(template="""The user has given the following introduction of himself/herself. Ask a followup about his intro to make the user comfortable. Intro given by the user: {intro}""",
158
  input_variables=['intro'])
159
  followup_chain = intro_followup | model
160
  output = followup_chain.invoke({'intro': user_intro})
161
+ return getattr(output, 'followup', None)
162
 
163
  def evaluate_answer(question, answer, model):
164
  """Evaluates the user's answer."""
165
+ if PromptTemplate is None or model is None:
166
+ # Simple heuristic evaluator
167
+ score = 50
168
+ review = "Thank you for your answer. Provide more details next time."
169
+ followup = None
170
+ # small heuristic: longer answers get better score
171
+ if answer and len(answer.split()) > 50:
172
+ score = 80
173
+ review = "Good answer — you covered several points."
174
+ elif answer and len(answer.split()) > 20:
175
+ score = 65
176
+ review = "Decent answer; add more concrete examples."
177
+ return type('O', (), {'marks': score, 'review': review, 'followup': followup})()
178
+
179
  evaluate_answer_prompt = PromptTemplate(template="""You are given a question and an answer. Evaluate the answer honestly on the question out of 100.
180
  Also generate a very short review on the answer telling the candidate about his answer. If he is wrong but close to the correct answer, give subtle hints.
181
  If a good followup question can be asked generate it but only if it is a genuine question.\nQuestion: {question}\n\n Answer: {answer}""",
 
193
 
194
  try:
195
  # Display the caption
196
+ if 'chat_history' not in st.session_state:
197
+ st.session_state.chat_history = []
198
  st.session_state.chat_history.append(f"**Interviewer:** {text}")
199
+
200
+ # Generate audio if gTTS available
201
+ if gTTS is None:
202
+ # No TTS available; just show text
203
+ return
204
+
205
  tts = gTTS(text=text, lang='en', slow=False)
206
  audio_fp = io.BytesIO()
207
  tts.write_to_fp(audio_fp)
208
  audio_fp.seek(0)
209
+
210
  # Display audio player
211
  st.audio(audio_fp, format='audio/mp3', autoplay=autoplay)
212
 
 
218
  if not audio_bytes:
219
  return "No audio recorded."
220
 
221
+ if sr is None:
222
+ st.warning("speech_recognition is not installed; microphone input unavailable.")
223
+ return None
224
+
225
  r = sr.Recognizer()
226
+
227
  # Need to save bytes to a temporary WAV file
 
228
  try:
229
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
230
  temp_wav.write(audio_bytes)
231
  temp_wav_path = temp_wav.name
232
+
 
233
  with sr.AudioFile(temp_wav_path) as source:
234
  audio_data = r.record(source)
235
+
 
236
  text = r.recognize_google(audio_data)
237
+ if 'chat_history' not in st.session_state:
238
+ st.session_state.chat_history = []
239
  st.session_state.chat_history.append(f"**You:** {text}")
240
  return text
241
+
242
  except sr.UnknownValueError:
243
  st.warning("Could not understand audio.")
244
  return None
 
249
  st.error(f"Error processing audio: {e}")
250
  return None
251
  finally:
 
252
  if 'temp_wav_path' in locals() and os.path.exists(temp_wav_path):
253
+ try:
254
+ os.remove(temp_wav_path)
255
+ except Exception:
256
+ pass
257
 
258
  # --- Main Streamlit App ---
259
 
 
261
  st.title("Interviewer.AI")
262
 
263
  # Initialize LLM and models
264
+ llm = None
265
+ gen_q_model = None
266
+ intro_model = None
267
+ eval_model = None
268
+
269
+ # First, load the key from the environment variable if genai is available
270
+ if genai is None or ChatGoogleGenerativeAI is None:
271
+ st.warning("Google GenAI or LangChain wrappers not available. App will use deterministic fallbacks.")
272
+ else:
273
+ try:
274
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
275
+ if not GOOGLE_API_KEY:
276
+ st.warning("GOOGLE_API_KEY not set; using fallbacks for LLM features.")
277
+ else:
278
+ genai.configure(api_key=GOOGLE_API_KEY)
279
+ # Initialize LLM and model wrappers
280
+ llm = get_llm(GOOGLE_API_KEY)
281
+ gen_q_model, intro_model, eval_model = get_models(llm)
282
+ except Exception as e:
283
+ st.warning(f"Could not initialize LLM: {e}. Using fallbacks.")
284
 
285
  # --- Session State Initialization ---
286
  # This is crucial for making the app work step-by-step
 
318
  else:
319
  # 2. Get AI Introduction
320
  intro_output = get_introduction(intro_model)
321
+ st.session_state.current_question = getattr(intro_output, 'question', "Can you introduce yourself?")
322
 
323
  # 3. Move to next stage and display intro
324
  st.session_state.stage = 'awaiting_intro'
325
+ text_to_speech_and_display(getattr(intro_output, 'intro', "Hello, I'm Interviewer.AI. Please introduce yourself."))
326
+ text_to_speech_and_display(getattr(intro_output, 'question', "Can you introduce yourself?"))
327
  st.rerun() # Rerun to update the UI
328
 
329
  # --- Main Interview Area (Stages > 0) ---
 
331
 
332
  # --- Chat History Display ---
333
  st.subheader("Interview Transcript")
334
+ chat_container = st.container()
335
  with chat_container:
336
  for entry in st.session_state.chat_history:
337
  st.markdown(entry)
338
+
339
+ # visual divider
340
+ try:
341
+ st.divider()
342
+ except Exception:
343
+ st.markdown('---')
344
 
345
  # --- Audio Recorder ---
346
  # This component returns audio bytes when the user stops recording
347
  st.write("Your turn to speak:")
348
  audio_bytes = mic_recorder(
349
+ start_prompt="Start Recording",
350
+ stop_prompt="Stop Recording",
351
  key='recorder'
352
  )
353
 
 
357
  st.rerun()
358
 
359
  # --- Process Recorded Audio ---
360
+ # mic_recorder may return None, bytes, or a dict with a 'bytes' key depending on implementation
361
+ def _extract_audio_bytes(rec):
362
+ if rec is None:
363
+ return None
364
+ if isinstance(rec, dict):
365
+ # some implementations return {'bytes': b'...', 'start':..., ...}
366
+ return rec.get('bytes') or rec.get('audio') or None
367
+ if isinstance(rec, (bytes, bytearray)):
368
+ return bytes(rec)
369
+ return None
370
+
371
+ extracted_audio = _extract_audio_bytes(audio_bytes)
372
+ if extracted_audio:
373
  with st.spinner("Transcribing your answer..."):
374
+ user_text = speech_to_text(extracted_audio)
375
 
376
  if user_text:
377
  # --- STAGE 1: Process User's Introduction ---