osanseviero commited on
Commit
7b8d71f
·
1 Parent(s): 64e127f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -1,7 +1,10 @@
1
- import gradio as gr
2
  import numpy as np
3
- import torch
4
  import random
 
 
 
 
 
5
 
6
  from transformers import AutoProcessor, MusicgenForConditionalGeneration
7
 
@@ -11,7 +14,7 @@ COLORS = [
11
  ["#0000ff", "#ff0000"],
12
  ]
13
 
14
- path = "facebook/musicgen-large"
15
  processor = AutoProcessor.from_pretrained(path)
16
  model = MusicgenForConditionalGeneration.from_pretrained(path, torch_dtype=torch.float16).to("cuda")
17
 
@@ -25,7 +28,12 @@ def predict(text):
25
  with torch.autocast("cuda"):
26
  outputs = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=512)
27
 
28
- return (32000, outputs[0][0].cpu().numpy().astype(np.float16)), gr.make_waveform((32000, outputs[0].cpu().numpy().astype(np.float16).ravel()), bars_color=random.choice(COLORS), bar_count=75)
 
 
 
 
 
29
 
30
 
31
  title = "MusicGen"
 
 
1
  import numpy as np
 
2
  import random
3
+ import subprocess
4
+ import tempfile
5
+ import torch
6
+
7
+ import gradio as gr
8
 
9
  from transformers import AutoProcessor, MusicgenForConditionalGeneration
10
 
 
14
  ["#0000ff", "#ff0000"],
15
  ]
16
 
17
+ path = "facebook/musicgen-small"
18
  processor = AutoProcessor.from_pretrained(path)
19
  model = MusicgenForConditionalGeneration.from_pretrained(path, torch_dtype=torch.float16).to("cuda")
20
 
 
28
  with torch.autocast("cuda"):
29
  outputs = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=512)
30
 
31
+ with tempfile.NamedTemporaryFile() as tmp:
32
+ video_path = gr.make_waveform((32000, outputs[0].cpu().numpy().astype(np.float16).ravel()), bars_color=random.choice(COLORS), bar_count=75)
33
+ command = f'ffmpeg -y -i {video_path} -vf "scale=250:150" {tmp.name}'
34
+ subprocess.run(command.split()])
35
+
36
+ return (32000, outputs[0][0].cpu().numpy().astype(np.float16)), tmp.name
37
 
38
 
39
  title = "MusicGen"