Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,26 @@ from PIL import Image
|
|
| 5 |
import base64
|
| 6 |
import io
|
| 7 |
|
| 8 |
-
|
| 9 |
processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
|
| 10 |
model = AutoModel.from_pretrained("facebook/dinov2-base")
|
| 11 |
|
| 12 |
-
# Recibe string base64
|
| 13 |
def get_embedding(base64_str):
|
| 14 |
-
# Separar encabezado
|
| 15 |
header, encoded = base64_str.split(",", 1)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 19 |
|
| 20 |
-
# Procesar
|
| 21 |
inputs = processor(images=image, return_tensors="pt")
|
| 22 |
with torch.no_grad():
|
| 23 |
embeddings = model(**inputs).last_hidden_state[:, 0]
|
| 24 |
return embeddings.squeeze().tolist()
|
| 25 |
|
| 26 |
-
|
| 27 |
-
iface.
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import base64
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
|
| 9 |
processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
|
| 10 |
model = AutoModel.from_pretrained("facebook/dinov2-base")
|
| 11 |
|
|
|
|
| 12 |
def get_embedding(base64_str):
|
|
|
|
| 13 |
header, encoded = base64_str.split(",", 1)
|
| 14 |
+
image_data = base64.b64decode(encoded)
|
| 15 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
|
|
|
| 16 |
|
|
|
|
| 17 |
inputs = processor(images=image, return_tensors="pt")
|
| 18 |
with torch.no_grad():
|
| 19 |
embeddings = model(**inputs).last_hidden_state[:, 0]
|
| 20 |
return embeddings.squeeze().tolist()
|
| 21 |
|
| 22 |
+
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=get_embedding,
|
| 25 |
+
inputs="text", # ahora recibimos un string base64
|
| 26 |
+
outputs="json",
|
| 27 |
+
description="Microservicio para extraer embeddings desde base64."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
iface.queue() # 👈 Esta línea activa el sistema de event_id y polling
|