Yjhhh commited on
Commit
04736e7
·
verified ·
1 Parent(s): 00ae6be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
app.py CHANGED
@@ -4,7 +4,7 @@ import torchaudio
4
  from audiocraft.models import MusicGen
5
  from audiocraft.data.audio import audio_write
6
  import logging
7
- import os
8
  import uuid
9
  from torch.cuda.amp import autocast
10
  import torch
@@ -16,10 +16,16 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
16
 
17
  logging.info("Carregando o modelo pré-treinado.")
18
  model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
19
- model.set_generation_params(duration=500)
20
 
21
  @spaces.GPU(duration=120)
22
- def generate_music(description, melody_audio):
 
 
 
 
 
 
23
  with autocast():
24
  logging.info("Iniciando a geração de música.")
25
  if description:
@@ -39,7 +45,6 @@ def generate_music(description, melody_audio):
39
  logging.info(f"Salvando a música gerada com o nome: {filename}")
40
  path = audio_write(filename, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
41
  print("Música salva em", path, ".")
42
- # Verifica a forma do tensor de áudio e se foi salvo corretamente
43
  logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
44
  logging.info("Música gerada e salva com sucesso.")
45
  if not os.path.exists(path):
@@ -47,30 +52,27 @@ def generate_music(description, melody_audio):
47
 
48
  return path
49
 
 
 
 
 
 
 
 
50
  # Define a interface Gradio
51
- description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
52
- melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
53
- output_path = gr.Audio(label="Generated Music", type="filepath")
 
54
 
55
  gr.Interface(
56
  fn=generate_music,
57
- inputs=[description, melody_audio],
58
  outputs=output_path,
59
  title="MusicGen Demo",
60
- description="Generate music using the MusicGen model by Nateraw.\n\n"
61
- "Model: musicgen-songstarter-v0.2\n"
62
- "Download the model [here](https://huggingface.co/nateraw/musicgen-songstarter-v0.2).\n\n"
63
- "musicgen-songstarter-v0.2 is a musicgen-stereo-melody-large fine-tuned on a dataset of melody loops from Nateraw's Splice sample library. "
64
- "It's intended to be used to generate song ideas that are useful for music producers. It generates stereo audio in 32khz.\n\n"
65
- "Compared to musicgen-songstarter-v0.1, this new version:\n"
66
- "- Was trained on 3x more unique, manually-curated samples that Nateraw painstakingly purchased on Splice\n"
67
- "- Is twice the size, bumped up from size medium ➡️ large transformer LM\n\n"
68
- "If you find this model interesting, please consider:\n"
69
- "- Following Nateraw on [GitHub](https://github.com/nateraw)\n"
70
- "- Following Nateraw on [Twitter](https://twitter.com/nateraw)\n\n"
71
- "Space created by [artificialguybr](https://twitter.com/artificialguybr) on Twitter.",
72
  examples=[
73
- ["trap, synthesizer, songstarters, dark, G# minor, 140 bpm", "./assets/kalhonaho.mp3"],
74
- ["upbeat, electronic, synth, dance, 120 bpm", None]
75
  ]
76
  ).launch()
 
4
  from audiocraft.models import MusicGen
5
  from audiocraft.data.audio import audio_write
6
  import logging
7
+ import os
8
  import uuid
9
  from torch.cuda.amp import autocast
10
  import torch
 
16
 
17
  logging.info("Carregando o modelo pré-treinado.")
18
  model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
19
+
20
 
21
  @spaces.GPU(duration=120)
22
+ def generate_music(description, melody_audio, duration):
23
+ # Remove spam da descrição
24
+ description = clean_text(description)
25
+
26
+ # Define a duração da música em segundos
27
+ model.set_generation_params(duration=int(duration * 1000))
28
+
29
  with autocast():
30
  logging.info("Iniciando a geração de música.")
31
  if description:
 
45
  logging.info(f"Salvando a música gerada com o nome: {filename}")
46
  path = audio_write(filename, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
47
  print("Música salva em", path, ".")
 
48
  logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
49
  logging.info("Música gerada e salva com sucesso.")
50
  if not os.path.exists(path):
 
52
 
53
  return path
54
 
55
+ def clean_text(text):
56
+ """Remove links e caracteres especiais de um texto."""
57
+ import re
58
+ text = re.sub(r'http\S+', '', text) # remove links
59
+ text = re.sub(r'[^a-zA-Z0-9\s]', '', text) # remove caracteres especiais
60
+ return text
61
+
62
  # Define a interface Gradio
63
+ description = gr.Textbox(label="Descrição", placeholder="acústico, guitarra, melodia, trap, menor, 90 bpm")
64
+ melody_audio = gr.Audio(label="Melodia de Áudio (opcional)", type="filepath")
65
+ duration = gr.Number(label="Duração (segundos)", value=10)
66
+ output_path = gr.Audio(label="Música Gerada", type="filepath")
67
 
68
  gr.Interface(
69
  fn=generate_music,
70
+ inputs=[description, melody_audio, duration],
71
  outputs=output_path,
72
  title="MusicGen Demo",
73
+ description="Gere música usando o modelo MusicGen by Nateraw.",
 
 
 
 
 
 
 
 
 
 
 
74
  examples=[
75
+ ["trap, sintetizador, iniciantes de música, escuro, Sol# menor, 140 bpm", "./assets/kalhonaho.mp3", 20],
76
+ ["animado, eletrônico, sintetizador, dança, 120 bpm", None, 30]
77
  ]
78
  ).launch()