FlavourGraph / app.py
vishal1232006's picture
Update app.py
13ee2dc verified
Raw History Blame Contribute Delete
4.02 kB
import gradio as gr
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# -------------------------
# Sample Data Preparation
# -------------------------
parts = ["East", "West", "North", "South"]
companies = ["Homemade", "Local", "International"]
flavors = ["Vanilla", "Chocolate", "Strawberry", "Mango", "Butterscotch"]
categories = ["Kids", "Teens", "Adults", "Seniors"]
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
# Generate Random Sales Data
np.random.seed(42)
data = []
for part in parts:
for company in companies:
for flavor in flavors:
for cat in categories:
for month in months:
data.append([part, company, flavor, cat, month, np.random.randint(50, 300)])
df = pd.DataFrame(data, columns=["Part", "Company", "Flavor", "Category", "Month", "Sales"])
# -------------------------
# Function to Plot Sales
# -------------------------
def plot_sales(part, company, flavor, category, chart_type):
filtered = df[
(df["Part"] == part) &
(df["Company"] == company) &
(df["Flavor"] == flavor) &
(df["Category"] == category)
]
if filtered.empty:
return "⚠ No sales data for this selection."
# Neon running colors
neon_colors = "Turbo"
# Optional tri-color scheme: ["black", "red", "yellow"]
if chart_type == "3D Scatter":
fig = px.scatter_3d(
filtered,
x="Month", y="Company", z="Sales",
color="Sales",
size="Sales",
symbol="Category",
title="🍦 3D Scatter Sales Graph",
height=650,
template="plotly_dark",
color_continuous_scale=neon_colors
)
fig.update_layout(font=dict(size=14, color="white"))
else: # 3D Line
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=filtered["Month"],
y=filtered["Company"],
z=filtered["Sales"],
mode="lines+markers",
line=dict(
width=8,
color=filtered["Sales"],
colorscale=neon_colors
),
marker=dict(
size=6,
color=filtered["Sales"],
colorscale=neon_colors
),
name=f"{flavor} - {category}"
))
fig.update_layout(
title="🍦 3D Line Sales Graph (Running Colors)",
scene=dict(
xaxis_title="Month",
yaxis_title="Company",
zaxis_title="Sales",
),
template="plotly_dark",
height=650,
font=dict(size=14, color="white"),
margin=dict(l=20, r=20, t=50, b=20)
)
return fig
# -------------------------
# Gradio UI Design
# -------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
<h1 style='text-align:center; color:#FFD700;'>✨ Scooplytics – Ice Cream Sales Analyzer ✨</h1>
<p style='text-align:center; font-size:18px; color:#BBBBBB;'>Discover which flavors, companies, and regions dominate ice cream sales in Coimbatore.</p>
"""
)
with gr.Row():
part = gr.Dropdown(parts, label="Select Part of Coimbatore")
company = gr.Dropdown(companies, label="Select Ice Cream Company")
with gr.Row():
flavor = gr.Dropdown(flavors, label="Select Flavor")
category = gr.Dropdown(categories, label="Select Customer Category")
chart_type = gr.Radio(["3D Line", "3D Scatter"], label="Graph Type", value="3D Line")
output = gr.Plot()
generate = gr.Button("🎨 Generate Graph")
generate.click(plot_sales, inputs=[part, company, flavor, category, chart_type], outputs=output)
# -------------------------
# Run App
# -------------------------
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)