Hasitha16 commited on
Commit
82211db
Β·
verified Β·
1 Parent(s): ff4a3a5

Update frontend.py

Browse files
Files changed (1) hide show
  1. frontend.py +28 -7
frontend.py CHANGED
@@ -1,6 +1,8 @@
1
  import streamlit as st
2
  import requests
3
  import pandas as pd
 
 
4
  from gtts import gTTS
5
  import base64
6
  from io import BytesIO
@@ -62,13 +64,32 @@ with st.sidebar:
62
 
63
  # Text-to-Speech
64
  def speak(text, lang='en'):
65
- tts = gTTS(text, lang=lang)
66
- mp3 = BytesIO()
67
- tts.write_to_fp(mp3)
68
- b64 = base64.b64encode(mp3.getvalue()).decode()
69
- st.markdown(f'<audio controls><source src="data:audio/mp3;base64,{b64}" type="audio/mp3"></audio>', unsafe_allow_html=True)
70
- mp3.seek(0)
71
- return mp3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  tab1, tab2 = st.tabs(["🧠 Analyze Review", "πŸ“š Bulk Reviews"])
74
 
 
1
  import streamlit as st
2
  import requests
3
  import pandas as pd
4
+ import azure.cognitiveservices.speech as speechsdk
5
+ import tempfile
6
  from gtts import gTTS
7
  import base64
8
  from io import BytesIO
 
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"])
95