Spaces:
Running
on
Zero
Running
on
Zero
v1
Browse files
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Image Embeddings
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Image Embeddings
|
| 3 |
+
emoji: 🖼️
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
processor = AutoImageProcessor.from_pretrained("nomic-ai/nomic-embed-vision-v1.5")
|
| 7 |
+
vision_model = AutoModel.from_pretrained("nomic-ai/nomic-embed-vision-v1.5", trust_remote_code=True)
|
| 8 |
+
|
| 9 |
+
@spaces.GPU
|
| 10 |
+
def ImgEmbed(image):
|
| 11 |
+
print(image);
|
| 12 |
+
inputs = processor(image, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
img_emb = vision_model(**inputs).last_hidden_state
|
| 15 |
+
img_embeddings = F.normalize(img_emb[:, 0], p=2, dim=1)
|
| 16 |
+
|
| 17 |
+
return img_embeddings[0].tolist();
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
img = gr.Image();
|
| 23 |
+
out = gr.Text();
|
| 24 |
+
|
| 25 |
+
btn = gr.Button("Get Embeddings")
|
| 26 |
+
btn.click(ImgEmbed, [img], [out])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
demo.launch(show_api=True)
|