hackerloi45 commited on
Commit
c65ef6e
Β·
1 Parent(s): c559bc7
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -14,28 +14,32 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
14
 
15
  COLLECTION = "lost_and_found"
16
 
17
- # Qdrant client
18
- qclient = qdrant_client.QdrantClient(":memory:") # In-memory for Hugging Face
19
  encoder = SentenceTransformer("clip-ViT-B-32")
20
 
21
- # Create collection
22
  if not qclient.collection_exists(COLLECTION):
23
  qclient.create_collection(
24
  collection_name=COLLECTION,
25
  vectors_config=models.VectorParams(size=512, distance=models.Distance.COSINE),
26
  )
27
 
 
28
  # ===============================
29
  # Encode Function (Text or Image)
30
  # ===============================
31
  def encode_data(text=None, image=None):
32
- if image is not None:
 
 
33
  return encoder.encode(Image.open(image).convert("RGB"))
34
  elif text:
35
  return encoder.encode([text])[0]
36
  else:
37
  return None
38
 
 
39
  # ===============================
40
  # Add Item
41
  # ===============================
@@ -44,16 +48,13 @@ def add_item(text, image, uploader_name, uploader_phone):
44
  img_path = None
45
  vector = None
46
 
47
- if image:
48
  img_id = str(uuid.uuid4())
49
  img_path = os.path.join(UPLOAD_DIR, f"{img_id}.png")
50
  image.save(img_path)
51
-
52
- # βœ… Always store image embedding if available
53
- vector = encode_data(image=img_path)
54
 
55
  elif text:
56
- # βœ… Fallback: text embedding
57
  vector = encode_data(text=text)
58
 
59
  if vector is None:
@@ -78,13 +79,14 @@ def add_item(text, image, uploader_name, uploader_phone):
78
  except Exception as e:
79
  return f"❌ Error: {e}"
80
 
 
81
  # ===============================
82
  # Search Function
83
  # ===============================
84
  def search_items(text, image, max_results, min_score):
85
  try:
86
  vector = None
87
- if image:
88
  vector = encode_data(image=image)
89
  elif text:
90
  vector = encode_data(text=text)
@@ -119,6 +121,7 @@ def search_items(text, image, max_results, min_score):
119
  except Exception as e:
120
  return f"❌ Error: {e}", []
121
 
 
122
  # ===============================
123
  # Delete All
124
  # ===============================
@@ -130,12 +133,14 @@ def clear_database():
130
  )
131
  return "πŸ—‘οΈ Database cleared!"
132
 
 
133
  # ===============================
134
  # Gradio UI
135
  # ===============================
136
  with gr.Blocks() as demo:
137
  gr.Markdown("## πŸ—οΈ Lost & Found - Database")
138
 
 
139
  with gr.Tab("βž• Add Item"):
140
  with gr.Row():
141
  text_input = gr.Textbox(label="Description (optional)")
@@ -152,6 +157,7 @@ with gr.Blocks() as demo:
152
  outputs=add_output,
153
  )
154
 
 
155
  with gr.Tab("πŸ” Search"):
156
  with gr.Row():
157
  search_text = gr.Textbox(label="Search by text (optional)")
@@ -169,6 +175,7 @@ with gr.Blocks() as demo:
169
  outputs=[search_text_out, search_gallery],
170
  )
171
 
 
172
  with gr.Tab("πŸ—‘οΈ Admin"):
173
  clear_btn = gr.Button("Clear Database")
174
  clear_out = gr.Textbox(label="Status")
 
14
 
15
  COLLECTION = "lost_and_found"
16
 
17
+ # Qdrant client (in-memory for Hugging Face)
18
+ qclient = qdrant_client.QdrantClient(":memory:")
19
  encoder = SentenceTransformer("clip-ViT-B-32")
20
 
21
+ # Create collection if not exists
22
  if not qclient.collection_exists(COLLECTION):
23
  qclient.create_collection(
24
  collection_name=COLLECTION,
25
  vectors_config=models.VectorParams(size=512, distance=models.Distance.COSINE),
26
  )
27
 
28
+
29
  # ===============================
30
  # Encode Function (Text or Image)
31
  # ===============================
32
  def encode_data(text=None, image=None):
33
+ if isinstance(image, Image.Image): # Image is already PIL
34
+ return encoder.encode(image.convert("RGB"))
35
+ elif isinstance(image, str): # Path to image
36
  return encoder.encode(Image.open(image).convert("RGB"))
37
  elif text:
38
  return encoder.encode([text])[0]
39
  else:
40
  return None
41
 
42
+
43
  # ===============================
44
  # Add Item
45
  # ===============================
 
48
  img_path = None
49
  vector = None
50
 
51
+ if isinstance(image, Image.Image): # PIL image
52
  img_id = str(uuid.uuid4())
53
  img_path = os.path.join(UPLOAD_DIR, f"{img_id}.png")
54
  image.save(img_path)
55
+ vector = encode_data(image=image)
 
 
56
 
57
  elif text:
 
58
  vector = encode_data(text=text)
59
 
60
  if vector is None:
 
79
  except Exception as e:
80
  return f"❌ Error: {e}"
81
 
82
+
83
  # ===============================
84
  # Search Function
85
  # ===============================
86
  def search_items(text, image, max_results, min_score):
87
  try:
88
  vector = None
89
+ if isinstance(image, Image.Image): # Search with PIL
90
  vector = encode_data(image=image)
91
  elif text:
92
  vector = encode_data(text=text)
 
121
  except Exception as e:
122
  return f"❌ Error: {e}", []
123
 
124
+
125
  # ===============================
126
  # Delete All
127
  # ===============================
 
133
  )
134
  return "πŸ—‘οΈ Database cleared!"
135
 
136
+
137
  # ===============================
138
  # Gradio UI
139
  # ===============================
140
  with gr.Blocks() as demo:
141
  gr.Markdown("## πŸ—οΈ Lost & Found - Database")
142
 
143
+ # --- Add Item Tab ---
144
  with gr.Tab("βž• Add Item"):
145
  with gr.Row():
146
  text_input = gr.Textbox(label="Description (optional)")
 
157
  outputs=add_output,
158
  )
159
 
160
+ # --- Search Tab ---
161
  with gr.Tab("πŸ” Search"):
162
  with gr.Row():
163
  search_text = gr.Textbox(label="Search by text (optional)")
 
175
  outputs=[search_text_out, search_gallery],
176
  )
177
 
178
+ # --- Admin Tab ---
179
  with gr.Tab("πŸ—‘οΈ Admin"):
180
  clear_btn = gr.Button("Clear Database")
181
  clear_out = gr.Textbox(label="Status")