File size: 1,891 Bytes
f41d3a4
c05c224
0bcf9d7
c041cb9
88a9a72
 
4f47fc6
 
c05c224
a1e273e
 
55d6b51
2e94bce
 
f41d3a4
c041cb9
10d5654
f86b0c1
c041cb9
774ebda
f41d3a4
774ebda
c041cb9
 
 
 
 
774ebda
c041cb9
 
 
 
f41d3a4
c041cb9
 
 
 
f6fc20c
c041cb9
f41d3a4
 
 
 
774ebda
10d5654
f41d3a4
 
 
c041cb9
 
 
f41d3a4
 
c041cb9
f41d3a4
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
import os
import torch
import gradio as gr
from TTS.tts.configs.xtts_config import XttsConfig  
from TTS.api import TTS

# βœ… Accept Coqui License Automatically
os.environ["COQUI_TOS_AGREED"] = "1"

# βœ… Allow `XttsConfig` in PyTorch's safe globals
torch.serialization.add_safe_globals([XttsConfig])

# βœ… Initialize XTTS Model (CPU-only)
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False).to("cpu")

print("βœ… Model loaded successfully!")
print(f"βœ… Supported Languages: {tts.languages}")  # βœ… FIXED

# βœ… Function for Voice Cloning
def generate_cloned_voice(text, reference_audio, language):
    output_path = "output.wav"

    if not text.strip():
        return "Error: Please enter some text.", None

    if not reference_audio:
        return "Error: Please upload a reference audio file.", None

    try:
        print("πŸ”„ Processing voice cloning...")
        tts.tts_to_file(text=text, speaker_wav=reference_audio, file_path=output_path, language=language)
        print("βœ… Voice cloning complete!")

        return output_path
    except Exception as e:
        print(f"❌ Error: {str(e)}")
        return f"Error: {str(e)}", None

# βœ… Create the Gradio Interface
interface = gr.Interface(
    fn=generate_cloned_voice,
    inputs=[
        gr.Textbox(label="Enter Translated Text"),
        gr.Audio(label="Upload Reference Audio", type="filepath"),
        gr.Dropdown(tts.languages, label="Select Target Language", value="en")  # βœ… FIXED
    ],
    outputs=gr.Audio(label="Generated Cloned Voice"),
    title="Free Voice Cloning API",
    description="Upload a sample voice and input text. Select a language, and the system will generate the text in the same voice.",
    allow_flagging="never",
    concurrency_limit=1  # Prevents multiple processes from running at once
)

# βœ… Launch the Gradio App
interface.launch()