File size: 2,579 Bytes
ee1c502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c2a12
 
ee1c502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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()