Spaces:
Sleeping
Sleeping
Delete app (1).py
Browse files- app (1).py +0 -114
app (1).py
DELETED
|
@@ -1,114 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
from datetime import datetime, timedelta
|
| 6 |
-
import random
|
| 7 |
-
import gradio as gr
|
| 8 |
-
from sklearn.linear_model import LinearRegression
|
| 9 |
-
import tempfile
|
| 10 |
-
|
| 11 |
-
def generate_synthetic_data(n_days=90):
|
| 12 |
-
dates = pd.date_range(end=datetime.today(), periods=n_days)
|
| 13 |
-
data = []
|
| 14 |
-
store_names = [f"Store_{i}" for i in range(1, 21)]
|
| 15 |
-
categories = ['Fashion', 'Electronics', 'Food', 'Entertainment', 'Home Decor']
|
| 16 |
-
zones = ['Ground Floor', 'First Floor', 'Second Floor']
|
| 17 |
-
|
| 18 |
-
for date in dates:
|
| 19 |
-
for store in store_names:
|
| 20 |
-
visitors = np.random.poisson(lam=150)
|
| 21 |
-
sales = np.random.normal(loc=visitors * 10, scale=visitors * 2)
|
| 22 |
-
dwell_time = np.random.normal(loc=30, scale=5)
|
| 23 |
-
category = random.choice(categories)
|
| 24 |
-
zone = random.choice(zones)
|
| 25 |
-
data.append({
|
| 26 |
-
'date': date,
|
| 27 |
-
'store': store,
|
| 28 |
-
'category': category,
|
| 29 |
-
'zone': zone,
|
| 30 |
-
'visitors': max(0, visitors),
|
| 31 |
-
'sales': max(0, sales),
|
| 32 |
-
'dwell_time': max(5, dwell_time)
|
| 33 |
-
})
|
| 34 |
-
return pd.DataFrame(data)
|
| 35 |
-
|
| 36 |
-
def forecast_sales(df, days_ahead=7):
|
| 37 |
-
daily_sales = df.groupby('date')['sales'].sum().reset_index()
|
| 38 |
-
daily_sales['day_num'] = (daily_sales['date'] - daily_sales['date'].min()).dt.days
|
| 39 |
-
X = daily_sales[['day_num']]
|
| 40 |
-
y = daily_sales['sales']
|
| 41 |
-
model = LinearRegression().fit(X, y)
|
| 42 |
-
last_day = daily_sales['day_num'].max()
|
| 43 |
-
future_days = np.arange(last_day + 1, last_day + 1 + days_ahead).reshape(-1, 1)
|
| 44 |
-
future_dates = [daily_sales['date'].max() + timedelta(days=i) for i in range(1, days_ahead + 1)]
|
| 45 |
-
predictions = model.predict(future_days)
|
| 46 |
-
forecast_df = pd.DataFrame({'date': future_dates, 'sales': predictions})
|
| 47 |
-
return forecast_df
|
| 48 |
-
|
| 49 |
-
def generate_chart(chart_type):
|
| 50 |
-
df = generate_synthetic_data()
|
| 51 |
-
forecast_df = forecast_sales(df)
|
| 52 |
-
|
| 53 |
-
daily_visits = df.groupby('date')['visitors'].sum().reset_index()
|
| 54 |
-
category_sales = df.groupby('category')['sales'].sum().reset_index()
|
| 55 |
-
zone_traffic = df.groupby('zone')['visitors'].sum().reset_index()
|
| 56 |
-
top_stores = df.groupby('store')['sales'].sum().nlargest(10).reset_index()
|
| 57 |
-
dwell_time = df.groupby('category')['dwell_time'].mean().reset_index()
|
| 58 |
-
|
| 59 |
-
fig, ax = plt.subplots(figsize=(8, 5))
|
| 60 |
-
plt.tight_layout()
|
| 61 |
-
|
| 62 |
-
if chart_type == "Daily Footfall":
|
| 63 |
-
ax.plot(daily_visits['date'], daily_visits['visitors'], color='blue')
|
| 64 |
-
ax.set_title('Daily Footfall')
|
| 65 |
-
ax.set_xlabel('Date')
|
| 66 |
-
ax.set_ylabel('Visitors')
|
| 67 |
-
elif chart_type == "Sales by Category":
|
| 68 |
-
ax.bar(category_sales['category'], category_sales['sales'], color='green')
|
| 69 |
-
ax.set_title('Sales by Category')
|
| 70 |
-
ax.set_ylabel('Sales')
|
| 71 |
-
elif chart_type == "Footfall by Zone":
|
| 72 |
-
ax.pie(zone_traffic['visitors'], labels=zone_traffic['zone'], autopct='%1.1f%%')
|
| 73 |
-
ax.set_title('Footfall by Zone')
|
| 74 |
-
elif chart_type == "Top 10 Stores by Sales":
|
| 75 |
-
ax.bar(top_stores['store'], top_stores['sales'], color='purple')
|
| 76 |
-
ax.set_title('Top 10 Stores by Sales')
|
| 77 |
-
ax.set_ylabel('Sales')
|
| 78 |
-
plt.xticks(rotation=45)
|
| 79 |
-
elif chart_type == "Avg Dwell Time by Category":
|
| 80 |
-
ax.bar(dwell_time['category'], dwell_time['dwell_time'], color='orange')
|
| 81 |
-
ax.set_title('Avg Dwell Time by Category')
|
| 82 |
-
ax.set_ylabel('Dwell Time (mins)')
|
| 83 |
-
elif chart_type == "Forecasted Sales (Next 7 Days)":
|
| 84 |
-
ax.plot(forecast_df['date'], forecast_df['sales'], color='red')
|
| 85 |
-
ax.set_title('Forecasted Sales (Next 7 Days)')
|
| 86 |
-
ax.set_xlabel('Date')
|
| 87 |
-
ax.set_ylabel('Predicted Sales')
|
| 88 |
-
else:
|
| 89 |
-
return None
|
| 90 |
-
|
| 91 |
-
tmp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 92 |
-
plt.savefig(tmp_file.name, bbox_inches='tight')
|
| 93 |
-
plt.close(fig)
|
| 94 |
-
return tmp_file.name
|
| 95 |
-
|
| 96 |
-
with gr.Blocks(title="📊 Mall Dashboard with Matplotlib") as demo:
|
| 97 |
-
gr.Markdown("## 🏬 Mall Analytics Dashboard (Matplotlib Version)")
|
| 98 |
-
chart_dropdown = gr.Dropdown(
|
| 99 |
-
choices=[
|
| 100 |
-
"Daily Footfall",
|
| 101 |
-
"Sales by Category",
|
| 102 |
-
"Footfall by Zone",
|
| 103 |
-
"Top 10 Stores by Sales",
|
| 104 |
-
"Avg Dwell Time by Category",
|
| 105 |
-
"Forecasted Sales (Next 7 Days)"
|
| 106 |
-
],
|
| 107 |
-
value="Daily Footfall",
|
| 108 |
-
label="Choose a Chart"
|
| 109 |
-
)
|
| 110 |
-
generate_button = gr.Button("Generate Chart")
|
| 111 |
-
image_output = gr.Image(label="Chart Output")
|
| 112 |
-
generate_button.click(fn=generate_chart, inputs=chart_dropdown, outputs=image_output)
|
| 113 |
-
|
| 114 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|