Spaces:
Runtime error
Runtime error
Generator.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
import numpy as np
|
| 5 |
+
import ffmpeg
|
| 6 |
+
from moviepy.editor import AudioFileClip, ImageSequenceClip
|
| 7 |
+
from diffusers import StableDiffusionPipeline
|
| 8 |
+
from tempfile import TemporaryDirectory
|
| 9 |
+
|
| 10 |
+
# Load AI Image Generation Model (Stable Diffusion)
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device)
|
| 13 |
+
|
| 14 |
+
def generate_frames(prompt, num_frames=30):
|
| 15 |
+
"""Generate a series of images based on the given prompt."""
|
| 16 |
+
images = []
|
| 17 |
+
for i in range(num_frames):
|
| 18 |
+
image = pipe(prompt).images[0]
|
| 19 |
+
images.append(image)
|
| 20 |
+
return images
|
| 21 |
+
|
| 22 |
+
def create_music_video(song, prompt):
|
| 23 |
+
"""Generate AI-based video from the uploaded song."""
|
| 24 |
+
|
| 25 |
+
with TemporaryDirectory() as temp_dir:
|
| 26 |
+
audio_path = os.path.join(temp_dir, "song.mp3")
|
| 27 |
+
video_path = os.path.join(temp_dir, "output.mp4")
|
| 28 |
+
|
| 29 |
+
# Save uploaded song
|
| 30 |
+
with open(audio_path, "wb") as f:
|
| 31 |
+
f.write(song)
|
| 32 |
+
|
| 33 |
+
# Extract duration
|
| 34 |
+
audio_clip = AudioFileClip(audio_path)
|
| 35 |
+
duration = audio_clip.duration
|
| 36 |
+
fps = 10
|
| 37 |
+
num_frames = int(duration * fps)
|
| 38 |
+
|
| 39 |
+
# Generate images
|
| 40 |
+
images = generate_frames(prompt, num_frames)
|
| 41 |
+
|
| 42 |
+
# Create video from images
|
| 43 |
+
image_sequence_clip = ImageSequenceClip([np.array(img) for img in images], fps=fps)
|
| 44 |
+
image_sequence_clip = image_sequence_clip.set_audio(audio_clip)
|
| 45 |
+
image_sequence_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
|
| 46 |
+
|
| 47 |
+
return video_path
|
| 48 |
+
|
| 49 |
+
# Gradio UI
|
| 50 |
+
interface = gr.Interface(
|
| 51 |
+
fn=create_music_video,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Audio(type="file", label="Upload Song"),
|
| 54 |
+
gr.Textbox(label="Describe the Music Video (Prompt)")
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.Video(label="Generated AI Music Video"),
|
| 57 |
+
title="AI Music Video Generator",
|
| 58 |
+
description="Upload a song and describe the music video you want. AI will generate a video using Stable Diffusion.",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Launc
|