chburhan64 commited on
Commit
9b36fc9
·
verified ·
1 Parent(s): 30eed7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -1,51 +1,54 @@
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()
 
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()