Pierre Chapuis commited on
Commit
da8336d
1 Parent(s): aa7dd73
Files changed (1) hide show
  1. app.py +97 -72
app.py CHANGED
@@ -18,12 +18,16 @@ with env.prefixed("ERASER_"):
18
  auth = None if API_KEY is None else httpx.BasicAuth("hf", API_KEY)
19
 
20
 
21
- def process_bbox(prompts: dict[str, Any]) -> Image.Image:
22
  assert isinstance(img := prompts["image"], Image.Image)
23
  assert isinstance(boxes := prompts["boxes"], list)
24
  assert len(boxes) == 1
25
  assert isinstance(box := boxes[0], dict)
26
  data = {"bbox": ",".join([str(box[k]) for k in ["xmin", "ymin", "xmax", "ymax"]])}
 
 
 
 
27
  with io.BytesIO() as f:
28
  img.save(f, format="JPEG")
29
  r = httpx.post(
@@ -33,6 +37,7 @@ def process_bbox(prompts: dict[str, Any]) -> Image.Image:
33
  verify=CA_BUNDLE or True,
34
  timeout=30.0,
35
  auth=auth,
 
36
  )
37
  r.raise_for_status()
38
  return Image.open(io.BytesIO(r.content))
@@ -42,8 +47,12 @@ def on_change_bbox(prompts: dict[str, Any]):
42
  return gr.update(interactive=len(prompts["boxes"]) > 0)
43
 
44
 
45
- def process_prompt(img: Image.Image, prompt: str) -> Image.Image:
46
  data = {"prompt": prompt}
 
 
 
 
47
  with io.BytesIO() as f:
48
  img.save(f, format="JPEG")
49
  r = httpx.post(
@@ -53,6 +62,7 @@ def process_prompt(img: Image.Image, prompt: str) -> Image.Image:
53
  verify=CA_BUNDLE or True,
54
  timeout=30.0,
55
  auth=auth,
 
56
  )
57
  r.raise_for_status()
58
  return Image.open(io.BytesIO(r.content))
@@ -108,43 +118,50 @@ with gr.Blocks() as demo:
108
  inputs=[iimg, prompt],
109
  outputs=[btn],
110
  )
111
- btn.click(fn=process_prompt, inputs=[iimg, prompt], outputs=[oimg])
 
 
 
 
 
112
 
113
- ex = gr.Examples(
114
- examples=[
115
- [
116
- "examples/white-towels-rattan-basket-white-table-with-bright-room-background.jpg",
117
- "soap",
118
- ],
119
- [
120
- "examples/interior-decor-with-mirror-potted-plant.jpg",
121
- "potted plant",
122
- ],
123
- [
124
- "examples/detail-ball-basketball-court-sunset.jpg",
125
- "basketball",
126
- ],
127
- [
128
- "examples/still-life-device-table_23-2150994394.jpg",
129
- "glass of water",
130
- ],
131
- [
132
- "examples/knife-fork-green-checkered-napkin_140725-63576.jpg",
133
- "knife and fork",
134
- ],
135
- [
136
- "examples/city-night-with-architecture-vibrant-lights_23-2149836930.jpg",
137
- "frontmost black car on right lane",
138
- ],
139
- [
140
- "examples/close-up-coffee-latte-wooden-table_23-2147893063.jpg",
141
- "coffee cup on plate",
142
- ],
143
- [
144
- "examples/empty-chair-with-vase-plant_74190-2078.jpg",
145
- "chair",
146
- ],
147
  ],
 
 
 
 
 
 
 
 
 
 
 
 
148
  inputs=[iimg, prompt],
149
  outputs=[oimg],
150
  fn=process_prompt,
@@ -170,47 +187,55 @@ with gr.Blocks() as demo:
170
  inputs=[annotator],
171
  outputs=[btn],
172
  )
