Spaces:
Build error
Build error
Ahmadkhan12
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -10,8 +10,8 @@ import logging
|
|
10 |
from scipy.io.wavfile import write as write_wav
|
11 |
from scipy import signal
|
12 |
from moviepy.editor import VideoFileClip, AudioFileClip
|
13 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
14 |
import requests
|
|
|
15 |
|
16 |
# Set up logging for better debug tracking
|
17 |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
|
@@ -47,15 +47,8 @@ with open("categories_places365.txt", "r") as f:
|
|
47 |
# Load AudioGen Medium and MusicGen Medium models
|
48 |
try:
|
49 |
logging.info("Loading AudioGen Medium and MusicGen Medium models...")
|
50 |
-
|
51 |
-
|
52 |
-
musicgen_processor = AutoProcessor.from_pretrained("facebook/musicgen-medium")
|
53 |
-
musicgen_model = AutoModelForCausalLM.from_pretrained("facebook/musicgen-medium")
|
54 |
-
|
55 |
-
# Move models to GPU if available
|
56 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
57 |
-
audiogen_model.to(device)
|
58 |
-
musicgen_model.to(device)
|
59 |
logging.info("AudioGen Medium and MusicGen Medium models loaded successfully.")
|
60 |
except Exception as e:
|
61 |
logging.error(f"Error loading AudioGen/MusicGen models: {e}")
|
@@ -111,16 +104,11 @@ def analyze_video(video_path):
|
|
111 |
def generate_audio_audiogen(scene, duration=10):
|
112 |
try:
|
113 |
logging.info(f"Generating audio for scene: {scene} using AudioGen Medium...")
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
return_tensors="pt",
|
118 |
-
).to(audiogen_model.device) # Move inputs to the same device as the model
|
119 |
-
with torch.no_grad():
|
120 |
-
audio = audiogen_model.generate(**inputs, max_new_tokens=duration * 50) # Adjust tokens for duration
|
121 |
-
audio = audio.cpu().numpy().squeeze()
|
122 |
audio_path = "generated_audio_audiogen.wav"
|
123 |
-
|
124 |
logging.info(f"Audio generated and saved to: {audio_path}")
|
125 |
return audio_path
|
126 |
except Exception as e:
|
@@ -131,16 +119,11 @@ def generate_audio_audiogen(scene, duration=10):
|
|
131 |
def generate_music_musicgen(scene, duration=10):
|
132 |
try:
|
133 |
logging.info(f"Generating music for scene: {scene} using MusicGen Medium...")
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
return_tensors="pt",
|
138 |
-
).to(musicgen_model.device) # Move inputs to the same device as the model
|
139 |
-
with torch.no_grad():
|
140 |
-
music = musicgen_model.generate(**inputs, max_new_tokens=duration * 50) # Adjust tokens for duration
|
141 |
-
music = music.cpu().numpy().squeeze()
|
142 |
music_path = "generated_music_musicgen.wav"
|
143 |
-
|
144 |
logging.info(f"Music generated and saved to: {music_path}")
|
145 |
return music_path
|
146 |
except Exception as e:
|
|
|
10 |
from scipy.io.wavfile import write as write_wav
|
11 |
from scipy import signal
|
12 |
from moviepy.editor import VideoFileClip, AudioFileClip
|
|
|
13 |
import requests
|
14 |
+
from audiocraft.models import AudioGen, MusicGen # Add this line
|
15 |
|
16 |
# Set up logging for better debug tracking
|
17 |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
47 |
# Load AudioGen Medium and MusicGen Medium models
|
48 |
try:
|
49 |
logging.info("Loading AudioGen Medium and MusicGen Medium models...")
|
50 |
+
audiogen_model = AudioGen.get_pretrained("facebook/audiogen-medium")
|
51 |
+
musicgen_model = MusicGen.get_pretrained("facebook/musicgen-medium")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
logging.info("AudioGen Medium and MusicGen Medium models loaded successfully.")
|
53 |
except Exception as e:
|
54 |
logging.error(f"Error loading AudioGen/MusicGen models: {e}")
|
|
|
104 |
def generate_audio_audiogen(scene, duration=10):
|
105 |
try:
|
106 |
logging.info(f"Generating audio for scene: {scene} using AudioGen Medium...")
|
107 |
+
audiogen_model.set_generation_params(duration=duration)
|
108 |
+
descriptions = [f"Ambient sounds of {scene}"]
|
109 |
+
wav = audiogen_model.generate(descriptions) # Generate audio
|
|
|
|
|
|
|
|
|
|
|
110 |
audio_path = "generated_audio_audiogen.wav"
|
111 |
+
sf.write(audio_path, wav.squeeze().cpu().numpy(), 32000) # Save as WAV file
|
112 |
logging.info(f"Audio generated and saved to: {audio_path}")
|
113 |
return audio_path
|
114 |
except Exception as e:
|
|
|
119 |
def generate_music_musicgen(scene, duration=10):
|
120 |
try:
|
121 |
logging.info(f"Generating music for scene: {scene} using MusicGen Medium...")
|
122 |
+
musicgen_model.set_generation_params(duration=duration)
|
123 |
+
descriptions = [f"Calm music for {scene}"]
|
124 |
+
wav = musicgen_model.generate(descriptions) # Generate music
|
|
|
|
|
|
|
|
|
|
|
125 |
music_path = "generated_music_musicgen.wav"
|
126 |
+
sf.write(music_path, wav.squeeze().cpu().numpy(), 32000) # Save as WAV file
|
127 |
logging.info(f"Music generated and saved to: {music_path}")
|
128 |
return music_path
|
129 |
except Exception as e:
|