Austinsz-Warehouse commited on
Commit
7d7401f
·
verified ·
1 Parent(s): 0ff0a7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -1,33 +1,33 @@
1
  import gradio as gr
2
  import torch
3
- import numpy as np
4
- from PIL import Image
5
  from diffusers import StableDiffusionInpaintPipeline
 
 
6
  import cv2
7
  import os
8
 
9
- # Load a small CPU-friendly inpainting model
10
- model_id = "runwayml/stable-diffusion-inpainting"
11
- pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
 
12
  pipe.to("cpu")
13
 
14
- def generate_drawing_video(image):
15
- # Resize input
16
  image = image.resize((256, 256))
17
 
18
- # Create a blank white mask
19
  mask = Image.new("L", image.size, color=255)
20
 
21
- video_frames = []
22
 
23
- # Split mask into blocks to simulate drawing
24
  height, width = image.size
25
  pixels_per_frame = 500
26
  flat_indices = np.arange(height*width)
27
  np.random.shuffle(flat_indices)
28
 
29
  current_mask = Image.new("L", image.size, color=0)
30
- img_array = np.array(image)
31
 
32
  for i in range(0, len(flat_indices), pixels_per_frame):
33
  indices = flat_indices[i:i+pixels_per_frame]
@@ -36,27 +36,28 @@ def generate_drawing_video(image):
36
  y = idx // width
37
  current_mask.putpixel((x, y), 255)
38
 
39
- # Generate partial "drawing" with AI inpainting
40
  out_img = pipe(prompt="pencil sketch", image=image, mask_image=current_mask).images[0]
41
- video_frames.append(cv2.cvtColor(np.array(out_img), cv2.COLOR_RGB2BGR))
42
 
43
  # Save video
44
  out_path = "/tmp/output.avi"
45
  fourcc = cv2.VideoWriter_fourcc(*"XVID")
46
- out = cv2.VideoWriter(out_path, fourcc, 15, (image.width, image.height))
47
- for frame in video_frames:
48
- out.write(frame)
49
- out.release()
50
 
51
  return out_path
52
 
 
53
  iface = gr.Interface(
54
  fn=generate_drawing_video,
55
  inputs=gr.Image(type="pil"),
56
  outputs=gr.Video(type="file"),
57
- title="AI Hand Drawing Simulator",
58
- description="Upload an image and watch an AI simulate a pencil drawing video.",
59
  )
60
 
61
- iface.launch()
62
-
 
1
  import gradio as gr
2
  import torch
 
 
3
  from diffusers import StableDiffusionInpaintPipeline
4
+ from PIL import Image
5
+ import numpy as np
6
  import cv2
7
  import os
8
 
9
+ # Load a CPU-friendly Stable Diffusion inpainting model
10
+ pipe = StableDiffusionInpaintPipeline.from_pretrained(
11
+ "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float32
12
+ )
13
  pipe.to("cpu")
14
 
15
+ def generate_drawing_video(image: Image.Image):
16
+ # Resize to manageable size for CPU
17
  image = image.resize((256, 256))
18
 
19
+ # Blank mask for inpainting
20
  mask = Image.new("L", image.size, color=255)
21
 
22
+ frames = []
23
 
24
+ # Create random pixel blocks to simulate hand drawing
25
  height, width = image.size
26
  pixels_per_frame = 500
27
  flat_indices = np.arange(height*width)
28
  np.random.shuffle(flat_indices)
29
 
30
  current_mask = Image.new("L", image.size, color=0)
 
31
 
32
  for i in range(0, len(flat_indices), pixels_per_frame):
33
  indices = flat_indices[i:i+pixels_per_frame]
 
36
  y = idx // width
37
  current_mask.putpixel((x, y), 255)
38
 
39
+ # Generate partial drawing
40
  out_img = pipe(prompt="pencil sketch", image=image, mask_image=current_mask).images[0]
41
+ frames.append(cv2.cvtColor(np.array(out_img), cv2.COLOR_RGB2BGR))
42
 
43
  # Save video
44
  out_path = "/tmp/output.avi"
45
  fourcc = cv2.VideoWriter_fourcc(*"XVID")
46
+ video_writer = cv2.VideoWriter(out_path, fourcc, 15, (image.width, image.height))
47
+ for frame in frames:
48
+ video_writer.write(frame)
49
+ video_writer.release()
50
 
51
  return out_path
52
 
53
+ # Create Gradio interface
54
  iface = gr.Interface(
55
  fn=generate_drawing_video,
56
  inputs=gr.Image(type="pil"),
57
  outputs=gr.Video(type="file"),
58
+ title="AI Hand Drawing Video",
59
+ description="Upload an image and watch an AI simulate a pencil drawing video on it.",
60
  )
61
 
62
+ if __name__ == "__main__":
63
+ iface.launch()