osanseviero commited on
Commit
9847153
·
1 Parent(s): 08bd4c4

Update app.py

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