Spaces:
Sleeping
Sleeping
added utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from groq import Groq
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import base64
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
def generate_and_play_audio(text: str, voice: str = "Aaliyah-PlayAI"):
|
| 7 |
+
"""
|
| 8 |
+
Generate speech from text using Groq TTS and play it via Streamlit.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
text (str): Text to synthesize.
|
| 12 |
+
voice (str): Voice model name (default: Aaliyah-PlayAI).
|
| 13 |
+
"""
|
| 14 |
+
speech_file_path = Path("speech.wav")
|
| 15 |
+
|
| 16 |
+
client = Groq()
|
| 17 |
+
response = client.audio.speech.create(
|
| 18 |
+
model="playai-tts",
|
| 19 |
+
voice=voice,
|
| 20 |
+
response_format="wav",
|
| 21 |
+
input=text,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Save audio output
|
| 25 |
+
with open(speech_file_path, "wb") as f:
|
| 26 |
+
f.write(response.read())
|
| 27 |
+
|
| 28 |
+
# Embed and play in Streamlit
|
| 29 |
+
with open(speech_file_path, "rb") as f:
|
| 30 |
+
b64_audio = base64.b64encode(f.read()).decode()
|
| 31 |
+
audio_html = f"""
|
| 32 |
+
<audio autoplay>
|
| 33 |
+
<source src="data:audio/wav;base64,{b64_audio}" type="audio/wav">
|
| 34 |
+
</audio>
|
| 35 |
+
"""
|
| 36 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|