Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,68 @@
|
|
| 1 |
-
# TTS Space: app.py
|
| 2 |
-
# This
|
| 3 |
import gradio as gr
|
| 4 |
-
from TTS.api import TTS
|
| 5 |
import torch
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
print(f"TTS Service: Using device: {device}")
|
| 15 |
|
| 16 |
-
# Load the powerful XTTS-v2 model into memory.
|
| 17 |
-
# This happens only once when the Space starts.
|
| 18 |
print("TTS Service: Loading model...")
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# This is the core function that will be exposed as an API.
|
| 23 |
def synthesize(text_to_speak, speaker_wav_path):
|
| 24 |
-
# Use the default voice if the provided path doesn't exist.
|
| 25 |
if not os.path.exists(speaker_wav_path):
|
| 26 |
print(f"Warning: Speaker file not found at '{speaker_wav_path}'. Using default.")
|
| 27 |
speaker_wav_path = DEFAULT_SPEAKER_WAV
|
| 28 |
|
| 29 |
-
# Check if the default voice exists
|
| 30 |
if not os.path.exists(speaker_wav_path):
|
| 31 |
raise gr.Error("The default 'tutor_voice.wav' file is missing! Please upload it.")
|
| 32 |
|
| 33 |
print(f"TTS Service: Synthesizing text: '{text_to_speak[:30]}...'")
|
| 34 |
-
|
| 35 |
-
# Define where to save the output audio
|
| 36 |
output_wav_path = "output.wav"
|
| 37 |
|
| 38 |
-
# Use the TTS model to generate speech and save it to a file.
|
| 39 |
tts.tts_to_file(
|
| 40 |
text=text_to_speak,
|
| 41 |
file_path=output_wav_path,
|
| 42 |
speaker_wav=speaker_wav_path,
|
| 43 |
-
language="en"
|
| 44 |
)
|
| 45 |
|
| 46 |
print(f"TTS Service: Audio saved to '{output_wav_path}'")
|
| 47 |
-
# The function returns the path to the generated audio file.
|
| 48 |
return output_wav_path
|
| 49 |
|
| 50 |
-
# Create a simple Gradio interface.
|
| 51 |
-
# This is useful for testing but our primary use is the API.
|
| 52 |
with gr.Blocks() as app:
|
| 53 |
-
gr.Markdown("# EveryPrep XII - TTS Voice Service")
|
| 54 |
-
gr.Markdown("This service generates audio from text using a cloned voice. It is primarily used via API.")
|
| 55 |
-
|
| 56 |
-
text_input = gr.Textbox(label="Text to Synthesize", value="Hello! I am your personal AI tutor.")
|
| 57 |
-
# Use our uploaded voice file as the default speaker.
|
| 58 |
-
speaker_input = gr.File(label="Speaker WAV (Optional)", value=DEFAULT_SPEAKER_WAV)
|
| 59 |
|
| 60 |
-
# This is the crucial part that creates the API endpoint.
|
| 61 |
-
# When our Replit app calls the API, it's this function that runs.
|
| 62 |
gr.Interface(
|
| 63 |
fn=synthesize,
|
| 64 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 65 |
outputs=gr.Audio(label="Synthesized Audio"),
|
| 66 |
title="TTS API Test Interface",
|
| 67 |
-
api_name="synthesize"
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# Launch the application.
|
| 71 |
app.launch()
|
|
|
|
| 1 |
+
# FINAL TTS Space: app.py
|
| 2 |
+
# This version includes the fix for the MeCab/unidic dependency issue.
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# --- FIX for MeCab/unidic START ---
|
| 8 |
+
# This command downloads the necessary Japanese dictionary for the TTS library.
|
| 9 |
+
# It runs only once when the Space builds.
|
| 10 |
+
print("Fix: Triggering unidic download...")
|
| 11 |
+
os.system('python -m unidic download')
|
| 12 |
+
print("Fix: Unidic download command executed.")
|
| 13 |
+
# --- FIX for MeCab/unidic END ---
|
| 14 |
+
|
| 15 |
+
from TTS.api import TTS
|
| 16 |
|
| 17 |
+
# --- Standard Application Code ---
|
| 18 |
+
|
| 19 |
+
DEFAULT_SPEAKER_WAV = "tutor_voice.wav"
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
print(f"TTS Service: Using device: {device}")
|
| 22 |
|
|
|
|
|
|
|
| 23 |
print("TTS Service: Loading model...")
|
| 24 |
+
try:
|
| 25 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
| 26 |
+
print("TTS Service: Model loaded successfully.")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"FATAL: Could not load TTS model. Error: {e}")
|
| 29 |
+
# If the model fails to load, we can't do anything else.
|
| 30 |
+
# This will cause the app to crash, and the logs will show the error.
|
| 31 |
+
raise e
|
| 32 |
|
|
|
|
| 33 |
def synthesize(text_to_speak, speaker_wav_path):
|
|
|
|
| 34 |
if not os.path.exists(speaker_wav_path):
|
| 35 |
print(f"Warning: Speaker file not found at '{speaker_wav_path}'. Using default.")
|
| 36 |
speaker_wav_path = DEFAULT_SPEAKER_WAV
|
| 37 |
|
|
|
|
| 38 |
if not os.path.exists(speaker_wav_path):
|
| 39 |
raise gr.Error("The default 'tutor_voice.wav' file is missing! Please upload it.")
|
| 40 |
|
| 41 |
print(f"TTS Service: Synthesizing text: '{text_to_speak[:30]}...'")
|
|
|
|
|
|
|
| 42 |
output_wav_path = "output.wav"
|
| 43 |
|
|
|
|
| 44 |
tts.tts_to_file(
|
| 45 |
text=text_to_speak,
|
| 46 |
file_path=output_wav_path,
|
| 47 |
speaker_wav=speaker_wav_path,
|
| 48 |
+
language="en"
|
| 49 |
)
|
| 50 |
|
| 51 |
print(f"TTS Service: Audio saved to '{output_wav_path}'")
|
|
|
|
| 52 |
return output_wav_path
|
| 53 |
|
|
|
|
|
|
|
| 54 |
with gr.Blocks() as app:
|
| 55 |
+
gr.Markdown("# EveryPrep XII - TTS Voice Service (v2 - Fixed)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
| 57 |
gr.Interface(
|
| 58 |
fn=synthesize,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Textbox(label="Text to Synthesize", value="This is a test of the fixed TTS service."),
|
| 61 |
+
gr.File(label="Speaker WAV (Optional)", value=DEFAULT_SPEAKER_WAV)
|
| 62 |
+
],
|
| 63 |
outputs=gr.Audio(label="Synthesized Audio"),
|
| 64 |
title="TTS API Test Interface",
|
| 65 |
+
api_name="synthesize"
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
app.launch()
|