Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ from transformers import MusicgenForConditionalGeneration, AutoProcessor
|
|
7 |
from scipy.io import wavfile
|
8 |
import ffmpeg
|
9 |
|
10 |
-
# Function to generate video frames
|
11 |
def generate_video(image, prompt, negative_prompt, video_length):
|
12 |
generator = torch.manual_seed(8888)
|
13 |
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
@@ -34,18 +33,18 @@ def generate_video(image, prompt, negative_prompt, video_length):
|
|
34 |
|
35 |
return frames
|
36 |
|
37 |
-
# Function to export frames to video
|
38 |
def export_frames_to_video(frames, output_file):
|
39 |
frames_np = [np.array(frame) for frame in frames]
|
40 |
clip = ImageSequenceClip(frames_np, fps=30)
|
41 |
clip.write_videofile(output_file, codec='libx264', audio=False)
|
42 |
|
43 |
-
# Function to generate music
|
44 |
def generate_music(prompt, unconditional=False):
|
45 |
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
46 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
47 |
model.to(device)
|
48 |
|
|
|
|
|
49 |
if unconditional:
|
50 |
unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
|
51 |
audio_values = model.generate(**unconditional_inputs, do_sample=True, max_new_tokens=256)
|
@@ -56,12 +55,14 @@ def generate_music(prompt, unconditional=False):
|
|
56 |
padding=True,
|
57 |
return_tensors="pt",
|
58 |
)
|
59 |
-
|
|
|
|
|
|
|
60 |
|
61 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
62 |
return audio_values[0].cpu().numpy(), sampling_rate
|
63 |
|
64 |
-
# Function to combine audio and video
|
65 |
def combine_audio_video(audio_file, video_file, output_file):
|
66 |
audio = ffmpeg.input(audio_file)
|
67 |
video = ffmpeg.input(video_file)
|
@@ -89,8 +90,8 @@ if st.sidebar.button("Generate Video and Music"):
|
|
89 |
|
90 |
# Video generation with progress bar
|
91 |
st.write("Generating video...")
|
92 |
-
|
93 |
-
export_frames_to_video(
|
94 |
st.video("output_video.mp4")
|
95 |
|
96 |
# Music generation with progress bar
|
|
|
7 |
from scipy.io import wavfile
|
8 |
import ffmpeg
|
9 |
|
|
|
10 |
def generate_video(image, prompt, negative_prompt, video_length):
|
11 |
generator = torch.manual_seed(8888)
|
12 |
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
|
|
33 |
|
34 |
return frames
|
35 |
|
|
|
36 |
def export_frames_to_video(frames, output_file):
|
37 |
frames_np = [np.array(frame) for frame in frames]
|
38 |
clip = ImageSequenceClip(frames_np, fps=30)
|
39 |
clip.write_videofile(output_file, codec='libx264', audio=False)
|
40 |
|
|
|
41 |
def generate_music(prompt, unconditional=False):
|
42 |
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
43 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
44 |
model.to(device)
|
45 |
|
46 |
+
# Simulate progress for music generation
|
47 |
+
st.progress(0) # Initialize progress bar
|
48 |
if unconditional:
|
49 |
unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
|
50 |
audio_values = model.generate(**unconditional_inputs, do_sample=True, max_new_tokens=256)
|
|
|
55 |
padding=True,
|
56 |
return_tensors="pt",
|
57 |
)
|
58 |
+
# Simulate progress by updating the progress bar
|
59 |
+
for i in range(1, 6): # Assuming 5 steps for demonstration
|
60 |
+
audio_values = model.generate(**inputs.to(device), do_sample=True, guidance_scale=3, max_new_tokens=256)
|
61 |
+
st.progress(i / 5) # Update progress bar
|
62 |
|
63 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
64 |
return audio_values[0].cpu().numpy(), sampling_rate
|
65 |
|
|
|
66 |
def combine_audio_video(audio_file, video_file, output_file):
|
67 |
audio = ffmpeg.input(audio_file)
|
68 |
video = ffmpeg.input(video_file)
|
|
|
90 |
|
91 |
# Video generation with progress bar
|
92 |
st.write("Generating video...")
|
93 |
+
video_frames = generate_video(image, prompt, negative_prompt, video_length)
|
94 |
+
export_frames_to_video(video_frames, "output_video.mp4")
|
95 |
st.video("output_video.mp4")
|
96 |
|
97 |
# Music generation with progress bar
|