omerXfaruq commited on
Commit
77403d5
1 Parent(s): d21fc46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -1,10 +1,18 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
2
 
3
  with gr.Blocks() as demo:
4
  gr.Markdown(
5
  """
6
  # Find Your Twins
7
-
8
  Upload your face and find the most similar people from the X dataset. Powered by [Upstash Vector](https://upstash.com) 🚀
9
  """
10
  )
@@ -12,28 +20,39 @@ with gr.Blocks() as demo:
12
  with gr.Tab("Basic"):
13
  with gr.Row():
14
  with gr.Column(scale=1):
15
- input_image = gr.Image()
16
  with gr.Column(scale=3):
17
  output_image = gr.Gallery()
18
 
19
-
20
- @input_image.change(inputs=input_image, outputs=output_image)
21
- def find_similar_faces(image):
22
- return [image] * 3
23
-
 
 
 
 
 
24
  with gr.Tab("Advanced"):
25
  with gr.Row():
26
  with gr.Column(scale=1):
27
- adv_input_image = gr.Image()
28
  adv_image_count = gr.Number(9, label="Image Count")
29
 
30
  with gr.Column(scale=3):
31
  adv_output_image = gr.Gallery(height=1000)
32
 
33
 
34
- @adv_input_image.change(inputs=[adv_input_image, adv_image_count], outputs=[adv_output_image])
35
  def find_similar_faces(image, count):
36
- return [image] * count
 
 
 
 
 
 
37
 
38
  if __name__ == "__main__":
39
- demo.launch()
 
1
  import gradio as gr
2
+ from torchvision.transforms import Resize
3
+ import torch
4
+ from upstash_vector import Index
5
+ import os
6
+
7
+ index = Index.from_env()
8
+
9
+ resize_transform = Resize((250,250))
10
 
11
  with gr.Blocks() as demo:
12
  gr.Markdown(
13
  """
14
  # Find Your Twins
15
+
16
  Upload your face and find the most similar people from the X dataset. Powered by [Upstash Vector](https://upstash.com) 🚀
17
  """
18
  )
 
20
  with gr.Tab("Basic"):
21
  with gr.Row():
22
  with gr.Column(scale=1):
23
+ input_image = gr.Image(type="pil")
24
  with gr.Column(scale=3):
25
  output_image = gr.Gallery()
26
 
27
+
28
+ @input_image.upload(inputs=input_image, outputs=output_image)
29
+ def find_similar_faces(image):
30
+ resized_image = resize_transform(image)
31
+ inputs = extractor(images=image, return_tensors="pt")
32
+ outputs = model(**inputs)
33
+ embed = outputs.last_hidden_state[0][0]
34
+ result = index.query(vector=embed.tolist(), top_k=3)
35
+ return[dataset["train"][int(vector.id[3:])]["image"] for vector in result]
36
+
37
  with gr.Tab("Advanced"):
38
  with gr.Row():
39
  with gr.Column(scale=1):
40
+ adv_input_image = gr.Image(type="pil")
41
  adv_image_count = gr.Number(9, label="Image Count")
42
 
43
  with gr.Column(scale=3):
44
  adv_output_image = gr.Gallery(height=1000)
45
 
46
 
47
+ @adv_input_image.upload(inputs=[adv_input_image, adv_image_count], outputs=[adv_output_image])
48
  def find_similar_faces(image, count):
49
+ resized_image = resize_transform(image)
50
+ inputs = extractor(images=image, return_tensors="pt")
51
+ outputs = model(**inputs)
52
+ embed = outputs.last_hidden_state[0][0]
53
+ result = index.query(vector=embed.tolist(), top_k=min(count, 9))
54
+ return[dataset["train"][int(vector.id[3:])]["image"] for vector in result]
55
+
56
 
57
  if __name__ == "__main__":
58
+ demo.launch(debug=True)