Hasitha16 commited on
Commit
6f0a5bf
Β·
verified Β·
1 Parent(s): 003bb51

Update frontend.py

Browse files
Files changed (1) hide show
  1. 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
- def speak(text, lang='en'):
67
- try:
68
- speech_key = st.secrets["AZURE_SPEECH_KEY"]
69
- region = st.secrets["AZURE_REGION"]
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
- synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
77
-
78
- result = synthesizer.speak_text_async(text).get()
79
- if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
80
- audio_data = result.audio_data
81
- mp3 = BytesIO(audio_data)
82
- b64 = base64.b64encode(mp3.getvalue()).decode()
83
- st.markdown(f'<audio controls><source src="data:audio/mp3;base64,{b64}" type="audio/mp3"></audio>', unsafe_allow_html=True)
84
- mp3.seek(0)
85
- return mp3
86
- else:
87
- st.warning("Azure TTS failed.")
88
- return BytesIO()
 
 
 
 
 
89
  except Exception as e:
90
- st.error(f"πŸ”Š Azure TTS Error: {e}")
91
- return BytesIO()
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
- audio = speak(data["summary"], lang=voice_lang)
173
- st.download_button("⬇️ Download Audio", audio.read(), "summary.mp3")
 
 
 
 
 
 
 
 
 
 
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()