Spaces:
Running
Running
File size: 1,272 Bytes
5e2592a b2f4955 3bb59f9 b43e4d4 3bb59f9 b2f4955 c6869ea a1340c4 f99d148 ed02bf5 f99d148 ed02bf5 b2f4955 3bb59f9 b2f4955 3bb59f9 b2f4955 3bb59f9 |
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 |
import gradio as gr
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
import torch
import soundfile as sf
model_name = "facebook/musicgen-small"
config = AutoConfig.from_pretrained(model_name)
if not hasattr(config, 'dropout'):
config.dropout = 0.1
if not hasattr(config, 'layerdrop'):
config.layerdrop = 0.1
if not hasattr(config, 'max_position_embeddings'):
config.max_position_embeddings = 2048
if not hasattr(config, 'num_attention_heads'):
config.num_attention_heads = 16
if not hasattr(config, 'num_hidden_layers'):
config.num_hidden_layers = 24
if not hasattr(config, 'num_hidden_layers'):
config.num_hidden_layers = 24
if not hasattr(config, 'hidden_size'):
config.hidden_size = 1024
model = AutoModelForCausalLM.from_pretrained(model_name, config=config)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def text_to_audio(prompt):
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
with torch.no_grad():
output = model.generate(input_ids)
audio_data = output[0].cpu().numpy()
audio_file = "generated_audio.wav"
sf.write(audio_file, audio_data, 22050)
return audio_file
gr.Interface(fn=text_to_audio, inputs="text", outputs="audio").launch() |