Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,128 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import scipy.io.wavfile
|
| 6 |
+
from utils.hparams import create_hparams
|
| 7 |
+
from train import load_model
|
| 8 |
+
from synthesis import generate_speech
|
| 9 |
+
from text import text_to_sequence
|
| 10 |
|
| 11 |
+
# Path configurations
|
| 12 |
+
MODEL_DIR = "trained_model"
|
| 13 |
+
MODEL_PATH = os.path.join(MODEL_DIR, "checkpoint_latest.pth.tar")
|
| 14 |
+
CONFIG_PATH = os.path.join(MODEL_DIR, "hparams.yml")
|
| 15 |
+
OUTPUT_PATH = "output.wav"
|
| 16 |
|
| 17 |
+
# Download model if it doesn't exist
|
| 18 |
+
def download_model():
|
| 19 |
+
if not os.path.exists(MODEL_DIR):
|
| 20 |
+
os.makedirs(MODEL_DIR)
|
| 21 |
+
|
| 22 |
+
if not os.path.exists(MODEL_PATH):
|
| 23 |
+
print("Downloading model...")
|
| 24 |
+
# Add model download code here
|
| 25 |
+
# For example:
|
| 26 |
+
# !wget -O MODEL_PATH https://path/to/model
|
| 27 |
+
raise Exception("You need to download the model checkpoint file and place it in trained_model/checkpoint_latest.pth.tar")
|
| 28 |
+
|
| 29 |
+
if not os.path.exists(CONFIG_PATH):
|
| 30 |
+
print("Downloading config...")
|
| 31 |
+
# Add config download code here
|
| 32 |
+
# For example:
|
| 33 |
+
# !wget -O CONFIG_PATH https://path/to/config
|
| 34 |
+
raise Exception("You need to download the hparams.yml file and place it in trained_model/hparams.yml")
|
| 35 |
|
| 36 |
+
# Initialize model
|
| 37 |
+
def init_model():
|
| 38 |
+
try:
|
| 39 |
+
download_model()
|
| 40 |
+
|
| 41 |
+
hparams = create_hparams(CONFIG_PATH)
|
| 42 |
+
model = load_model(hparams)
|
| 43 |
+
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device('cpu'))['state_dict'])
|
| 44 |
+
model.eval()
|
| 45 |
+
|
| 46 |
+
return model, hparams
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error initializing model: {str(e)}")
|
| 49 |
+
return None, None
|
| 50 |
|
| 51 |
+
# Generate speech
|
| 52 |
+
def synthesize(text, model, hparams):
|
| 53 |
+
try:
|
| 54 |
+
sequence = np.array(text_to_sequence(text, ['burmese_cleaners']))[None, :]
|
| 55 |
+
sequence = torch.autograd.Variable(torch.from_numpy(sequence)).cpu().long()
|
| 56 |
+
|
| 57 |
+
mel_outputs, mel_outputs_postnet, _, alignments = model.inference(sequence)
|
| 58 |
+
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
waveform = generate_speech(mel_outputs_postnet, hparams)
|
| 61 |
+
|
| 62 |
+
scipy.io.wavfile.write(OUTPUT_PATH, hparams.sampling_rate, waveform)
|
| 63 |
+
|
| 64 |
+
return OUTPUT_PATH, None
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return None, str(e)
|
| 67 |
|
| 68 |
+
# Gradio interface
|
| 69 |
+
def tts_interface(text):
|
| 70 |
+
if not text.strip():
|
| 71 |
+
return None, "Please enter some text."
|
| 72 |
+
|
| 73 |
+
global MODEL, HPARAMS
|
| 74 |
+
if MODEL is None or HPARAMS is None:
|
| 75 |
+
MODEL, HPARAMS = init_model()
|
| 76 |
+
|
| 77 |
+
if MODEL is None:
|
| 78 |
+
return None, "Model could not be initialized. Please check logs."
|
| 79 |
+
|
| 80 |
+
audio_path, error = synthesize(text, MODEL, HPARAMS)
|
| 81 |
+
|
| 82 |
+
if error:
|
| 83 |
+
return None, f"Error generating speech: {error}"
|
| 84 |
+
|
| 85 |
+
return audio_path, "Speech generated successfully!"
|
| 86 |
|
| 87 |
+
# Initialize global model variables
|
| 88 |
+
MODEL, HPARAMS = None, None
|
| 89 |
|
| 90 |
+
# Create Gradio interface
|
| 91 |
+
demo = gr.Interface(
|
| 92 |
+
fn=tts_interface,
|
| 93 |
+
inputs=[
|
| 94 |
+
gr.Textbox(
|
| 95 |
+
lines=3,
|
| 96 |
+
placeholder="Enter Burmese text here...",
|
| 97 |
+
label="Text"
|
| 98 |
+
)
|
| 99 |
+
],
|
| 100 |
+
outputs=[
|
| 101 |
+
gr.Audio(label="Generated Speech"),
|
| 102 |
+
gr.Textbox(label="Status")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
],
|
| 104 |
+
title="Myanmar (Burmese) Text-to-Speech",
|
| 105 |
+
description="""
|
| 106 |
+
This is a demo of the Myanmar Text-to-Speech system developed by hpbyte.
|
| 107 |
+
Enter Burmese text in the box below and click 'Submit' to generate speech.
|
| 108 |
+
|
| 109 |
+
GitHub Repository: https://github.com/hpbyte/myanmar-tts
|
| 110 |
+
""",
|
| 111 |
+
allow_flagging="never",
|
| 112 |
+
examples=[
|
| 113 |
+
["မင်္ဂလာပါ"],
|
| 114 |
+
["မြန်မာစကားပြောစနစ်ကို ကြိုဆိုပါတယ်"],
|
| 115 |
+
["ဒီစနစ်ဟာ မြန်မာစာကို အသံအဖြစ် ပြောင်းပေးနိုင်ပါတယ်"],
|
| 116 |
+
]
|
| 117 |
)
|
| 118 |
|
| 119 |
+
# Initialize model at startup
|
| 120 |
+
try:
|
| 121 |
+
MODEL, HPARAMS = init_model()
|
| 122 |
+
print("Model initialized successfully!")
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"Error initializing model: {str(e)}")
|
| 125 |
|
| 126 |
+
# Launch the app
|
| 127 |
if __name__ == "__main__":
|
| 128 |
+
demo.launch()
|