-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (69 loc) · 2.92 KB
/
app.py
File metadata and controls
83 lines (69 loc) · 2.92 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import os
import requests
def get_murf_api_key():
try:
return st.secrets["MURF_API_KEY"]
except:
api_key = os.getenv("MURF_API_KEY")
if not api_key:
st.error("❌ API key not found. Add to `.streamlit/secrets.toml` or set `MURF_API_KEY` environment variable.")
st.stop()
return api_key
def init_murf_client():
try:
from murf import Murf
return Murf(api_key=get_murf_api_key())
except ImportError:
st.error("❌ Murf SDK not installed. Run `pip install murf`")
st.stop()
def main():
st.set_page_config(page_title="Murf TTS Demo", page_icon="🎙️")
st.title("🎙️ Murf Text-to-Speech Demo")
client = init_murf_client()
text = st.text_area("Enter text to speak:", height=150)
voice_options = {
"Natalie (👩, en-US)": "en-US-natalie",
"Miles (👨, en-US)": "en-US-miles"
}
voice_name = st.radio("Choose a voice:", list(voice_options.keys()))
voice_id = voice_options[voice_name]
if st.button("🎧 Generate Audio"):
if not text.strip():
st.error("❌ Please enter some text.")
else:
with st.spinner("Generating audio…"):
try:
# Generate speech
response = client.text_to_speech.generate(
text=text,
voice_id=voice_id,
format="MP3",
sample_rate=24000.0
)
# ✅ Correct attribute: audio_file is the downloadable MP3 URL
audio_url = response.audio_file
if not audio_url:
st.error("❌ No audio URL returned.")
return
# Download the audio file
audio_response = requests.get(audio_url)
if audio_response.status_code != 200:
st.error("❌ Failed to download audio.")
return
audio_bytes = audio_response.content
# Save to file
fname = "speech.mp3"
with open(fname, "wb") as f:
f.write(audio_bytes)
st.success("✅ Audio generated!")
st.audio(fname, format="audio/mp3")
st.download_button("⬇️ Download MP3", data=audio_bytes, file_name="speech.mp3", mime="audio/mp3")
except Exception as e:
error_msg = str(e)
if "insufficient credits" in error_msg.lower():
st.error("❌ Insufficient API credits.")
else:
st.error(f"❌ Failed to generate audio: {e}")
if __name__ == "__main__":
main()