Files changed (4) hide show
  1. README.md +1 -1
  2. __pycache__/app.cpython-310.pyc +0 -0
  3. app.py +18 -50
  4. requirements.txt +1 -1
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: FindYourTwins
3
  emoji: 📉
4
  colorFrom: green
5
  colorTo: red
 
1
  ---
2
+ title: FindYourSiblings
3
  emoji: 📉
4
  colorFrom: green
5
  colorTo: red
__pycache__/app.cpython-310.pyc DELETED
Binary file (3.13 kB)
 
app.py CHANGED
@@ -11,16 +11,12 @@ model = AutoModel.from_pretrained(model_ckpt)
11
  hidden_dim = model.config.hidden_size
12
  dataset = load_dataset("BounharAbdelaziz/Face-Aging-Dataset")
13
 
14
- TOP_K = 1000
15
- BASE_COUNT=4
16
- MAX_COUNT = 10
17
-
18
  with gr.Blocks() as demo:
19
  gr.Markdown(
20
  """
21
  # Find Your Twins
22
 
23
- Upload your face and find the most similar faces from [Face Aging Dataset](https://huggingface.co/datasets/BounharAbdelaziz/Face-Aging-Dataset) using Google's [VIT](https://huggingface.co/google/vit-base-patch16-224-in21k) model. For best results please use 1x1 ratio face images, take a look at examples. Also increasing count in the advanced section results with more accurate searches. Disclaimer, this demo doesn't find your twins :), it finds similar face parts, shapes, features(nose, cheek, face, forehead shapes) that are encoded in the model. The Vector similarity search is powered by [Upstash Vector](https://upstash.com) 🚀. You can check our blog [post](https://huggingface.co/blog/omerXfaruq/serverless-image-similarity-with-upstash-vector) to learn more.
24
  """
25
  )
26
 
@@ -28,27 +24,25 @@ with gr.Blocks() as demo:
28
  with gr.Row():
29
  with gr.Column(scale=1):
30
  input_image = gr.Image(type="pil")
31
- with gr.Column(scale=2):
32
- output_images = gr.Gallery()
 
33
 
34
- @input_image.change(inputs=input_image, outputs=output_images)
35
  async def find_similar_faces(image):
36
  if image is None:
37
  return None
38
  inputs = extractor(images=image, return_tensors="pt")
39
  outputs = model(**inputs)
40
  embed = outputs.last_hidden_state[0][0]
41
- result = await index.query(vector=embed.tolist(), top_k=TOP_K)
42
- return [dataset["train"][int(vector.id)]["image"] for vector in result[:BASE_COUNT]]
 
43
 
44
  gr.Examples(
45
- examples=[
46
- dataset["train"][6]["image"],
47
- dataset["train"][7]["image"],
48
- dataset["train"][8]["image"],
49
- ],
50
  inputs=input_image,
51
- outputs=output_images,
52
  fn=find_similar_faces,
53
  cache_examples=False,
54
  )
@@ -57,47 +51,21 @@ with gr.Blocks() as demo:
57
  with gr.Row():
58
  with gr.Column(scale=1):
59
  adv_input_image = gr.Image(type="pil")
60
- adv_image_count = gr.Slider(1, MAX_COUNT, BASE_COUNT, label="Image Count")
61
- adv_button = gr.Button("Submit")
62
 
63
- with gr.Column(scale=2):
64
- adv_output_images = gr.Gallery()
65
 
 
 
66
  async def find_similar_faces(image, count):
67
- if image is None:
68
- return None
69
  inputs = extractor(images=image, return_tensors="pt")
70
  outputs = model(**inputs)
71
  embed = outputs.last_hidden_state[0][0]
72
  result = await index.query(
73
- vector=embed.tolist(), top_k=TOP_K
74
  )
75
- return [dataset["train"][int(vector.id)]["image"] for vector in result[:int(count)]]
76
-
77
- adv_button.click(
78
- fn=find_similar_faces,
79
- inputs=[adv_input_image, adv_image_count],
80
- outputs=[adv_output_images],
81
- )
82
- adv_input_image.change(
83
- fn=find_similar_faces,
84
- inputs=[adv_input_image, adv_image_count],
85
- outputs=[adv_output_images],
86
- )
87
- gr.Examples(
88
- examples=[
89
- [dataset["train"][6]["image"], BASE_COUNT],
90
- [dataset["train"][7]["image"], BASE_COUNT],
91
- [dataset["train"][8]["image"], BASE_COUNT],
92
- ],
93
- inputs=[adv_input_image, adv_image_count],
94
- outputs=adv_output_images,
95
- fn=find_similar_faces,
96
- cache_examples=False,
97
- )
98
-
99
-
100
 
101
  if __name__ == "__main__":
102
- demo.queue(default_concurrency_limit=40)
103
- demo.launch()
 
11
  hidden_dim = model.config.hidden_size
12
  dataset = load_dataset("BounharAbdelaziz/Face-Aging-Dataset")
13
 
 
 
 
 
14
  with gr.Blocks() as demo:
15
  gr.Markdown(
16
  """
17
  # Find Your Twins
18
 
19
+ Upload your face and find the most similar people from [Face Aging Dataset](https://huggingface.co/datasets/BounharAbdelaziz/Face-Aging-Dataset) using Google's [VIT](https://huggingface.co/google/vit-base-patch16-224-in21k) model. The task of finding most similar vectors is powered by [Upstash Vector](https://upstash.com) 🚀. Check our blog post *here*.
20
  """
21
  )
22
 
 
24
  with gr.Row():
25
  with gr.Column(scale=1):
26
  input_image = gr.Image(type="pil")
27
+ with gr.Column(scale=3):
28
+ output_image = gr.Gallery(height=800)
29
+
30
 
31
+ @input_image.change(inputs=input_image, outputs=output_image)
32
  async def find_similar_faces(image):
33
  if image is None:
34
  return None
35
  inputs = extractor(images=image, return_tensors="pt")
36
  outputs = model(**inputs)
37
  embed = outputs.last_hidden_state[0][0]
38
+ result = await index.query(vector=embed.tolist(), top_k=4)
39
+ return [dataset["train"][int(vector.id)]["image"] for vector in result]
40
+
41
 
42
  gr.Examples(
43
+ examples=[dataset["train"][6]["image"]],
 
 
 
 
44
  inputs=input_image,
45
+ outputs=output_image,
46
  fn=find_similar_faces,
47
  cache_examples=False,
48
  )
 
51
  with gr.Row():
52
  with gr.Column(scale=1):
53
  adv_input_image = gr.Image(type="pil")
54
+ adv_image_count = gr.Number(9, label="Image Count")
 
55
 
56
+ with gr.Column(scale=3):
57
+ adv_output_image = gr.Gallery(height=1000)
58
 
59
+
60
+ @adv_input_image.upload(inputs=[adv_input_image, adv_image_count], outputs=[adv_output_image])
61
  async def find_similar_faces(image, count):
 
 
62
  inputs = extractor(images=image, return_tensors="pt")
63
  outputs = model(**inputs)
64
  embed = outputs.last_hidden_state[0][0]
65
  result = await index.query(
66
+ vector=embed.tolist(), top_k=max(1, min(19, count))
67
  )
68
+ return [dataset["train"][int(vector.id)]["image"] for vector in result]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  if __name__ == "__main__":
71
+ demo.launch(debug=True)
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- torch
2
  transformers
3
  datasets
4
  upstash-vector
 
1
+ torchvision
2
  transformers
3
  datasets
4
  upstash-vector