bramw commited on
Commit
a072a82
·
1 Parent(s): 20bc4c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -9
app.py CHANGED
@@ -7,10 +7,94 @@ from io import BytesIO
7
  import requests
8
 
9
 
10
- def f(x):
11
- return x
12
 
13
- description = '**This demo is temporarily out of order** \n A gradio demo for [EDICT](https://arxiv.org/abs/2211.12446) (CVPR23)'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # description = gr.Markdown(description)
15
 
16
  article = """
@@ -37,10 +121,20 @@ As with most StableDiffusion approaches, faces/text are often problematic to ren
37
  A returned black image means that the [Safety Checker](https://huggingface.co/CompVis/stable-diffusion-safety-checker) triggered on the photo. This happens in odd cases sometimes (it often rejects
38
  the huggingface logo or variations), but we need to keep it in for obvious reasons.
39
  """
 
40
 
41
- demo = gr.Blocks()
42
-
43
- with demo:
44
- gr.Markdown(description)
45
- gr.Markdown(article)
46
- demo.launch()
 
 
 
 
 
 
 
 
 
 
7
  import requests
8
 
9
 
10
+ endpoint = Endpoint()
 
11
 
12
+ def local_edict(x, source_text, edit_text,
13
+ edit_strength, guidance_scale,
14
+ steps=50, mix_weight=0.93, ):
15
+ x = Image.fromarray(x)
16
+ return_im = EDICT_editing(x,
17
+ source_text,
18
+ edit_text,
19
+ steps=steps,
20
+ mix_weight=mix_weight,
21
+ init_image_strength=edit_strength,
22
+ guidance_scale=guidance_scale
23
+ )[0]
24
+ return np.array(return_im)
25
+
26
+ def encode_image(image):
27
+ buffered = BytesIO()
28
+ image.save(buffered, format="JPEG", quality=95)
29
+ buffered.seek(0)
30
+
31
+ return buffered
32
+
33
+
34
+
35
+ def decode_image(img_obj):
36
+ img = Image.open(img_obj).convert("RGB")
37
+ return img
38
+
39
+ def edict(x, source_text, edit_text,
40
+ edit_strength, guidance_scale,
41
+ steps=50, mix_weight=0.93, ):
42
+
43
+ url = endpoint.url
44
+ url = url + "/api/edit"
45
+ headers = {### Misc.
46
+
47
+ "User-Agent": "EDICT HuggingFace Space",
48
+ "Auth-Token": get_token(),
49
+ }
50
+
51
+ data = {
52
+ "source_text": source_text,
53
+ "edit_text": edit_text,
54
+ "edit_strength": edit_strength,
55
+ "guidance_scale": guidance_scale,
56
+ }
57
+
58
+ image = encode_image(Image.fromarray(x))
59
+ files = {"image": image}
60
+
61
+ response = requests.post(url, data=data, files=files, headers=headers)
62
+
63
+ if response.status_code == 200:
64
+ return np.array(decode_image(BytesIO(response.content)))
65
+ else:
66
+ return "Error: " + response.text
67
+ # x = decode_image(response)
68
+ # return np.array(x)
69
+
70
+ examples = [
71
+ ['square_ims/american_gothic.jpg', 'A painting of two people frowning', 'A painting of two people smiling', 0.5, 3],
72
+ ['square_ims/colloseum.jpg', 'An old ruined building', 'A new modern office building', 0.8, 3],
73
+ ]
74
+
75
+
76
+ examples.append(['square_ims/scream.jpg', 'A painting of someone screaming', 'A painting of an alien', 0.5, 3])
77
+ examples.append(['square_ims/yosemite.jpg', 'Granite forest valley', 'Granite desert valley', 0.8, 3])
78
+ examples.append(['square_ims/einstein.jpg', 'Mouth open', 'Mouth closed', 0.8, 3])
79
+ examples.append(['square_ims/einstein.jpg', 'A man', 'A man in K.I.S.S. facepaint', 0.8, 3])
80
+ """
81
+ examples.extend([
82
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Chinese New Year cupcake', 0.8, 3],
83
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Union Jack cupcake', 0.8, 3],
84
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Nigerian flag cupcake', 0.8, 3],
85
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Santa Claus cupcake', 0.8, 3],
86
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'An Easter cupcake', 0.8, 3],
87
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A hedgehog cupcake', 0.8, 3],
88
+ ['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A rose cupcake', 0.8, 3],
89
+ ])
90
+ """
91
+
92
+ for dog_i in [1, 2]:
93
+ for breed in ['Golden Retriever', 'Chihuahua', 'Dalmatian']:
94
+ examples.append([f'square_ims/imagenet_dog_{dog_i}.jpg', 'A dog', f'A {breed}', 0.8, 3])
95
+
96
+
97
+ description = 'A gradio demo for [EDICT](https://arxiv.org/abs/2211.12446) (CVPR23)'
98
  # description = gr.Markdown(description)
99
 
100
  article = """
 
121
  A returned black image means that the [Safety Checker](https://huggingface.co/CompVis/stable-diffusion-safety-checker) triggered on the photo. This happens in odd cases sometimes (it often rejects
122
  the huggingface logo or variations), but we need to keep it in for obvious reasons.
123
  """
124
+ # article = gr.Markdown(description)
125
 
126
+ iface = gr.Interface(fn=edict, inputs=[gr.Image(interactive=False),
127
+ gr.Textbox(label="Original Description"),
128
+ gr.Textbox(label="Edit Description"),
129
+ # 50, # gr.Slider(5, 50, value=20, step=1),
130
+ # 0.93, # gr.Slider(0.5, 1, value=0.7, step=0.05),
131
+ gr.Slider(0.0, 1, value=0.8, step=0.05),
132
+ gr.Slider(0, 10, value=3, step=0.5),
133
+ ],
134
+ examples = examples,
135
+ outputs="image",
136
+ description=description,
137
+ article=article,
138
+ cache_examples=False
139
+ )
140
+ iface.launch()