aach456 commited on
Commit
0ea9241
1 Parent(s): fb20e57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -90
app.py CHANGED
@@ -1,106 +1,61 @@
1
- import streamlit as st
2
- from PIL import Image
3
  import torch
4
  import numpy as np
5
- from moviepy.editor import ImageSequenceClip
6
  from transformers import MusicgenForConditionalGeneration, AutoProcessor
7
- from scipy.io import wavfile
8
- import ffmpeg
9
- from diffusers import I2VGenXLPipeline
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")
13
- print(f"Using device: {device}")
14
- pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float32)
15
- pipeline.to(device)
16
-
17
- frames = []
18
- total_frames = video_length * 20 # Assuming 20 frames per second
19
-
20
- # Generate frames with progress tracking
21
- for i in range(total_frames):
22
- frame = pipeline(
23
- prompt=prompt,
24
- image=image,
25
- num_inference_steps=2,
26
- negative_prompt=negative_prompt,
27
- guidance_scale=9.0,
28
- generator=generator,
29
- num_frames=1
30
- ).frames[0]
31
- frames.append(frame)
32
- st.progress((i + 1) / total_frames) # Update progress bar
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)
51
  else:
52
  processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
53
- inputs = processor(
54
- text=prompt,
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)
69
- output = ffmpeg.output(video, audio, output_file, vcodec='copy', acodec='aac')
70
- ffmpeg.run(output)
71
-
72
- # Streamlit UI
73
- st.title("AI-Powered Video and Music Generation")
74
-
75
- st.sidebar.title("Options")
76
-
77
- st.sidebar.subheader("Video Generation")
78
- image = st.sidebar.file_uploader("Upload an image", type=["jpg", "png"])
79
- prompt = st.sidebar.text_input("Enter the prompt")
80
- negative_prompt = st.sidebar.text_input("Enter the negative prompt")
81
- video_length = st.sidebar.number_input("Enter the video length (seconds)", min_value=1, value=10)
82
-
83
- st.sidebar.subheader("Music Generation")
84
- music_prompt = st.sidebar.text_input("Enter the music prompt")
85
- unconditional = st.sidebar.checkbox("Generate unconditional music")
86
-
87
- if st.sidebar.button("Generate Video and Music"):
88
- if image is not None:
89
- image = Image.open(image)
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
98
- st.write("Generating music...")
99
- audio_values, sampling_rate = generate_music(music_prompt, unconditional)
100
- wavfile.write("musicgen_out.wav", sampling_rate, audio_values)
101
- st.audio("musicgen_out.wav")
102
-
103
- # Combine audio and video
104
- st.write("Combining audio and video...")
105
- combine_audio_video("musicgen_out.wav", "output_video.mp4", "combined_output.mp4")
106
- st.video("combined_output.mp4")
 
1
+ import gradio as gr
 
2
  import torch
3
  import numpy as np
 
4
  from transformers import MusicgenForConditionalGeneration, AutoProcessor
5
+ import scipy.io.wavfile
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def generate_music(prompt, unconditional=False):
8
  model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
  model.to(device)
11
 
12
+ # Generate music
 
13
  if unconditional:
14
  unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
15
  audio_values = model.generate(**unconditional_inputs, do_sample=True, max_new_tokens=256)
16
  else:
17
  processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
18
+ inputs = processor(text=prompt, padding=True, return_tensors="pt")
19
+ audio_values = model.generate(**inputs.to(device), do_sample=True, guidance_scale=3, max_new_tokens=256)
 
 
 
 
 
 
 
20
 
21
  sampling_rate = model.config.audio_encoder.sampling_rate
22
+ audio_file = "musicgen_out.wav"
23
+
24
+ # Ensure audio_values is 1D and scale if necessary
25
+ audio_data = audio_values[0].cpu().numpy()
26
+
27
+ # Check if audio_data is in the correct format
28
+ if audio_data.ndim > 1:
29
+ audio_data = audio_data[0] # Take the first channel if stereo
30
+
31
+ # Scale audio data to 16-bit PCM format
32
+ audio_data = np.clip(audio_data, -1.0, 1.0) # Ensure values are in the range [-1, 1]
33
+ audio_data = (audio_data * 32767).astype(np.int16) # Scale to int16
34
+
35
+ # Save the generated audio
36
+ scipy.io.wavfile.write(audio_file, sampling_rate, audio_data)
37
+
38
+ return audio_file
39
+
40
+ def interface(prompt, unconditional):
41
+ audio_file = generate_music(prompt, unconditional)
42
+ return audio_file
43
+
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("# AI-Powered Music Generation")
46
+
47
+ with gr.Row():
48
+ prompt_input = gr.Textbox(label="Enter the Music Prompt")
49
+ unconditional_checkbox = gr.Checkbox(label="Generate Unconditional Music")
50
+
51
+ generate_button = gr.Button("Generate Music")
52
+ output_audio = gr.Audio(label="Output Music")
53
+
54
+ generate_button.click(
55
+ interface,
56
+ inputs=[prompt_input, unconditional_checkbox],
57
+ outputs=output_audio,
58
+ show_progress=True
59
+ )
60
+
61
+ demo.launch(share=True)