Marcel0123 commited on
Commit
98ce8b0
Β·
verified Β·
1 Parent(s): 5f3c681

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -1,5 +1,10 @@
1
- # app.py (CPU-optimized)
2
  # Neurale netwerken als tekenaars β€” ControlNet (scribble) demo voor CPU
 
 
 
 
 
3
  import os, random, numpy as np
4
  from typing import Optional
5
  from PIL import Image, ImageDraw
@@ -14,7 +19,7 @@ from diffusers import (
14
  MODEL_BASE = os.environ.get("SD_BASE_MODEL", "runwayml/stable-diffusion-v1-5")
15
  MODEL_CN = os.environ.get("CN_SCRIBBLE_MODEL", "lllyasviel/sd-controlnet-scribble")
16
 
17
- DEVICE = "cpu" # geforceerd CPU
18
  DTYPE = torch.float32
19
  TARGET_SIZE = 512 # lager = sneller op CPU
20
 
@@ -36,12 +41,15 @@ def _lazy_load_pipeline():
36
  return pipe
37
 
38
  def to_rgba(img):
39
- if img is None: return None
 
 
40
  if isinstance(img, np.ndarray):
41
- if img.ndim == 2:
42
- return Image.fromarray(img).convert("RGBA")
43
- pil = Image.fromarray(img.astype(np.uint8))
44
- return pil.convert("RGBA") if pil.mode != "RGBA" else pil
 
45
  if isinstance(img, Image.Image):
46
  return img.convert("RGBA")
47
  try:
@@ -49,19 +57,22 @@ def to_rgba(img):
49
  except Exception:
50
  return None
51
 
52
- def rgba_to_scribble(img: Image.Image) -> Image.Image:
 
53
  img = to_rgba(img)
54
- if img is None: return None
 
55
  # transparant β†’ wit
56
  bg = Image.new("RGBA", img.size, (255,255,255,255))
57
  img = Image.alpha_composite(bg, img)
 
58
  arr = np.array(img.convert("L"))
59
  thr = max(25, int(arr.mean()*0.9))
60
  lines = arr < thr
61
  out = np.full((*arr.shape,3), 255, dtype=np.uint8)
62
  out[lines] = (0,0,0)
63
  scribble = Image.fromarray(out, "RGB")
64
- # vierkant + resize naar 512
65
  m = max(scribble.width, scribble.height)
66
  sq = Image.new("RGB", (m,m), (255,255,255))
67
  sq.paste(scribble, ((m-scribble.width)//2, (m-scribble.height)//2))
@@ -75,7 +86,7 @@ def run(drawing, prompt, negative_prompt, guidance_scale=7.0,
75
  if cn_image is None:
76
  raise gr.Error("Schets niet leesbaar. Probeer opnieuw of upload PNG/JPG.")
77
 
78
- if not prompt.strip():
79
  prompt = "clean pencil sketch, coherent completion, subtle shading, high quality"
80
  if seed is None or int(seed) < 0:
81
  seed = random.randint(0, 2**31-1)
@@ -110,12 +121,11 @@ def make_example_scribble() -> Image.Image:
110
 
111
  CSS = ".gradio-container{max-width:1100px} #combo img{border-radius:10px}"
112
 
113
- with gr.Blocks(title="πŸ–ŒοΈ Neurale netwerken als tekenaars (CPU)", css=CSS, theme=gr.themes.Soft()) as demo:
114
  gr.Markdown("## πŸ–ŒοΈ Neurale netwerken als tekenaars (CPU)\nTeken links of klik **Voorbeeld-schets**; AI vult aan met Stable Diffusion + ControlNet (scribble).")
115
  with gr.Row():
116
  with gr.Column():
117
- canvas = gr.Image(label="Schets (penseel-tool)", sources=["upload","clipboard"],
118
- type="pil", image_mode="RGBA", tool="sketch", height=420)
119
  prompt = gr.Textbox(label="Prompt", value="realistische appel met blad, potloodschets")
120
  negative_prompt = gr.Textbox(label="Negative prompt (optioneel)", value="tekst, watermerk, wazig")
121
  with gr.Accordion("Geavanceerd", open=False):
 
1
+ # app.py (CPU-optimized v3)
2
  # Neurale netwerken als tekenaars β€” ControlNet (scribble) demo voor CPU
3
+ # Wijzigingen:
4
+ # - Verwijderd: Image(..., tool="sketch") β†’ niet ondersteund in jouw Gradio-versie
5
+ # - Gebruikt gr.Sketchpad (retourneert RGBA-numpy), met robuuste conversie
6
+ # - CPU optimalisaties: 512px, DPMSolver, 12 steps, attention_slicing, single-queue
7
+
8
  import os, random, numpy as np
9
  from typing import Optional
10
  from PIL import Image, ImageDraw
 
19
  MODEL_BASE = os.environ.get("SD_BASE_MODEL", "runwayml/stable-diffusion-v1-5")
20
  MODEL_CN = os.environ.get("CN_SCRIBBLE_MODEL", "lllyasviel/sd-controlnet-scribble")
21
 
22
+ DEVICE = "cpu" # geforceerd CPU
23
  DTYPE = torch.float32
24
  TARGET_SIZE = 512 # lager = sneller op CPU
25
 
 
41
  return pipe
42
 
43
  def to_rgba(img):
44
+ """Accepteer numpy of PIL en geef een PIL RGBA image terug."""
45
+ if img is None:
46
+ return None
47
  if isinstance(img, np.ndarray):
48
+ # Sketchpad geeft HxWx4 (RGBA) of HxWx3 terug
49
+ if img.dtype != np.uint8:
50
+ img = img.astype(np.uint8)
51
+ pil = Image.fromarray(img)
52
+ return pil.convert("RGBA")
53
  if isinstance(img, Image.Image):
54
  return img.convert("RGBA")
55
  try:
 
57
  except Exception:
58
  return None
59
 
60
+ def rgba_to_scribble(img) -> Image.Image:
61
+ """Zet RGBA schets om naar zwart-op-wit scribble, vierkant 512x512."""
62
  img = to_rgba(img)
63
+ if img is None:
64
+ return None
65
  # transparant β†’ wit
66
  bg = Image.new("RGBA", img.size, (255,255,255,255))
67
  img = Image.alpha_composite(bg, img)
68
+ # grijs + drempel
69
  arr = np.array(img.convert("L"))
70
  thr = max(25, int(arr.mean()*0.9))
71
  lines = arr < thr
72
  out = np.full((*arr.shape,3), 255, dtype=np.uint8)
73
  out[lines] = (0,0,0)
74
  scribble = Image.fromarray(out, "RGB")
75
+ # vierkant + resize
76
  m = max(scribble.width, scribble.height)
77
  sq = Image.new("RGB", (m,m), (255,255,255))
78
  sq.paste(scribble, ((m-scribble.width)//2, (m-scribble.height)//2))
 
86
  if cn_image is None:
87
  raise gr.Error("Schets niet leesbaar. Probeer opnieuw of upload PNG/JPG.")
88
 
89
+ if not prompt or not str(prompt).strip():
90
  prompt = "clean pencil sketch, coherent completion, subtle shading, high quality"
91
  if seed is None or int(seed) < 0:
92
  seed = random.randint(0, 2**31-1)
 
121
 
122
  CSS = ".gradio-container{max-width:1100px} #combo img{border-radius:10px}"
123
 
124
+ with gr.Blocks(title="πŸ–ŒοΈ Neurale netwerken als tekenaars (CPU v3)", css=CSS, theme=gr.themes.Soft()) as demo:
125
  gr.Markdown("## πŸ–ŒοΈ Neurale netwerken als tekenaars (CPU)\nTeken links of klik **Voorbeeld-schets**; AI vult aan met Stable Diffusion + ControlNet (scribble).")
126
  with gr.Row():
127
  with gr.Column():
128
+ canvas = gr.Sketchpad(label="Schets (teken in zwart op transparant/wit)", height=420)
 
129
  prompt = gr.Textbox(label="Prompt", value="realistische appel met blad, potloodschets")
130
  negative_prompt = gr.Textbox(label="Negative prompt (optioneel)", value="tekst, watermerk, wazig")
131
  with gr.Accordion("Geavanceerd", open=False):