Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,13 @@ from TTS.api import TTS
|
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# ------------------ Configuration ------------------
|
| 7 |
YOURTTS_MODEL = "tts_models/multilingual/multi-dataset/your_tts"
|
| 8 |
FIXED_SPEAKER_PATH = "speakers/voice2.wav"
|
| 9 |
-
|
| 10 |
-
# Initialize model once
|
| 11 |
tts = TTS(YOURTTS_MODEL, gpu=False)
|
| 12 |
|
| 13 |
-
# ------------------ Core Synthesis Function ------------------
|
| 14 |
def synthesize(text):
|
| 15 |
output_path = "output.wav"
|
| 16 |
start_time = time.time()
|
| 17 |
-
|
| 18 |
if not os.path.exists(FIXED_SPEAKER_PATH):
|
| 19 |
return None, {"error": f"❌ Speaker file not found: {FIXED_SPEAKER_PATH}"}
|
| 20 |
|
|
@@ -31,7 +26,6 @@ def synthesize(text):
|
|
| 31 |
total_time = time.time() - start_time
|
| 32 |
est_duration = len(text.split()) / 2.5
|
| 33 |
rtf = round(total_time / est_duration, 3)
|
| 34 |
-
|
| 35 |
return output_path, {
|
| 36 |
"language": "English",
|
| 37 |
"processing_time_sec": round(total_time, 3),
|
|
@@ -40,28 +34,15 @@ def synthesize(text):
|
|
| 40 |
"speaker_used": os.path.basename(FIXED_SPEAKER_PATH)
|
| 41 |
}
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
output_audio = gr.Audio(label="Output Audio", type="filepath")
|
| 56 |
-
metadata_json = gr.JSON(label="Meta Info (Time, Model, RTF, etc.)")
|
| 57 |
-
|
| 58 |
-
# This adds both UI and API under the same app
|
| 59 |
-
generate_btn.click(
|
| 60 |
-
fn=synthesize,
|
| 61 |
-
inputs=[input_text],
|
| 62 |
-
outputs=[output_audio, metadata_json],
|
| 63 |
-
api_name="/predict" # <- this exposes the API endpoint
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# ------------------ Launch ------------------
|
| 67 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=True)
|
|
|
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 6 |
YOURTTS_MODEL = "tts_models/multilingual/multi-dataset/your_tts"
|
| 7 |
FIXED_SPEAKER_PATH = "speakers/voice2.wav"
|
|
|
|
|
|
|
| 8 |
tts = TTS(YOURTTS_MODEL, gpu=False)
|
| 9 |
|
|
|
|
| 10 |
def synthesize(text):
|
| 11 |
output_path = "output.wav"
|
| 12 |
start_time = time.time()
|
|
|
|
| 13 |
if not os.path.exists(FIXED_SPEAKER_PATH):
|
| 14 |
return None, {"error": f"❌ Speaker file not found: {FIXED_SPEAKER_PATH}"}
|
| 15 |
|
|
|
|
| 26 |
total_time = time.time() - start_time
|
| 27 |
est_duration = len(text.split()) / 2.5
|
| 28 |
rtf = round(total_time / est_duration, 3)
|
|
|
|
| 29 |
return output_path, {
|
| 30 |
"language": "English",
|
| 31 |
"processing_time_sec": round(total_time, 3),
|
|
|
|
| 34 |
"speaker_used": os.path.basename(FIXED_SPEAKER_PATH)
|
| 35 |
}
|
| 36 |
|
| 37 |
+
# ✅ Define both the UI and API on the same Interface object
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=synthesize,
|
| 40 |
+
inputs=[gr.Textbox(label="Text")],
|
| 41 |
+
outputs=[gr.Audio(type="filepath"), gr.JSON()],
|
| 42 |
+
title="YourTTS Voice Cloning (English Only, Fixed Speaker)",
|
| 43 |
+
allow_flagging="never"
|
| 44 |
+
)
|
| 45 |
+
demo.api_name = "/predict" # ✅ explicit API name registration
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|