aach456 commited on
Commit
f6cb684
1 Parent(s): bafe453

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -7
app.py CHANGED
@@ -1,20 +1,26 @@
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,11 +35,15 @@ def generate_video(image, prompt, negative_prompt, video_length):
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,6 +51,7 @@ 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,10 +66,21 @@ def generate_music(prompt, unconditional=False):
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):
@@ -78,7 +100,7 @@ def interface(image_path, prompt, negative_prompt, video_length, music_prompt, u
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")
 
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 scipy.io.wavfile
10
  import ffmpeg
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) # Move the model to the selected device
22
 
23
+ # Generate frames with progress tracking
24
  frames = []
25
  total_frames = video_length * 30 # Assuming 30 frames per second
26
 
 
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) # Set the frames per second
45
  clip.write_videofile(output_file, codec='libx264', audio=False)
46
+
47
  return output_file
48
 
49
  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
 
67
  sampling_rate = model.config.audio_encoder.sampling_rate
68
  audio_file = "musicgen_out.wav"
69
+
70
+ # Ensure audio_values is 1D and scale if necessary
71
  audio_data = audio_values[0].cpu().numpy()
72
+
73
+ # Check if audio_data is in the correct format
74
+ if audio_data.ndim > 1:
75
+ audio_data = audio_data[0] # Take the first channel if stereo
76
+
77
+ # Scale audio data to 16-bit PCM format
78
+ audio_data = np.clip(audio_data, -1.0, 1.0) # Ensure values are in the range [-1, 1]
79
+ audio_data = (audio_data * 32767).astype(np.int16) # Scale to int16
80
+
81
+ # Save the generated audio
82
  scipy.io.wavfile.write(audio_file, sampling_rate, audio_data)
83
+
84
  return audio_file
85
 
86
  def combine_audio_video(audio_file, video_file):
 
100
 
101
  with gr.Blocks() as demo:
102
  gr.Markdown("# AI-Powered Video and Music Generation")
103
+
104
  with gr.Row():
105
  image_input = gr.Image(type="filepath", label="Upload Image")
106
  prompt_input = gr.Textbox(label="Enter the Video Prompt")