Spaces:
Running
on
Zero
Running
on
Zero
πwπ
Browse files- app.py +34 -4
- requirements.txt +5 -0
app.py
CHANGED
|
@@ -1,6 +1,36 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoProcessor, AutoModelForZeroShotImageClassification
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
|
| 7 |
+
dataset = load_dataset("not-lain/embedded-pokemon", split="train")
|
| 8 |
+
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
processor = AutoProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
| 12 |
+
model = AutoModelForZeroShotImageClassification.from_pretrained(
|
| 13 |
+
"openai/clip-vit-large-patch14", device_map=device
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def search(query: str, k: int = 4 ):
|
| 19 |
+
"""a function that embeds a new image and returns the most probable results"""
|
| 20 |
+
|
| 21 |
+
pixel_values = processor(images = query, return_tensors="pt")['pixel_values'] # embed new image
|
| 22 |
+
pixel_values = pixel_values.to(device)
|
| 23 |
+
img_emb = model.get_image_features(pixel_values)[0] # because 1 element
|
| 24 |
+
img_emb = img_emb.cpu().detach().numpy() # because datasets only works with numpy
|
| 25 |
+
|
| 26 |
+
scores, retrieved_examples = dataset.get_nearest_examples( # retrieve results
|
| 27 |
+
"embeddings", img_emb, # compare our new embedded query with the dataset embeddings
|
| 28 |
+
k=k # get only top k results
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
return retrieved_examples["image"]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface(search, inputs="image", outputs=["gallery"])
|
| 35 |
+
|
| 36 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets
|
| 2 |
+
torch
|
| 3 |
+
spaces
|
| 4 |
+
accelerate
|
| 5 |
+
faiss-cpu
|