Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import ffmpeg
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
+
|
| 7 |
+
# Load Stable Diffusion for AI image generation
|
| 8 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
|
| 9 |
+
pipe = pipe.to("cuda")
|
| 10 |
+
|
| 11 |
+
def generate_image(prompt, style=None):
|
| 12 |
+
"""Generate an AI image from a text prompt and style."""
|
| 13 |
+
full_prompt = f"{style} {prompt}" if style else prompt
|
| 14 |
+
image = pipe(full_prompt).images[0]
|
| 15 |
+
return image
|
| 16 |
+
|
| 17 |
+
def create_video(images, audio_path, output_path="output_video.mp4", fps=1):
|
| 18 |
+
"""Create a video using ffmpeg without moviepy."""
|
| 19 |
+
image_files = []
|
| 20 |
+
for i, img in enumerate(images):
|
| 21 |
+
img_path = f"frame_{i}.png"
|
| 22 |
+
img.save(img_path)
|
| 23 |
+
image_files.append(img_path)
|
| 24 |
+
|
| 25 |
+
# Use ffmpeg to generate video
|
| 26 |
+
input_images = "frame_%d.png"
|
| 27 |
+
ffmpeg.input(input_images, framerate=fps).output(output_path, vcodec="libx264", pix_fmt="yuv420p").run()
|
| 28 |
+
|
| 29 |
+
# Add audio using ffmpeg
|
| 30 |
+
video_with_audio = "final_output.mp4"
|
| 31 |
+
ffmpeg.input(output_path).input(audio_path).output(video_with_audio, codec="copy").run()
|
| 32 |
+
|
| 33 |
+
return video_with_audio
|
| 34 |
+
|
| 35 |
+
def process_input(prompt, style, audio_file):
|
| 36 |
+
"""Handles user input, generates images, and creates the AI video."""
|
| 37 |
+
images = [generate_image(prompt, style)]
|
| 38 |
+
video_path = create_video(images, audio_file.name)
|
| 39 |
+
return video_path
|
| 40 |
+
|
| 41 |
+
# Gradio UI
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=process_input,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Textbox(label="Text Prompt"),
|
| 46 |
+
gr.Textbox(label="Style (e.g., Roblox, Pixel Art, Realistic)"),
|
| 47 |
+
gr.Audio(label="Upload MP3", type="file")
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Video(label="Generated AI Video"),
|
| 50 |
+
title="MP3 & Text to AI Video Generator",
|
| 51 |
+
description="Upload an MP3, enter a text prompt, select a style, and generate an AI video."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch(share=True)
|