Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from transformers import AutoTokenizer, AutoModelForTextToWaveform
|
|
| 4 |
import torch
|
| 5 |
from scipy.io.wavfile import write
|
| 6 |
import numpy as np
|
| 7 |
-
import uuid
|
| 8 |
import io
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
@@ -12,7 +11,7 @@ app = FastAPI()
|
|
| 12 |
# Load model and tokenizer
|
| 13 |
model_name = "facebook/musicgen-medium"
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
-
model = AutoModelForTextToWaveform.from_pretrained(model_name)
|
| 16 |
|
| 17 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
model.to(device)
|
|
@@ -30,9 +29,12 @@ def generate_music(request: MusicRequest):
|
|
| 30 |
sampling_rate = model.config.sampling_rate
|
| 31 |
audio_values = audio_values.cpu().numpy().squeeze()
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
# Convert audio to bytes
|
| 34 |
audio_bytes = io.BytesIO()
|
| 35 |
-
write(audio_bytes, sampling_rate,
|
| 36 |
audio_bytes.seek(0)
|
| 37 |
|
| 38 |
return Response(content=audio_bytes.read(), media_type="audio/wav", headers={"Content-Disposition": "attachment; filename=generated_music.wav"})
|
|
|
|
| 4 |
import torch
|
| 5 |
from scipy.io.wavfile import write
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 11 |
# Load model and tokenizer
|
| 12 |
model_name = "facebook/musicgen-medium"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
+
model = AutoModelForTextToWaveform.from_pretrained(model_name, attn_implementation="eager")
|
| 15 |
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
model.to(device)
|
|
|
|
| 29 |
sampling_rate = model.config.sampling_rate
|
| 30 |
audio_values = audio_values.cpu().numpy().squeeze()
|
| 31 |
|
| 32 |
+
# Normalize audio values to fit int16 range
|
| 33 |
+
audio_values = np.clip(audio_values * 32767, -32768, 32767).astype(np.int16)
|
| 34 |
+
|
| 35 |
# Convert audio to bytes
|
| 36 |
audio_bytes = io.BytesIO()
|
| 37 |
+
write(audio_bytes, sampling_rate, audio_values)
|
| 38 |
audio_bytes.seek(0)
|
| 39 |
|
| 40 |
return Response(content=audio_bytes.read(), media_type="audio/wav", headers={"Content-Disposition": "attachment; filename=generated_music.wav"})
|