File size: 1,442 Bytes
f3c0815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from transformers import TTSConfig, TTSForConditionalGeneration
import numpy as np
import soundfile as sf

# Load your model and configuration
def load_model(model_path, config_path):
    config = TTSConfig.from_json_file(config_path)
    model = TTSForConditionalGeneration.from_pretrained(model_path, config=config)
    model.eval()
    return model

# Define the path to your model and config
MODEL_PATH = 'path/to/best_model.pth'
CONFIG_PATH = 'path/to/config.json'

model = load_model(MODEL_PATH, CONFIG_PATH)

# Define the function to generate speech
def generate_speech(text):
    # Convert text to input format
    inputs = tokenizer(text, return_tensors="pt")
    
    with torch.no_grad():
        # Generate speech
        outputs = model.generate(inputs['input_ids'])
    
    # Convert outputs to numpy array (audio waveform)
    # This conversion depends on your model
    audio_waveform = outputs.squeeze().numpy()

    # Save the waveform to a temporary file
    temp_file = 'temp.wav'
    sf.write(temp_file, audio_waveform, 22050)  # Adjust sample rate if necessary
    
    return temp_file

# Define Gradio interface
interface = gr.Interface(
    fn=generate_speech,
    inputs="text",
    outputs="audio",
    title="Text-to-Speech Model",
    description="Generate speech from text using your TTS model."
)

# Launch the Gradio interface
if __name__ == "__main__":
    interface.launch()