Yashashvibhardwaj commited on
Commit
dae3a4f
·
verified ·
1 Parent(s): f3a0ffa

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +34 -12
main.py CHANGED
@@ -15,10 +15,10 @@ os.environ["SENTENCE_TRANSFORMERS_HOME"] = "./cache"
15
 
16
  app = FastAPI()
17
 
18
- # Enable CORS (for frontend HTML to connect)
19
  app.add_middleware(
20
  CORSMiddleware,
21
- allow_origins=["*"],
22
  allow_credentials=True,
23
  allow_methods=["*"],
24
  allow_headers=["*"],
@@ -42,24 +42,46 @@ def root():
42
 
43
 
44
  @app.post("/search_text")
45
- def search_text(query: str = Form(...), top_k: int = 5):
46
  """
47
  Search products using text query.
48
  """
49
  query_emb = model.encode([query], convert_to_numpy=True)
50
  distances, indices = index.search(query_emb, top_k)
51
- results = [products[i] for i in indices[0]]
52
- return {"query": query, "results": results}
53
 
 
 
 
 
 
 
54
 
55
- @app.post("/search_image")
56
- async def search_image(file: UploadFile = File(...), top_k: int = 5):
 
 
 
57
  """
58
- Search products using image query.
59
  """
60
- image_bytes = await file.read()
61
- image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
 
 
 
 
 
 
 
 
62
  image_emb = model.encode([image], convert_to_numpy=True)
63
  distances, indices = index.search(image_emb, top_k)
64
- results = [products[i] for i in indices[0]]
65
- return {"results": results}
 
 
 
 
 
 
 
 
15
 
16
  app = FastAPI()
17
 
18
+ # Enable CORS (so frontend on Netlify can call backend on HF)
19
  app.add_middleware(
20
  CORSMiddleware,
21
+ allow_origins=["*"], # for now allow all, can restrict to Netlify domain
22
  allow_credentials=True,
23
  allow_methods=["*"],
24
  allow_headers=["*"],
 
42
 
43
 
44
  @app.post("/search_text")
45
+ def search_text(query: str = Form(...), top_k: int = 5, min_score: float = 0.0):
46
  """
47
  Search products using text query.
48
  """
49
  query_emb = model.encode([query], convert_to_numpy=True)
50
  distances, indices = index.search(query_emb, top_k)
 
 
51
 
52
+ results = []
53
+ for score, idx in zip(distances[0], indices[0]):
54
+ if score >= min_score: # filter by threshold
55
+ item = products[idx]
56
+ item["score"] = float(score)
57
+ results.append(item)
58
 
59
+ return {"matches": results}
60
+
61
+
62
+ @app.post("/match") # 👈 Renamed to match frontend
63
+ async def search_image(file: UploadFile = File(None), image_url: str = Form(None), top_k: int = 5, min_score: float = 0.0):
64
  """
65
+ Search products using image query (upload or URL).
66
  """
67
+ if file:
68
+ image_bytes = await file.read()
69
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
70
+ elif image_url:
71
+ import requests
72
+ response = requests.get(image_url)
73
+ image = Image.open(io.BytesIO(response.content)).convert("RGB")
74
+ else:
75
+ return {"error": "No image provided"}
76
+
77
  image_emb = model.encode([image], convert_to_numpy=True)
78
  distances, indices = index.search(image_emb, top_k)
79
+
80
+ results = []
81
+ for score, idx in zip(distances[0], indices[0]):
82
+ if score >= min_score:
83
+ item = products[idx]
84
+ item["score"] = float(score)
85
+ results.append(item)
86
+
87
+ return {"matches": results}