fvilar commited on
Commit
e4eff4e
·
1 Parent(s): e3457ea

Add specimen type selector with URINE default

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -25,6 +25,8 @@ SPACE_ROOT = Path(__file__).resolve().parent
25
  WATERMARK_TEXT = "go to carb-connect.com fully use"
26
  UPGRADE_MESSAGE = "Go to carb-connect.com to keep using this app"
27
  UPGRADE_URL = "https://carb-connect.com"
 
 
28
 
29
  CUSTOM_CSS = """
30
  :root {
@@ -253,14 +255,19 @@ def _load_example_choices() -> list[tuple[str, str]]:
253
  return choices
254
 
255
 
256
- def _post_image_echo(image_path: str, hf_token: str) -> httpx.Response:
 
 
 
 
257
  infer_url = f"{TRIAL_GATEWAY_URL}/v1/trial/infer"
258
  headers = {"Authorization": f"Bearer {hf_token}"}
259
  mime_type = mimetypes.guess_type(image_path)[0] or "application/octet-stream"
 
260
 
261
  with open(image_path, "rb") as img_file:
262
  files = {"image": (Path(image_path).name, img_file, mime_type)}
263
- return httpx.post(infer_url, headers=headers, files=files, timeout=45.0)
264
 
265
 
266
  def _content_type_to_format(content_type: str) -> str:
@@ -319,6 +326,7 @@ def _apply_watermark(image_bytes: bytes, content_type: str) -> bytes:
319
 
320
  def _run_image_trial(
321
  input_image_path: str | None,
 
322
  upgrade_cta_visible: bool,
323
  oauth_token: gr.OAuthToken | None = None,
324
  ) -> tuple[str | None, str, str, bool]:
@@ -341,7 +349,7 @@ def _run_image_trial(
341
  )
342
 
343
  try:
344
- response = _post_image_echo(input_image_path, oauth_token.token)
345
  except httpx.HTTPError as error:
346
  return (
347
  None,
@@ -464,6 +472,11 @@ with gr.Blocks(
464
  if not EXAMPLE_CHOICES:
465
  gr.Markdown(NO_EXAMPLES_MESSAGE)
466
  input_image = gr.Image(type="filepath", label="Input image")
 
 
 
 
 
467
  run_button = gr.Button("Send image", elem_id="run-btn")
468
  with gr.Column(scale=1, elem_classes=["panel"]):
469
  output_image = gr.Image(type="filepath", label="Output image")
@@ -478,7 +491,7 @@ with gr.Blocks(
478
 
479
  run_button.click(
480
  _run_image_trial,
481
- inputs=[input_image, upgrade_cta_state],
482
  outputs=[output_image, trial_status, upgrade_cta_html, upgrade_cta_state],
483
  show_progress="full",
484
  )
 
25
  WATERMARK_TEXT = "go to carb-connect.com fully use"
26
  UPGRADE_MESSAGE = "Go to carb-connect.com to keep using this app"
27
  UPGRADE_URL = "https://carb-connect.com"
28
+ SPECIMEN_TYPE_OPTIONS = ["URINE", "BLOOD (AEROBIC)", "BLOOD (ANAEROBIC)"]
29
+ DEFAULT_SPECIMEN_TYPE = "URINE"
30
 
31
  CUSTOM_CSS = """
32
  :root {
 
255
  return choices
256
 
257
 
258
+ def _normalize_specimen_type(specimen_type: str | None) -> str:
259
+ return specimen_type if specimen_type in SPECIMEN_TYPE_OPTIONS else DEFAULT_SPECIMEN_TYPE
260
+
261
+
262
+ def _post_image_echo(image_path: str, hf_token: str, specimen_type: str | None) -> httpx.Response:
263
  infer_url = f"{TRIAL_GATEWAY_URL}/v1/trial/infer"
264
  headers = {"Authorization": f"Bearer {hf_token}"}
265
  mime_type = mimetypes.guess_type(image_path)[0] or "application/octet-stream"
266
+ form_data = {"specimen_type": _normalize_specimen_type(specimen_type)}
267
 
268
  with open(image_path, "rb") as img_file:
269
  files = {"image": (Path(image_path).name, img_file, mime_type)}
270
+ return httpx.post(infer_url, headers=headers, files=files, data=form_data, timeout=45.0)
271
 
272
 
273
  def _content_type_to_format(content_type: str) -> str:
 
326
 
327
  def _run_image_trial(
328
  input_image_path: str | None,
329
+ specimen_type: str | None,
330
  upgrade_cta_visible: bool,
331
  oauth_token: gr.OAuthToken | None = None,
332
  ) -> tuple[str | None, str, str, bool]:
 
349
  )
350
 
351
  try:
352
+ response = _post_image_echo(input_image_path, oauth_token.token, specimen_type)
353
  except httpx.HTTPError as error:
354
  return (
355
  None,
 
472
  if not EXAMPLE_CHOICES:
473
  gr.Markdown(NO_EXAMPLES_MESSAGE)
474
  input_image = gr.Image(type="filepath", label="Input image")
475
+ specimen_type_input = gr.Dropdown(
476
+ label="Type of specimen",
477
+ choices=SPECIMEN_TYPE_OPTIONS,
478
+ value=DEFAULT_SPECIMEN_TYPE,
479
+ )
480
  run_button = gr.Button("Send image", elem_id="run-btn")
481
  with gr.Column(scale=1, elem_classes=["panel"]):
482
  output_image = gr.Image(type="filepath", label="Output image")
 
491
 
492
  run_button.click(
493
  _run_image_trial,
494
+ inputs=[input_image, specimen_type_input, upgrade_cta_state],
495
  outputs=[output_image, trial_status, upgrade_cta_html, upgrade_cta_state],
496
  show_progress="full",
497
  )