WebashalarForML commited on
Commit
9ca2c04
·
verified ·
1 Parent(s): 10de96a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -36,7 +36,6 @@ with open(f"{BLOCKS_DIR}/embeddings.json", "r") as f:
36
  image_paths = [item["file-path"] for item in embedding_json]
37
  image_embeds = np.array([item["embeddings"] for item in embedding_json])
38
 
39
-
40
  # ============================== #
41
  # HELPER: Decode Base64 Image #
42
  # ============================== #
@@ -45,16 +44,11 @@ def decode_base64_image(b64_string):
45
  img = Image.open(BytesIO(img_data)).convert("RGB")
46
  return img
47
 
48
-
49
  # ============================== #
50
  # API ROUTE #
51
  # ============================== #
52
  @app.route("/match", methods=["POST"])
53
  def match_image():
54
- """
55
- Input: JSON { "images": ["<base64_img1>", "<base64_img2>", ...] }
56
- Output: Best match path + score for each input image
57
- """
58
  data = request.get_json()
59
  if "images" not in data:
60
  return jsonify({"error": "No images provided"}), 400
@@ -62,16 +56,14 @@ def match_image():
62
  results = []
63
  for b64_img in data["images"]:
64
  try:
65
- # Decode and embed input image
66
  img = decode_base64_image(b64_img)
67
- query_embed = np.array(clip_embd.embed_image([img])) # embed_image can take PIL images
68
 
69
- # Cosine similarity with stored embeddings
70
  sims = cosine_similarity(query_embed, image_embeds)[0]
71
  best_idx = np.argmax(sims)
72
 
73
  results.append({
74
- "input": b64_img[:50] + "...", # partial preview
75
  "best_match": {
76
  "name": os.path.basename(image_paths[best_idx]),
77
  "path": image_paths[best_idx],
@@ -84,8 +76,16 @@ def match_image():
84
  return jsonify(results)
85
 
86
 
 
 
 
 
 
 
 
 
87
  # ============================== #
88
  # MAIN ENTRY #
89
  # ============================== #
90
  if __name__ == "__main__":
91
- app.run(debug=True, port=7860)
 
36
  image_paths = [item["file-path"] for item in embedding_json]
37
  image_embeds = np.array([item["embeddings"] for item in embedding_json])
38
 
 
39
  # ============================== #
40
  # HELPER: Decode Base64 Image #
41
  # ============================== #
 
44
  img = Image.open(BytesIO(img_data)).convert("RGB")
45
  return img
46
 
 
47
  # ============================== #
48
  # API ROUTE #
49
  # ============================== #
50
  @app.route("/match", methods=["POST"])
51
  def match_image():
 
 
 
 
52
  data = request.get_json()
53
  if "images" not in data:
54
  return jsonify({"error": "No images provided"}), 400
 
56
  results = []
57
  for b64_img in data["images"]:
58
  try:
 
59
  img = decode_base64_image(b64_img)
60
+ query_embed = np.array(clip_embd.embed_image([img]))
61
 
 
62
  sims = cosine_similarity(query_embed, image_embeds)[0]
63
  best_idx = np.argmax(sims)
64
 
65
  results.append({
66
+ "input": b64_img[:50] + "...",
67
  "best_match": {
68
  "name": os.path.basename(image_paths[best_idx]),
69
  "path": image_paths[best_idx],
 
76
  return jsonify(results)
77
 
78
 
79
+ # ============================== #
80
+ # SIMPLE HTML UI #
81
+ # ============================== #
82
+ @app.route("/", methods=["GET", "POST"])
83
+ def index():
84
+ return render_template("index.html")
85
+
86
+
87
  # ============================== #
88
  # MAIN ENTRY #
89
  # ============================== #
90
  if __name__ == "__main__":
91
+ app.run(debug=True, port=7860)