forked from Jos100703/Anthropic-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_api.py
More file actions
395 lines (327 loc) · 13.1 KB
/
Copy pathui_api.py
File metadata and controls
395 lines (327 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from matching.merger import merge_intermediate_results
from scrapers.electronic4you import Electronic4YouScraper
from scrapers.etec import ETecScraper
from scrapers.utils import extract_brand, load_source_products, write_json
ROOT = Path(__file__).resolve().parent
RESULTS_DIR = ROOT / "results"
TARGET_POOL_PATHS = [
ROOT / "target_pool_tv_&_audio.json",
ROOT / "data" / "target_pool_tv_&_audio.json",
]
RESULT_FILES: Dict[str, Path] = {
"matching_visible": RESULTS_DIR / "matching_visible_tv_audio.json",
"scraping_expert": RESULTS_DIR / "scraping_expert_tv_audio.json",
"scraping_cyberport": RESULTS_DIR / "scraping_cyberport_tv_audio.json",
"scraping_electronic4you": RESULTS_DIR / "scraping_electronic4you_tv_audio.json",
"scraping_etec": RESULTS_DIR / "scraping_etec_tv_audio.json",
}
SUBMISSION_PATH = RESULTS_DIR / "submission_tv_audio.json"
class PipelineRunRequest(BaseModel):
query: Optional[str] = None
specs: Optional[str] = None
run_all: bool = False
def _spec_features(specs: Dict[str, Any], limit: int = 6) -> List[str]:
items: List[str] = []
for key, value in specs.items():
if not value:
continue
value_text = str(value).strip()
if not value_text:
continue
if len(value_text) > 26:
continue
items.append(f"{key}: {value_text}")
if len(items) >= limit:
break
return items
def _optional_price(value: Any) -> Optional[float]:
if value is None:
return None
try:
parsed = float(value)
except (TypeError, ValueError):
return None
if parsed <= 0:
return None
return parsed
def _source_to_ui_product(source: Dict[str, Any]) -> Dict[str, Any]:
specs = source.get("specifications") or {}
features = _spec_features(specs)
return {
"id": str(source.get("reference") or ""),
"name": str(source.get("name") or ""),
"category": str(source.get("category") or "TV & Audio"),
"price": _optional_price(source.get("price_eur")),
"description": f"Source product • {extract_brand(source) or 'Unknown brand'}",
"features": features,
"image": str(source.get("image_url") or "https://placehold.co/800x450?text=Product"),
"sourceReference": str(source.get("reference") or ""),
}
def _load_target_products() -> List[Dict[str, Any]]:
for path in TARGET_POOL_PATHS:
if path.exists():
payload = json.loads(path.read_text(encoding="utf-8"))
if isinstance(payload, list):
return payload
raise FileNotFoundError("Could not find target pool JSON file")
def _target_to_ui_product(target: Dict[str, Any]) -> Dict[str, Any]:
specs = target.get("specifications") or {}
features = _spec_features(specs)
retailer = str(target.get("retailer") or "").strip()
if retailer:
features = [retailer, *features]
return {
"id": str(target.get("reference") or ""),
"name": str(target.get("name") or ""),
"category": str(target.get("category") or "TV & Audio"),
"price": _optional_price(target.get("price_eur")),
"description": f"Target product • {extract_brand(target) or 'Unknown brand'}",
"features": features,
"image": str(target.get("image_url") or "https://placehold.co/800x450?text=Product"),
"retailer": retailer or None,
"competitorReference": str(target.get("reference") or ""),
}
def _load_intermediate() -> Dict[str, List[Dict[str, Any]]]:
loaded: Dict[str, List[Dict[str, Any]]] = {}
for key, path in RESULT_FILES.items():
if path.exists():
try:
payload = json.loads(path.read_text(encoding="utf-8"))
loaded[key] = payload if isinstance(payload, list) else []
except Exception:
loaded[key] = []
else:
loaded[key] = []
return loaded
def _append_or_replace(result_path: Path, new_items: List[Dict[str, Any]]) -> None:
existing: List[Dict[str, Any]] = []
if result_path.exists():
payload = json.loads(result_path.read_text(encoding="utf-8"))
if isinstance(payload, list):
existing = payload
index: Dict[Tuple[str, str], int] = {}
for idx, item in enumerate(existing):
key = (
str(item.get("source_reference") or ""),
str((item.get("match") or {}).get("competitor_retailer") or ""),
)
index[key] = idx
for item in new_items:
key = (
str(item.get("source_reference") or ""),
str((item.get("match") or {}).get("competitor_retailer") or ""),
)
if key in index:
existing[index[key]] = item
else:
index[key] = len(existing)
existing.append(item)
write_json(result_path, existing)
def _run_merge(source_products: Optional[List[Dict[str, Any]]] = None) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
if source_products is None:
source_products = load_source_products()
existing_files = [path for path in RESULT_FILES.values() if path.exists()]
merged, stats = merge_intermediate_results(existing_files, source_products=source_products)
write_json(SUBMISSION_PATH, merged)
return merged, stats
def _confidence_map(intermediate: Dict[str, List[Dict[str, Any]]]) -> Dict[str, float]:
conf: Dict[str, float] = {}
for items in intermediate.values():
for item in items:
ref = str((item.get("match") or {}).get("reference") or "")
value = float(item.get("confidence") or 0.0)
if ref and value > conf.get(ref, 0.0):
conf[ref] = value
return conf
def _pipeline_results_to_ui(
source_products: List[Dict[str, Any]],
submission: List[Dict[str, Any]],
confidence_map: Dict[str, float],
query: str = "",
specs: str = "",
) -> List[Dict[str, Any]]:
source_by_ref = {str(item.get("reference") or ""): item for item in source_products}
cards: List[Dict[str, Any]] = []
for source_entry in submission:
source_ref = str(source_entry.get("source_reference") or "")
source = source_by_ref.get(source_ref)
if not source:
continue
for competitor in source_entry.get("competitors", []):
comp_name = str(competitor.get("competitor_product_name") or "")
comp_ref = str(competitor.get("reference") or "")
retailer = str(competitor.get("competitor_retailer") or "")
price = _optional_price(competitor.get("competitor_price"))
source_specs = source.get("specifications") or {}
features = _spec_features(source_specs)
if retailer:
features = [retailer, *features]
cards.append(
{
"id": f"{source_ref}::{comp_ref}",
"name": comp_name,
"category": str(source.get("category") or "TV & Audio"),
"price": price,
"description": f"Source: {source.get('name', '')}",
"features": features,
"image": str(source.get("image_url") or "https://placehold.co/800x450?text=Match"),
"retailer": retailer,
"sourceReference": source_ref,
"competitorReference": comp_ref,
"confidence": confidence_map.get(comp_ref),
}
)
query_text = query.strip().lower()
if query_text:
cards = [
card
for card in cards
if query_text in card["name"].lower()
or query_text in card["description"].lower()
or query_text in (card.get("retailer") or "").lower()
or query_text in (card.get("sourceReference") or "").lower()
]
spec_terms = [token.strip().lower() for token in specs.split(",") if token.strip()]
if spec_terms:
cards = [
card
for card in cards
if any(term in " ".join([card["name"], card["description"], *card["features"]]).lower() for term in spec_terms)
]
return cards
def _query_matches(product: Dict[str, Any], query: str) -> bool:
q = query.lower()
return (
q in str(product.get("name") or "").lower()
or q in str(product.get("reference") or "").lower()
or q in str(extract_brand(product)).lower()
)
app = FastAPI(title="Person C UI Adapter API", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/health")
def health() -> Dict[str, str]:
return {"status": "ok"}
@app.get("/api/products")
def get_products(dataset: str = Query(default="target", pattern="^(source|target)$")) -> List[Dict[str, Any]]:
if dataset == "source":
products = load_source_products()
return [_source_to_ui_product(product) for product in products]
products = _load_target_products()
return [_target_to_ui_product(product) for product in products]
@app.get("/api/search/suggestions")
def search_suggestions(q: str = Query(default="", max_length=120)) -> List[Dict[str, Any]]:
products = _load_target_products()
query = q.strip().lower()
if not query:
return []
ranked = []
for product in products:
ui_product = _target_to_ui_product(product)
haystack = " ".join(
[
ui_product["name"],
ui_product["category"],
ui_product["description"],
ui_product.get("retailer") or "",
*ui_product["features"],
]
).lower()
if query in haystack:
score = 0
if query in ui_product["name"].lower():
score += 10
if query in extract_brand(product).lower():
score += 7
if query in (ui_product.get("retailer") or "").lower():
score += 5
if query in ui_product["category"].lower():
score += 3
ranked.append((score, ui_product))
ranked.sort(key=lambda pair: pair[0], reverse=True)
return [pair[1] for pair in ranked[:8]]
@app.post("/api/pipeline/run")
def pipeline_run(payload: PipelineRunRequest) -> Dict[str, Any]:
source_products = load_source_products()
if payload.run_all:
targets = source_products
run_mode = "all"
else:
if not payload.query or not payload.query.strip():
raise HTTPException(status_code=400, detail="query is required when run_all=false")
query = payload.query.strip()
targets = [item for item in source_products if _query_matches(item, query)]
run_mode = "selected"
if not targets:
raise HTTPException(status_code=404, detail="No source products matched query")
e4y = Electronic4YouScraper(
min_interval_seconds=1.0,
cache_path=RESULTS_DIR / "cache_electronic4you.json",
)
etec = ETecScraper(min_interval_seconds=0.8)
new_matches: List[Dict[str, Any]] = []
for product in targets:
try:
e4y_match = e4y.scrape_product(product)
if e4y_match:
new_matches.append(e4y_match)
except Exception:
pass
try:
etec_match = etec.scrape_product(product)
if etec_match:
new_matches.append(etec_match)
except Exception:
pass
if new_matches:
_append_or_replace(RESULT_FILES["scraping_electronic4you"], [m for m in new_matches if (m.get("match") or {}).get("competitor_retailer") == "electronic4you.at"])
_append_or_replace(RESULT_FILES["scraping_etec"], [m for m in new_matches if (m.get("match") or {}).get("competitor_retailer") == "e-tec.at"])
merged, stats = _run_merge(source_products)
return {
"run_mode": run_mode,
"targets_processed": len(targets),
"matches_added": len(new_matches),
"submission_size": len(merged),
"stats": stats,
}
@app.get("/api/pipeline/results")
def pipeline_results(
query: str = Query(default=""),
specs: str = Query(default=""),
) -> Dict[str, Any]:
source_products = load_source_products()
if not SUBMISSION_PATH.exists():
_, stats = _run_merge(source_products)
else:
_, stats = merge_intermediate_results(
[path for path in RESULT_FILES.values() if path.exists()],
source_products=source_products,
)
if SUBMISSION_PATH.exists():
submission = json.loads(SUBMISSION_PATH.read_text(encoding="utf-8"))
else:
submission = []
confidence_map = _confidence_map(_load_intermediate())
products = _pipeline_results_to_ui(
source_products=source_products,
submission=submission,
confidence_map=confidence_map,
query=query,
specs=specs,
)
return {
"products": products,
"stats": stats,
}