Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
import numpy as np
|
4 |
from diffusers import I2VGenXLPipeline
|
5 |
from transformers import MusicgenForConditionalGeneration, AutoProcessor
|
6 |
from PIL import Image
|
7 |
from moviepy.editor import ImageSequenceClip
|
|
|
8 |
import io
|
9 |
-
import ffmpeg
|
10 |
import scipy.io.wavfile
|
|
|
11 |
|
12 |
def generate_video(image, prompt, negative_prompt, video_length):
|
13 |
generator = torch.manual_seed(8888)
|
14 |
-
|
15 |
-
# Set the device to CPU or a non-NVIDIA GPU
|
16 |
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
17 |
-
print(f"Using device: {device}")
|
18 |
-
|
19 |
-
# Load the pipeline
|
20 |
pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float32)
|
21 |
-
pipeline.to(device)
|
22 |
|
23 |
-
# Generate frames with progress tracking
|
24 |
frames = []
|
25 |
total_frames = video_length * 30 # Assuming 30 frames per second
|
26 |
|
@@ -35,15 +29,11 @@ def generate_video(image, prompt, negative_prompt, video_length):
|
|
35 |
num_frames=1
|
36 |
).frames[0]
|
37 |
frames.append(np.array(frame))
|
|
|
38 |
|
39 |
-
# Update progress
|
40 |
-
yield (i + 1) / total_frames # Yield progress
|
41 |
-
|
42 |
-
# Create a video clip from the frames
|
43 |
output_file = "output_video.mp4"
|
44 |
-
clip = ImageSequenceClip(frames, fps=30)
|
45 |
clip.write_videofile(output_file, codec='libx264', audio=False)
|
46 |
-
|
47 |
return output_file
|
48 |
|
49 |
def generate_music(prompt, unconditional=False):
|
@@ -51,7 +41,6 @@ def generate_music(prompt, unconditional=False):
|
|
51 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
52 |
model.to(device)
|
53 |
|
54 |
-
# Generate music
|
55 |
if unconditional:
|
56 |
unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
|
57 |
audio_values = model.generate(**unconditional_inputs, do_sample=True, max_new_tokens=256)
|
@@ -66,9 +55,10 @@ def generate_music(prompt, unconditional=False):
|
|
66 |
|
67 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
68 |
audio_file = "musicgen_out.wav"
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
72 |
return audio_file
|
73 |
|
74 |
def combine_audio_video(audio_file, video_file):
|
@@ -79,26 +69,16 @@ def combine_audio_video(audio_file, video_file):
|
|
79 |
ffmpeg.run(output)
|
80 |
return output_file
|
81 |
|
82 |
-
# Gradio interface
|
83 |
def interface(image_path, prompt, negative_prompt, video_length, music_prompt, unconditional):
|
84 |
-
# Convert the uploaded image path to a PIL Image
|
85 |
image = Image.open(image_path)
|
86 |
-
|
87 |
-
# Generate video and track progress
|
88 |
video_file = generate_video(image, prompt, negative_prompt, video_length)
|
89 |
-
|
90 |
-
# Generate music
|
91 |
audio_file = generate_music(music_prompt, unconditional)
|
92 |
-
|
93 |
-
# Combine audio and video
|
94 |
combined_file = combine_audio_video(audio_file, video_file)
|
95 |
-
|
96 |
return combined_file
|
97 |
|
98 |
-
# Create Gradio Blocks
|
99 |
with gr.Blocks() as demo:
|
100 |
gr.Markdown("# AI-Powered Video and Music Generation")
|
101 |
-
|
102 |
with gr.Row():
|
103 |
image_input = gr.Image(type="filepath", label="Upload Image")
|
104 |
prompt_input = gr.Textbox(label="Enter the Video Prompt")
|
@@ -110,13 +90,11 @@ with gr.Blocks() as demo:
|
|
110 |
generate_button = gr.Button("Generate Video and Music")
|
111 |
output_video = gr.Video(label="Output Video with Sound")
|
112 |
|
113 |
-
# Define the button action
|
114 |
generate_button.click(
|
115 |
interface,
|
116 |
inputs=[image_input, prompt_input, negative_prompt_input, video_length_input, music_prompt_input, unconditional_checkbox],
|
117 |
outputs=output_video,
|
118 |
-
show_progress=True
|
119 |
)
|
120 |
|
121 |
-
# Launch the Gradio app
|
122 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
3 |
from diffusers import I2VGenXLPipeline
|
4 |
from transformers import MusicgenForConditionalGeneration, AutoProcessor
|
5 |
from PIL import Image
|
6 |
from moviepy.editor import ImageSequenceClip
|
7 |
+
import numpy as np
|
8 |
import io
|
|
|
9 |
import scipy.io.wavfile
|
10 |
+
import ffmpeg
|
11 |
|
12 |
def generate_video(image, prompt, negative_prompt, video_length):
|
13 |
generator = torch.manual_seed(8888)
|
|
|
|
|
14 |
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
|
|
|
|
|
|
15 |
pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float32)
|
16 |
+
pipeline.to(device)
|
17 |
|
|
|
18 |
frames = []
|
19 |
total_frames = video_length * 30 # Assuming 30 frames per second
|
20 |
|
|
|
29 |
num_frames=1
|
30 |
).frames[0]
|
31 |
frames.append(np.array(frame))
|
32 |
+
yield (i + 1) / total_frames # Update progress
|
33 |
|
|
|
|
|
|
|
|
|
34 |
output_file = "output_video.mp4"
|
35 |
+
clip = ImageSequenceClip(frames, fps=30)
|
36 |
clip.write_videofile(output_file, codec='libx264', audio=False)
|
|
|
37 |
return output_file
|
38 |
|
39 |
def generate_music(prompt, unconditional=False):
|
|
|
41 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
42 |
model.to(device)
|
43 |
|
|
|
44 |
if unconditional:
|
45 |
unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
|
46 |
audio_values = model.generate(**unconditional_inputs, do_sample=True, max_new_tokens=256)
|
|
|
55 |
|
56 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
57 |
audio_file = "musicgen_out.wav"
|
58 |
+
audio_data = audio_values[0].cpu().numpy()
|
59 |
+
audio_data = np.clip(audio_data, -1.0, 1.0)
|
60 |
+
audio_data = (audio_data * 32767).astype(np.int16)
|
61 |
+
scipy.io.wavfile.write(audio_file, sampling_rate, audio_data)
|
62 |
return audio_file
|
63 |
|
64 |
def combine_audio_video(audio_file, video_file):
|
|
|
69 |
ffmpeg.run(output)
|
70 |
return output_file
|
71 |
|
|
|
72 |
def interface(image_path, prompt, negative_prompt, video_length, music_prompt, unconditional):
|
|
|
73 |
image = Image.open(image_path)
|
|
|
|
|
74 |
video_file = generate_video(image, prompt, negative_prompt, video_length)
|
|
|
|
|
75 |
audio_file = generate_music(music_prompt, unconditional)
|
|
|
|
|
76 |
combined_file = combine_audio_video(audio_file, video_file)
|
|
|
77 |
return combined_file
|
78 |
|
|
|
79 |
with gr.Blocks() as demo:
|
80 |
gr.Markdown("# AI-Powered Video and Music Generation")
|
81 |
+
|
82 |
with gr.Row():
|
83 |
image_input = gr.Image(type="filepath", label="Upload Image")
|
84 |
prompt_input = gr.Textbox(label="Enter the Video Prompt")
|
|
|
90 |
generate_button = gr.Button("Generate Video and Music")
|
91 |
output_video = gr.Video(label="Output Video with Sound")
|
92 |
|
|
|
93 |
generate_button.click(
|
94 |
interface,
|
95 |
inputs=[image_input, prompt_input, negative_prompt_input, video_length_input, music_prompt_input, unconditional_checkbox],
|
96 |
outputs=output_video,
|
97 |
+
show_progress=True
|
98 |
)
|
99 |
|
|
|
100 |
demo.launch()
|