chburhan64 commited on
Commit
505f557
·
verified ·
1 Parent(s): bfcb2bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
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 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
 
 
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