Spaces:
Paused
Paused
File size: 13,972 Bytes
1de1d01 c2730ef 8ffbb9e 9ac8107 7b2b6d1 1de1d01 fdf70dd 7b2b6d1 c2730ef 5d44843 c2730ef fff57eb c2730ef fff57eb c2730ef 8ffbb9e c2730ef 8ffbb9e c2730ef 8ffbb9e c2730ef 8ffbb9e 7b2b6d1 323762f 7b2b6d1 c2730ef 7b2b6d1 5f5f45e 323762f c2730ef 8ffbb9e c2730ef 7b2b6d1 c2730ef 7b2b6d1 c2730ef 5f5f45e 7b2b6d1 c2730ef 7b2b6d1 c2730ef 1de1d01 fa9ab33 c2730ef 8ffbb9e fa9ab33 8ffbb9e c2730ef 1de1d01 fa9ab33 323762f fa9ab33 323762f c2730ef b889d72 323762f b889d72 575ea5f b889d72 575ea5f 323762f b889d72 323762f b889d72 cbe7430 fa9ab33 cbe7430 fa9ab33 cbe7430 fa9ab33 cbe7430 fa9ab33 cbe7430 fff57eb cbe7430 | 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 | import gradio as gr
import pandas as pd
import random
import threading
import time
# --- Load dataset ---
df = pd.read_csv("amazon_eco-friendly_products.csv")
# --- Clean price column ---
def clean_price(price_str):
try:
if isinstance(price_str, str) and "$" in price_str:
return float(price_str.replace("$", "").strip())
return float(price_str)
except:
return None
df["price"] = df["price"].apply(clean_price)
df = df.dropna(subset=["price"])
# --- Environmental quotes ---
quotes = [
"π Small choices, big changes.",
"β»οΈ Every eco-friendly swap plants a seed for tomorrow.",
"π± A bamboo brush today keeps plastic away.",
"πΏ Be the change you shop for.",
"π Plastic lasts 500 years, your choice lasts forever.",
"π§ Save water, save life.",
"π Renewable is reliable.",
"π The Earth doesnβt need more stuff, it needs better stuff.",
"π Consume less, choose wise.",
"π² A greener choice is a cleaner future.",
"π You donβt need to be perfect, just better than yesterday.",
"π± One toothbrush less, a million smiles more.",
"β»οΈ What you buy today builds tomorrow.",
"πΏ Nature is not a place to visit, itβs home.",
"π Sustainable is beautiful.",
"π Every drop counts; save water, save life.",
"π Eco choices today are gifts for tomorrow.",
"π Green living is smart living.",
"π Protect the planet, protect yourself.",
"πΊ A cleaner Earth starts with mindful habits.",
"πΎ Small eco steps create huge impact.",
"π Go green, think global, act local.",
"π₯ Reduce waste, light the path for future generations.",
"π Eat consciously, live sustainably.",
"πΈ Nature thrives when you choose wisely.",
"π Less consumption, more conservation.",
"π‘ Energy saved is a planet saved.",
"π» Plant trees, grow hope.",
"π± Minimalism is sustainability in action.",
"π Care for the Earthβitβs the only home we have.",
"β»οΈ Waste less, live more.",
"πΏ Green habits, brighter future.",
"π§ Clean water, clear conscience.",
"π Sun-powered is future-powered.",
"π Your choices echo through generations.",
"π² Forests are worth more than goldβprotect them.",
"π Reduce, reuse, rethink.",
"πΈ Live lightly, tread softly.",
"πΎ Sustainability is love for the next generation.",
"π Small actions, massive change."
]
# --- Impact messages ---
impacts = {
"bamboo toothbrush": "By choosing a bamboo brush, you prevent ~4 plastic brushes a year from ending in landfills.",
"reusable bottle": "One reusable bottle saves ~1,460 plastic bottles a year.",
"eco bag": "A single reusable bag replaces ~700 plastic bags annually.",
"solar light": "Solar lights cut down ~90kg COβ emissions per year.",
"compostable plates": "Using compostable plates diverts hundreds of plastic plates from landfills yearly.",
"beeswax wrap": "Replacing plastic wrap with beeswax saves ~200 feet of plastic wrap per year.",
"stainless steel straw": "One reusable straw prevents ~500 plastic straws from polluting oceans annually.",
"reusable coffee cup": "Switching to a reusable cup saves ~400 disposable cups yearly.",
"LED bulb": "Using LED bulbs reduces ~150kg COβ emissions per year compared to incandescent bulbs.",
"bamboo cutlery": "One set of bamboo cutlery prevents ~100 plastic utensils from entering landfills each year.",
"recycled notebook": "Using recycled notebooks saves ~12 trees per 100 notebooks produced.",
"eco-friendly detergent": "Switching to eco detergent reduces harmful chemicals in water, saving aquatic life.",
"solar charger": "Solar chargers reduce dependency on grid electricity, cutting ~100kg COβ annually.",
"reusable food container": "One container saves ~200 plastic bags and wraps per year.",
"water-saving showerhead": "Water-saving showerheads save ~30,000 liters of water annually per household.",
"eco soap": "Using biodegradable soap prevents harmful chemicals from entering rivers and oceans.",
"bamboo mat": "Bamboo mats reduce plastic and synthetic mat usage, saving the environment.",
"recycled toilet paper": "One roll of recycled toilet paper saves ~17 trees compared to virgin paper.",
"energy-efficient appliances": "Switching to energy-efficient appliances reduces electricity consumption significantly.",
"organic cotton clothing": "Choosing organic cotton avoids ~5,000 liters of water per kg of fabric.",
"plant-based cleaning products": "Plant-based cleaners reduce chemical pollution in water systems.",
"reusable sandwich wrap": "One wrap replaces hundreds of single-use plastic sandwich bags yearly.",
"eco shampoo bar": "Shampoo bars save ~2 plastic bottles per year per person.",
"bamboo hairbrush": "Using a bamboo hairbrush prevents plastic brush pollution in landfills.",
"recycled packaging": "Products with recycled packaging save trees and reduce plastic waste.",
"biodegradable trash bags": "Switching to biodegradable trash bags reduces plastic landfill waste annually.",
"solar water heater": "Solar water heaters reduce electricity demand and cut COβ emissions.",
"compost bin": "Composting kitchen waste reduces methane emissions from landfills.",
"eco toothpaste": "Eco toothpaste tubes save ~1 plastic tube per person every month.",
"reusable menstrual products": "Reusable pads or cups reduce ~240 disposable items per person per year.",
"energy-saving power strip": "Using smart strips prevents phantom energy waste from electronics.",
"bamboo kitchenware": "Bamboo utensils replace plastic alternatives, reducing landfill waste.",
"eco laundry bag": "Using reusable laundry bags reduces microplastic pollution from synthetic clothes.",
"recycled pens": "One recycled pen saves ~5 plastic pens from going to landfill.",
"bamboo sunglasses": "Bamboo sunglasses reduce reliance on plastic frames, saving the environment.",
"solar backpack": "Solar backpacks charge devices sustainably without electricity.",
"eco yoga mat": "Eco-friendly yoga mats reduce PVC usage and chemical pollution.",
"compostable cutlery": "Switching prevents hundreds of plastic utensils from polluting landfills.",
"biodegradable soap wrapper": "Prevents plastic from entering oceans and decomposes naturally.",
"recycled water bottle": "Recycled bottles save energy and reduce plastic production.",
"bamboo tissue box": "Bamboo alternatives reduce plastic waste and promote sustainable forestry.",
"eco dish brush": "Using a bamboo dish brush prevents plastic waste from entering landfills."
}
# --- State ---
shown_products = []
batch_size = 9
# --- Product rendering ---
def render_products(products):
if products.empty:
return "<div class='card'>β No products found.</div>"
html = ""
for _, row in products.iterrows():
title = row.get("title", "Unknown Product")
if len(title) > 50:
title = title[:47] + "..."
price = f"${row['price']:.2f}" if row.get("price") else "N/A"
rating = row.get("rating", "N/A")
url = row.get("url", "#")
category = row.get("category", "Eco Product")
in_stock_text = row.get("inStockText", "")
description = row.get("description", "No description available.")
if len(description) > 150:
description = description[:147] + "..."
# Impact message
impact_msg = ""
for key, msg in impacts.items():
if key in str(title).lower():
impact_msg = f"<div class='impact'>{msg}</div>"
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 ""
html += f"""
<div class="card" title="Rating: {rating}" data-desc="{description}">
{img_html}
<h3>{title}</h3>
<p class="price">{price}</p>
<p class="category">Category: {category}</p>
<p>{in_stock_text}</p>
{impact_msg}
<a href="{url}" target="_blank">View Product</a>
</div>
"""
return f"<div class='grid-container'>{html}</div>"
# --- Filters, search, sort ---
def get_filtered_products(category="All", sort_by="Rating", min_price=0, max_price=1000, in_stock=False, query=""):
products = df.copy()
if category != "All":
products = products[products["category"].str.contains(category, case=False, na=False)]
if query:
products = products[
products["title"].str.contains(query, case=False, na=False) |
products["brand"].str.contains(query, case=False, na=False) |
products["category"].str.contains(query, case=False, na=False)
]
products = products[(products["price"] >= min_price) & (products["price"] <= max_price)]
if in_stock:
products = products[products["inStock"] == True]
if sort_by == "Price: Low to High":
products = products.sort_values("price", ascending=True)
elif sort_by == "Price: High to Low":
products = products.sort_values("price", ascending=False)
elif sort_by == "Rating":
products = products.sort_values("rating", ascending=False)
return products
def show_products(category, sort_by, min_price, max_price, in_stock, query):
global shown_products
products = get_filtered_products(category, sort_by, min_price, max_price, in_stock, query)
shown_products = products.head(batch_size)
return render_products(shown_products)
def load_more(category, sort_by, min_price, max_price, in_stock, query):
global shown_products
products = get_filtered_products(category, sort_by, min_price, max_price, in_stock, query)
already_shown_ids = set(shown_products["id"]) if not pd.DataFrame(shown_products).empty else set()
remaining = products[~products["id"].isin(already_shown_ids)]
if remaining.empty:
return render_products(shown_products)
next_batch = remaining.head(batch_size)
shown_products = pd.concat([pd.DataFrame(shown_products), next_batch])
return render_products(shown_products)
# --- Random products on start ---
def show_random_products():
global shown_products
shown_products = df.sample(n=batch_size)
return render_products(shown_products)
# --- Quote Slideshow ---
current_quote = [random.choice(quotes)]
def get_quote():
return f"<div class='quote-box'>{current_quote[0]}</div>"
def cycle_quotes():
while True:
current_quote[0] = random.choice(quotes)
time.sleep(5)
threading.Thread(target=cycle_quotes, daemon=True).start()
# --- Gradio UI ---
with gr.Blocks(css="""
body {background: #0d1117; color: #e6edf3; font-family: 'Segoe UI', sans-serif; margin:0; padding:0;}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 15px;
justify-items: center;
}
.card {
backdrop-filter: blur(12px);
background: rgba(255,255,255,0.05);
border-radius: 16px;
padding: 20px;
box-shadow: 0 0 15px rgba(0,255,150,0.2);
transition: transform 0.2s ease;
width: 220px;
min-height: 320px;
text-align:center;
position: relative;
overflow: hidden;
}
.card:hover {transform: scale(1.05); box-shadow: 0 0 25px rgba(0,255,150,0.5);}
.card::after {
content: attr(data-desc);
position: absolute;
background: rgba(0,0,0,0.9);
color: #fff;
padding: 10px;
border-radius: 8px;
font-size: 0.85em;
white-space: normal;
width: 250px;
display: none;
z-index: 1000;
}
.card:hover::after { display: block; }
.card:hover::after {
top: 0;
left: 100%;
margin-left: 10px;
}
@media(max-width: 1024px) {
.card:hover::after {
left: auto;
right: 100%;
margin-left: 0;
margin-right: 10px;
}
}
@media(max-width: 767px) {
.card:hover::after {
position: static;
display: block;
width: auto;
margin-top: 10px;
}
}
h3 {font-size: 1.1em; margin: 5px 0; word-wrap: break-word;}
.price {color: #39ff14; font-weight: bold;}
a {color: #58a6ff; text-decoration: none;}
a:hover {text-decoration: underline;}
.quote-box {padding: 12px; margin: 10px 0; background: rgba(0,255,100,0.1); border-left: 4px solid #39ff14; font-style: italic; border-radius: 8px; text-align:center;}
.impact {color: #ffd700; margin-top: 6px; font-size: 0.9em;}
""") as demo:
# Search & filters on top
with gr.Row():
query = gr.Textbox(label="Search")
category = gr.Dropdown(["All"] + sorted(df["category"].dropna().unique().tolist()), label="Category")
sort_by = gr.Dropdown(["Price: Low to High", "Price: High to Low", "Rating"], label="Sort By", value="Rating")
min_price = gr.Number(label="Min Price", value=0)
max_price = gr.Number(label="Max Price", value=1000)
in_stock = gr.Checkbox(label="In Stock Only")
show_btn = gr.Button("π Show Products")
load_more_btn = gr.Button("β Load More")
output = gr.HTML()
# Quote row
with gr.Row():
quote_output = gr.HTML()
demo.load(get_quote, None, quote_output)
# Show random products on page load
demo.load(show_random_products, None, output)
show_btn.click(show_products, [category, sort_by, min_price, max_price, in_stock, query], output)
load_more_btn.click(load_more, [category, sort_by, min_price, max_price, in_stock, query], output)
def show_footer():
return """
<div style="text-align:center; font-size:0.8em; color:#888; margin-top:20px;">
Β© Krishna Jha | <a href="https://www.instagram.com/kosmos.cpp/" target="_blank" style="color:#888;">@kosmos.cpp</a>
</div>
"""
demo.launch() |