Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from datetime import datetime, timedelta | |
| import random | |
| import gradio as gr | |
| from sklearn.linear_model import LinearRegression | |
| import tempfile | |
| def generate_synthetic_data(n_days=90): | |
| dates = pd.date_range(end=datetime.today(), periods=n_days) | |
| data = [] | |
| store_names = [f"Store_{i}" for i in range(1, 21)] | |
| categories = ['Fashion', 'Electronics', 'Food', 'Entertainment', 'Home Decor'] | |
| zones = ['Ground Floor', 'First Floor', 'Second Floor'] | |
| for date in dates: | |
| for store in store_names: | |
| visitors = np.random.poisson(lam=150) | |
| sales = np.random.normal(loc=visitors * 10, scale=visitors * 2) | |
| dwell_time = np.random.normal(loc=30, scale=5) | |
| category = random.choice(categories) | |
| zone = random.choice(zones) | |
| data.append({ | |
| 'date': date, | |
| 'store': store, | |
| 'category': category, | |
| 'zone': zone, | |
| 'visitors': max(0, visitors), | |
| 'sales': max(0, sales), | |
| 'dwell_time': max(5, dwell_time) | |
| }) | |
| return pd.DataFrame(data) | |
| def forecast_sales(df, days_ahead=7): | |
| daily_sales = df.groupby('date')['sales'].sum().reset_index() | |
| daily_sales['day_num'] = (daily_sales['date'] - daily_sales['date'].min()).dt.days | |
| X = daily_sales[['day_num']] | |
| y = daily_sales['sales'] | |
| model = LinearRegression().fit(X, y) | |
| last_day = daily_sales['day_num'].max() | |
| future_days = np.arange(last_day + 1, last_day + 1 + days_ahead).reshape(-1, 1) | |
| future_dates = [daily_sales['date'].max() + timedelta(days=i) for i in range(1, days_ahead + 1)] | |
| predictions = model.predict(future_days) | |
| forecast_df = pd.DataFrame({'date': future_dates, 'sales': predictions}) | |
| return forecast_df | |
| def generate_chart(chart_type): | |
| df = generate_synthetic_data() | |
| forecast_df = forecast_sales(df) | |
| daily_visits = df.groupby('date')['visitors'].sum().reset_index() | |
| category_sales = df.groupby('category')['sales'].sum().reset_index() | |
| zone_traffic = df.groupby('zone')['visitors'].sum().reset_index() | |
| top_stores = df.groupby('store')['sales'].sum().nlargest(10).reset_index() | |
| dwell_time = df.groupby('category')['dwell_time'].mean().reset_index() | |
| fig, ax = plt.subplots(figsize=(8, 5)) | |
| plt.tight_layout() | |
| if chart_type == "Daily Footfall": | |
| ax.plot(daily_visits['date'], daily_visits['visitors'], color='blue') | |
| ax.set_title('Daily Footfall') | |
| ax.set_xlabel('Date') | |
| ax.set_ylabel('Visitors') | |
| elif chart_type == "Sales by Category": | |
| ax.bar(category_sales['category'], category_sales['sales'], color='green') | |
| ax.set_title('Sales by Category') | |
| ax.set_ylabel('Sales') | |
| elif chart_type == "Footfall by Zone": | |
| ax.pie(zone_traffic['visitors'], labels=zone_traffic['zone'], autopct='%1.1f%%') | |
| ax.set_title('Footfall by Zone') | |
| elif chart_type == "Top 10 Stores by Sales": | |
| ax.bar(top_stores['store'], top_stores['sales'], color='purple') | |
| ax.set_title('Top 10 Stores by Sales') | |
| ax.set_ylabel('Sales') | |
| plt.xticks(rotation=45) | |
| elif chart_type == "Avg Dwell Time by Category": | |
| ax.bar(dwell_time['category'], dwell_time['dwell_time'], color='orange') | |
| ax.set_title('Avg Dwell Time by Category') | |
| ax.set_ylabel('Dwell Time (mins)') | |
| elif chart_type == "Forecasted Sales (Next 7 Days)": | |
| ax.plot(forecast_df['date'], forecast_df['sales'], color='red') | |
| ax.set_title('Forecasted Sales (Next 7 Days)') | |
| ax.set_xlabel('Date') | |
| ax.set_ylabel('Predicted Sales') | |
| else: | |
| return None | |
| tmp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False) | |
| plt.savefig(tmp_file.name, bbox_inches='tight') | |
| plt.close(fig) | |
| return tmp_file.name | |
| with gr.Blocks(title="π Mall Dashboard with Matplotlib") as demo: | |
| gr.Markdown("## π¬ Mall Analytics Dashboard (Matplotlib Version)") | |
| chart_dropdown = gr.Dropdown( | |
| choices=[ | |
| "Daily Footfall", | |
| "Sales by Category", | |
| "Footfall by Zone", | |
| "Top 10 Stores by Sales", | |
| "Avg Dwell Time by Category", | |
| "Forecasted Sales (Next 7 Days)" | |
| ], | |
| value="Daily Footfall", | |
| label="Choose a Chart" | |
| ) | |
| generate_button = gr.Button("Generate Chart") | |
| image_output = gr.Image(label="Chart Output") | |
| generate_button.click(fn=generate_chart, inputs=chart_dropdown, outputs=image_output) | |
| demo.launch() |