Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
591 lines (501 loc) · 21.7 KB
/
Copy pathserver.py
File metadata and controls
591 lines (501 loc) · 21.7 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
from __future__ import annotations
import html
import json
import os
import re
import urllib.parse
import urllib.error
import urllib.request
from http import HTTPStatus
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
HOST = "127.0.0.1"
PORT = 4173
MODEL = os.environ.get("FURNISHFRAME_GEMINI_MODEL", "gemini-3.1-flash-image-preview")
ANALYSIS_MODEL = os.environ.get("FURNISHFRAME_ANALYSIS_MODEL", "gemini-2.5-flash")
API_ROOT = "https://generativelanguage.googleapis.com/v1beta/models"
WEB_ROOT = Path(__file__).resolve().parent
class FurnishFrameHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=str(WEB_ROOT), **kwargs)
def do_POST(self) -> None:
if self.path not in {"/api/generate", "/api/analyze", "/api/similar_products"}:
self.send_error(HTTPStatus.NOT_FOUND, "Unknown endpoint")
return
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
self.respond_json(
HTTPStatus.BAD_REQUEST,
{"error": "Set GEMINI_API_KEY before starting server.py."},
)
return
try:
content_length = int(self.headers.get("Content-Length", "0"))
payload = json.loads(self.rfile.read(content_length))
if self.path == "/api/analyze":
request_body = build_analysis_request(payload)
gemini_response = call_gemini_api(request_body, api_key, ANALYSIS_MODEL)
result = extract_analysis_result(gemini_response)
elif self.path == "/api/similar_products":
result = search_similar_products(payload, api_key)
else:
request_body = build_gemini_request(payload)
gemini_response = call_gemini_api(request_body, api_key, MODEL)
result = extract_generation_result(gemini_response)
except ValueError as exc:
self.respond_json(HTTPStatus.BAD_REQUEST, {"error": str(exc)})
return
except urllib.error.HTTPError as exc:
body = exc.read().decode("utf-8", errors="replace")
self.respond_json(
exc.code,
{"error": f"Gemini API error {exc.code}", "details": body},
)
return
except urllib.error.URLError as exc:
self.respond_json(
HTTPStatus.BAD_GATEWAY,
{"error": "Could not reach Gemini API", "details": str(exc.reason)},
)
return
self.respond_json(HTTPStatus.OK, result)
def respond_json(self, status: HTTPStatus, body: dict) -> None:
encoded = json.dumps(body).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
def build_gemini_request(payload: dict) -> dict:
room_image_data_url = payload.get("roomImageDataUrl")
room_analysis = payload.get("roomAnalysis") or {}
room_finishes = payload.get("roomFinishes") or {}
furniture = payload.get("furniture", [])
user_prompt = str(payload.get("prompt", "")).strip()
if not room_image_data_url:
raise ValueError("roomImageDataUrl is required")
mime_type, base64_data = parse_data_url(room_image_data_url)
furniture_lines = []
for item in furniture:
product_url = str(item.get("productUrl", "")).strip()
measurement_note = (
" Use the linked Amazon listing as the authoritative source for exact product measurements "
"to the best of your ability."
if "amazon." in product_url.lower()
else ""
)
source_note = f" Product link: {product_url}." if product_url else ""
furniture_lines.append(
f"- {item['name']} at approximately ({round(item['x'])}%, {round(item['y'])}%) "
f"with scale {item['scale']} and rotation {item['rotation']} degrees."
f"{source_note}{measurement_note}"
)
mapping_lines = describe_room_mapping(room_analysis)
finish_lines = describe_room_finishes(room_finishes)
prompt_parts = [
"Use the provided room photo as the base image.",
"Use the structured room analysis below as the primary scene map instead of relying only on the raw image.",
*mapping_lines,
*finish_lines,
"Add only the staged furniture items listed below.",
"Do not redesign, replace, remove, or restyle any existing architecture, decor, furniture, windows, doors, art, rugs, or lighting already present in the room, except for explicitly requested wall and floor finish changes.",
"Preserve the original camera position, room layout, perspective, materials, shadows, and all existing objects unless one of the staged items physically occludes a small portion of them.",
"If a requested furniture placement conflicts with a wall, doorway, window, or existing object, keep the item but shift it minimally to the nearest plausible floor position.",
"Render a single photorealistic still image from the uploaded camera viewpoint.",
"For any staged item with a product link, follow the real product proportions and dimensions as closely as possible.",
"If the product link is an Amazon listing, use the exact measurements from that Amazon listing to the best of your ability.",
"If exact measurements are unavailable, infer conservative real-world dimensions from the product image, title, and typical category proportions instead of inventing exaggerated sizes.",
"Stage the room photorealistically with the following furniture placements.",
"\n".join(furniture_lines) if furniture_lines else "- No extra furniture placements were supplied.",
f"Total staged items to add: {len(furniture)}.",
"Viewpoint: keep the camera close to the uploaded photo viewpoint.",
"Do not introduce any unrequested new furniture or decor.",
"Preserve room geometry, perspective, and lighting unless explicitly changed.",
]
if user_prompt:
prompt_parts.append(f"Additional direction: {user_prompt}")
return {
"contents": [
{
"parts": [
{
"inline_data": {
"mime_type": mime_type,
"data": base64_data,
}
},
{"text": "\n".join(prompt_parts)},
]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K",
},
},
}
def describe_room_finishes(room_finishes: dict) -> list[str]:
wall_color = str(room_finishes.get("wallColor", "")).strip()
wall_material = str(room_finishes.get("wallMaterial", "")).strip()
floor_color = str(room_finishes.get("floorColor", "")).strip()
floor_material = str(room_finishes.get("floorMaterial", "")).strip()
lines = []
if wall_color or wall_material:
wall_parts = []
if wall_color:
wall_parts.append(f"color {wall_color}")
if wall_material:
wall_parts.append(f"material {wall_material}")
lines.append(f"Update the visible walls to {' with '.join(wall_parts)}.")
if floor_color or floor_material:
floor_parts = []
if floor_color:
floor_parts.append(f"color {floor_color}")
if floor_material:
floor_parts.append(f"material {floor_material}")
lines.append(f"Update the visible floor to {' with '.join(floor_parts)}.")
if lines:
lines.append("Keep the room geometry, floor plan, trim, and lighting consistent while applying these finish changes.")
return lines
def build_analysis_request(payload: dict) -> dict:
room_image_data_url = payload.get("roomImageDataUrl")
if not room_image_data_url:
raise ValueError("roomImageDataUrl is required")
mime_type, base64_data = parse_data_url(room_image_data_url)
prompt = "\n".join(
[
"Analyze this room photo and return a compact JSON room map for furniture staging.",
"Return valid JSON only with these keys:",
'{'
'"summary": string,'
'"roomType": string,'
'"cameraView": string,'
'"floorPolygon": [{"x": number, "y": number}],'
'"wallZones": [{"name": string, "x": number, "y": number, "width": number, "height": number}],'
'"avoidZones": [{"name": string, "x": number, "y": number, "width": number, "height": number}],'
'"placementGuidance": [string],'
'"lighting": string'
'}',
"Use percentages from 0 to 100 for every x, y, width, and height field.",
"Keep floorPolygon to 3-6 points that approximate the visible walkable floor.",
"Be concrete and concise.",
]
)
return {
"contents": [
{
"parts": [
{
"inline_data": {
"mime_type": mime_type,
"data": base64_data,
}
},
{"text": prompt},
]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"temperature": 0.2,
},
}
def parse_data_url(data_url: str) -> tuple[str, str]:
if not data_url.startswith("data:") or "," not in data_url:
raise ValueError("roomImageDataUrl must be a valid data URL")
header, encoded = data_url.split(",", 1)
if ";base64" not in header:
raise ValueError("roomImageDataUrl must contain base64 image data")
mime_type = header[5:].split(";", 1)[0]
if not mime_type.startswith("image/"):
raise ValueError("roomImageDataUrl must contain an image")
return mime_type, encoded
def call_gemini_api(request_body: dict, api_key: str, model: str) -> dict:
url = f"{API_ROOT}/{model}:generateContent"
request = urllib.request.Request(
url,
data=json.dumps(request_body).encode("utf-8"),
headers={
"Content-Type": "application/json",
"x-goog-api-key": api_key,
},
method="POST",
)
with urllib.request.urlopen(request, timeout=90) as response:
return json.loads(response.read().decode("utf-8"))
def extract_generation_result(response: dict) -> dict:
candidates = response.get("candidates") or []
if not candidates:
raise ValueError("Gemini response did not include any candidates")
parts = (candidates[0].get("content") or {}).get("parts") or []
image_data_url = ""
text_parts = []
for part in parts:
inline_data = part.get("inlineData") or part.get("inline_data")
if inline_data and inline_data.get("data"):
mime_type = inline_data.get("mimeType") or inline_data.get("mime_type") or "image/png"
image_data_url = f"data:{mime_type};base64,{inline_data['data']}"
elif part.get("text"):
text_parts.append(part["text"])
return {
"imageDataUrl": image_data_url,
"meta": {
"model": MODEL,
"text": "\n".join(text_parts).strip() or "No text response.",
},
}
def search_similar_products(payload: dict, api_key: str | None = None) -> dict:
items = payload.get("items")
if not isinstance(items, list) or not items:
raise ValueError("items must include at least one staged item")
prompt = str(payload.get("prompt", "")).strip()
room_type = str((payload.get("roomAnalysis") or {}).get("roomType", "")).strip()
generated_image_data_url = (
payload.get("generatedImageDataUrl")
or payload.get("imageDataUrl")
or payload.get("resultImageDataUrl")
)
query_specs: dict[str, dict[str, object]] = {}
if api_key and generated_image_data_url:
try:
request_body = build_similar_product_query_request(
generated_image_data_url,
items,
prompt,
room_type,
)
response = call_gemini_api(request_body, api_key, ANALYSIS_MODEL)
query_specs = extract_similar_product_queries(response)
except Exception:
query_specs = {}
searches = []
for item in items[:6]:
if not isinstance(item, dict):
continue
item_name = str(item.get("name", "")).strip()
if not item_name:
continue
spec = query_specs.get(item_name.lower(), {})
query = str(spec.get("searchQuery", "")).strip() or build_product_search_query(
item_name,
prompt,
room_type,
)
results = fetch_duckduckgo_results(query)
searches.append(
{
"itemName": item_name,
"query": query,
"results": results,
"sourceProductUrl": str(item.get("productUrl", "")).strip(),
"traits": spec.get("traits", []),
}
)
return {"searches": searches}
def build_similar_product_query_request(
image_data_url: str,
items: list[dict],
prompt: str,
room_type: str,
) -> dict:
mime_type, base64_data = parse_data_url(image_data_url)
item_lines = [f"- {str(item.get('name', '')).strip()}" for item in items[:6] if item.get("name")]
instructions = [
"Analyze the provided generated interior image and produce highly specific shopping search queries.",
"Focus only on the staged furniture items listed below.",
*item_lines,
f"Room type: {room_type or 'unknown'}",
f"Style direction: {prompt or 'unspecified'}",
"For each listed item, identify the visible design details from the image as precisely as possible.",
"Use concise shopping-oriented descriptors like material, color, silhouette, arm style, leg style, finish, upholstery, era, and size impression.",
"Return valid JSON only in this format:",
'{"items":[{"itemName":string,"searchQuery":string,"traits":[string]}]}',
"Make each searchQuery specific enough to find visually similar products, not generic category pages.",
]
return {
"contents": [
{
"parts": [
{
"inline_data": {
"mime_type": mime_type,
"data": base64_data,
}
},
{"text": "\n".join(instructions)},
]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"temperature": 0.2,
},
}
def extract_similar_product_queries(response: dict) -> dict[str, dict[str, object]]:
candidates = response.get("candidates") or []
if not candidates:
raise ValueError("Gemini similar-product query generation returned no candidates")
parts = (candidates[0].get("content") or {}).get("parts") or []
text_payload = "\n".join(part["text"] for part in parts if part.get("text")).strip()
if not text_payload:
raise ValueError("Gemini similar-product query generation returned no JSON payload")
parsed = json.loads(strip_json_fence(text_payload))
items = parsed.get("items")
if not isinstance(items, list):
return {}
results = {}
for item in items:
if not isinstance(item, dict):
continue
item_name = str(item.get("itemName", "")).strip()
if not item_name:
continue
results[item_name.lower()] = {
"searchQuery": str(item.get("searchQuery", "")).strip(),
"traits": normalize_strings(item.get("traits")),
}
return results
def build_product_search_query(item_name: str, prompt: str, room_type: str) -> str:
style_terms = re.findall(r"[A-Za-z][A-Za-z-]+", prompt.lower())
trimmed_style = " ".join(style_terms[:4])
parts = [item_name]
if trimmed_style:
parts.append(trimmed_style)
if room_type:
parts.append(room_type)
parts.append("furniture")
return " ".join(parts)
def fetch_duckduckgo_results(query: str) -> list[dict[str, str]]:
url = f"https://html.duckduckgo.com/html/?q={urllib.parse.quote_plus(query)}"
request = urllib.request.Request(
url,
headers={
"User-Agent": "Mozilla/5.0",
},
method="GET",
)
with urllib.request.urlopen(request, timeout=20) as response:
html_text = response.read().decode("utf-8", errors="replace")
matches = re.findall(
r'<a[^>]+class="result__a"[^>]+href="([^"]+)"[^>]*>(.*?)</a>',
html_text,
flags=re.IGNORECASE | re.DOTALL,
)
results = []
for href, raw_title in matches:
clean_url = normalize_duckduckgo_href(href)
clean_title = clean_html_text(raw_title)
if not clean_url or not clean_title:
continue
results.append(
{
"title": clean_title,
"url": clean_url,
"displayUrl": urllib.parse.urlparse(clean_url).netloc,
}
)
if len(results) >= 5:
break
return results
def normalize_duckduckgo_href(href: str) -> str:
parsed = urllib.parse.urlparse(html.unescape(href))
if parsed.netloc and parsed.scheme in {"http", "https"}:
return href
query = urllib.parse.parse_qs(parsed.query)
target = query.get("uddg", [""])[0]
return urllib.parse.unquote(target)
def clean_html_text(value: str) -> str:
no_tags = re.sub(r"<[^>]+>", "", value)
return " ".join(html.unescape(no_tags).split())
def extract_analysis_result(response: dict) -> dict:
candidates = response.get("candidates") or []
if not candidates:
raise ValueError("Gemini analysis did not include any candidates")
parts = (candidates[0].get("content") or {}).get("parts") or []
text_payload = "\n".join(part["text"] for part in parts if part.get("text")).strip()
if not text_payload:
raise ValueError("Gemini analysis did not include a JSON response")
cleaned_payload = strip_json_fence(text_payload)
try:
parsed = json.loads(cleaned_payload)
except json.JSONDecodeError as exc:
raise ValueError(f"Could not parse room analysis JSON: {exc}") from exc
return {
"summary": str(parsed.get("summary", "")).strip(),
"roomType": str(parsed.get("roomType", "")).strip(),
"cameraView": str(parsed.get("cameraView", "")).strip(),
"floorPolygon": normalize_points(parsed.get("floorPolygon")),
"wallZones": normalize_rects(parsed.get("wallZones")),
"avoidZones": normalize_rects(parsed.get("avoidZones")),
"placementGuidance": normalize_strings(parsed.get("placementGuidance")),
"lighting": str(parsed.get("lighting", "")).strip(),
"model": ANALYSIS_MODEL,
}
def strip_json_fence(text: str) -> str:
stripped = text.strip()
if stripped.startswith("```"):
lines = stripped.splitlines()
if len(lines) >= 3:
return "\n".join(lines[1:-1]).strip()
return stripped
def describe_room_mapping(room_analysis: dict) -> list[str]:
if not room_analysis:
return ["- No structured room analysis was supplied."]
floor_polygon = room_analysis.get("floorPolygon") or []
wall_zones = room_analysis.get("wallZones") or []
avoid_zones = room_analysis.get("avoidZones") or []
guidance = room_analysis.get("placementGuidance") or []
return [
f"- Room summary: {room_analysis.get('summary') or 'Unknown room layout.'}",
f"- Room type: {room_analysis.get('roomType') or 'unknown'}",
f"- Camera view: {room_analysis.get('cameraView') or 'unknown'}",
f"- Lighting: {room_analysis.get('lighting') or 'unspecified'}",
f"- Floor polygon: {json.dumps(floor_polygon)}",
f"- Wall zones: {json.dumps(wall_zones)}",
f"- Avoid zones: {json.dumps(avoid_zones)}",
f"- Placement guidance: {'; '.join(guidance) if guidance else 'none'}",
]
def normalize_points(value: object) -> list[dict[str, float]]:
if not isinstance(value, list):
return []
points: list[dict[str, float]] = []
for item in value:
if not isinstance(item, dict):
continue
if "x" not in item or "y" not in item:
continue
points.append({"x": clamp_percent(item["x"]), "y": clamp_percent(item["y"])})
return points
def normalize_rects(value: object) -> list[dict[str, float | str]]:
if not isinstance(value, list):
return []
rects: list[dict[str, float | str]] = []
for item in value:
if not isinstance(item, dict):
continue
rects.append(
{
"name": str(item.get("name", "")).strip(),
"x": clamp_percent(item.get("x", 0)),
"y": clamp_percent(item.get("y", 0)),
"width": clamp_percent(item.get("width", 0)),
"height": clamp_percent(item.get("height", 0)),
}
)
return rects
def normalize_strings(value: object) -> list[str]:
if not isinstance(value, list):
return []
return [str(item).strip() for item in value if str(item).strip()]
def clamp_percent(value: object) -> float:
try:
numeric = float(value)
except (TypeError, ValueError):
return 0.0
return max(0.0, min(100.0, numeric))
if __name__ == "__main__":
server = ThreadingHTTPServer((HOST, PORT), FurnishFrameHandler)
print(f"Serving FurnishFrame at http://{HOST}:{PORT}")
server.serve_forever()