Spaces:
Running
Running
Add url params parsing
Browse files
README.md
CHANGED
|
@@ -10,3 +10,5 @@ pinned: false
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
| 14 |
+
- Added url param parsing, e.g. ?transform=HorizontalFlip will open app with selected HorizontalFlip
|
app.py
CHANGED
|
@@ -6,12 +6,12 @@ import gradio as gr
|
|
| 6 |
import inspect
|
| 7 |
import io
|
| 8 |
import numpy as np
|
| 9 |
-
import os
|
| 10 |
|
|
|
|
| 11 |
from copy import deepcopy
|
| 12 |
from functools import wraps
|
| 13 |
from PIL import Image, ImageDraw
|
| 14 |
-
from typing import get_type_hints
|
| 15 |
from mixpanel import Mixpanel
|
| 16 |
|
| 17 |
from utils import is_not_supported_transform
|
|
@@ -90,6 +90,11 @@ for mask in BASE64_DEFAULT_MASKS:
|
|
| 90 |
)
|
| 91 |
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def track_event(event_name, user_id="unknown", properties=None):
|
| 94 |
if properties is None:
|
| 95 |
properties = {}
|
|
@@ -97,6 +102,15 @@ def track_event(event_name, user_id="unknown", properties=None):
|
|
| 97 |
logger.info(f"Event tracked: {event_name} - {properties}")
|
| 98 |
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def run_with_retry(compose):
|
| 101 |
@wraps(compose)
|
| 102 |
def wrapper(*args, **kwargs):
|
|
@@ -125,7 +139,7 @@ def run_with_retry(compose):
|
|
| 125 |
return wrapper
|
| 126 |
|
| 127 |
|
| 128 |
-
def draw_boxes(image, boxes, color=(255, 0, 0), thickness=
|
| 129 |
"""Draw boxes with PIL."""
|
| 130 |
pil_image = Image.fromarray(image)
|
| 131 |
draw = ImageDraw.Draw(pil_image)
|
|
@@ -206,15 +220,13 @@ def get_formatted_signature(function_or_class, indentation=4):
|
|
| 206 |
return result
|
| 207 |
|
| 208 |
|
| 209 |
-
def get_formatted_transform(
|
| 210 |
-
transform_name = transforms_keys[transform_number]
|
| 211 |
track_event("transform_selected", properties={"transform_name": transform_name})
|
| 212 |
transform = transforms_map[transform_name]
|
| 213 |
return f"A.{transform.__name__}{get_formatted_signature(transform)}"
|
| 214 |
|
| 215 |
|
| 216 |
-
def get_formatted_transform_docs(
|
| 217 |
-
transform_name = transforms_keys[transform_number]
|
| 218 |
transform = transforms_map[transform_name]
|
| 219 |
return transform.__doc__.strip("\n")
|
| 220 |
|
|
@@ -282,6 +294,10 @@ def update_code_and_docs(select):
|
|
| 282 |
docs = get_formatted_transform_docs(select)
|
| 283 |
return code, docs
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
with gr.Blocks() as demo:
|
| 287 |
gr.Markdown(HEADER)
|
|
@@ -292,23 +308,19 @@ with gr.Blocks() as demo:
|
|
| 292 |
label="Select a transformation",
|
| 293 |
choices=transforms_keys,
|
| 294 |
value=DEFAULT_TRANSFORM,
|
| 295 |
-
type="
|
| 296 |
interactive=True,
|
| 297 |
)
|
| 298 |
with gr.Accordion("Documentation (click to expand)", open=False):
|
| 299 |
docs = gr.TextArea(
|
| 300 |
-
get_formatted_transform_docs(
|
| 301 |
-
transforms_keys.index(DEFAULT_TRANSFORM)
|
| 302 |
-
),
|
| 303 |
show_label=False,
|
| 304 |
interactive=False,
|
| 305 |
)
|
| 306 |
code = gr.Code(
|
| 307 |
label="Code",
|
| 308 |
language="python",
|
| 309 |
-
value=get_formatted_transform(
|
| 310 |
-
transforms_keys.index(DEFAULT_TRANSFORM)
|
| 311 |
-
),
|
| 312 |
interactive=True,
|
| 313 |
lines=5,
|
| 314 |
)
|
|
@@ -333,12 +345,13 @@ with gr.Blocks() as demo:
|
|
| 333 |
columns=3,
|
| 334 |
show_label=False,
|
| 335 |
)
|
| 336 |
-
|
| 337 |
select.change(fn=update_code_and_docs, inputs=[select], outputs=[code, docs])
|
| 338 |
button.click(
|
| 339 |
fn=update_augmented_images, inputs=[image, code], outputs=[augmented_image]
|
| 340 |
)
|
| 341 |
-
|
|
|
|
|
|
|
| 342 |
|
| 343 |
if __name__ == "__main__":
|
| 344 |
demo.launch()
|
|
|
|
| 6 |
import inspect
|
| 7 |
import io
|
| 8 |
import numpy as np
|
|
|
|
| 9 |
|
| 10 |
+
from dataclasses import dataclass
|
| 11 |
from copy import deepcopy
|
| 12 |
from functools import wraps
|
| 13 |
from PIL import Image, ImageDraw
|
| 14 |
+
from typing import get_type_hints, Optional
|
| 15 |
from mixpanel import Mixpanel
|
| 16 |
|
| 17 |
from utils import is_not_supported_transform
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
|
| 93 |
+
@dataclass
|
| 94 |
+
class RequestParams:
|
| 95 |
+
user_ip: str
|
| 96 |
+
transform_name: Optional[str]
|
| 97 |
+
|
| 98 |
def track_event(event_name, user_id="unknown", properties=None):
|
| 99 |
if properties is None:
|
| 100 |
properties = {}
|
|
|
|
| 102 |
logger.info(f"Event tracked: {event_name} - {properties}")
|
| 103 |
|
| 104 |
|
| 105 |
+
def get_params(request: gr.Request) -> RequestParams:
|
| 106 |
+
"""Parse input request parameters."""
|
| 107 |
+
ip = request.client.host
|
| 108 |
+
transform_name = request.query_params.get("transform", None)
|
| 109 |
+
params = RequestParams(user_ip=ip, transform_name=transform_name)
|
| 110 |
+
track_event("app_opened", user_id=params.user_ip, properties={"transform_name": params.transform_name})
|
| 111 |
+
return params
|
| 112 |
+
|
| 113 |
+
|
| 114 |
def run_with_retry(compose):
|
| 115 |
@wraps(compose)
|
| 116 |
def wrapper(*args, **kwargs):
|
|
|
|
| 139 |
return wrapper
|
| 140 |
|
| 141 |
|
| 142 |
+
def draw_boxes(image, boxes, color=(255, 0, 0), thickness=1) -> np.ndarray:
|
| 143 |
"""Draw boxes with PIL."""
|
| 144 |
pil_image = Image.fromarray(image)
|
| 145 |
draw = ImageDraw.Draw(pil_image)
|
|
|
|
| 220 |
return result
|
| 221 |
|
| 222 |
|
| 223 |
+
def get_formatted_transform(transform_name):
|
|
|
|
| 224 |
track_event("transform_selected", properties={"transform_name": transform_name})
|
| 225 |
transform = transforms_map[transform_name]
|
| 226 |
return f"A.{transform.__name__}{get_formatted_signature(transform)}"
|
| 227 |
|
| 228 |
|
| 229 |
+
def get_formatted_transform_docs(transform_name):
|
|
|
|
| 230 |
transform = transforms_map[transform_name]
|
| 231 |
return transform.__doc__.strip("\n")
|
| 232 |
|
|
|
|
| 294 |
docs = get_formatted_transform_docs(select)
|
| 295 |
return code, docs
|
| 296 |
|
| 297 |
+
def update_code_and_docs_on_start(url_params: gr.Request):
|
| 298 |
+
params = get_params(url_params)
|
| 299 |
+
transform_name = params.transform_name if params.transform_name is not None else DEFAULT_TRANSFORM
|
| 300 |
+
return gr.update(value=transform_name)
|
| 301 |
|
| 302 |
with gr.Blocks() as demo:
|
| 303 |
gr.Markdown(HEADER)
|
|
|
|
| 308 |
label="Select a transformation",
|
| 309 |
choices=transforms_keys,
|
| 310 |
value=DEFAULT_TRANSFORM,
|
| 311 |
+
type="value",
|
| 312 |
interactive=True,
|
| 313 |
)
|
| 314 |
with gr.Accordion("Documentation (click to expand)", open=False):
|
| 315 |
docs = gr.TextArea(
|
| 316 |
+
get_formatted_transform_docs(DEFAULT_TRANSFORM),
|
|
|
|
|
|
|
| 317 |
show_label=False,
|
| 318 |
interactive=False,
|
| 319 |
)
|
| 320 |
code = gr.Code(
|
| 321 |
label="Code",
|
| 322 |
language="python",
|
| 323 |
+
value=get_formatted_transform(DEFAULT_TRANSFORM),
|
|
|
|
|
|
|
| 324 |
interactive=True,
|
| 325 |
lines=5,
|
| 326 |
)
|
|
|
|
| 345 |
columns=3,
|
| 346 |
show_label=False,
|
| 347 |
)
|
|
|
|
| 348 |
select.change(fn=update_code_and_docs, inputs=[select], outputs=[code, docs])
|
| 349 |
button.click(
|
| 350 |
fn=update_augmented_images, inputs=[image, code], outputs=[augmented_image]
|
| 351 |
)
|
| 352 |
+
demo.load(
|
| 353 |
+
update_code_and_docs_on_start, inputs=None, outputs=[select]
|
| 354 |
+
)
|
| 355 |
|
| 356 |
if __name__ == "__main__":
|
| 357 |
demo.launch()
|