Spaces:
Sleeping
Sleeping
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator | |
import streamlit as st | |
from ibm_watson import TextToSpeechV1 | |
import os | |
from google import genai | |
import re | |
voices = [ | |
"en-US_MichaelV3Voice", | |
"en-US_LisaV3Voice", | |
"en-US_AllisonV3Voice" | |
] | |
genai_client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) | |
genai_model = "gemini-2.0-flash" | |
text_to_speech_authenticator = IAMAuthenticator( | |
apikey=os.getenv("TEXT_TO_SPEECH_APIKEY") | |
) | |
text_to_speech_engine = TextToSpeechV1( | |
authenticator=text_to_speech_authenticator | |
) | |
text_to_speech_engine.set_service_url(os.getenv("TEXT_TO_SPEECH_URL")) | |
def main(): | |
st.title("SSML Enhanced Text-to-Speech Synthesis") | |
st.write("This experimental app utilizes Google Gemini to automatically insert Speech Synthesis Markup Language (SSML) tags into input text. By then processing this enriched text through IBM's Text to Speech engine, the app aims to produce higher-quality, more nuanced, and human-like synthesized speech.") | |
col1, col2 = st.columns(2) | |
with col1: | |
text = st.text_input("Input Text") | |
voice = st.selectbox("Select Voice", voices) | |
if st.button("Synthesize"): | |
with col2: | |
with st.spinner("Processing text input with Gemini..."): | |
genai_response = genai_client.models.generate_content( | |
model=genai_model, | |
contents=[ | |
"Your goal is to help the text-to-speech engine sound more natural.", | |
f'The input text is: "{text}"', | |
"- Improve the input text with SSML tags", | |
"Return only the XML tags and the text content" | |
] | |
) | |
text_to_synthesize = re.sub(r'```xml|```', '', genai_response.text) | |
with st.spinner("Synthesizing speech..."): | |
text_to_speech_response = text_to_speech_engine.synthesize( | |
text_to_synthesize, | |
accept='audio/wav', | |
voice=voice | |
).get_result().content | |
with col2: | |
st.write("Gemini Response") | |
st.markdown(genai_response.text) | |
st.write("Generated Audio") | |
st.audio(text_to_speech_response, format="audio/wav") | |
if __name__ == "__main__": | |
main() | |