Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,96 +7,9 @@ from io import BytesIO
|
|
| 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 f(x):
|
| 40 |
return x
|
| 41 |
|
| 42 |
-
def edict(x, source_text, edit_text,
|
| 43 |
-
edit_strength, guidance_scale,
|
| 44 |
-
steps=50, mix_weight=0.93, ):
|
| 45 |
-
|
| 46 |
-
url = endpoint.url
|
| 47 |
-
url = url + "/api/edit"
|
| 48 |
-
headers = {### Misc.
|
| 49 |
-
|
| 50 |
-
"User-Agent": "EDICT HuggingFace Space",
|
| 51 |
-
"Auth-Token": get_token(),
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
data = {
|
| 55 |
-
"source_text": source_text,
|
| 56 |
-
"edit_text": edit_text,
|
| 57 |
-
"edit_strength": edit_strength,
|
| 58 |
-
"guidance_scale": guidance_scale,
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
image = encode_image(Image.fromarray(x))
|
| 62 |
-
files = {"image": image}
|
| 63 |
-
|
| 64 |
-
response = requests.post(url, data=data, files=files, headers=headers)
|
| 65 |
-
|
| 66 |
-
if response.status_code == 200:
|
| 67 |
-
return np.array(decode_image(BytesIO(response.content)))
|
| 68 |
-
else:
|
| 69 |
-
return "Error: " + response.text
|
| 70 |
-
# x = decode_image(response)
|
| 71 |
-
# return np.array(x)
|
| 72 |
-
|
| 73 |
-
examples = [
|
| 74 |
-
['square_ims/american_gothic.jpg', 'A painting of two people frowning', 'A painting of two people smiling', 0.5, 3],
|
| 75 |
-
['square_ims/colloseum.jpg', 'An old ruined building', 'A new modern office building', 0.8, 3],
|
| 76 |
-
]
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
examples.append(['square_ims/scream.jpg', 'A painting of someone screaming', 'A painting of an alien', 0.5, 3])
|
| 80 |
-
examples.append(['square_ims/yosemite.jpg', 'Granite forest valley', 'Granite desert valley', 0.8, 3])
|
| 81 |
-
examples.append(['square_ims/einstein.jpg', 'Mouth open', 'Mouth closed', 0.8, 3])
|
| 82 |
-
examples.append(['square_ims/einstein.jpg', 'A man', 'A man in K.I.S.S. facepaint', 0.8, 3])
|
| 83 |
-
"""
|
| 84 |
-
examples.extend([
|
| 85 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Chinese New Year cupcake', 0.8, 3],
|
| 86 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Union Jack cupcake', 0.8, 3],
|
| 87 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Nigerian flag cupcake', 0.8, 3],
|
| 88 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A Santa Claus cupcake', 0.8, 3],
|
| 89 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'An Easter cupcake', 0.8, 3],
|
| 90 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A hedgehog cupcake', 0.8, 3],
|
| 91 |
-
['square_ims/imagenet_cake_2.jpg', 'A cupcake', 'A rose cupcake', 0.8, 3],
|
| 92 |
-
])
|
| 93 |
-
"""
|
| 94 |
-
|
| 95 |
-
for dog_i in [1, 2]:
|
| 96 |
-
for breed in ['Golden Retriever', 'Chihuahua', 'Dalmatian']:
|
| 97 |
-
examples.append([f'square_ims/imagenet_dog_{dog_i}.jpg', 'A dog', f'A {breed}', 0.8, 3])
|
| 98 |
-
|
| 99 |
-
|
| 100 |
description = '*This demo is temporarily suspended for internal reasons* A gradio demo for [EDICT](https://arxiv.org/abs/2211.12446) (CVPR23)'
|
| 101 |
# description = gr.Markdown(description)
|
| 102 |
|
|
@@ -124,23 +37,11 @@ As with most StableDiffusion approaches, faces/text are often problematic to ren
|
|
| 124 |
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
|
| 125 |
the huggingface logo or variations), but we need to keep it in for obvious reasons.
|
| 126 |
"""
|
| 127 |
-
|
| 128 |
-
"""
|
| 129 |
-
fn=edict, inputs=["image",
|
| 130 |
-
gr.Textbox(label="Original Description"),
|
| 131 |
-
gr.Textbox(label="Edit Description"),
|
| 132 |
-
# 50, # gr.Slider(5, 50, value=20, step=1),
|
| 133 |
-
# 0.93, # gr.Slider(0.5, 1, value=0.7, step=0.05),
|
| 134 |
-
gr.Slider(0.0, 1, value=0.8, step=0.05),
|
| 135 |
-
gr.Slider(0, 10, value=3, step=0.5),
|
| 136 |
-
],
|
| 137 |
-
"""
|
| 138 |
iface = gr.Interface(fn=f,
|
| 139 |
-
inputs=[
|
| 140 |
-
'str'
|
| 141 |
-
],
|
| 142 |
# examples = examples,
|
| 143 |
-
outputs="
|
| 144 |
description=description,
|
| 145 |
article=article)
|
| 146 |
iface.launch()
|
|
|
|
| 7 |
import requests
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def f(x):
|
| 11 |
return x
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
description = '*This demo is temporarily suspended for internal reasons* A gradio demo for [EDICT](https://arxiv.org/abs/2211.12446) (CVPR23)'
|
| 14 |
# description = gr.Markdown(description)
|
| 15 |
|
|
|
|
| 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 |
iface = gr.Interface(fn=f,
|
| 42 |
+
inputs=['image'],
|
|
|
|
|
|
|
| 43 |
# examples = examples,
|
| 44 |
+
outputs="image",
|
| 45 |
description=description,
|
| 46 |
article=article)
|
| 47 |
iface.launch()
|