Spaces:
Sleeping
Sleeping
Commit ·
4d3c123
1
Parent(s): 38c0157
Final fix: filter faiss -1 results — 100% stable on HF Spaces
Browse files
app.py
CHANGED
|
@@ -13,25 +13,30 @@ index = faiss.read_index("partvision_index.faiss")
|
|
| 13 |
skus = pickle.load(open("partvision_sku_mapping.pkl", "rb"))
|
| 14 |
|
| 15 |
def search(image):
|
| 16 |
-
# Handle Gradio input (numpy or PIL)
|
| 17 |
if isinstance(image, np.ndarray):
|
| 18 |
image = Image.fromarray(image.astype('uint8'))
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
inputs = processor(images=image, return_tensors="pt")
|
| 22 |
with torch.no_grad():
|
| 23 |
query = model.get_image_features(**inputs)
|
| 24 |
query /= query.norm(dim=-1, keepdim=True)
|
| 25 |
-
|
| 26 |
D, I = index.search(query.cpu().numpy(), k=5)
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
gr.Interface(
|
| 31 |
fn=search,
|
| 32 |
inputs=gr.Image(type="pil", label="Drop or snap any marine part"),
|
| 33 |
outputs=gr.Textbox(label="Top 5 CWR Matches"),
|
| 34 |
title="PicSea PartVision — 34 439 Parts @ 94%+ Accuracy",
|
| 35 |
-
description="Impellers • Anodes • Filters • Belts • Starters — instant CWR
|
| 36 |
live=True
|
| 37 |
).launch()
|
|
|
|
| 13 |
skus = pickle.load(open("partvision_sku_mapping.pkl", "rb"))
|
| 14 |
|
| 15 |
def search(image):
|
|
|
|
| 16 |
if isinstance(image, np.ndarray):
|
| 17 |
image = Image.fromarray(image.astype('uint8'))
|
| 18 |
+
elif isinstance(image, str):
|
| 19 |
+
image = Image.open(image)
|
| 20 |
+
|
| 21 |
inputs = processor(images=image, return_tensors="pt")
|
| 22 |
with torch.no_grad():
|
| 23 |
query = model.get_image_features(**inputs)
|
| 24 |
query /= query.norm(dim=-1, keepdim=True)
|
| 25 |
+
|
| 26 |
D, I = index.search(query.cpu().numpy(), k=5)
|
| 27 |
+
|
| 28 |
+
results = []
|
| 29 |
+
for i, dist in zip(I[0], D[0]):
|
| 30 |
+
if i != -1 and i < len(skus): # ← THIS LINE FIXES THE CRASH
|
| 31 |
+
results.append(f"SKU {skus[i]} — {float(dist):.1%} match")
|
| 32 |
+
|
| 33 |
+
return "\n".join(results) if results else "No close match found — try a clearer photo"
|
| 34 |
|
| 35 |
gr.Interface(
|
| 36 |
fn=search,
|
| 37 |
inputs=gr.Image(type="pil", label="Drop or snap any marine part"),
|
| 38 |
outputs=gr.Textbox(label="Top 5 CWR Matches"),
|
| 39 |
title="PicSea PartVision — 34 439 Parts @ 94%+ Accuracy",
|
| 40 |
+
description="Impellers • Anodes • Filters • Belts • Starters — instant CWR match",
|
| 41 |
live=True
|
| 42 |
).launch()
|