Spaces:
Runtime error
Runtime error
Commit
Β·
22ef1d5
1
Parent(s):
a06f639
v6
Browse files
app.py
CHANGED
|
@@ -1,176 +1,85 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
import uuid
|
| 4 |
-
import
|
| 5 |
-
import
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
-
import gradio as gr
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
genai_client = genai.Client(api_key=GEMINI_API_KEY) if GEMINI_API_KEY else None
|
| 35 |
-
|
| 36 |
-
if not QDRANT_URL:
|
| 37 |
-
raise RuntimeError("Please set QDRANT_URL environment variable")
|
| 38 |
-
|
| 39 |
-
qclient = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)
|
| 40 |
-
COLLECTION = "lost_found_items"
|
| 41 |
-
VECTOR_SIZE = clip_model.get_sentence_embedding_dimension()
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
collection_name=COLLECTION,
|
| 48 |
-
|
| 49 |
)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# -------------------------
|
| 54 |
-
# Helpers
|
| 55 |
-
# -------------------------
|
| 56 |
-
def embed_text(text: str):
|
| 57 |
-
return clip_model.encode(text, convert_to_numpy=True)
|
| 58 |
-
|
| 59 |
-
def embed_image_pil(pil_img: Image.Image):
|
| 60 |
-
pil_img = pil_img.convert("RGB")
|
| 61 |
-
return clip_model.encode(pil_img, convert_to_numpy=True)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return ""
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
"Tags should be short single words or two-word phrases (e.g. 'black backpack', 'water bottle'). "
|
| 71 |
-
"Respond only with tags, no extra explanation."
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
payload["finder_phone"] = finder_phone
|
| 92 |
-
|
| 93 |
-
if uploaded_image is not None:
|
| 94 |
-
img_bytes = io.BytesIO()
|
| 95 |
-
uploaded_image.convert("RGB").save(img_bytes, format="PNG")
|
| 96 |
-
img_bytes.seek(0)
|
| 97 |
-
|
| 98 |
-
vec = embed_image_pil(uploaded_image).tolist()
|
| 99 |
-
payload["has_image"] = True
|
| 100 |
-
|
| 101 |
-
payload["tags"] = gen_tags_from_image_file(img_bytes)
|
| 102 |
-
payload["image_b64"] = base64.b64encode(img_bytes.getvalue()).decode("utf-8")
|
| 103 |
-
else:
|
| 104 |
-
vec = embed_text(text_description).tolist()
|
| 105 |
-
payload["has_image"] = False
|
| 106 |
-
if genai_client:
|
| 107 |
-
try:
|
| 108 |
-
resp = genai_client.models.generate_content(
|
| 109 |
-
model="gemini-2.5-flash",
|
| 110 |
-
contents=f"Give 4 short, comma-separated tags for this item described as: {text_description}. Reply only with tags."
|
| 111 |
-
)
|
| 112 |
-
payload["tags"] = resp.text.strip()
|
| 113 |
-
except Exception:
|
| 114 |
-
payload["tags"] = ""
|
| 115 |
-
else:
|
| 116 |
-
payload["tags"] = ""
|
| 117 |
-
|
| 118 |
-
try:
|
| 119 |
-
point = PointStruct(id=item_id, vector=vec, payload=payload)
|
| 120 |
-
qclient.upsert(collection_name=COLLECTION, points=[point], wait=True)
|
| 121 |
except Exception as e:
|
| 122 |
-
return f"Error
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# -------------------------
|
| 127 |
-
# App logic: search
|
| 128 |
-
# -------------------------
|
| 129 |
-
def search_items(query_image, query_text, limit: int = 5, min_score: float = 0.90):
|
| 130 |
-
if query_image is not None:
|
| 131 |
-
qvec = embed_image_pil(query_image).tolist()
|
| 132 |
-
elif query_text and len(query_text.strip()) > 0:
|
| 133 |
-
qvec = embed_text(query_text).tolist()
|
| 134 |
-
else:
|
| 135 |
-
return "β οΈ Please provide a query image or some query text.", []
|
| 136 |
-
|
| 137 |
-
try:
|
| 138 |
-
hits = qclient.search(collection_name=COLLECTION, query_vector=qvec, limit=limit)
|
| 139 |
-
except Exception as e:
|
| 140 |
-
return f"β Error querying Qdrant: {e}", []
|
| 141 |
-
|
| 142 |
-
if not hits:
|
| 143 |
-
return "No results found.", []
|
| 144 |
-
|
| 145 |
-
results_text = []
|
| 146 |
-
results_imgs = []
|
| 147 |
-
for h in hits:
|
| 148 |
-
score = getattr(h, "score", None)
|
| 149 |
-
if score is None or score < min_score:
|
| 150 |
-
continue
|
| 151 |
-
|
| 152 |
-
payload = h.payload or {}
|
| 153 |
-
text_entry = (
|
| 154 |
-
f"id:{h.id} | score:{score:.4f} | mode:{payload.get('mode','')} | tags:{payload.get('tags','')} "
|
| 155 |
-
f"| text:{payload.get('text','')} | finder:{payload.get('finder_name','-')} | phone:{payload.get('finder_phone','-')}"
|
| 156 |
-
)
|
| 157 |
-
results_text.append(text_entry)
|
| 158 |
-
|
| 159 |
-
if payload.get("has_image") and "image_b64" in payload:
|
| 160 |
-
try:
|
| 161 |
-
img = Image.open(io.BytesIO(base64.b64decode(payload["image_b64"])))
|
| 162 |
-
results_imgs.append(img)
|
| 163 |
-
except Exception:
|
| 164 |
-
pass
|
| 165 |
-
|
| 166 |
-
if not results_text:
|
| 167 |
-
return f"No results above similarity threshold {min_score}", []
|
| 168 |
-
|
| 169 |
-
return "\n\n".join(results_text), results_imgs
|
| 170 |
-
|
| 171 |
-
# -------------------------
|
| 172 |
-
# App logic: clear images
|
| 173 |
-
# -------------------------
|
| 174 |
def clear_all_images():
|
| 175 |
try:
|
| 176 |
qclient.delete(
|
|
@@ -179,38 +88,37 @@ def clear_all_images():
|
|
| 179 |
"filter": {"must": [{"key": "has_image", "match": {"value": True}}]}
|
| 180 |
}
|
| 181 |
)
|
| 182 |
-
return "ποΈ All items
|
| 183 |
except Exception as e:
|
| 184 |
-
return f"β Error
|
| 185 |
|
| 186 |
-
# -------------------------
|
| 187 |
# Gradio UI
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
with gr.
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import uuid
|
| 3 |
+
from qdrant_client import QdrantClient
|
| 4 |
+
from qdrant_client.models import PointStruct
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
# Connect to Qdrant
|
| 10 |
+
COLLECTION = "lost_and_found"
|
| 11 |
+
qclient = QdrantClient(":memory:") # use in-memory for demo, replace with host/port for persistence
|
| 12 |
+
|
| 13 |
+
# Load CLIP model
|
| 14 |
+
model = SentenceTransformer("sentence-transformers/clip-ViT-B-32-multilingual-v1")
|
| 15 |
+
|
| 16 |
+
# Ensure collection exists
|
| 17 |
+
qclient.recreate_collection(
|
| 18 |
+
collection_name=COLLECTION,
|
| 19 |
+
vectors_config={"size": 512, "distance": "Cosine"}
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Encode helper
|
| 23 |
+
def encode_data(text=None, image=None):
|
| 24 |
+
if image is not None:
|
| 25 |
+
img = Image.open(image).convert("RGB")
|
| 26 |
+
emb = model.encode(img, convert_to_numpy=True, normalize_embeddings=True)
|
| 27 |
+
elif text:
|
| 28 |
+
emb = model.encode(text, convert_to_numpy=True, normalize_embeddings=True)
|
| 29 |
+
else:
|
| 30 |
+
raise ValueError("Need text or image")
|
| 31 |
+
return emb.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Add item
|
| 34 |
+
def add_item(mode, text, image, name, phone):
|
| 35 |
+
try:
|
| 36 |
+
vector = encode_data(text=text if text else None, image=image if image else None)
|
| 37 |
+
payload = {
|
| 38 |
+
"mode": mode,
|
| 39 |
+
"text": text,
|
| 40 |
+
"has_image": image is not None,
|
| 41 |
+
}
|
| 42 |
+
if mode == "found":
|
| 43 |
+
payload["finder_name"] = name
|
| 44 |
+
payload["finder_phone"] = phone
|
| 45 |
+
qclient.upsert(
|
| 46 |
collection_name=COLLECTION,
|
| 47 |
+
points=[PointStruct(id=str(uuid.uuid4()), vector=vector.tolist(), payload=payload)]
|
| 48 |
)
|
| 49 |
+
return "β
Item added successfully!"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"β Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Search items
|
| 54 |
+
def search_items(query_image, query_text, limit, min_score):
|
|
|
|
| 55 |
try:
|
| 56 |
+
query_vector = encode_data(
|
| 57 |
+
text=query_text if query_text else None,
|
| 58 |
+
image=query_image if query_image else None
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
+
results = qclient.search(
|
| 61 |
+
collection_name=COLLECTION,
|
| 62 |
+
query_vector=query_vector.tolist(),
|
| 63 |
+
limit=limit,
|
| 64 |
)
|
| 65 |
+
out_texts, out_imgs = [], []
|
| 66 |
+
for r in results:
|
| 67 |
+
if r.score < min_score:
|
| 68 |
+
continue
|
| 69 |
+
pl = r.payload
|
| 70 |
+
info = f"id:{r.id} | score:{r.score:.4f} | mode:{pl.get('mode','')}"
|
| 71 |
+
if pl.get("text"):
|
| 72 |
+
info += f" | text:{pl['text']}"
|
| 73 |
+
if pl.get("mode") == "found":
|
| 74 |
+
info += f" | found by: {pl.get('finder_name','?')} ({pl.get('finder_phone','?')})"
|
| 75 |
+
out_texts.append(info)
|
| 76 |
+
if pl.get("has_image"):
|
| 77 |
+
out_imgs.append(query_image) # just echo search image (or store actual image paths if needed)
|
| 78 |
+
return "\n".join(out_texts) if out_texts else "No matches.", out_imgs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"β Error: {e}", []
|
| 81 |
|
| 82 |
+
# Clear all images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def clear_all_images():
|
| 84 |
try:
|
| 85 |
qclient.delete(
|
|
|
|
| 88 |
"filter": {"must": [{"key": "has_image", "match": {"value": True}}]}
|
| 89 |
}
|
| 90 |
)
|
| 91 |
+
return "ποΈ All image items cleared!"
|
| 92 |
except Exception as e:
|
| 93 |
+
return f"β Error clearing images: {e}"
|
| 94 |
|
|
|
|
| 95 |
# Gradio UI
|
| 96 |
+
with gr.Blocks() as demo:
|
| 97 |
+
gr.Markdown("# π Lost & Found System")
|
| 98 |
+
|
| 99 |
+
with gr.Tab("β Add Item"):
|
| 100 |
+
mode = gr.Radio(["lost", "found"], label="Mode")
|
| 101 |
+
text = gr.Textbox(label="Describe the item (optional)")
|
| 102 |
+
img = gr.Image(type="filepath", label="Upload image (optional)")
|
| 103 |
+
name = gr.Textbox(label="Finder's Name (only if found)", placeholder="John Doe")
|
| 104 |
+
phone = gr.Textbox(label="Finder's Phone (only if found)", placeholder="+1234567890")
|
| 105 |
+
add_btn = gr.Button("Add Item")
|
| 106 |
+
add_out = gr.Textbox(label="Add result")
|
| 107 |
+
add_btn.click(add_item, inputs=[mode, text, img, name, phone], outputs=add_out)
|
| 108 |
+
|
| 109 |
+
with gr.Tab("π Search"):
|
| 110 |
+
query_text = gr.Textbox(label="Search by text (optional)")
|
| 111 |
+
query_img = gr.Image(type="filepath", label="Search by image (optional)")
|
| 112 |
+
max_results = gr.Slider(1, 10, value=5, step=1, label="Max results")
|
| 113 |
+
score_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.01, label="Min similarity threshold")
|
| 114 |
+
search_btn = gr.Button("Search")
|
| 115 |
+
search_out = gr.Textbox(label="Search results (text)")
|
| 116 |
+
gallery = gr.Gallery(label="Search Results", show_label=True, elem_id="gallery", columns=2, height="auto")
|
| 117 |
+
search_btn.click(search_items, inputs=[query_img, query_text, max_results, score_slider], outputs=[search_out, gallery])
|
| 118 |
+
|
| 119 |
+
with gr.Tab("ποΈ Admin"):
|
| 120 |
+
clear_btn = gr.Button("Clear All Images")
|
| 121 |
+
clear_out = gr.Textbox(label="Clear Result")
|
| 122 |
+
clear_btn.click(clear_all_images, outputs=clear_out)
|
| 123 |
+
|
| 124 |
+
demo.launch()
|