File size: 2,777 Bytes
b6fd3a8 78b0078 d4a2e16 78b0078 b6fd3a8 d4a2e16 b6fd3a8 d4a2e16 b6fd3a8 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import gradio as gr
import torch
import torchaudio
import os
import re
import subprocess
from transformers import AutoModelForCausalLM
from yarngpt_utils import AudioTokenizer
# Download model files if they don't exist
def download_if_not_exists(url, filename):
if not os.path.exists(filename):
print(f"Downloading {filename}...")
subprocess.run(["wget", url, "-O", filename])
print(f"Downloaded {filename}")
# Download necessary files
download_if_not_exists(
"https://huggingface.co/novateur/WavTokenizer-medium-speech-75token/resolve/main/wavtokenizer_mediumdata_frame75_3s_nq1_code4096_dim512_kmeans200_attn.yaml",
"wavtokenizer_config.yaml"
)
download_if_not_exists(
"https://huggingface.co/novateur/WavTokenizer-large-speech-75token/blob/main/wavtokenizer_large_speech_320_v2.ckpt",
"wavtokenizer_model.ckpt"
)
# Initialize the model (this runs when the app starts)
def initialize_model():
# Set paths
hf_path = "saheedniyi/YarnGPT"
wav_tokenizer_config_path = "wavtokenizer_config.yaml"
wav_tokenizer_model_path = "wavtokenizer_model.ckpt"
# Create AudioTokenizer
audio_tokenizer = AudioTokenizer(
hf_path, wav_tokenizer_model_path, wav_tokenizer_config_path
)
# Load model
model = AutoModelForCausalLM.from_pretrained(hf_path, torch_dtype="auto").to(audio_tokenizer.device)
return model, audio_tokenizer
# Generate audio from text
def generate_speech(text, speaker_name):
# Create prompt
prompt = audio_tokenizer.create_prompt(text, speaker_name)
# Tokenize prompt
input_ids = audio_tokenizer.tokenize_prompt(prompt)
# Generate output
output = model.generate(
input_ids=input_ids,
temperature=0.1,
repetition_penalty=1.1,
max_length=4000,
)
# Convert to audio codes
codes = audio_tokenizer.get_codes(output)
# Convert codes to audio
audio = audio_tokenizer.get_audio(codes)
# Save audio temporarily
temp_path = "output.wav"
torchaudio.save(temp_path, audio, sample_rate=24000)
return temp_path
# Load model globally
print("Loading model...")
model, audio_tokenizer = initialize_model()
print("Model loaded!")
# Create Gradio interface
speakers = ["idera", "emma", "jude", "osagie", "tayo", "zainab", "joke", "regina", "remi", "umar", "chinenye"]
demo = gr.Interface(
fn=generate_speech,
inputs=[
gr.Textbox(lines=5, placeholder="Enter text here..."),
gr.Dropdown(choices=speakers, label="Speaker", value="idera")
],
outputs=gr.Audio(type="filepath"),
title="YarnGPT: Nigerian Accented Text-to-Speech",
description="Generate natural-sounding Nigerian accented speech from text."
)
demo.launch() |