Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -83,30 +83,39 @@ def generate_voice(script: str, speaker: str = "default"):
|
|
83 |
return f"Error generating voice-over: {e}"
|
84 |
|
85 |
# ---------------------------------------------------------------------
|
86 |
-
# Music Generation Function
|
87 |
# ---------------------------------------------------------------------
|
88 |
@spaces.GPU(duration=300)
|
89 |
def generate_music(prompt: str, audio_length: int):
|
90 |
try:
|
91 |
-
|
92 |
-
|
|
|
93 |
|
|
|
94 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
95 |
musicgen_model.to(device)
|
96 |
|
|
|
97 |
inputs = musicgen_processor(text=[prompt], padding=True, return_tensors="pt").to(device)
|
|
|
|
|
98 |
outputs = musicgen_model.generate(**inputs, max_new_tokens=audio_length)
|
99 |
|
|
|
100 |
audio_data = outputs[0, 0].cpu().numpy()
|
101 |
normalized_audio = (audio_data / max(abs(audio_data)) * 32767).astype("int16")
|
102 |
|
103 |
-
|
|
|
104 |
write(output_path, 44100, normalized_audio)
|
105 |
|
106 |
return output_path
|
|
|
107 |
except Exception as e:
|
108 |
return f"Error generating music: {e}"
|
109 |
|
|
|
110 |
# ---------------------------------------------------------------------
|
111 |
# Audio Blending Function with Ducking
|
112 |
# ---------------------------------------------------------------------
|
|
|
83 |
return f"Error generating voice-over: {e}"
|
84 |
|
85 |
# ---------------------------------------------------------------------
|
86 |
+
# Music Generation Function (using facebook/musicgen-medium)
|
87 |
# ---------------------------------------------------------------------
|
88 |
@spaces.GPU(duration=300)
|
89 |
def generate_music(prompt: str, audio_length: int):
|
90 |
try:
|
91 |
+
# Load facebook/musicgen-medium model
|
92 |
+
musicgen_model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-medium")
|
93 |
+
musicgen_processor = AutoProcessor.from_pretrained("facebook/musicgen-medium")
|
94 |
|
95 |
+
# Move the model to the appropriate device (CUDA or CPU)
|
96 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
97 |
musicgen_model.to(device)
|
98 |
|
99 |
+
# Prepare inputs
|
100 |
inputs = musicgen_processor(text=[prompt], padding=True, return_tensors="pt").to(device)
|
101 |
+
|
102 |
+
# Generate music
|
103 |
outputs = musicgen_model.generate(**inputs, max_new_tokens=audio_length)
|
104 |
|
105 |
+
# Process audio data
|
106 |
audio_data = outputs[0, 0].cpu().numpy()
|
107 |
normalized_audio = (audio_data / max(abs(audio_data)) * 32767).astype("int16")
|
108 |
|
109 |
+
# Save generated music to a file
|
110 |
+
output_path = f"{tempfile.gettempdir()}/musicgen_medium_generated_music.wav"
|
111 |
write(output_path, 44100, normalized_audio)
|
112 |
|
113 |
return output_path
|
114 |
+
|
115 |
except Exception as e:
|
116 |
return f"Error generating music: {e}"
|
117 |
|
118 |
+
|
119 |
# ---------------------------------------------------------------------
|
120 |
# Audio Blending Function with Ducking
|
121 |
# ---------------------------------------------------------------------
|