173
- btn.click(fn=process_bbox, inputs=[annotator], outputs=[oimg])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
  ex = gr.Examples(
176
- examples=[
177
- {
178
- "image": "examples/white-towels-rattan-basket-white-table-with-bright-room-background.jpg",
179
- "boxes": [{"xmin": 836, "ymin": 475, "xmax": 1125, "ymax": 1013}],
180
- },
181
- {
182
- "image": "examples/interior-decor-with-mirror-potted-plant.jpg",
183
- "boxes": [{"xmin": 47, "ymin": 907, "xmax": 397, "ymax": 1633}],
184
- },
185
- {
186
- "image": "examples/detail-ball-basketball-court-sunset.jpg",
187
- "boxes": [{"xmin": 673, "ymin": 954, "xmax": 911, "ymax": 1186}],
188
- },
189
- {
190
- "image": "examples/still-life-device-table_23-2150994394.jpg",
191
- "boxes": [{"xmin": 429, "ymin": 586, "xmax": 571, "ymax": 834}],
192
- },
193
- {
194
- "image": "examples/knife-fork-green-checkered-napkin_140725-63576.jpg",
195
- "boxes": [{"xmin": 972, "ymin": 226, "xmax": 1092, "ymax": 1023}],
196
- },
197
- {
198
- "image": "examples/city-night-with-architecture-vibrant-lights_23-2149836930.jpg",
199
- "boxes": [{"xmin": 215, "ymin": 637, "xmax": 411, "ymax": 855}],
200
- },
201
- {
202
- "image": "examples/close-up-coffee-latte-wooden-table_23-2147893063.jpg",
203
- "boxes": [{"xmin": 255, "ymin": 456, "xmax": 1080, "ymax": 1064}],
204
- },
205
- {
206
- "image": "examples/empty-chair-with-vase-plant_74190-2078.jpg",
207
- "boxes": [{"xmin": 35, "ymin": 320, "xmax": 383, "ymax": 983}],
208
- },
209
- ],
210
  inputs=[annotator],
211
  outputs=[oimg],
212
  fn=process_bbox,
213
  cache_examples=True,
214
  )
215
 
 
216
  demo.launch(show_api=False)
 
18
  auth = None if API_KEY is None else httpx.BasicAuth("hf", API_KEY)
19
 
20
 
21
+ def process_bbox(prompts: dict[str, Any], request: gr.Request) -> Image.Image:
22
  assert isinstance(img := prompts["image"], Image.Image)
23
  assert isinstance(boxes := prompts["boxes"], list)
24
  assert len(boxes) == 1
25
  assert isinstance(box := boxes[0], dict)
26
  data = {"bbox": ",".join([str(box[k]) for k in ["xmin", "ymin", "xmax", "ymax"]])}
27
+ headers = {}
28
+ if request: # avoid DOS - can be None despite type hint!
29
+ client_ip = request.headers.get("x-forwarded-for") or request.client.host
30
+ headers = {"X-HF-Client-IP": client_ip}
31
  with io.BytesIO() as f:
32
  img.save(f, format="JPEG")
33
  r = httpx.post(
 
37
  verify=CA_BUNDLE or True,
38
  timeout=30.0,
39
  auth=auth,
40
+ headers=headers,
41
  )
42
  r.raise_for_status()
43
  return Image.open(io.BytesIO(r.content))
 
47
  return gr.update(interactive=len(prompts["boxes"]) > 0)
48
 
49
 
50
+ def process_prompt(img: Image.Image, prompt: str, request: gr.Request) -> Image.Image:
51
  data = {"prompt": prompt}
52
+ headers = {}
53
+ if request: # avoid DOS - can be None despite type hint!
54
+ client_ip = request.headers.get("x-forwarded-for") or request.client.host
55
+ headers = {"X-HF-Client-IP": client_ip}
56
  with io.BytesIO() as f:
57
  img.save(f, format="JPEG")
58
  r = httpx.post(
 
62
  verify=CA_BUNDLE or True,
63
  timeout=30.0,
64
  auth=auth,
65
+ headers=headers,
66
  )
67
  r.raise_for_status()
68
  return Image.open(io.BytesIO(r.content))
 
118
  inputs=[iimg, prompt],
119
  outputs=[btn],
120
  )
121
+ btn.click(
122
+ fn=process_prompt,
123
+ inputs=[iimg, prompt],
124
+ outputs=[oimg],
125
+ api_name=False,
126
+ )
127
 
