Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,41 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import os
|
| 3 |
-
import io
|
| 4 |
-
import tempfile
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
from langdetect import detect, LangDetectException
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Initialize ElevenLabs client
|
| 10 |
-
# Make sure to set ELEVEN_API_KEY in your environment or as a Gradio secret
|
| 11 |
-
client = ElevenLabs(api_key=os.getenv("ELEVEN_API_KEY"))
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
Create a safe filename from text.
|
| 16 |
-
"""
|
| 17 |
-
cleaned_text = re.sub(r"[^\w\s]", "", text)
|
| 18 |
-
words = cleaned_text.split()
|
| 19 |
-
if not words:
|
| 20 |
-
return "output.mp3"
|
| 21 |
-
first_n_words = words[:max_words]
|
| 22 |
-
return "-".join(first_n_words).lower() + ".mp3"
|
| 23 |
|
| 24 |
-
def
|
| 25 |
if not text.strip():
|
| 26 |
return None
|
| 27 |
|
| 28 |
-
#
|
| 29 |
try:
|
| 30 |
lang = detect(text)
|
| 31 |
except LangDetectException:
|
| 32 |
lang = "en"
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Generate audio with ElevenLabs
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
mp3_bytes.write(chunk)
|
| 46 |
-
mp3_bytes.seek(0)
|
| 47 |
-
|
| 48 |
-
# Save to temp file with custom name
|
| 49 |
-
tmp_dir = tempfile.mkdtemp()
|
| 50 |
-
out_filename = filename_from_text(text)
|
| 51 |
-
file_path = os.path.join(tmp_dir, out_filename)
|
| 52 |
-
with open(file_path, "wb") as f:
|
| 53 |
-
f.write(mp3_bytes.getbuffer())
|
| 54 |
-
|
| 55 |
-
# Return filepath for Gradio to play/download
|
| 56 |
-
return file_path, file_path
|
| 57 |
-
|
| 58 |
-
# Gradio interface
|
| 59 |
-
with gr.Blocks(title="🗣️ Text → ElevenLabs TTS with Auto Language Detection") as demo:
|
| 60 |
-
gr.Markdown("## Enter text and generate speech (MP3) with ElevenLabs TTS")
|
| 61 |
-
|
| 62 |
-
text_input = gr.Textbox(label="Enter text", lines=3, placeholder="Type something…")
|
| 63 |
generate_btn = gr.Button("🔊 Generate", variant="primary")
|
| 64 |
audio_output = gr.Audio(label="Generated Audio", type="filepath")
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
generate_btn.click(
|
| 68 |
-
generate_tts,
|
| 69 |
-
inputs=text_input,
|
| 70 |
-
outputs=[audio_output, download_output],
|
| 71 |
-
show_progress="minimal"
|
| 72 |
-
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from elevenlabs import generate, save
|
| 3 |
from langdetect import detect, LangDetectException
|
| 4 |
+
import tempfile
|
| 5 |
+
import datetime
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Your ElevenLabs API key (replace with your own)
|
| 8 |
+
ELEVENLABS_API_KEY = "YOUR_API_KEY"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def text_to_speech(text: str):
|
| 11 |
if not text.strip():
|
| 12 |
return None
|
| 13 |
|
| 14 |
+
# Auto language detection
|
| 15 |
try:
|
| 16 |
lang = detect(text)
|
| 17 |
except LangDetectException:
|
| 18 |
lang = "en"
|
| 19 |
|
| 20 |
+
# Generate a timestamped MP3 filename
|
| 21 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 22 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f"_{lang}_{timestamp}.mp3")
|
| 23 |
+
|
| 24 |
# Generate audio with ElevenLabs
|
| 25 |
+
audio_data = generate(text=text, voice="alloy", model="eleven_multilingual_v1", api_key=ELEVENLABS_API_KEY)
|
| 26 |
+
|
| 27 |
+
# Save to file
|
| 28 |
+
save(audio_data, tmp_file.name)
|
| 29 |
+
return tmp_file.name
|
| 30 |
+
|
| 31 |
+
with gr.Blocks(title="🗣️ Text → Neural Voice (ElevenLabs)") as demo:
|
| 32 |
+
gr.Markdown("## 🗣️ Text → Neural Voice with Auto Language Detection")
|
| 33 |
+
|
| 34 |
+
text_input = gr.Textbox(label="Enter text", lines=3, placeholder="Type anything…")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
generate_btn = gr.Button("🔊 Generate", variant="primary")
|
| 36 |
audio_output = gr.Audio(label="Generated Audio", type="filepath")
|
| 37 |
+
|
| 38 |
+
generate_btn.click(text_to_speech, inputs=text_input, outputs=audio_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
demo.launch()
|