Ava2lon commited on
Commit
8416358
·
verified ·
1 Parent(s): 8043dc1

Delete generate.py

Browse files
Files changed (1) hide show
  1. generate.py +0 -39
generate.py DELETED
@@ -1,39 +0,0 @@
1
- import torch
2
- from transformers import AutoProcessor, MusicgenForConditionalGeneration
3
- import soundfile as sf
4
- import uuid
5
-
6
- MODEL_NAME = "facebook/musicgen-small"
7
-
8
- print("Loading MusicGen model...")
9
-
10
- processor = AutoProcessor.from_pretrained(MODEL_NAME)
11
- model = MusicgenForConditionalGeneration.from_pretrained(MODEL_NAME)
12
-
13
- device = "cuda" if torch.cuda.is_available() else "cpu"
14
- model.to(device)
15
-
16
- def generate_music(prompt, duration):
17
-
18
- inputs = processor(
19
- text=[prompt],
20
- padding=True,
21
- return_tensors="pt"
22
- ).to(device)
23
-
24
- audio_values = model.generate(
25
- **inputs,
26
- max_new_tokens=int(duration * 50)
27
- )
28
-
29
- filename = f"/tmp/{uuid.uuid4()}.wav"
30
-
31
- sampling_rate = model.config.audio_encoder.sampling_rate
32
-
33
- sf.write(
34
- filename,
35
- audio_values[0, 0].cpu().numpy(),
36
- sampling_rate
37
- )
38
-
39
- return filename