Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +59 -21
src/streamlit_app.py
CHANGED
|
@@ -24,6 +24,10 @@ try:
|
|
| 24 |
from gtts import gTTS
|
| 25 |
except Exception as e:
|
| 26 |
st.error(f"error importing gtts: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# --- Pydantic Models (from your code) ---
|
| 28 |
|
| 29 |
class questions(BaseModel):
|
|
@@ -171,35 +175,68 @@ If a good followup question can be asked generate it but only if it is a genuine
|
|
| 171 |
|
| 172 |
import io # Make sure 'import io' is at the top of your file
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
def text_to_speech_and_display(text, autoplay=True):
|
| 175 |
-
"""
|
|
|
|
|
|
|
| 176 |
if not text:
|
| 177 |
return
|
| 178 |
|
| 179 |
try:
|
| 180 |
-
# Display the caption
|
| 181 |
if 'chat_history' not in st.session_state:
|
| 182 |
st.session_state.chat_history = []
|
| 183 |
st.session_state.chat_history.append(f"**Interviewer:** {text}")
|
| 184 |
|
| 185 |
-
#
|
| 186 |
-
|
| 187 |
-
st.warning("gTTS library not found, audio playback is disabled.")
|
| 188 |
-
return
|
| 189 |
-
|
| 190 |
-
tts = gTTS(text=text, lang='en', slow=False)
|
| 191 |
-
audio_fp = io.BytesIO()
|
| 192 |
-
tts.write_to_fp(audio_fp)
|
| 193 |
-
audio_fp.seek(0)
|
| 194 |
|
| 195 |
-
# Display audio player
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
-
# This will catch
|
| 200 |
-
st.error(f"Error
|
| 201 |
-
st.info("This
|
| 202 |
-
# ---
|
| 203 |
# We are replacing it with a text_input
|
| 204 |
|
| 205 |
|
|
@@ -314,18 +351,19 @@ elif st.session_state.stage == 'processing_resume':
|
|
| 314 |
st.warning("AI Calls are in TEST MODE.")
|
| 315 |
|
| 316 |
# 1. Generate DUMMY Questions
|
| 317 |
-
|
|
|
|
| 318 |
|
| 319 |
# 2. Get DUMMY AI Introduction
|
| 320 |
intro_output = get_introduction(intro_model)
|
| 321 |
|
| 322 |
-
st.session_state.current_question = intro_output
|
| 323 |
|
| 324 |
# 3. Move to next stage and display intro
|
| 325 |
st.session_state.stage = 'awaiting_intro'
|
| 326 |
|
| 327 |
-
text_to_speech_and_display(intro_output
|
| 328 |
-
text_to_speech_and_display(intro_output
|
| 329 |
|
| 330 |
# Clean up the resume text from session state
|
| 331 |
if 'resume_text' in st.session_state:
|
|
|
|
| 24 |
from gtts import gTTS
|
| 25 |
except Exception as e:
|
| 26 |
st.error(f"error importing gtts: {e}")
|
| 27 |
+
try:
|
| 28 |
+
from google.cloud import texttospeech
|
| 29 |
+
except Exception:
|
| 30 |
+
texttospeech = None
|
| 31 |
# --- Pydantic Models (from your code) ---
|
| 32 |
|
| 33 |
class questions(BaseModel):
|
|
|
|
| 175 |
|
| 176 |
import io # Make sure 'import io' is at the top of your file
|
| 177 |
|
| 178 |
+
# --- REPLACED: Official Google Cloud TTS Function ---
|
| 179 |
+
@st.cache_data
|
| 180 |
+
def synthesize_speech(text):
|
| 181 |
+
"""
|
| 182 |
+
Synthesizes speech from the given text using Google Cloud TTS
|
| 183 |
+
and returns the audio content as bytes.
|
| 184 |
+
"""
|
| 185 |
+
if texttospeech is None:
|
| 186 |
+
st.warning("google-cloud-texttospeech library not found, audio playback is disabled.")
|
| 187 |
+
return None
|
| 188 |
+
|
| 189 |
+
# Instantiates a client
|
| 190 |
+
client = texttospeech.TextToSpeechClient()
|
| 191 |
+
|
| 192 |
+
# Set the text input to be synthesized
|
| 193 |
+
synthesis_input = texttospeech.SynthesisInput(text=text)
|
| 194 |
+
|
| 195 |
+
# Build the voice request
|
| 196 |
+
voice = texttospeech.VoiceSelectionParams(
|
| 197 |
+
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Select the type of audio file you want
|
| 201 |
+
audio_config = texttospeech.AudioConfig(
|
| 202 |
+
audio_encoding=texttospeech.AudioEncoding.MP3
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
# Perform the text-to-speech request
|
| 206 |
+
response = client.synthesize_speech(
|
| 207 |
+
input=synthesis_input, voice=voice, audio_config=audio_config
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
return response.audio_content
|
| 211 |
+
|
| 212 |
+
|
| 213 |
def text_to_speech_and_display(text, autoplay=True):
|
| 214 |
+
"""
|
| 215 |
+
Displays the text and plays the synthesized audio.
|
| 216 |
+
"""
|
| 217 |
if not text:
|
| 218 |
return
|
| 219 |
|
| 220 |
try:
|
| 221 |
+
# 1. Display the caption in chat
|
| 222 |
if 'chat_history' not in st.session_state:
|
| 223 |
st.session_state.chat_history = []
|
| 224 |
st.session_state.chat_history.append(f"**Interviewer:** {text}")
|
| 225 |
|
| 226 |
+
# 2. Synthesize speech
|
| 227 |
+
audio_content = synthesize_speech(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
# 3. Display audio player
|
| 230 |
+
if audio_content:
|
| 231 |
+
st.audio(audio_content, format='audio/mp3', autoplay=autoplay)
|
| 232 |
+
else:
|
| 233 |
+
st.info("Audio generation is disabled or failed.")
|
| 234 |
|
| 235 |
except Exception as e:
|
| 236 |
+
# This will catch any API errors (like 403, 404, etc.)
|
| 237 |
+
st.error(f"Error during text-to-speech: {e}")
|
| 238 |
+
st.info("This usually means the 'Cloud Text-to-Speech API' is not enabled in your Google Cloud project.")
|
| 239 |
+
# --- END OF REPLACEMENT ---
|
| 240 |
# We are replacing it with a text_input
|
| 241 |
|
| 242 |
|
|
|
|
| 351 |
st.warning("AI Calls are in TEST MODE.")
|
| 352 |
|
| 353 |
# 1. Generate DUMMY Questions
|
| 354 |
+
resume_text = st.session_state.resume_text
|
| 355 |
+
st.session_state.questions = generate_questions_from_resume(resume_text, gen_q_model)
|
| 356 |
|
| 357 |
# 2. Get DUMMY AI Introduction
|
| 358 |
intro_output = get_introduction(intro_model)
|
| 359 |
|
| 360 |
+
st.session_state.current_question = intro_output.question
|
| 361 |
|
| 362 |
# 3. Move to next stage and display intro
|
| 363 |
st.session_state.stage = 'awaiting_intro'
|
| 364 |
|
| 365 |
+
text_to_speech_and_display(intro_output.intro)
|
| 366 |
+
text_to_speech_and_display(intro_output.question)
|
| 367 |
|
| 368 |
# Clean up the resume text from session state
|
| 369 |
if 'resume_text' in st.session_state:
|