Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import TTSConfig, TTSForConditionalGeneration
|
4 |
+
import numpy as np
|
5 |
+
import soundfile as sf
|
6 |
+
|
7 |
+
# Load your model and configuration
|
8 |
+
def load_model(model_path, config_path):
|
9 |
+
config = TTSConfig.from_json_file(config_path)
|
10 |
+
model = TTSForConditionalGeneration.from_pretrained(model_path, config=config)
|
11 |
+
model.eval()
|
12 |
+
return model
|
13 |
+
|
14 |
+
# Define the path to your model and config
|
15 |
+
MODEL_PATH = 'path/to/best_model.pth'
|
16 |
+
CONFIG_PATH = 'path/to/config.json'
|
17 |
+
|
18 |
+
model = load_model(MODEL_PATH, CONFIG_PATH)
|
19 |
+
|
20 |
+
# Define the function to generate speech
|
21 |
+
def generate_speech(text):
|
22 |
+
# Convert text to input format
|
23 |
+
inputs = tokenizer(text, return_tensors="pt")
|
24 |
+
|
25 |
+
with torch.no_grad():
|
26 |
+
# Generate speech
|
27 |
+
outputs = model.generate(inputs['input_ids'])
|
28 |
+
|
29 |
+
# Convert outputs to numpy array (audio waveform)
|
30 |
+
# This conversion depends on your model
|
31 |
+
audio_waveform = outputs.squeeze().numpy()
|
32 |
+
|
33 |
+
# Save the waveform to a temporary file
|
34 |
+
temp_file = 'temp.wav'
|
35 |
+
sf.write(temp_file, audio_waveform, 22050) # Adjust sample rate if necessary
|
36 |
+
|
37 |
+
return temp_file
|
38 |
+
|
39 |
+
# Define Gradio interface
|
40 |
+
interface = gr.Interface(
|
41 |
+
fn=generate_speech,
|
42 |
+
inputs="text",
|
43 |
+
outputs="audio",
|
44 |
+
title="Text-to-Speech Model",
|
45 |
+
description="Generate speech from text using your TTS model."
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the Gradio interface
|
49 |
+
if __name__ == "__main__":
|
50 |
+
interface.launch()
|