Spaces:
Runtime error
Runtime error
| # app.py | |
| import os | |
| import uuid | |
| import io | |
| import base64 # <-- FIX: This was missing | |
| from PIL import Image | |
| import gradio as gr | |
| from sentence_transformers import SentenceTransformer | |
| import google.generativeai as genai # <-- FIX: Correct import for the genai library | |
| from qdrant_client import QdrantClient | |
| from qdrant_client.http.models import VectorParams, Distance, PointStruct | |
| # Note: The QDRANT_URL, QDRANT_API_KEY, and GEMINI_API_KEY environment variables | |
| # must be set for this application to work correctly. | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") | |
| QDRANT_URL = os.environ.get("QDRANT_URL") | |
| QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY") | |
| print("Loading CLIP model...") | |
| MODEL_ID = "sentence-transformers/clip-ViT-B-32-multilingual-v1" | |
| clip_model = SentenceTransformer(MODEL_ID) | |
| # Initialize the GenAI client with the correct API key | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| if not QDRANT_URL: | |
| raise RuntimeError("Set QDRANT_URL env var") | |
| qclient = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY) | |
| COLLECTION = "lost_found_items" | |
| VECTOR_SIZE = 512 | |
| # Only create the collection if it doesn't already exist | |
| if not qclient.get_collections().collections: | |
| qclient.create_collection( | |
| collection_name=COLLECTION, | |
| vectors_config=VectorParams(size=VECTOR_SIZE, distance=Distance.COSINE), | |
| ) | |
| def embed_text(text: str): | |
| return clip_model.encode(text, convert_to_numpy=True) | |
| def embed_image_pil(pil_img: Image.Image): | |
| return clip_model.encode(pil_img, convert_to_numpy=True) | |
| # FIX: This function is updated to take a PIL Image object directly and | |
| # uses an inlineData object for the Gemini API call, as file upload is | |
| # not supported for gemini-2.5-flash in this manner. | |
| def gen_tags_from_image(pil_img: Image.Image) -> str: | |
| if not GEMINI_API_KEY: | |
| return "" | |
| try: | |
| # Convert PIL Image to a byte array | |
| img_bytes = io.BytesIO() | |
| pil_img.save(img_bytes, format="PNG") | |
| img_bytes.seek(0) | |
| # Use inlineData to pass the image to the model | |
| model = genai.GenerativeModel("gemini-2.5-flash") | |
| prompt = ("Give 4 short tags (comma-separated) describing this item in the image. " | |
| "Respond only with tags.") | |
| image_part = { | |
| "mime_type": "image/png", | |
| "data": img_bytes.getvalue() | |
| } | |
| resp = model.generate_content([prompt, image_part]) | |
| return resp.text.strip() | |
| except Exception as e: | |
| print(f"Error generating tags: {e}") | |
| return "" | |
| def add_item(mode: str, uploaded_image, text_description: str): | |
| item_id = str(uuid.uuid4()) | |
| payload = {"mode": mode, "text": text_description} | |
| if uploaded_image: | |
| # Use the PIL image directly for embedding | |
| vec = embed_image_pil(uploaded_image).tolist() | |
| payload["has_image"] = True | |
| # FIX: Pass the PIL image object to the tag generation function | |
| payload["tags"] = gen_tags_from_image(uploaded_image) | |
| # Convert the PIL image to base64 string for storage in payload | |
| img_bytes = io.BytesIO() | |
| uploaded_image.save(img_bytes, format="PNG") | |
| img_bytes.seek(0) | |
| payload["image_b64"] = base64.b64encode(img_bytes.read()).decode("utf-8") | |
| else: | |
| vec = embed_text(text_description).tolist() | |
| payload["has_image"] = False | |
| 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','')}" | |
| def search_items(query_image, query_text, limit: int = 5): | |
| if query_image: | |
| qvec = embed_image_pil(query_image).tolist() | |
| elif query_text: | |
| qvec = embed_text(query_text).tolist() | |
| else: | |
| return "Provide query image or text." | |
| hits = qclient.search(collection_name=COLLECTION, query_vector=qvec, limit=limit) | |
| if not hits: | |
| return "No results." | |
| results = [] | |
| for h in hits: | |
| payload = h.payload or {} | |
| score = getattr(h, "score", 0) | |
| results.append( | |
| f"ID:{h.id}\nScore:{float(score):.4f}\nMode:{payload.get('mode','')}\n" | |
| f"Tags:{payload.get('tags','')}\nText:{payload.get('text','')}\n" | |
| ) | |
| return "\n\n".join(results) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Lost & Found Helper") | |
| with gr.Row(): | |
| with gr.Column(): | |
| mode = gr.Radio(["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") | |
| add_btn = gr.Button("Add item") | |
| add_out = gr.Textbox(interactive=False, label="Result") | |
| with gr.Column(): | |
| query_img = gr.Image(type="pil", label="Search by image (optional)") | |
| query_text = gr.Textbox(lines=2, label="Search by text (optional)") | |
| search_btn = gr.Button("Search") | |
| search_out = gr.Textbox(interactive=False, label="Search results") | |
| add_btn.click(add_item, inputs=[mode, upload_img, text_desc], outputs=[add_out]) | |
| search_btn.click(search_items, inputs=[query_img, query_text], outputs=[search_out]) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |