Spaces:
Runtime error
Runtime error
| # app.py | |
| import os | |
| import uuid | |
| import io | |
| import base64 | |
| from PIL import Image | |
| import gradio as gr | |
| import numpy as np | |
| from sentence_transformers import SentenceTransformer | |
| from google import genai | |
| from qdrant_client import QdrantClient | |
| from qdrant_client.http.models import VectorParams, Distance, PointStruct | |
| # ------------------------- | |
| # CONFIG | |
| # ------------------------- | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "").strip() | |
| QDRANT_URL = os.environ.get("QDRANT_URL", "").strip() | |
| QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY", "").strip() | |
| print("Loading CLIP model (this may take 20-60s the first time)...") | |
| MODEL_ID = "sentence-transformers/clip-ViT-B-32-multilingual-v1" | |
| clip_model = SentenceTransformer(MODEL_ID) | |
| VECTOR_SIZE = clip_model.get_sentence_embedding_dimension() | |
| genai_client = genai.Client(api_key=GEMINI_API_KEY) if GEMINI_API_KEY else None | |
| if not QDRANT_URL: | |
| raise RuntimeError("Please set QDRANT_URL environment variable") | |
| qclient = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY) | |
| COLLECTION = "lost_found_items" | |
| try: | |
| if not qclient.collection_exists(COLLECTION): | |
| qclient.create_collection( | |
| collection_name=COLLECTION, | |
| vectors_config=VectorParams(size=VECTOR_SIZE, distance=Distance.COSINE), | |
| ) | |
| except Exception as e: | |
| print("Error initializing Qdrant collection:", e) | |
| # ------------------------- | |
| # Helpers | |
| # ------------------------- | |
| def embed_text(text: str): | |
| return clip_model.encode([text], convert_to_numpy=True)[0] | |
| def embed_image_pil(pil_img: Image.Image): | |
| pil_img = pil_img.convert("RGB") | |
| np_img = np.array(pil_img) | |
| return clip_model.encode([np_img], convert_to_numpy=True)[0] | |
| def gen_tags_from_image_file(image_bytes: io.BytesIO) -> str: | |
| if genai_client is None: | |
| return "" | |
| try: | |
| file_obj = genai_client.files.upload(file=image_bytes) | |
| prompt_text = ( | |
| "Give 4 short tags (comma-separated) describing this item in the image. " | |
| "Tags should be short single words or two-word phrases. Respond only with tags." | |
| ) | |
| response = genai_client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=[prompt_text, file_obj], | |
| ) | |
| return response.text.strip() | |
| except Exception as e: | |
| print("Error generating tags:", e) | |
| return "" | |
| def decode_image_from_b64(b64_str: str): | |
| try: | |
| img_bytes = base64.b64decode(b64_str) | |
| return Image.open(io.BytesIO(img_bytes)) | |
| except Exception: | |
| return None | |
| # ------------------------- | |
| # Add item | |
| # ------------------------- | |
| def add_item(mode: str, uploaded_image, text_description: str, finder_name: str, finder_phone: str): | |
| item_id = str(uuid.uuid4()) | |
| payload = {"mode": mode, "text": text_description} | |
| # If "found", save finder info | |
| if mode == "found": | |
| payload["finder_name"] = finder_name | |
| payload["finder_phone"] = finder_phone | |
| try: | |
| if uploaded_image is not None: | |
| img_bytes = io.BytesIO() | |
| uploaded_image.convert("RGB").save(img_bytes, format="PNG") | |
| img_bytes.seek(0) | |
| vec = embed_image_pil(uploaded_image).tolist() | |
| payload["has_image"] = True | |
| payload["tags"] = gen_tags_from_image_file(img_bytes) | |
| payload["image_b64"] = base64.b64encode(img_bytes.getvalue()).decode("utf-8") | |
| else: | |
| vec = embed_text(text_description).tolist() | |
| payload["has_image"] = False | |
| if genai_client: | |
| try: | |
| resp = genai_client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=f"Give 4 short, comma-separated tags for this item described as: {text_description}. Reply only with tags." | |
| ) | |
| payload["tags"] = resp.text.strip() | |
| except Exception: | |
| payload["tags"] = "" | |
| else: | |
| payload["tags"] = "" | |
| point = PointStruct(id=item_id, vector=vec, payload=payload) | |
| qclient.upsert(collection_name=COLLECTION, points=[point], wait=True) | |
| return f"β Saved item id: {item_id}\nTags: {payload.get('tags','')}" | |
| except Exception as e: | |
| return f"β Error saving to Qdrant: {e}" | |
| # ------------------------- | |
| # Search | |
| # ------------------------- | |
| def search_items(query_image, query_text, limit: int = 5, min_score: float = 0.90): | |
| if query_image is not None: | |
| qvec = embed_image_pil(query_image).tolist() | |
| elif query_text and len(query_text.strip()) > 0: | |
| qvec = embed_text(query_text).tolist() | |
| else: | |
| return [], "β οΈ Please provide a query image or some query text." | |
| try: | |
| hits = qclient.search(collection_name=COLLECTION, query_vector=qvec, limit=limit) | |
| except Exception as e: | |
| return [], f"β Error querying Qdrant: {e}" | |
| if not hits: | |
| return [], "No results found." | |
| images, captions = [], [] | |
| for h in hits: | |
| score = getattr(h, "score", None) | |
| if score is None or score < min_score: | |
| continue | |
| payload = h.payload or {} | |
| caption = f"ID: {h.id}\nScore: {score:.4f}\nMode: {payload.get('mode','')}\nTags: {payload.get('tags','')}\nText: {payload.get('text','')}" | |
| # If it's a found item, show finder details | |
| if payload.get("mode") == "found": | |
| caption += f"\nπ€ Finder: {payload.get('finder_name','N/A')} | π {payload.get('finder_phone','N/A')}" | |
| captions.append(caption) | |
| if payload.get("has_image") and payload.get("image_b64"): | |
| img = decode_image_from_b64(payload["image_b64"]) | |
| if img: | |
| images.append(img) | |
| else: | |
| images.append(Image.new("RGB", (200,200), color="gray")) | |
| else: | |
| img = Image.new("RGB", (200,200), color="lightblue") | |
| images.append(img) | |
| if not images: | |
| return [], f"No results above similarity threshold {min_score:.2f}" | |
| return list(zip(images, captions)), "" | |
| # ------------------------- | |
| # Gradio UI | |
| # ------------------------- | |
| with gr.Blocks(title="Lost & Found β Simple Helper") as demo: | |
| gr.Markdown("## π§³ Lost & Found Helper β Upload items, then search by image or text.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| mode = gr.Radio(choices=["lost", "found"], value="lost", label="Add as") | |
| upload_img = gr.Image(type="pil", label="Item photo (optional)") | |
| text_desc = gr.Textbox(lines=2, placeholder="Short description", label="Description (optional)") | |
| finder_name = gr.Textbox(lines=1, placeholder="Finder name (only if found)", label="Finder Name") | |
| finder_phone = gr.Textbox(lines=1, placeholder="Finder phone (only if found)", label="Finder Phone") | |
| add_btn = gr.Button("β Add item") | |
| add_out = gr.Textbox(label="Add result", interactive=False) | |
| with gr.Column(): | |
| gr.Markdown("### π Search") | |
| query_img = gr.Image(type="pil", label="Search by image (optional)") | |
| query_text = gr.Textbox(lines=2, label="Search by text (optional)") | |
| limit_slider = gr.Slider(1, 10, value=5, step=1, label="Max results") | |
| score_slider = gr.Slider(0.0, 1.0, value=0.90, step=0.01, label="Min similarity score") | |
| search_btn = gr.Button("π Search") | |
| gallery = gr.Gallery( | |
| label="Search Results", | |
| show_label=True, | |
| elem_id="gallery", | |
| columns=2, | |
| height="auto" | |
| ) | |
| search_msg = gr.Textbox(label="Message", interactive=False) | |
| add_btn.click( | |
| add_item, | |
| inputs=[mode, upload_img, text_desc, finder_name, finder_phone], | |
| outputs=[add_out] | |
| ) | |
| search_btn.click( | |
| search_items, | |
| inputs=[query_img, query_text, limit_slider, score_slider], | |
| outputs=[gallery, search_msg] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |