Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
# Load dataset
|
| 6 |
df = pd.read_csv("amazon_eco-friendly_products.csv")
|
| 7 |
|
| 8 |
-
# ---
|
| 9 |
def clean_price(price_str):
|
| 10 |
try:
|
| 11 |
if isinstance(price_str, str) and "$" in price_str:
|
|
@@ -44,8 +44,8 @@ impacts = {
|
|
| 44 |
"solar light": "Solar lights cut down ~90kg CO₂ emissions per year.",
|
| 45 |
}
|
| 46 |
|
| 47 |
-
# --- Product
|
| 48 |
-
def get_products(category, sort_by, min_price, max_price, in_stock):
|
| 49 |
products = df.copy()
|
| 50 |
|
| 51 |
if category != "All":
|
|
@@ -54,7 +54,7 @@ def get_products(category, sort_by, min_price, max_price, in_stock):
|
|
| 54 |
products = products[(products["price"] >= min_price) & (products["price"] <= max_price)]
|
| 55 |
|
| 56 |
if in_stock:
|
| 57 |
-
products = products[products["
|
| 58 |
|
| 59 |
if sort_by == "Price: Low to High":
|
| 60 |
products = products.sort_values("price", ascending=True)
|
|
@@ -71,24 +71,28 @@ def get_products(category, sort_by, min_price, max_price, in_stock):
|
|
| 71 |
html = f"<div class='quote-box'>{quote}</div>"
|
| 72 |
|
| 73 |
for _, row in products.head(9).iterrows():
|
| 74 |
-
|
| 75 |
-
price = f"${row['price']:.2f}"
|
| 76 |
rating = row.get("rating", "N/A")
|
| 77 |
url = row.get("url", "#")
|
| 78 |
category = row.get("category", "Eco Product")
|
| 79 |
-
|
| 80 |
|
| 81 |
-
#
|
| 82 |
impact_msg = ""
|
| 83 |
for key, msg in impacts.items():
|
| 84 |
-
if key in
|
| 85 |
impact_msg = f"<div class='impact'>{msg}</div>"
|
| 86 |
|
|
|
|
|
|
|
| 87 |
html += f"""
|
| 88 |
-
<div class="card" title="
|
| 89 |
-
|
|
|
|
| 90 |
<p class="price">{price}</p>
|
| 91 |
<p class="category">Category: {category}</p>
|
|
|
|
| 92 |
{impact_msg}
|
| 93 |
<a href="{url}" target="_blank">View Product</a>
|
| 94 |
</div>
|
|
@@ -98,7 +102,7 @@ def get_products(category, sort_by, min_price, max_price, in_stock):
|
|
| 98 |
# --- Gradio UI ---
|
| 99 |
with gr.Blocks(css="""
|
| 100 |
body {background: #0d1117; color: #e6edf3; font-family: 'Segoe UI', sans-serif;}
|
| 101 |
-
.card {backdrop-filter: blur(12px); background: rgba(255,255,255,0.05); border-radius: 16px; padding: 20px; margin: 10px; box-shadow: 0 0 15px rgba(0,255,150,0.2); transition: transform 0.2s ease;}
|
| 102 |
.card:hover {transform: scale(1.05); box-shadow: 0 0 25px rgba(0,255,150,0.5);}
|
| 103 |
.price {color: #39ff14; font-weight: bold;}
|
| 104 |
a {color: #58a6ff; text-decoration: none;}
|
|
@@ -114,11 +118,11 @@ a:hover {text-decoration: underline;}
|
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
min_price = gr.Number(label="Min Price", value=0)
|
| 117 |
-
max_price = gr.Number(label="Max Price", value=
|
| 118 |
in_stock = gr.Checkbox(label="In Stock Only")
|
| 119 |
|
| 120 |
show_btn = gr.Button("🔍 Show Products")
|
| 121 |
-
output = gr.HTML()
|
| 122 |
|
| 123 |
show_btn.click(get_products, [category, sort_by, min_price, max_price, in_stock], output)
|
| 124 |
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
# --- Load dataset ---
|
| 6 |
df = pd.read_csv("amazon_eco-friendly_products.csv")
|
| 7 |
|
| 8 |
+
# --- Clean price column ---
|
| 9 |
def clean_price(price_str):
|
| 10 |
try:
|
| 11 |
if isinstance(price_str, str) and "$" in price_str:
|
|
|
|
| 44 |
"solar light": "Solar lights cut down ~90kg CO₂ emissions per year.",
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# --- Product rendering function ---
|
| 48 |
+
def get_products(category="All", sort_by="Rating", min_price=0, max_price=1000, in_stock=False):
|
| 49 |
products = df.copy()
|
| 50 |
|
| 51 |
if category != "All":
|
|
|
|
| 54 |
products = products[(products["price"] >= min_price) & (products["price"] <= max_price)]
|
| 55 |
|
| 56 |
if in_stock:
|
| 57 |
+
products = products[products["inStock"] == True]
|
| 58 |
|
| 59 |
if sort_by == "Price: Low to High":
|
| 60 |
products = products.sort_values("price", ascending=True)
|
|
|
|
| 71 |
html = f"<div class='quote-box'>{quote}</div>"
|
| 72 |
|
| 73 |
for _, row in products.head(9).iterrows():
|
| 74 |
+
title = row.get("title", "Unknown Product")
|
| 75 |
+
price = f"${row['price']:.2f}" if row.get("price") else "N/A"
|
| 76 |
rating = row.get("rating", "N/A")
|
| 77 |
url = row.get("url", "#")
|
| 78 |
category = row.get("category", "Eco Product")
|
| 79 |
+
in_stock_text = row.get("inStockText", "")
|
| 80 |
|
| 81 |
+
# Impact message check
|
| 82 |
impact_msg = ""
|
| 83 |
for key, msg in impacts.items():
|
| 84 |
+
if key in str(title).lower():
|
| 85 |
impact_msg = f"<div class='impact'>{msg}</div>"
|
| 86 |
|
| 87 |
+
img_html = f"<img src='{row['img_url']}' style='width:120px; height:120px; object-fit:cover; border-radius:10px;'/>" if row.get("img_url") else ""
|
| 88 |
+
|
| 89 |
html += f"""
|
| 90 |
+
<div class="card" title="Rating: {rating}">
|
| 91 |
+
{img_html}
|
| 92 |
+
<h3>{title}</h3>
|
| 93 |
<p class="price">{price}</p>
|
| 94 |
<p class="category">Category: {category}</p>
|
| 95 |
+
<p>{in_stock_text}</p>
|
| 96 |
{impact_msg}
|
| 97 |
<a href="{url}" target="_blank">View Product</a>
|
| 98 |
</div>
|
|
|
|
| 102 |
# --- Gradio UI ---
|
| 103 |
with gr.Blocks(css="""
|
| 104 |
body {background: #0d1117; color: #e6edf3; font-family: 'Segoe UI', sans-serif;}
|
| 105 |
+
.card {backdrop-filter: blur(12px); background: rgba(255,255,255,0.05); border-radius: 16px; padding: 20px; margin: 10px; box-shadow: 0 0 15px rgba(0,255,150,0.2); transition: transform 0.2s ease; display:inline-block; vertical-align:top; width:200px; text-align:center;}
|
| 106 |
.card:hover {transform: scale(1.05); box-shadow: 0 0 25px rgba(0,255,150,0.5);}
|
| 107 |
.price {color: #39ff14; font-weight: bold;}
|
| 108 |
a {color: #58a6ff; text-decoration: none;}
|
|
|
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
min_price = gr.Number(label="Min Price", value=0)
|
| 121 |
+
max_price = gr.Number(label="Max Price", value=1000)
|
| 122 |
in_stock = gr.Checkbox(label="In Stock Only")
|
| 123 |
|
| 124 |
show_btn = gr.Button("🔍 Show Products")
|
| 125 |
+
output = gr.HTML(get_products()) # Show products immediately on load
|
| 126 |
|
| 127 |
show_btn.click(get_products, [category, sort_by, min_price, max_price, in_stock], output)
|
| 128 |
|