Tirath5504 commited on
Commit
534596f
1 Parent(s): 1a02b21

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +63 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import torch
4
+ import transformers
5
+ from packaging.version import parse
6
+ import sys
7
+ import io
8
+ import importlib.metadata as importlib_metadata
9
+ import soundfile as sf
10
+ import importlib.metadata as importlib_metadata
11
+
12
+ loading_kwargs = {}
13
+ if parse(importlib_metadata.version("transformers")) >= parse("4.40.0"):
14
+ loading_kwargs["attn_implementation"] = "eager"
15
+
16
+ def generate(prompt):
17
+ model = transformers.MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small", torchscript=True, return_dict=False, **loading_kwargs)
18
+ sample_length = 8
19
+ n_tokens = sample_length * model.config.audio_encoder.frame_rate + 3
20
+ sampling_rate = model.config.audio_encoder.sampling_rate
21
+ processor = transformers.AutoProcessor.from_pretrained("facebook/musicgen-small")
22
+ inputs = processor(
23
+ text=[
24
+ prompt,
25
+ ],
26
+ padding=True,
27
+ return_tensors="pt",
28
+ )
29
+ audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=n_tokens)
30
+ waveform = audio_values[0].cpu().squeeze() * 2**15
31
+ audio_buffer = io.BytesIO()
32
+ sf.write(audio_buffer, waveform.numpy().astype(np.int16), sampling_rate, format='WAV')
33
+ audio_buffer.seek(0)
34
+ return audio_buffer
35
+
36
+ st.title("Music Generator")
37
+
38
+ text_prompt = st.text_input("Text Prompt", "")
39
+
40
+ examples = [
41
+ "80s pop track with bassy drums and synth",
42
+ "Earthy tones, environmentally conscious, ukulele-infused, harmonic, breezy, easygoing, organic instrumentation, gentle grooves",
43
+ "90s rock song with loud guitars and heavy drums",
44
+ "Heartful EDM with beautiful synths and chords",
45
+ ]
46
+
47
+ st.sidebar.title("Examples")
48
+ for example in examples:
49
+ if st.sidebar.button(example):
50
+ text_prompt = example
51
+ st.experimental_rerun()
52
+
53
+ if st.button("Generate Audio"):
54
+ if text_prompt:
55
+ with st.spinner("Generating audio..."):
56
+ audio_output = generate(text_prompt)
57
+ st.audio(audio_output, format='audio/wav')
58
+ else:
59
+ st.warning("Please enter a text prompt.")
60
+
61
+ # Debugging
62
+ if st.checkbox("Show debug info"):
63
+ st.write("Text Prompt:", text_prompt)
requirements.txt ADDED
Binary file (3.57 kB). View file