JoPmt commited on
Commit
102fb88
1 Parent(s): a7a36ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -47
app.py CHANGED
@@ -5,77 +5,37 @@ import random, os, gc, base64, io
5
  import cv2
6
  import torch
7
  from accelerate import Accelerator
8
- from transformers import pipeline, DPTImageProcessor, DPTForDepthEstimation, DPTFeatureExtractor
9
  from diffusers.utils import load_image
10
  from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline
11
  from gradio_client import Client
12
 
13
  accelerator = Accelerator(cpu=True)
 
14
  pipe_prior = accelerator.prepare(KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float32))
15
  pipe_prior = accelerator.prepare(pipe_prior.to("cpu"))
16
  pipe = accelerator.prepare(KandinskyV22ControlnetPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float32))
17
  pipe = accelerator.prepare(pipe.to("cpu"))
18
  generator = torch.Generator("cpu").manual_seed(random.randint(1, 867346))
19
 
20
- processor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
21
- feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
22
- model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
23
-
24
- def process_image(image):
25
- # prepare image for the model
26
- encoding = feature_extractor(image, return_tensors="pt")
27
-
28
- # forward pass
29
- with torch.no_grad():
30
- outputs = model(**encoding)
31
- predicted_depth = outputs.predicted_depth
32
-
33
- # interpolate to original size
34
- prediction = torch.nn.functional.interpolate(
35
- predicted_depth.unsqueeze(1),
36
- size=image.size[::-1],
37
- mode="bicubic",
38
- align_corners=False,
39
- ).squeeze()
40
- output = prediction.cpu().numpy()
41
- formatted = (output * 255 / np.max(output)).astype('uint8')
42
- img = Image.fromarray(formatted)
43
- return img
44
-
45
- def make_hint(note):
46
- ##client = Client("https://adpro-dpt-depth06.hf.space/")
47
- ##imoge = client.predict(note, api_name="/predict")
48
- ##image_dota = base64.b64decode(imoge)
49
- ##image = Image.open(io.BytesIO(image_dota))
50
- ##in_mage = processor(images=note, return_tensors="pt")
51
- ##with torch.no_grad():
52
- ##in_dep = tell(**in_mage)
53
- prod_depth = process_image(note)
54
-
55
- ##prediction = torch.nn.functional.interpolate(predicted_depth.unsqueeze(1),size=note.size[::-1],mode="bicubic",align_corners=False,)
56
-
57
- ##in_dep = prediction.squeeze().cpu().numpy()
58
- ##formatted = (in_dep * 255 / np.max(in_dep)).astype("uint8")
59
- ##depth = Image.fromarray(formatted)
60
-
61
- image = load_image(prod_depth)
62
  image = np.array(image)
63
  image = image[:, :, None]
64
  image = np.concatenate([image, image, image], axis=2)
65
  detected_map = torch.from_numpy(image).float() / 255.0
66
- ##hint = detected_map.permute(2, 0, 1)
67
- hint = detected_map.permute(3, 0, 1, 2)
68
  return hint
69
 
70
  def plex(goof,prompt):
71
  gc.collect()
72
  goof = load_image(goof)
73
- goof = goof.convert("RGB")
74
  goof.save('./gf.png', 'PNG')
 
75
  ##base64_string = ''
76
  ##with open('./gf.png', 'rb') as image_file:
77
  ## base64_string = base64.b64encode(image_file.read()).decode('utf-8')
78
- hint = make_hint(goof).unsqueeze(0).to("cpu")
79
  negative_prior_prompt = "lowres,text,bad quality,jpeg artifacts,ugly,bad face,extra fingers,blurry,bad anatomy,extra limbs,fused fingers,long neck,watermark,signature"
80
  image_emb, zero_image_emb = pipe_prior(prompt=prompt, negative_prompt=negative_prior_prompt, num_inference_steps=5,generator=generator).to_tuple()
81
 
 
5
  import cv2
6
  import torch
7
  from accelerate import Accelerator
8
+ from transformers import pipeline
9
  from diffusers.utils import load_image
10
  from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline
11
  from gradio_client import Client
12
 
13
  accelerator = Accelerator(cpu=True)
14
+ depth_estimator = accelerator.prepare(pipeline("depth-estimation", model="Intel/dpt-hybrid-midas"))
15
  pipe_prior = accelerator.prepare(KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float32))
16
  pipe_prior = accelerator.prepare(pipe_prior.to("cpu"))
17
  pipe = accelerator.prepare(KandinskyV22ControlnetPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float32))
18
  pipe = accelerator.prepare(pipe.to("cpu"))
19
  generator = torch.Generator("cpu").manual_seed(random.randint(1, 867346))
20
 
21
+ def make_hint(image, depth_estimator):
22
+ image = depth_estimator(image)["depth"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  image = np.array(image)
24
  image = image[:, :, None]
25
  image = np.concatenate([image, image, image], axis=2)
26
  detected_map = torch.from_numpy(image).float() / 255.0
27
+ hint = detected_map.permute(2, 0, 1)
 
28
  return hint
29
 
30
  def plex(goof,prompt):
31
  gc.collect()
32
  goof = load_image(goof)
 
33
  goof.save('./gf.png', 'PNG')
34
+ goof = goof.convert("RGB")
35
  ##base64_string = ''
36
  ##with open('./gf.png', 'rb') as image_file:
37
  ## base64_string = base64.b64encode(image_file.read()).decode('utf-8')
38
+ hint = make_hint(goof, depth_estimator).unsqueeze(0).to("cpu")
39
  negative_prior_prompt = "lowres,text,bad quality,jpeg artifacts,ugly,bad face,extra fingers,blurry,bad anatomy,extra limbs,fused fingers,long neck,watermark,signature"
40
  image_emb, zero_image_emb = pipe_prior(prompt=prompt, negative_prompt=negative_prior_prompt, num_inference_steps=5,generator=generator).to_tuple()
41