Spaces:
Sleeping
Sleeping
Update frontend.py
Browse files- frontend.py +38 -26
frontend.py
CHANGED
|
@@ -63,32 +63,33 @@ with st.sidebar:
|
|
| 63 |
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
| 64 |
|
| 65 |
# Text-to-Speech
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=region)
|
| 72 |
-
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=False)
|
| 73 |
-
|
| 74 |
-
speech_config.speech_synthesis_voice_name = f"{lang}-US-JennyNeural" if lang == "en" else f"{lang}-Standard-A"
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
-
st.error(f"
|
| 91 |
-
return
|
| 92 |
|
| 93 |
|
| 94 |
tab1, tab2 = st.tabs(["π§ Analyze Review", "π Bulk Reviews"])
|
|
@@ -168,9 +169,20 @@ with tab1:
|
|
| 168 |
except Exception as e:
|
| 169 |
st.warning(f"π§ͺ Logging failed: {e}")
|
| 170 |
|
|
|
|
| 171 |
st.subheader("π Audio Summary")
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
st.markdown("### π Ask a Follow-Up")
|
| 176 |
sentiment = data["sentiment"]["label"].lower()
|
|
|
|
| 63 |
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
| 64 |
|
| 65 |
# Text-to-Speech
|
| 66 |
+
# Setup usage tracking
|
| 67 |
+
if "tts_usage_count" not in st.session_state:
|
| 68 |
+
st.session_state.tts_usage_count = 0
|
| 69 |
+
if "enable_audio" not in st.session_state:
|
| 70 |
+
st.session_state.enable_audio = False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Azure TTS function
|
| 73 |
+
def azure_speak(text, lang='en-US'):
|
| 74 |
+
try:
|
| 75 |
+
speech_config = speechsdk.SpeechConfig(
|
| 76 |
+
subscription=st.secrets["AZURE_SPEECH_KEY"],
|
| 77 |
+
region=st.secrets["AZURE_REGION"]
|
| 78 |
+
)
|
| 79 |
+
speech_config.speech_synthesis_language = lang
|
| 80 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmpfile:
|
| 81 |
+
audio_config = speechsdk.audio.AudioOutputConfig(filename=tmpfile.name)
|
| 82 |
+
synthesizer = speechsdk.SpeechSynthesizer(speech_config, audio_config)
|
| 83 |
+
result = synthesizer.speak_text_async(text).get()
|
| 84 |
+
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
|
| 85 |
+
st.session_state.tts_usage_count += 1
|
| 86 |
+
return tmpfile.name
|
| 87 |
+
else:
|
| 88 |
+
st.error("Speech synthesis failed.")
|
| 89 |
+
return None
|
| 90 |
except Exception as e:
|
| 91 |
+
st.error(f"Azure TTS error: {e}")
|
| 92 |
+
return None
|
| 93 |
|
| 94 |
|
| 95 |
tab1, tab2 = st.tabs(["π§ Analyze Review", "π Bulk Reviews"])
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
st.warning(f"π§ͺ Logging failed: {e}")
|
| 171 |
|
| 172 |
+
# Only show toggle and button for audio
|
| 173 |
st.subheader("π Audio Summary")
|
| 174 |
+
st.session_state.enable_audio = st.toggle("π§ Generate Audio Summary")
|
| 175 |
+
|
| 176 |
+
if st.session_state.enable_audio:
|
| 177 |
+
if st.session_state.tts_usage_count > 20:
|
| 178 |
+
st.warning("π Azure TTS usage limit reached for this session.")
|
| 179 |
+
else:
|
| 180 |
+
if st.button("βΆοΈ Generate & Play Audio"):
|
| 181 |
+
audio_path = azure_speak(data["summary"], lang=f"{voice_lang}-US")
|
| 182 |
+
if audio_path:
|
| 183 |
+
audio_bytes = open(audio_path, "rb").read()
|
| 184 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 185 |
+
st.download_button("β¬οΈ Download Audio", audio_bytes, "summary.mp3")
|
| 186 |
|
| 187 |
st.markdown("### π Ask a Follow-Up")
|
| 188 |
sentiment = data["sentiment"]["label"].lower()
|