Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -26,9 +26,20 @@ app = FastAPI(title="E-Bikes Semantic Search API",
|
|
| 26 |
description="API for finding similar e-bikes based on semantic search",
|
| 27 |
version="1.0.0")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Request and response models
|
| 30 |
class SearchRequest(BaseModel):
|
| 31 |
description: str
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
class BikeMatch(BaseModel):
|
| 34 |
id: str
|
|
@@ -96,7 +107,7 @@ def load_ebikes_data(file_path="data.json"):
|
|
| 96 |
try:
|
| 97 |
with open(file_path, 'r') as f:
|
| 98 |
data = json.load(f)
|
| 99 |
-
return data.get('
|
| 100 |
except Exception as e:
|
| 101 |
print(f"Error loading e-bikes data: {e}")
|
| 102 |
return []
|
|
@@ -114,8 +125,9 @@ def create_and_upload_embeddings(ebikes_data, encoder, pinecone_index):
|
|
| 114 |
metadata.append({
|
| 115 |
'id': bike['id'],
|
| 116 |
'name': bike['name'],
|
| 117 |
-
'type': bike['
|
| 118 |
-
'description': bike['description']
|
|
|
|
| 119 |
})
|
| 120 |
|
| 121 |
# Create embeddings
|
|
@@ -168,7 +180,7 @@ async def health_check():
|
|
| 168 |
return {"status": "healthy"}
|
| 169 |
|
| 170 |
@app.post("/search", response_model=SearchResponse)
|
| 171 |
-
async def search_ebikes(description: str):
|
| 172 |
"""
|
| 173 |
Search for e-bikes similar to the provided description
|
| 174 |
|
|
@@ -177,12 +189,14 @@ async def search_ebikes(description: str):
|
|
| 177 |
try:
|
| 178 |
# Create embedding for the query
|
| 179 |
query_embedding = encoder.encode(description)[0]
|
| 180 |
-
|
|
|
|
| 181 |
# Query Pinecone
|
| 182 |
results = pinecone_index.query(
|
| 183 |
vector=query_embedding.tolist(),
|
| 184 |
top_k=3,
|
| 185 |
-
include_metadata=True
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
# Parse results
|
|
|
|
| 26 |
description="API for finding similar e-bikes based on semantic search",
|
| 27 |
version="1.0.0")
|
| 28 |
|
| 29 |
+
def build_filter(pt: Optional[str], cat: Optional[str]) -> dict | None:
|
| 30 |
+
filt = {}
|
| 31 |
+
if pt:
|
| 32 |
+
filt["product_type"] = pt # shorthand $eq
|
| 33 |
+
if cat:
|
| 34 |
+
filt["category"] = cat
|
| 35 |
+
return filt or None
|
| 36 |
+
|
| 37 |
# Request and response models
|
| 38 |
class SearchRequest(BaseModel):
|
| 39 |
description: str
|
| 40 |
+
top_k: int = 3
|
| 41 |
+
product_type: Optional[str] = None # "ebike" or "escooter"
|
| 42 |
+
category: Optional[str] = None # e.g. "mountain"
|
| 43 |
|
| 44 |
class BikeMatch(BaseModel):
|
| 45 |
id: str
|
|
|
|
| 107 |
try:
|
| 108 |
with open(file_path, 'r') as f:
|
| 109 |
data = json.load(f)
|
| 110 |
+
return data.get('pogo-cycles-data', [])
|
| 111 |
except Exception as e:
|
| 112 |
print(f"Error loading e-bikes data: {e}")
|
| 113 |
return []
|
|
|
|
| 125 |
metadata.append({
|
| 126 |
'id': bike['id'],
|
| 127 |
'name': bike['name'],
|
| 128 |
+
'type': bike['product_type'],
|
| 129 |
+
'description': bike['description'],
|
| 130 |
+
'category': bike['category']
|
| 131 |
})
|
| 132 |
|
| 133 |
# Create embeddings
|
|
|
|
| 180 |
return {"status": "healthy"}
|
| 181 |
|
| 182 |
@app.post("/search", response_model=SearchResponse)
|
| 183 |
+
async def search_ebikes(description: str,filters:dict):
|
| 184 |
"""
|
| 185 |
Search for e-bikes similar to the provided description
|
| 186 |
|
|
|
|
| 189 |
try:
|
| 190 |
# Create embedding for the query
|
| 191 |
query_embedding = encoder.encode(description)[0]
|
| 192 |
+
filter_payload = build_filter(filters.get("product_type"), filters.get("category"))
|
| 193 |
+
|
| 194 |
# Query Pinecone
|
| 195 |
results = pinecone_index.query(
|
| 196 |
vector=query_embedding.tolist(),
|
| 197 |
top_k=3,
|
| 198 |
+
include_metadata=True,
|
| 199 |
+
filter=filter_payload
|
| 200 |
)
|
| 201 |
|
| 202 |
# Parse results
|