sayakpaul HF staff commited on
Commit
acc1570
1 Parent(s): 01d1259

refactor: user facing app code.

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -9,6 +9,8 @@ import gradio as gr
9
  from datasets import load_dataset
10
  from transformers import AutoModel
11
 
 
 
12
  from similarity_utils import LSH, BuildLSHTable, Table
13
 
14
  seed = 42
@@ -45,11 +47,8 @@ def query(image, top_k):
45
  candidates.append(candidate_dataset[int(image_id)]["image"])
46
  labels.append(f"Label: {label}")
47
 
48
- candidates.insert(0, image)
49
- labels.insert(0, "Query Image")
50
-
51
  for i, candidate in enumerate(candidates):
52
- filename = f"{i}.png"
53
  candidate.save(filename)
54
  images.append(filename)
55
 
@@ -58,12 +57,18 @@ def query(image, top_k):
58
  return list(zip(images, labels))
59
 
60
 
 
 
 
 
61
  # You can set the type of gr.Image to be PIL, numpy or str (filepath)
62
  # Not sure what the best for this demo is.
63
  gr.Interface(
64
  query,
65
  inputs=[gr.Image(type="pil"), gr.Slider(value=5, minimum=1, maximum=10, step=1)],
66
- outputs=gr.Gallery(),
67
  # Filenames denote the integer labels. Know here: https://hf.co/datasets/beans
 
 
68
  examples=[["0.png", 5], ["1.png", 5], ["2.png", 5]],
69
  ).launch()
 
9
  from datasets import load_dataset
10
  from transformers import AutoModel
11
 
12
+ # `LSH` and `Table` imports are necessary in order for the
13
+ # `lsh.pickle` file to load successfully.
14
  from similarity_utils import LSH, BuildLSHTable, Table
15
 
16
  seed = 42
 
47
  candidates.append(candidate_dataset[int(image_id)]["image"])
48
  labels.append(f"Label: {label}")
49
 
 
 
 
50
  for i, candidate in enumerate(candidates):
51
+ filename = f"similar_{i}.png"
52
  candidate.save(filename)
53
  images.append(filename)
54
 
 
57
  return list(zip(images, labels))
58
 
59
 
60
+ title = "Fetch similar beans."
61
+ description = "This Space demos an image similarity system. You can refer to [this notebook](TODO) to know the details of the system. You can pick any image from the available samples below. On the right hand side, you'll find the similar images returned"
62
+ " by the system. The example images have been named with their corresponding integer class labels for easier identification. The fetched images will also have their integer labels tagged so that you can validate the correctness of the results."
63
+
64
  # You can set the type of gr.Image to be PIL, numpy or str (filepath)
65
  # Not sure what the best for this demo is.
66
  gr.Interface(
67
  query,
68
  inputs=[gr.Image(type="pil"), gr.Slider(value=5, minimum=1, maximum=10, step=1)],
69
+ outputs=gr.Gallery().style(grid=[3], height="auto"),
70
  # Filenames denote the integer labels. Know here: https://hf.co/datasets/beans
71
+ title=title,
72
+ description=description,
73
  examples=[["0.png", 5], ["1.png", 5], ["2.png", 5]],
74
  ).launch()