File size: 1,023 Bytes
daf2fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from groq import Groq
from pathlib import Path
import base64
import streamlit as st

def generate_and_play_audio(text: str, voice: str = "Aaliyah-PlayAI"):
    """
    Generate speech from text using Groq TTS and play it via Streamlit.

    Args:
        text (str): Text to synthesize.
        voice (str): Voice model name (default: Aaliyah-PlayAI).
    """
    speech_file_path = Path("speech.wav")

    client = Groq()
    response = client.audio.speech.create(
        model="playai-tts",
        voice=voice,
        response_format="wav",
        input=text,
    )

    # Save audio output
    with open(speech_file_path, "wb") as f:
        f.write(response.read())

    # Embed and play in Streamlit
    with open(speech_file_path, "rb") as f:
        b64_audio = base64.b64encode(f.read()).decode()
        audio_html = f"""
        <audio autoplay>
            <source src="data:audio/wav;base64,{b64_audio}" type="audio/wav">
        </audio>
        """
        st.markdown(audio_html, unsafe_allow_html=True)