patrickvonplaten commited on
Commit
30f09bf
1 Parent(s): ac91a8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -4,7 +4,9 @@ from PIL import Image
4
  import qrcode
5
  from pathlib import Path
6
  from multiprocessing import cpu_count
7
-
 
 
8
 
9
  from diffusers import (
10
  StableDiffusionPipeline,
@@ -17,7 +19,14 @@ from diffusers import (
17
  EulerDiscreteScheduler,
18
  )
19
 
20
- from PIL import Image
 
 
 
 
 
 
 
21
 
22
  qrcode_generator = qrcode.QRCode(
23
  version=1,
@@ -39,16 +48,6 @@ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
39
  pipe.enable_xformers_memory_efficient_attention()
40
 
41
 
42
- sd_pipe = StableDiffusionPipeline.from_pretrained(
43
- "stabilityai/stable-diffusion-2-1",
44
- torch_dtype=torch.float16,
45
- safety_checker=None,
46
- )
47
- sd_pipe.to("cuda")
48
- sd_pipe.scheduler = DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config)
49
- sd_pipe.enable_xformers_memory_efficient_attention()
50
-
51
-
52
  def resize_for_condition_image(input_image: Image.Image, resolution: int):
53
  input_image = input_image.convert("RGB")
54
  W, H = input_image.size
@@ -117,15 +116,8 @@ def inference(
117
  elif init_image is None or init_image.size == (1, 1):
118
  print("Generating random image from prompt using Stable Diffusion")
119
  # generate image from prompt
120
- out = sd_pipe(
121
- prompt=prompt,
122
- negative_prompt=negative_prompt,
123
- generator=generator,
124
- num_inference_steps=25,
125
- num_images_per_prompt=1,
126
- ) # type: ignore
127
-
128
- init_image = out.images[0]
129
  else:
130
  print("Using provided init image")
131
  init_image = resize_for_condition_image(init_image, 768)
 
4
  import qrcode
5
  from pathlib import Path
6
  from multiprocessing import cpu_count
7
+ import requests
8
+ import io
9
+ from PIL import Image
10
 
11
  from diffusers import (
12
  StableDiffusionPipeline,
 
19
  EulerDiscreteScheduler,
20
  )
21
 
22
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
23
+ HF_TOKEN = os.environ.get("HF_TOKEN")
24
+
25
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
26
+
27
+ def query(payload):
28
+ response = requests.post(API_URL, headers=headers, json=payload)
29
+ return response.content
30
 
31
  qrcode_generator = qrcode.QRCode(
32
  version=1,
 
48
  pipe.enable_xformers_memory_efficient_attention()
49
 
50
 
 
 
 
 
 
 
 
 
 
 
51
  def resize_for_condition_image(input_image: Image.Image, resolution: int):
52
  input_image = input_image.convert("RGB")
53
  W, H = input_image.size
 
116
  elif init_image is None or init_image.size == (1, 1):
117
  print("Generating random image from prompt using Stable Diffusion")
118
  # generate image from prompt
119
+ image_bytes = query({"inputs": prompt})
120
+ init_image = Image.open(io.BytesIO(image_bytes))
 
 
 
 
 
 
 
121
  else:
122
  print("Using provided init image")
123
  init_image = resize_for_condition_image(init_image, 768)