hackerloi45 commited on
Commit
9e00920
Β·
1 Parent(s): dd67299

logic improvisation

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -14,7 +14,7 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
14
  qclient = QdrantClient(":memory:")
15
  COLLECTION = "lost_and_found"
16
 
17
- # Create collection (with deprecation fix)
18
  if not qclient.collection_exists(COLLECTION):
19
  qclient.create_collection(
20
  COLLECTION,
@@ -42,9 +42,9 @@ def encode_data(text=None, image=None):
42
 
43
 
44
  # ==============================
45
- # Add Item
46
  # ==============================
47
- def add_item(mode, text, image, uploader_name, uploader_phone):
48
  try:
49
  img_path = None
50
  if image:
@@ -61,7 +61,6 @@ def add_item(mode, text, image, uploader_name, uploader_phone):
61
  id=str(uuid.uuid4()),
62
  vector=vector.tolist(),
63
  payload={
64
- "mode": mode,
65
  "text": text or "",
66
  "uploader_name": uploader_name or "N/A",
67
  "uploader_phone": uploader_phone or "N/A",
@@ -71,13 +70,13 @@ def add_item(mode, text, image, uploader_name, uploader_phone):
71
  )
72
  ],
73
  )
74
- return f"βœ… Added successfully as {mode}!"
75
  except Exception as e:
76
  return f"❌ Error: {e}"
77
 
78
 
79
  # ==============================
80
- # Search Items
81
  # ==============================
82
  def search_items(text, image, max_results, min_score):
83
  try:
@@ -94,14 +93,12 @@ def search_items(text, image, max_results, min_score):
94
  texts, imgs = [], []
95
  for r in results:
96
  p = r.payload
97
- desc = (
98
- f"id:{r.id} | score:{r.score:.3f} | mode:{p.get('mode','')} | text:{p.get('text','')}"
99
- )
100
 
101
- # Always show uploader details
102
  uploader_name = p.get("uploader_name", "N/A") or "N/A"
103
  uploader_phone = p.get("uploader_phone", "N/A") or "N/A"
104
- desc += f" | uploader:{uploader_name} ({uploader_phone})"
105
 
106
  texts.append(desc)
107
  if p.get("has_image") and "image_path" in p:
@@ -125,24 +122,23 @@ def clear_all():
125
  # Gradio UI
126
  # ==============================
127
  with gr.Blocks() as demo:
128
- gr.Markdown("# πŸ”‘ Lost & Found Image/Text Search")
129
 
130
- with gr.Tab("βž• Add Item"):
131
- mode = gr.Radio(["lost", "found"], label="Mode", value="found")
132
  text_in = gr.Textbox(label="Description (optional)")
133
  img_in = gr.Image(type="pil", label="Upload Image")
134
- uploader_name = gr.Textbox(label="Your Name")
135
- uploader_phone = gr.Textbox(label="Your Phone")
136
  add_btn = gr.Button("Add to Database")
137
  add_out = gr.Textbox(label="Status")
138
 
139
  add_btn.click(
140
  add_item,
141
- inputs=[mode, text_in, img_in, uploader_name, uploader_phone],
142
  outputs=add_out,
143
  )
144
 
145
- with gr.Tab("πŸ” Search"):
146
  search_text = gr.Textbox(label="Search by text (optional)")
147
  search_img = gr.Image(type="pil", label="Search by image (optional)")
148
  max_results = gr.Slider(1, 10, value=5, step=1, label="Max results")
 
14
  qclient = QdrantClient(":memory:")
15
  COLLECTION = "lost_and_found"
16
 
17
+ # Create collection
18
  if not qclient.collection_exists(COLLECTION):
19
  qclient.create_collection(
20
  COLLECTION,
 
42
 
43
 
44
  # ==============================
45
+ # Add Item (finder uploads)
46
  # ==============================
47
+ def add_item(text, image, uploader_name, uploader_phone):
48
  try:
49
  img_path = None
50
  if image:
 
61
  id=str(uuid.uuid4()),
62
  vector=vector.tolist(),
63
  payload={
 
64
  "text": text or "",
65
  "uploader_name": uploader_name or "N/A",
66
  "uploader_phone": uploader_phone or "N/A",
 
70
  )
71
  ],
72
  )
73
+ return "βœ… Item added to database!"
74
  except Exception as e:
75
  return f"❌ Error: {e}"
76
 
77
 
78
  # ==============================
79
+ # Search Items (for lost things)
80
  # ==============================
81
  def search_items(text, image, max_results, min_score):
82
  try:
 
93
  texts, imgs = [], []
94
  for r in results:
95
  p = r.payload
96
+ desc = f"id:{r.id} | score:{r.score:.3f} | text:{p.get('text','')}"
 
 
97
 
98
+ # Show finder details
99
  uploader_name = p.get("uploader_name", "N/A") or "N/A"
100
  uploader_phone = p.get("uploader_phone", "N/A") or "N/A"
101
+ desc += f" | finder:{uploader_name} ({uploader_phone})"
102
 
103
  texts.append(desc)
104
  if p.get("has_image") and "image_path" in p:
 
122
  # Gradio UI
123
  # ==============================
124
  with gr.Blocks() as demo:
125
+ gr.Markdown("# πŸ”‘ Lost & Found Database")
126
 
127
+ with gr.Tab("βž• Add Found Item"):
 
128
  text_in = gr.Textbox(label="Description (optional)")
129
  img_in = gr.Image(type="pil", label="Upload Image")
130
+ uploader_name = gr.Textbox(label="Finder Name")
131
+ uploader_phone = gr.Textbox(label="Finder Phone")
132
  add_btn = gr.Button("Add to Database")
133
  add_out = gr.Textbox(label="Status")
134
 
135
  add_btn.click(
136
  add_item,
137
+ inputs=[text_in, img_in, uploader_name, uploader_phone],
138
  outputs=add_out,
139
  )
140
 
141
+ with gr.Tab("πŸ” Search Lost Item"):
142
  search_text = gr.Textbox(label="Search by text (optional)")
143
  search_img = gr.Image(type="pil", label="Search by image (optional)")
144
  max_results = gr.Slider(1, 10, value=5, step=1, label="Max results")