Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import StableDiffusionPipeline
|
| 4 |
-
from transformers import AutoProcessor, AutoModel
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Load Text-to-Image
|
|
|
|
| 8 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 9 |
-
"nitrosocke/redshift-diffusion", torch_dtype=torch.float16
|
| 10 |
-
).to(
|
| 11 |
|
| 12 |
-
# Load Image-to-Video
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
# Function to
|
| 18 |
def generate_image(prompt):
|
| 19 |
image = image_pipe(prompt).images[0]
|
| 20 |
image_path = "generated_image.png"
|
| 21 |
image.save(image_path)
|
| 22 |
return image_path
|
| 23 |
|
| 24 |
-
# Function to
|
| 25 |
def generate_video(image_path):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
video_output = video_model(**inputs)
|
| 29 |
-
|
| 30 |
video_path = "generated_video.mp4"
|
| 31 |
-
|
| 32 |
return video_path
|
| 33 |
|
| 34 |
# Gradio Interface
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
gr.Markdown("## 🎨 AI Cartoon Image & Video Generator")
|
| 37 |
-
|
| 38 |
with gr.Row():
|
| 39 |
prompt_input = gr.Textbox(label="Enter Text Prompt", placeholder="A 3D cartoon cat playing in a park")
|
| 40 |
generate_image_btn = gr.Button("Generate Image")
|
| 41 |
-
|
| 42 |
image_output = gr.Image(label="Generated Image")
|
| 43 |
-
|
| 44 |
with gr.Row():
|
| 45 |
generate_video_btn = gr.Button("Convert to Video")
|
| 46 |
video_output = gr.Video(label="Generated Video")
|
| 47 |
-
|
| 48 |
download_image = gr.File(label="Download Image")
|
| 49 |
download_video = gr.File(label="Download Video")
|
| 50 |
-
|
| 51 |
generate_image_btn.click(generate_image, inputs=[prompt_input], outputs=[image_output, download_image])
|
| 52 |
generate_video_btn.click(generate_video, inputs=[image_output], outputs=[video_output, download_video])
|
| 53 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, DiffusionPipeline
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Load Text-to-Image Model (Redshift Diffusion)
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 9 |
+
"nitrosocke/redshift-diffusion", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 10 |
+
).to(device)
|
| 11 |
|
| 12 |
+
# Load Image-to-Video Model (Zeroscope v2 XL)
|
| 13 |
+
video_model = DiffusionPipeline.from_pretrained(
|
| 14 |
+
"cerspense/zeroscope_v2_XL", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
+
).to(device)
|
| 16 |
|
| 17 |
+
# Function to Generate Image from Text
|
| 18 |
def generate_image(prompt):
|
| 19 |
image = image_pipe(prompt).images[0]
|
| 20 |
image_path = "generated_image.png"
|
| 21 |
image.save(image_path)
|
| 22 |
return image_path
|
| 23 |
|
| 24 |
+
# Function to Convert Image to Video
|
| 25 |
def generate_video(image_path):
|
| 26 |
+
image = image_pipe(prompt).images[0] # Reload image for video generation
|
| 27 |
+
video_frames = video_model(image) # Generate video frames
|
|
|
|
|
|
|
| 28 |
video_path = "generated_video.mp4"
|
| 29 |
+
video_frames.save(video_path) # Save output video
|
| 30 |
return video_path
|
| 31 |
|
| 32 |
# Gradio Interface
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
gr.Markdown("## 🎨 AI Cartoon Image & Video Generator")
|
| 35 |
+
|
| 36 |
with gr.Row():
|
| 37 |
prompt_input = gr.Textbox(label="Enter Text Prompt", placeholder="A 3D cartoon cat playing in a park")
|
| 38 |
generate_image_btn = gr.Button("Generate Image")
|
| 39 |
+
|
| 40 |
image_output = gr.Image(label="Generated Image")
|
| 41 |
+
|
| 42 |
with gr.Row():
|
| 43 |
generate_video_btn = gr.Button("Convert to Video")
|
| 44 |
video_output = gr.Video(label="Generated Video")
|
| 45 |
+
|
| 46 |
download_image = gr.File(label="Download Image")
|
| 47 |
download_video = gr.File(label="Download Video")
|
| 48 |
+
|
| 49 |
generate_image_btn.click(generate_image, inputs=[prompt_input], outputs=[image_output, download_image])
|
| 50 |
generate_video_btn.click(generate_video, inputs=[image_output], outputs=[video_output, download_video])
|
| 51 |
|