Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import StableDiffusionPipeline
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
-
"
|
| 9 |
-
).to("cuda")
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
).to("cuda")
|
| 15 |
|
| 16 |
-
# Function to generate
|
| 17 |
-
def
|
| 18 |
image = image_pipe(prompt).images[0]
|
| 19 |
-
image_path = "
|
| 20 |
image.save(image_path)
|
| 21 |
return image_path
|
| 22 |
|
| 23 |
-
# Function to
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
video_output.save(video_path)
|
| 29 |
return video_path
|
| 30 |
|
| 31 |
-
# Gradio
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.Markdown("
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
gr.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 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 model (Redshift Diffusion)
|
| 8 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 9 |
+
"nitrosocke/redshift-diffusion", torch_dtype=torch.float16
|
| 10 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
|
| 12 |
+
# Load Image-to-Video model (Zeroscope v2 XL)
|
| 13 |
+
video_model_id = "cerspense/zeroscope_v2_XL"
|
| 14 |
+
processor = AutoProcessor.from_pretrained(video_model_id)
|
| 15 |
+
video_model = AutoModel.from_pretrained(video_model_id).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 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 |
+
with torch.no_grad():
|
| 27 |
+
inputs = processor(images=image_path, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 28 |
+
video_output = video_model(**inputs)
|
| 29 |
+
|
| 30 |
+
video_path = "generated_video.mp4"
|
| 31 |
video_output.save(video_path)
|
| 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 |
|
| 54 |
demo.launch()
|