mattricesound commited on
Commit
3d13b69
β€’
1 Parent(s): 9b5e466

Init commit

Browse files
backend.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template
2
+ from flask_socketio import SocketIO, emit
3
+ # from flask_cors import CORS
4
+ from audiocraft.models import musicgen
5
+ import torchaudio
6
+ import soundfile as sf
7
+
8
+
9
+ # app = Flask(__name__)
10
+ app = Flask(__name__, static_folder="../build", static_url_path="/")
11
+ app.debug = True
12
+ app.secret_key = "random secret key!"
13
+ # CORS(app)
14
+ # cors = CORS(app, resource={r"/*": {"origins": "*"}})
15
+ socketio = SocketIO(app, cors_allowed_origins="*")
16
+ print("Loading model...")
17
+ model = musicgen.MusicGen.get_pretrained("melody")
18
+ model.set_generation_params(duration=8)
19
+
20
+ @app.route("/")
21
+ def index():
22
+ print("HI")
23
+ return render_template("index.html")
24
+
25
+
26
+ @socketio.on("connect")
27
+ def connect():
28
+ print("Client connected")
29
+ stream_audio()
30
+
31
+
32
+ @socketio.on("disconnect")
33
+ def disconnect():
34
+ print("Client disconnected")
35
+
36
+ def stream_audio(data):
37
+ descriptions = ["Film score epic moment"]
38
+ melody, sr = torchaudio.load("./asitwas_vocals.wav")
39
+ print("Running inference...")
40
+ wav = model.generate_with_chroma(descriptions, melody[None].expand(1, -1, -1), sr)
41
+ model_sampling_rate = 32000
42
+ sf.write("output.wav", wav[0].numpy().T, model_sampling_rate)
43
+ chunk_size = 1024
44
+ for i in range(0, len(wav[0]), chunk_size):
45
+ chunk = wav[0][i : i + chunk_size]# * 500
46
+ emit("audio_chunk", chunk.tolist())
47
+
48
+
49
+ if __name__ == "__main__":
50
+ socketio.run(app)
inference.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from audiocraft.models import musicgen
2
+ import torchaudio
3
+ import soundfile as sf
4
+
5
+ print("Loading model...")
6
+ # model = musicgen.MusicGen.get_pretrained("melody")
7
+ model = musicgen.MusicGen.get_pretrained("small")
8
+ model.set_generation_params(duration=8)
9
+
10
+ descriptions = ["Film score epic moment"]
11
+ melody, sr = torchaudio.load("./asitwas_vocals.wav")
12
+ print("Running inference...")
13
+ # wav = model.generate_with_chroma(descriptions, melody[None].expand(1, -1, -1), sr)
14
+ wav = model.generate(descriptions) # generates 3 samples.
15
+ model_sampling_rate = 32000
16
+ sf.write("output.wav", wav[0].numpy().T, model_sampling_rate)
MusicGen_Melody_Demo.ipynb β†’ notebooks/MusicGen_Melody_Demo.ipynb RENAMED
File without changes
output.wav ADDED
Binary file (512 kB). View file
 
setup.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from setuptools import setup, find_packages
3
+
4
+ NAME = "soundsauce"
5
+ DESCRIPTION = ""
6
+ URL = ""
7
+ EMAIL = ""
8
+ AUTHOR = ""
9
+ REQUIRES_PYTHON = ">=3.9.0"
10
+ VERSION = "0.0.1"
11
+
12
+ HERE = Path(__file__).parent
13
+
14
+ try:
15
+ with open(HERE / "README.md", encoding="utf-8") as f:
16
+ long_description = "\n" + f.read()
17
+ except FileNotFoundError:
18
+ long_description = DESCRIPTION
19
+
20
+ setup(
21
+ name=NAME,
22
+ version=VERSION,
23
+ description=DESCRIPTION,
24
+ long_description=long_description,
25
+ long_description_content_type="text/markdown",
26
+ author=AUTHOR,
27
+ author_email=EMAIL,
28
+ python_requires=REQUIRES_PYTHON,
29
+ url=URL,
30
+ packages=find_packages(),
31
+ install_requires=[
32
+ "torch>=2.0",
33
+ "torchaudio>=2.0.1",
34
+ "soundfile",
35
+ "flask",
36
+ "flask-socketio",
37
+ "audiocraft@git+https://github.com/facebookresearch/audiocraft"
38
+ ],
39
+ include_package_data=True,
40
+ )