Spaces:
Running
Running
File size: 2,826 Bytes
88c3dda 1159f20 88c3dda 1159f20 88c3dda 1159f20 b0f6a83 1159f20 1bcaf02 1159f20 b0f6a83 1159f20 577ed7a b0f6a83 1bcaf02 1159f20 b0f6a83 1159f20 b0f6a83 577ed7a 1bcaf02 da134c2 1159f20 1bcaf02 1159f20 577ed7a b0f6a83 1159f20 8d595ea 577ed7a b0f6a83 1159f20 da134c2 667085e 1bcaf02 8d595ea 1bcaf02 1159f20 667085e 1159f20 667085e 1159f20 667085e b0f6a83 88c3dda 1159f20 1bcaf02 f68a155 1159f20 88c3dda 1159f20 88c3dda 1159f20 4ed7f20 |
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 |
import gradio as gr
from gtts import gTTS
import tempfile
import os
# Define a simple dictionary of available languages for gTTS
# We can't use the massive list from tts_voice.py because gTTS uses simple language codes.
gtts_languages = {
"English": "en",
"Mandarin Chinese": "zh-cn",
"French": "fr",
"German": "de",
"Spanish": "es",
"Japanese": "ja",
"Korean": "ko",
"Russian": "ru"
}
# The main function is now SYNCHRONOUS (no 'async')
def text_to_speech_gtts(text, language_name, slow_speed): # ADDED slow_speed
# 1. Input Validation
if not text or not text.strip():
return "ERROR: Input text cannot be empty.", None
try:
# Get the language code from the name
lang_code = gtts_languages[language_name]
# Create a temporary file path
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
tmp_path = tmp_file.name
# Initialize gTTS object - USE slow_speed from input
tts = gTTS(text=text, lang=lang_code, slow=slow_speed)
# Save the audio file (synchronous operation) [cite: 4]
tts.save(tmp_path)
return "Speech synthesis complete: {}".format(text), tmp_path
except Exception as e:
# Handle all gTTS-related errors (e.g., language not supported, network failure)
return (f"ERROR: Failed to generate audio. Details: {str(e)}", None)
# --- Modern Gradio Component Syntax (Translated UI) ---
input_text = gr.Textbox(lines=5, label="Input Text")
speech_speed = gr.Radio(
choices=[("Normal", False), ("Slow", True)], # Custom labels and corresponding boolean values
value=False, # Default to Normal speed
label="Speech Speed"
)
output_text = gr.Textbox(label="Output Text")
output_audio = gr.Audio(
type="filepath",
label="Generated Audio File",
interactive=True # The file will still be available for download without 'live=False'
)
default_language = "English" # Use English as the default language
language = gr.Dropdown(
choices=list(gtts_languages.keys()),
value=default_language,
label="Language" # Dropped 'Voice' because gTTS has no specific voice selection
)
# --- Gradio Interface Definition ---
interface = gr.Interface(
fn=text_to_speech_gtts, # Use the new synchronous function
inputs=[input_text, language, speech_speed], # ADDED speech_speed
outputs=[output_text, output_audio],
title="Google TTS Text-to-Speech (Simple Version)",
description="Convert text into audio using the reliable Google Text-to-Speech service. (Max 100 characters for optimal stability)"
)
# --- Standard Synchronous Launch Command ---
if __name__ == "__main__":
# Gradio runs synchronous functions reliably without extra configuration.
interface.launch() |