Spaces:
Running
Running
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.cluster import KMeans | |
| from sklearn.tree import DecisionTreeClassifier | |
| # ----------------------------- | |
| # Dummy Dataset (Replace with your actual model if available) | |
| # ----------------------------- | |
| np.random.seed(42) | |
| data = [] | |
| for _ in range(200): | |
| data.append([ | |
| np.random.uniform(0.0, 1.0), # GreenPurchaseRatio | |
| np.random.uniform(1.0, 7.0), # CarbonFootprintScore | |
| np.random.uniform(0.0, 1.0), # RecyclingRate | |
| np.random.uniform(0, 30), # EcoPremiumWillingness | |
| np.random.randint(1, 40), # PurchaseFrequency | |
| np.random.randint(0, 100) # SustainabilityScore | |
| ]) | |
| columns = [ | |
| "GreenPurchaseRatio", | |
| "CarbonFootprintScore", | |
| "RecyclingRate", | |
| "EcoPremiumWillingness", | |
| "PurchaseFrequency", | |
| "SustainabilityScore" | |
| ] | |
| df_dummy = pd.DataFrame(data, columns=columns) | |
| # ----------------------------- | |
| # Train Models | |
| # ----------------------------- | |
| scaler = StandardScaler() | |
| X_scaled = scaler.fit_transform(df_dummy) | |
| kmeans = KMeans(n_clusters=4, random_state=42, n_init=10) | |
| clusters = kmeans.fit_predict(X_scaled) | |
| df_dummy["Cluster"] = clusters | |
| tree = DecisionTreeClassifier(max_depth=5, random_state=42) | |
| tree.fit(X_scaled, clusters) | |
| # ----------------------------- | |
| # Cluster Names | |
| # ----------------------------- | |
| cluster_names = { | |
| 0: "Price Driven", | |
| 1: "Eco Indifferent", | |
| 2: "Green Champion", | |
| 3: "Eco Curious" | |
| } | |
| # ----------------------------- | |
| # Marketing Recommendations | |
| # ----------------------------- | |
| recommendations = { | |
| "Green Champion": """ | |
| Premium Eco Products | |
| Sustainability Rewards | |
| Carbon Offset Programs | |
| Exclusive Green Membership | |
| """, | |
| "Eco Curious": """ | |
| Sustainability Awareness Campaigns | |
| First-Time Green Discounts | |
| Eco Product Recommendations | |
| Loyalty Incentives | |
| """, | |
| "Price Driven": """ | |
| Cost Saving Offers | |
| Bundle Discounts | |
| Budget Friendly Eco Products | |
| Cashback Campaigns | |
| """, | |
| "Eco Indifferent": """ | |
| Convenience-Based Promotions | |
| Product Quality Campaigns | |
| Personalized Deals | |
| Retention Strategies | |
| """ | |
| } | |
| # ----------------------------- | |
| # Prediction Function | |
| # ----------------------------- | |
| def predict_segment( | |
| gpr, | |
| carbon, | |
| recycle, | |
| premium, | |
| freq, | |
| score | |
| ): | |
| sample = pd.DataFrame([[ | |
| gpr, | |
| carbon, | |
| recycle, | |
| premium, | |
| freq, | |
| score | |
| ]], columns=columns) | |
| sample_scaled = scaler.transform(sample) | |
| pred = tree.predict(sample_scaled)[0] | |
| segment = cluster_names[pred] | |
| strategy = recommendations[segment] | |
| return segment, strategy | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # 🌿 GreenLeaf Retail Analytics Dashboard | |
| ### AI-Powered Customer Segmentation System | |
| This system uses: | |
| - K-Means Clustering | |
| - Decision Tree Classification | |
| to identify customer segments and recommend targeted marketing strategies. | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("## Customer Inputs") | |
| gpr = gr.Slider( | |
| 0, | |
| 1, | |
| value=0.5, | |
| label="Green Purchase Ratio" | |
| ) | |
| carbon = gr.Slider( | |
| 1, | |
| 7, | |
| value=3, | |
| label="Carbon Footprint Score" | |
| ) | |
| recycle = gr.Slider( | |
| 0, | |
| 1, | |
| value=0.5, | |
| label="Recycling Engagement Rate" | |
| ) | |
| premium = gr.Slider( | |
| 0, | |
| 30, | |
| value=10, | |
| label="Eco Premium Willingness (%)" | |
| ) | |
| freq = gr.Slider( | |
| 1, | |
| 40, | |
| value=15, | |
| label="Purchase Frequency" | |
| ) | |
| score = gr.Slider( | |
| 0, | |
| 100, | |
| value=50, | |
| label="Sustainability Score" | |
| ) | |
| predict_btn = gr.Button( | |
| "Predict Customer Segment", | |
| variant="primary" | |
| ) | |
| with gr.Column(): | |
| gr.Markdown("## Prediction Result") | |
| segment_output = gr.Textbox( | |
| label="Customer Segment", | |
| lines=2 | |
| ) | |
| recommendation_output = gr.Textbox( | |
| label="Recommended Marketing Strategy", | |
| lines=10 | |
| ) | |
| predict_btn.click( | |
| fn=predict_segment, | |
| inputs=[ | |
| gpr, | |
| carbon, | |
| recycle, | |
| premium, | |
| freq, | |
| score | |
| ], | |
| outputs=[ | |
| segment_output, | |
| recommendation_output | |
| ] | |
| ) | |
| with gr.Accordion("Model Information", open=False): | |
| gr.Markdown(""" | |
| ### Project Details | |
| #### Dataset Features | |
| - Green Purchase Ratio | |
| - Carbon Footprint Score | |
| - Recycling Engagement Rate | |
| - Eco Premium Willingness | |
| - Purchase Frequency | |
| - Sustainability Score | |
| #### Algorithms Used | |
| - K-Means Clustering (K = 4) | |
| - Decision Tree Classifier | |
| #### Customer Segments | |
| - Green Champion | |
| - Eco Curious | |
| - Price Driven | |
| - Eco Indifferent | |
| #### Business Objective | |
| Customer Segmentation for Green Retail Marketing. | |
| """) | |
| demo.launch() |