BenjaminPittsley commited on
Commit
71558a8
·
1 Parent(s): 271fdc2

Add app.py and requirements

Browse files
Files changed (2) hide show
  1. app.py +64 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from TTS.api import TTS
3
+ import os
4
+
5
+ # Initialize TTS (XTTS-v2)
6
+ # This will download the model on the first run
7
+ # We use gpu=True if available, otherwise False
8
+ use_gpu = True
9
+ try:
10
+ import torch
11
+ if not torch.cuda.is_available():
12
+ use_gpu = False
13
+ except:
14
+ use_gpu = False
15
+
16
+ print(f"Initializing TTS with GPU={use_gpu}...")
17
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=use_gpu)
18
+
19
+ # Output folder
20
+ os.makedirs("output", exist_ok=True)
21
+
22
+ def generate_speech(text, voice_id):
23
+ """
24
+ Generates speech from text using a specific voice clone.
25
+ voice_id: The name of the wav file in 'voices/' folder to clone.
26
+ """
27
+ output_path = "output/output.wav"
28
+
29
+ # Map voice_id to a sample file
30
+ # You must upload these files to your Space's 'voices/' folder
31
+ speaker_wav = f"voices/{voice_id}.wav"
32
+
33
+ if not os.path.exists(speaker_wav):
34
+ # Fallback to a default voice if specific one missing, or error out
35
+ # For now, let's list available voices to help debug
36
+ available = os.listdir("voices") if os.path.exists("voices") else []
37
+ return None, f"Error: Voice ID '{voice_id}' not found. Available: {available}"
38
+
39
+ # Generate
40
+ tts.tts_to_file(
41
+ text=text,
42
+ file_path=output_path,
43
+ speaker_wav=speaker_wav,
44
+ language="en"
45
+ )
46
+
47
+ return output_path, "Success"
48
+
49
+ # Define Gradio Interface
50
+ iface = gr.Interface(
51
+ fn=generate_speech,
52
+ inputs=[
53
+ gr.Textbox(label="Text"),
54
+ gr.Textbox(label="Voice ID (e.g. 'narrator', 'aribeth')")
55
+ ],
56
+ outputs=[
57
+ gr.Audio(label="Generated Audio", type="filepath"),
58
+ gr.Textbox(label="Status")
59
+ ],
60
+ title="Unreal Engine TTS Backend (XTTS-v2)",
61
+ description="API for DGG_001 Game. Send POST requests to /run/predict"
62
+ )
63
+
64
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ TTS
2
+ torch
3
+ torchaudio