Update utils/formatter.py
Browse files- utils/formatter.py +72 -39
utils/formatter.py
CHANGED
@@ -1,46 +1,79 @@
|
|
1 |
-
|
2 |
-
def format_html(recommendations):
|
3 |
-
if not recommendations:
|
4 |
-
print("No recommendations found.") # Debug log
|
5 |
-
return "No recommendations found."
|
6 |
-
|
7 |
-
result_html = "<h3>Recommended Jewelry Items💍💎:</h3>"
|
8 |
-
for i, rec in enumerate(recommendations, 1):
|
9 |
-
metadata = rec["metadata"]
|
10 |
-
|
11 |
-
# Start the recommendation tile
|
12 |
-
result_html += f"<div style='margin-bottom:15px; padding:10px; border:1px solid #ddd; border-radius:5px;'>"
|
13 |
-
|
14 |
-
# Add the title
|
15 |
-
result_html += f"<h4>#{i}: {metadata.get('TITLE', 'unavailabe😔☹')}</h4>"
|
16 |
-
|
17 |
-
# Add images side by side using Flexbox
|
18 |
-
if metadata.get('LINK1') or metadata.get('LINK2'):
|
19 |
-
result_html += "<div style='display: flex; gap: 10px; margin-bottom: 10px;'>"
|
20 |
-
if metadata.get('LINK1'):
|
21 |
-
result_html += f"<img src='{metadata['LINK1']}' style='max-width: 200px; max-height: 200px; border-radius: 5px;'>"
|
22 |
-
if metadata.get('LINK2'):
|
23 |
-
result_html += f"<img src='{metadata['LINK2']}' style='max-width: 200px; max-height: 200px; border-radius: 5px;'>"
|
24 |
-
result_html += "</div>"
|
25 |
-
else:
|
26 |
-
result_html += "<p>No image available</p>"
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
# Close the recommendation tile
|
39 |
-
result_html += "</div>"
|
40 |
|
41 |
-
print("HTML output generated:", result_html) # Debug log
|
42 |
-
return result_html
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
|
46 |
|
|
|
1 |
+
# formatter.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
class ResultFormatter:
|
4 |
+
"""Formats recommendation results for display."""
|
5 |
+
|
6 |
+
@staticmethod
|
7 |
+
def format_html(recommendations):
|
8 |
+
if not recommendations:
|
9 |
+
print("No recommendations found.") # Debug log
|
10 |
+
return "No recommendations found."
|
11 |
+
|
12 |
+
result_html = "<h3>Recommended Jewelry Items💍💎:</h3>"
|
13 |
+
for i, rec in enumerate(recommendations, 1):
|
14 |
+
metadata = rec["metadata"]
|
15 |
+
|
16 |
+
# Start the recommendation tile
|
17 |
+
result_html += f"<div style='margin-bottom:15px; padding:10px; border:1px solid #ddd; border-radius:5px;'>"
|
18 |
+
|
19 |
+
# Add the title
|
20 |
+
result_html += f"<h4>#{i}: {metadata.get('TITLE', 'unavailabe😔☹')}</h4>"
|
21 |
+
|
22 |
+
# Add images side by side using Flexbox
|
23 |
+
if metadata.get('LINK1') or metadata.get('LINK2'):
|
24 |
+
result_html += "<div style='display: flex; gap: 10px; margin-bottom: 10px;'>"
|
25 |
+
if metadata.get('LINK1'):
|
26 |
+
result_html += f"<img src='{metadata['LINK1']}' style='max-width: 300px; max-height: 300px; border-radius: 5px;'>"
|
27 |
+
if metadata.get('LINK2'):
|
28 |
+
result_html += f"<img src='{metadata['LINK2']}' style='max-width: 300px; max-height: 300px; border-radius: 5px;'>"
|
29 |
+
result_html += "</div>"
|
30 |
+
else:
|
31 |
+
result_html += "<p>No image available</p>"
|
32 |
+
|
33 |
+
# Add metadata details
|
34 |
+
result_html += f"<p><b>Tanishq design id:</b> {metadata.get('Design_ID', 'fetching...')}</p>"
|
35 |
+
result_html += f"<p><b>Category: </b> {metadata.get('CATEGORY_TYPE', 'fetching...')}</p>"
|
36 |
+
result_html += f"<p><b>Carets: </b> {metadata.get('GOLD_KARATAGE', 'fetching...')}</p>"
|
37 |
+
result_html += f"<p><b>Description: </b> {metadata.get('SHORT_DESCRIPTION', 'No description available')}</p>"
|
38 |
+
result_html += f"<p><b>Price in 💲(approx):</b> $ {metadata.get('PRICE', 'N/A')}</p>"
|
39 |
+
result_html += f"<p><b>Metal type: </b> {metadata.get('METAL', 'fetching')}</p>"
|
40 |
+
result_html += f"<p><b>Jewellery type: </b> {metadata.get('JEWELLERY_TYPE', 'fetching...')}</p>"
|
41 |
+
result_html += f"<p><b>Similarity👉👈Score:</b> {rec['similarity_score']:.4f}</p>"
|
42 |
+
|
43 |
+
# Close the recommendation tile
|
44 |
+
result_html += "</div>"
|
45 |
+
|
46 |
+
print("HTML output generated:", result_html) # Debug log
|
47 |
+
return result_html
|
48 |
|
|
|
|
|
49 |
|
|
|
|
|
50 |
|
51 |
+
|
52 |
+
@staticmethod
|
53 |
+
def format_json(recommendations):
|
54 |
+
"""Format recommendations as JSON.
|
55 |
+
|
56 |
+
Args:
|
57 |
+
recommendations (list): List of recommendation dictionaries
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
list: Clean JSON-serializable results
|
61 |
+
"""
|
62 |
+
if not recommendations:
|
63 |
+
return []
|
64 |
+
|
65 |
+
results = []
|
66 |
+
for rec in recommendations:
|
67 |
+
results.append({
|
68 |
+
"TITLE": rec["metadata"].get("TITLE", "Unknown"),
|
69 |
+
"CATEGORY_TYPE": rec["metadata"].get("CATEGORY_TYPE", "Unknown"),
|
70 |
+
"SHORT_DESCRIPTION": rec["metadata"].get("SHORT_DESCRIPTION", "No description"),
|
71 |
+
"PRICE": rec["metadata"].get("PRICE", "N/A"),
|
72 |
+
"similarity_score": round(rec["similarity_score"], 4),
|
73 |
+
"image_link🔗": rec["metadata"].get("LINK1", None)
|
74 |
+
})
|
75 |
+
|
76 |
+
return results
|
77 |
|
78 |
|
79 |
|