yonadab commited on
Commit
61bc9be
·
verified ·
1 Parent(s): 2ee2ea3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor, AutoModel
3
+ import torch
4
+
5
+ # Cargar el modelo DINOv2 una sola vez
6
+ processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
7
+ model = AutoModel.from_pretrained("facebook/dinov2-base")
8
+
9
+ def get_embedding(image):
10
+ inputs = processor(images=image, return_tensors="pt")
11
+ with torch.no_grad():
12
+ embeddings = model(**inputs).last_hidden_state[:, 0] # CLS token
13
+ return embeddings.squeeze().tolist()
14
+
15
+ # Gradio UI solo para aceptar imágenes y devolver JSON
16
+ iface = gr.Interface(
17
+ fn=get_embedding,
18
+ inputs=gr.Image(type="pil"),
19
+ outputs="json",
20
+ description="Microservicio para extraer embeddings de imágenes usando DINOv2."
21
+ )
22
+
23
+ iface.launch()