Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,61 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 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)
|
|
|
|
|
|
|
|
|