-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (115 loc) · 4.79 KB
/
app.py
File metadata and controls
140 lines (115 loc) · 4.79 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import streamlit as st
import gtts
import tempfile
import os
import time
import hashlib
# Configure page
st.set_page_config(
page_title="Text to Speech",
page_icon="🎙️",
layout="centered"
)
# Languages for gTTS
LANGUAGES = {
"English": "en",
"Spanish": "es",
"French": "fr",
"German": "de",
"Italian": "it",
"Portuguese": "pt",
"Japanese": "ja",
"Korean": "ko",
"Chinese": "zh",
"Russian": "ru",
"Arabic": "ar",
"Hindi": "hi"
}
# Initialize session state for caching
if 'cached_audio' not in st.session_state:
st.session_state.cached_audio = {}
if 'last_request_time' not in st.session_state:
st.session_state.last_request_time = 0
if 'request_count' not in st.session_state:
st.session_state.request_count = 0
def get_text_hash(text, language):
"""Generate hash for caching"""
return hashlib.md5(f"{text}_{language}".encode()).hexdigest()
def can_make_request():
"""Check if we can make a request (rate limiting)"""
current_time = time.time()
# Reset counter every hour
if current_time - st.session_state.last_request_time > 3600:
st.session_state.request_count = 0
st.session_state.last_request_time = current_time
# Allow max 10 requests per hour (conservative limit)
return st.session_state.request_count < 10
# Title
st.title("🎙️ Text to Speech")
# Rate limit info
col1, col2 = st.columns([3, 1])
with col1:
language = st.selectbox("Language:", list(LANGUAGES.keys()))
with col2:
remaining = max(0, 10 - st.session_state.request_count)
st.metric("Requests left", remaining)
# Text input
text = st.text_area("Enter text:", height=200, placeholder="Type or paste your text here...")
# Auto-generate when text is entered (with caching and rate limiting)
if text.strip():
text_hash = get_text_hash(text, language)
# Check if we have cached audio for this text+language combo
if text_hash in st.session_state.cached_audio:
st.audio(st.session_state.cached_audio[text_hash], format='audio/mp3')
# Download button
st.download_button(
label="📥 Download as MP3",
data=st.session_state.cached_audio[text_hash],
file_name="speech.mp3",
mime="audio/mp3"
)
st.success("✅ Using cached audio (no API call needed)")
elif can_make_request():
try:
with st.spinner("Generating audio..."):
# Add small delay to be respectful to the API
time.sleep(1)
tts = gtts.gTTS(text=text, lang=LANGUAGES[language], slow=False)
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file:
tts.save(tmp_file.name)
with open(tmp_file.name, 'rb') as f:
audio_bytes = f.read()
# Cache the results
st.session_state.cached_audio[text_hash] = audio_bytes
st.session_state.request_count += 1
os.unlink(tmp_file.name)
st.audio(audio_bytes, format='audio/mp3')
# Download button
st.download_button(
label="📥 Download as MP3",
data=audio_bytes,
file_name="speech.mp3",
mime="audio/mp3"
)
st.success(f"✅ Audio generated! ({st.session_state.request_count}/10 requests used)")
except Exception as e:
error_msg = str(e)
if "429" in error_msg or "Too Many Requests" in error_msg:
st.error("⚠️ Rate limit reached. Please wait a few minutes before trying again.")
st.info("💡 Try using shorter text or wait for the limit to reset.")
elif "403" in error_msg:
st.error("🚫 Access forbidden. The service might be temporarily unavailable.")
else:
st.error(f"❌ Error: {error_msg}")
else:
st.warning("⏳ Rate limit reached (10 requests/hour). Please wait before making more requests.")
st.info("💡 Previously generated audio is still available from cache above!")
# Still show cached audio if available
if text_hash in st.session_state.cached_audio:
st.audio(st.session_state.cached_audio[text_hash], format='audio/mp3')
st.download_button(
label="📥 Download as MP3",
data=st.session_state.cached_audio[text_hash],
file_name="speech.mp3",
mime="audio/mp3"
)