Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, AnimateDiffPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load the 3D Cartoon Image Generator (DreamShaper)
|
| 7 |
+
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
"Lykon/DreamShaper", torch_dtype=torch.float16
|
| 9 |
+
).to("cuda")
|
| 10 |
+
|
| 11 |
+
# Load the Animation Model (AnimateDiff)
|
| 12 |
+
video_pipe = AnimateDiffPipeline.from_pretrained(
|
| 13 |
+
"guoyww/animatediff", torch_dtype=torch.float16
|
| 14 |
+
).to("cuda")
|
| 15 |
+
|
| 16 |
+
# Function to generate a 3D cartoon image
|
| 17 |
+
def generate_cartoon_image(prompt):
|
| 18 |
+
image = image_pipe(prompt).images[0]
|
| 19 |
+
image_path = "generated_cartoon.png"
|
| 20 |
+
image.save(image_path)
|
| 21 |
+
return image_path
|
| 22 |
+
|
| 23 |
+
# Function to generate an animated video from an uploaded image
|
| 24 |
+
def generate_cartoon_video(image):
|
| 25 |
+
image = Image.open(image).convert("RGB")
|
| 26 |
+
video_output = video_pipe(image)["video"]
|
| 27 |
+
video_path = "generated_cartoon_animation.mp4"
|
| 28 |
+
video_output.save(video_path)
|
| 29 |
+
return video_path
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("# 🎨 AI 3D Cartoon Image & Animation Generator")
|
| 34 |
+
|
| 35 |
+
# Text-to-Image Section
|
| 36 |
+
with gr.Tab("Generate Cartoon Image"):
|
| 37 |
+
gr.Markdown("Enter a text prompt to generate a 3D cartoon-style image")
|
| 38 |
+
text_input = gr.Textbox(label="Enter your prompt")
|
| 39 |
+
img_output = gr.Image(label="Generated Cartoon Image")
|
| 40 |
+
img_button = gr.Button("Generate Image")
|
| 41 |
+
img_button.click(generate_cartoon_image, inputs=text_input, outputs=img_output)
|
| 42 |
+
|
| 43 |
+
# Image-to-Video Section
|
| 44 |
+
with gr.Tab("Generate Cartoon Animation"):
|
| 45 |
+
gr.Markdown("Upload a 3D cartoon-style image to create an animation")
|
| 46 |
+
img_input = gr.Image(type="filepath", label="Upload Cartoon Image")
|
| 47 |
+
vid_output = gr.Video(label="Generated Animation")
|
| 48 |
+
vid_button = gr.Button("Generate Animation")
|
| 49 |
+
vid_button.click(generate_cartoon_video, inputs=img_input, outputs=vid_output)
|
| 50 |
+
|
| 51 |
+
demo.launch()
|