Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Visual Search API - HuggingFace Space
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
|
@@ -7,12 +8,7 @@ import gradio as gr
|
|
| 7 |
import torch
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
-
|
| 11 |
-
# Pinecone config from HF Secrets
|
| 12 |
-
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
|
| 13 |
-
PINECONE_HOST = os.environ.get('PINECONE_HOST')
|
| 14 |
-
|
| 15 |
-
print(f"Pinecone configured: Key={'yes' if PINECONE_API_KEY else 'NO'}, Host={'yes' if PINECONE_HOST else 'NO'}")
|
| 16 |
|
| 17 |
# Model (loaded on first use)
|
| 18 |
model = None
|
|
@@ -48,79 +44,36 @@ def get_embedding(image: Image.Image) -> list:
|
|
| 48 |
return emb.tolist()
|
| 49 |
|
| 50 |
|
| 51 |
-
def query_pinecone(embedding: list, top_k: int = 12) -> list:
|
| 52 |
-
"""Query Pinecone for similar products."""
|
| 53 |
-
if not PINECONE_API_KEY or not PINECONE_HOST:
|
| 54 |
-
return []
|
| 55 |
-
|
| 56 |
-
import requests
|
| 57 |
-
|
| 58 |
-
resp = requests.post(
|
| 59 |
-
f"https://{PINECONE_HOST}/query",
|
| 60 |
-
headers={
|
| 61 |
-
"Api-Key": PINECONE_API_KEY,
|
| 62 |
-
"Content-Type": "application/json"
|
| 63 |
-
},
|
| 64 |
-
json={
|
| 65 |
-
"vector": embedding,
|
| 66 |
-
"topK": top_k,
|
| 67 |
-
"includeMetadata": True
|
| 68 |
-
},
|
| 69 |
-
timeout=15
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
if resp.status_code != 200:
|
| 73 |
-
return []
|
| 74 |
-
|
| 75 |
-
matches = resp.json().get('matches', [])
|
| 76 |
-
return [
|
| 77 |
-
{
|
| 78 |
-
'handle': m.get('metadata', {}).get('handle', m.get('id')),
|
| 79 |
-
'title': m.get('metadata', {}).get('title', ''),
|
| 80 |
-
'score': m.get('score', 0),
|
| 81 |
-
}
|
| 82 |
-
for m in matches
|
| 83 |
-
]
|
| 84 |
-
|
| 85 |
-
|
| 86 |
def search(image):
|
| 87 |
-
"""
|
| 88 |
if image is None:
|
| 89 |
-
return "No image provided"
|
| 90 |
|
| 91 |
try:
|
| 92 |
-
print("
|
| 93 |
embedding = get_embedding(image)
|
| 94 |
-
print(f"Embedding
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
if not products:
|
| 104 |
-
return "No similar products found (check if index has data)"
|
| 105 |
|
| 106 |
-
result = "\n".join([
|
| 107 |
-
f"{i+1}. {p['title']} ({p['handle']}) - score: {p['score']:.3f}"
|
| 108 |
-
for i, p in enumerate(products)
|
| 109 |
-
])
|
| 110 |
-
return result
|
| 111 |
except Exception as e:
|
| 112 |
import traceback
|
| 113 |
traceback.print_exc()
|
| 114 |
-
return
|
| 115 |
|
| 116 |
|
| 117 |
-
#
|
| 118 |
demo = gr.Interface(
|
| 119 |
fn=search,
|
| 120 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 121 |
-
outputs=gr.Textbox(label="
|
| 122 |
-
title="Visual
|
| 123 |
-
description="Upload an image to
|
| 124 |
)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
|
|
|
| 1 |
"""
|
| 2 |
Visual Search API - HuggingFace Space
|
| 3 |
+
Returns embedding vector for external Pinecone queries
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 8 |
import torch
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Model (loaded on first use)
|
| 14 |
model = None
|
|
|
|
| 44 |
return emb.tolist()
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def search(image):
|
| 48 |
+
"""Return embedding vector as JSON."""
|
| 49 |
if image is None:
|
| 50 |
+
return json.dumps({"error": "No image provided"})
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
print("Generating embedding...")
|
| 54 |
embedding = get_embedding(image)
|
| 55 |
+
print(f"Embedding generated: {len(embedding)} dimensions")
|
| 56 |
|
| 57 |
+
# Return embedding as JSON
|
| 58 |
+
result = {
|
| 59 |
+
"embedding": embedding,
|
| 60 |
+
"dimensions": len(embedding)
|
| 61 |
+
}
|
| 62 |
+
return json.dumps(result, indent=2)
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
import traceback
|
| 66 |
traceback.print_exc()
|
| 67 |
+
return json.dumps({"error": str(e)})
|
| 68 |
|
| 69 |
|
| 70 |
+
# Gradio interface - returns embedding as JSON
|
| 71 |
demo = gr.Interface(
|
| 72 |
fn=search,
|
| 73 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 74 |
+
outputs=gr.Textbox(label="Embedding Vector (JSON)", lines=15),
|
| 75 |
+
title="Visual Search - Embedding Generator",
|
| 76 |
+
description="Upload an image to get its 512-dimensional CLIP embedding as JSON."
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|