Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
from torchvision.transforms import Resize | |
from upstash_vector import Index | |
from datasets import load_dataset | |
from transformers import AutoFeatureExtractor, AutoModel | |
index = Index.from_env() | |
print(os.environ["UPSTASH_VECTOR_REST_URL"]) | |
print(os.environ["UPSTASH_VECTOR_REST_TOKEN"]) | |
resize_transform = Resize((250, 250)) | |
model_ckpt = "google/vit-base-patch16-224-in21k" | |
extractor = AutoFeatureExtractor.from_pretrained(model_ckpt) | |
model = AutoModel.from_pretrained(model_ckpt) | |
hidden_dim = model.config.hidden_size | |
dataset = load_dataset("HengJi/human_faces") | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# Find Your Twins | |
Upload your face and find the most similar people from the X dataset. Powered by [Upstash Vector](https://upstash.com) 🚀 | |
""" | |
) | |
with gr.Tab("Basic"): | |
with gr.Row(): | |
with gr.Column(scale=1): | |
input_image = gr.Image(type="pil") | |
with gr.Column(scale=3): | |
output_image = gr.Gallery() | |
def find_similar_faces(image): | |
resized_image = resize_transform(image) | |
inputs = extractor(images=resized_image, return_tensors="pt") | |
outputs = model(**inputs) | |
embed = outputs.last_hidden_state[0][0] | |
result = index.query(vector=embed.tolist(), top_k=3) | |
return [dataset["train"][int(vector.id[3:])]["image"] for vector in result] | |
with gr.Tab("Advanced"): | |
with gr.Row(): | |
with gr.Column(scale=1): | |
adv_input_image = gr.Image(type="pil") | |
adv_image_count = gr.Number(9, label="Image Count") | |
with gr.Column(scale=3): | |
adv_output_image = gr.Gallery(height=1000) | |
def find_similar_faces(image, count): | |
resized_image = resize_transform(image) | |
inputs = extractor(images=resized_image, return_tensors="pt") | |
outputs = model(**inputs) | |
embed = outputs.last_hidden_state[0][0] | |
result = index.query(vector=embed.tolist(), top_k=min(count, 9)) | |
return [dataset["train"][int(vector.id[3:])]["image"] for vector in result] | |
if __name__ == "__main__": | |
demo.launch(debug=True) | |