128
+ examples = [
129
+ [
130
+ "examples/white-towels-rattan-basket-white-table-with-bright-room-background.jpg",
131
+ "soap",
132
+ ],
133
+ [
134
+ "examples/interior-decor-with-mirror-potted-plant.jpg",
135
+ "potted plant",
136
+ ],
137
+ [
138
+ "examples/detail-ball-basketball-court-sunset.jpg",
139
+ "basketball",
140
+ ],
141
+ [
142
+ "examples/still-life-device-table_23-2150994394.jpg",
143
+ "glass of water",
144
+ ],
145
+ [
146
+ "examples/knife-fork-green-checkered-napkin_140725-63576.jpg",
147
+ "knife and fork",
148
+ ],
149
+ [
150
+ "examples/city-night-with-architecture-vibrant-lights_23-2149836930.jpg",
151
+ "frontmost black car on right lane",
 
 
 
 
 
 
 
 
 
 
152
  ],
153
+ [
154
+ "examples/close-up-coffee-latte-wooden-table_23-2147893063.jpg",
155
+ "coffee cup on plate",
156
+ ],
157
+ [
158
+ "examples/empty-chair-with-vase-plant_74190-2078.jpg",
159
+ "chair",
160
+ ],
161
+ ]
162
+
163
+ ex = gr.Examples(
164
+ examples=[x + [None] for x in examples],
165
  inputs=[iimg, prompt],
166
  outputs=[oimg],
167
  fn=process_prompt,
 
187
  inputs=[annotator],
188
  outputs=[btn],
189
  )
190
+ btn.click(
191
+ fn=process_bbox,
192
+ inputs=[annotator],
193
+ outputs=[oimg],
194
+ api_name=False,
195
+ )
196
+
197
+ examples = [
198
+ {
199
+ "image": "examples/white-towels-rattan-basket-white-table-with-bright-room-background.jpg",
200
+ "boxes": [{"xmin": 836, "ymin": 475, "xmax": 1125, "ymax": 1013}],
201
+ },
202
+ {
203
+ "image": "examples/interior-decor-with-mirror-potted-plant.jpg",
204
+ "boxes": [{"xmin": 47, "ymin": 907, "xmax": 397, "ymax": 1633}],
205
+ },
206
+ {
207
+ "image": "examples/detail-ball-basketball-court-sunset.jpg",
208
+ "boxes": [{"xmin": 673, "ymin": 954, "xmax": 911, "ymax": 1186}],
209
+ },
210
+ {
211
+ "image": "examples/still-life-device-table_23-2150994394.jpg",
212
+ "boxes": [{"xmin": 429, "ymin": 586, "xmax": 571, "ymax": 834}],
213
+ },
214
+ {
215
+ "image": "examples/knife-fork-green-checkered-napkin_140725-63576.jpg",
216
+ "boxes": [{"xmin": 972, "ymin": 226, "xmax": 1092, "ymax": 1023}],
217
+ },
218
+ {
219
+ "image": "examples/city-night-with-architecture-vibrant-lights_23-2149836930.jpg",
220
+ "boxes": [{"xmin": 215, "ymin": 637, "xmax": 411, "ymax": 855}],
221
+ },
222
+ {
223
+ "image": "examples/close-up-coffee-latte-wooden-table_23-2147893063.jpg",
224
+ "boxes": [{"xmin": 255, "ymin": 456, "xmax": 1080, "ymax": 1064}],
225
+ },
226
+ {
227
+ "image": "examples/empty-chair-with-vase-plant_74190-2078.jpg",
228
+ "boxes": [{"xmin": 35, "ymin": 320, "xmax": 383, "ymax": 983}],
229
+ },
230
+ ]
231
 
232
  ex = gr.Examples(
233
+ examples=[[x, None] for x in examples],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  inputs=[annotator],
235
  outputs=[oimg],
236
  fn=process_bbox,
237
  cache_examples=True,
238
  )
239
 
240
+ demo.queue(max_size=30, api_open=False)
241
  demo.launch(show_api=False)