Spaces:
Runtime error
Runtime error
omerXfaruq
commited on
Commit
·
83305ef
1
Parent(s):
b33e6dd
- Add examples
Browse files- Switch to async client
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from upstash_vector import
|
3 |
-
from datasets import load_dataset
|
4 |
from transformers import AutoFeatureExtractor, AutoModel
|
|
|
5 |
|
6 |
-
index =
|
7 |
|
8 |
model_ckpt = "google/vit-base-patch16-224-in21k"
|
9 |
extractor = AutoFeatureExtractor.from_pretrained(model_ckpt)
|
@@ -16,7 +16,7 @@ with gr.Blocks() as demo:
|
|
16 |
"""
|
17 |
# Find Your Twins
|
18 |
|
19 |
-
Upload your face and find the most similar people from BounharAbdelaziz/Face-Aging-Dataset
|
20 |
"""
|
21 |
)
|
22 |
|
@@ -28,14 +28,25 @@ with gr.Blocks() as demo:
|
|
28 |
output_image = gr.Gallery(height=800)
|
29 |
|
30 |
|
31 |
-
@input_image.
|
32 |
-
def find_similar_faces(image):
|
|
|
|
|
33 |
inputs = extractor(images=image, return_tensors="pt")
|
34 |
outputs = model(**inputs)
|
35 |
embed = outputs.last_hidden_state[0][0]
|
36 |
-
result = index.query(vector=embed.tolist(), top_k=4)
|
37 |
return [dataset["train"][int(vector.id)]["image"] for vector in result]
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
with gr.Tab("Advanced"):
|
40 |
with gr.Row():
|
41 |
with gr.Column(scale=1):
|
@@ -46,14 +57,14 @@ with gr.Blocks() as demo:
|
|
46 |
adv_output_image = gr.Gallery(height=1000)
|
47 |
|
48 |
|
49 |
-
@adv_input_image.upload(
|
50 |
-
|
51 |
-
)
|
52 |
-
def find_similar_faces(image, count):
|
53 |
inputs = extractor(images=image, return_tensors="pt")
|
54 |
outputs = model(**inputs)
|
55 |
embed = outputs.last_hidden_state[0][0]
|
56 |
-
result = index.query(
|
|
|
|
|
57 |
return [dataset["train"][int(vector.id)]["image"] for vector in result]
|
58 |
|
59 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
+
from upstash_vector import AsyncIndex
|
|
|
3 |
from transformers import AutoFeatureExtractor, AutoModel
|
4 |
+
from datasets import load_dataset
|
5 |
|
6 |
+
index = AsyncIndex.from_env()
|
7 |
|
8 |
model_ckpt = "google/vit-base-patch16-224-in21k"
|
9 |
extractor = AutoFeatureExtractor.from_pretrained(model_ckpt)
|
|
|
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 |
|
|
|
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 |
+
)
|
49 |
+
|
50 |
with gr.Tab("Advanced"):
|
51 |
with gr.Row():
|
52 |
with gr.Column(scale=1):
|
|
|
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__":
|