Spaces:
Sleeping
Sleeping
Update frontend.py
Browse files- 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 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|