Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,61 @@
|
|
| 1 |
from __future__ import annotations
|
|
|
|
| 2 |
from typing import Iterable
|
| 3 |
import gradio as gr
|
| 4 |
-
from gradio.themes.base import Base
|
| 5 |
-
from gradio.themes.utils import colors, fonts, sizes
|
| 6 |
-
import time
|
| 7 |
-
|
| 8 |
import pandas as pd
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import numpy as np
|
|
|
|
| 11 |
import math
|
| 12 |
-
|
| 13 |
-
import matplotlib.ticker as ticker
|
| 14 |
-
|
| 15 |
import torch
|
| 16 |
from chronos import ChronosPipeline
|
| 17 |
import warnings
|
|
|
|
|
|
|
|
|
|
| 18 |
warnings.filterwarnings("ignore")
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
radius_size: sizes.Size | str = sizes.radius_md,
|
| 29 |
-
text_size: sizes.Size | str = sizes.text_lg,
|
| 30 |
-
font: fonts.Font
|
| 31 |
-
| str
|
| 32 |
-
| Iterable[fonts.Font | str] = (
|
| 33 |
-
fonts.GoogleFont("Quicksand"),
|
| 34 |
-
"ui-sans-serif",
|
| 35 |
-
"sans-serif",
|
| 36 |
-
),
|
| 37 |
-
font_mono: fonts.Font
|
| 38 |
-
| str
|
| 39 |
-
| Iterable[fonts.Font | str] = (
|
| 40 |
-
fonts.GoogleFont("IBM Plex Mono"),
|
| 41 |
-
"ui-monospace",
|
| 42 |
-
"monospace",
|
| 43 |
-
),
|
| 44 |
-
):
|
| 45 |
-
super().__init__(
|
| 46 |
-
primary_hue=primary_hue,
|
| 47 |
-
secondary_hue=secondary_hue,
|
| 48 |
-
neutral_hue=neutral_hue,
|
| 49 |
-
spacing_size=spacing_size,
|
| 50 |
-
radius_size=radius_size,
|
| 51 |
-
text_size=text_size,
|
| 52 |
-
font=font,
|
| 53 |
-
font_mono=font_mono,
|
| 54 |
-
)
|
| 55 |
-
super().set(
|
| 56 |
-
body_background_fill="repeating-linear-gradient(45deg, *primary_200, *primary_200 10px, *primary_50 10px, *primary_50 20px)",
|
| 57 |
-
body_background_fill_dark="repeating-linear-gradient(45deg, *primary_800, *primary_800 10px, *primary_900 10px, *primary_900 20px)",
|
| 58 |
-
button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
|
| 59 |
-
button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
|
| 60 |
-
button_primary_text_color="white",
|
| 61 |
-
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
|
| 62 |
-
slider_color="*secondary_300",
|
| 63 |
-
slider_color_dark="*secondary_600",
|
| 64 |
-
block_title_text_weight="600",
|
| 65 |
-
block_border_width="3px",
|
| 66 |
-
block_shadow="*shadow_drop_lg",
|
| 67 |
-
button_primary_shadow="*shadow_drop_lg",
|
| 68 |
-
button_large_padding="32px",
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
-
def process_data(csv_file):
|
| 75 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Read the CSV file
|
| 77 |
df = pd.read_csv(csv_file.name)
|
| 78 |
|
| 79 |
-
df[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
df['month'] = df['date'].dt.month
|
| 81 |
df['year'] = df['date'].dt.year
|
|
|
|
| 82 |
|
| 83 |
monthly_sales = df.groupby(['year', 'month'])['sold_qty'].sum().reset_index()
|
| 84 |
monthly_sales = monthly_sales.rename(columns={'year': 'year', 'month': 'month', 'sold_qty': 'y'})
|
|
@@ -166,13 +143,45 @@ def process_data(csv_file):
|
|
| 166 |
return None
|
| 167 |
|
| 168 |
# Create Gradio interface
|
| 169 |
-
with gr.Blocks(theme=
|
| 170 |
gr.Markdown("# Chronos Forecasting - Tops infosolutions Pvt Ltd")
|
| 171 |
gr.Markdown("Upload a CSV file and click 'Forecast' to generate sales forecast for next 12 months .")
|
| 172 |
|
|
|
|
|
|
|
| 173 |
with gr.Row():
|
| 174 |
file_input = gr.File(label="Upload CSV File", file_types=[".csv"])
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
with gr.Row():
|
| 177 |
visualize_btn = gr.Button("Forecast", variant="primary")
|
| 178 |
|
|
@@ -182,12 +191,35 @@ with gr.Blocks(theme=seafoam) as demo:
|
|
| 182 |
with gr.Row():
|
| 183 |
pivot_plot_output = gr.Plot(label="Monthly Sales Pivot Table")
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
visualize_btn.click(
|
| 186 |
fn=process_data,
|
| 187 |
-
inputs=[file_input],
|
| 188 |
outputs=[plot_output, pivot_plot_output]
|
| 189 |
)
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
# Launch the app
|
| 192 |
if __name__ == "__main__":
|
| 193 |
demo.launch()
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
+
|
| 3 |
from typing import Iterable
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import numpy as np
|
| 8 |
+
import os
|
| 9 |
import math
|
|
|
|
|
|
|
|
|
|
| 10 |
import torch
|
| 11 |
from chronos import ChronosPipeline
|
| 12 |
import warnings
|
| 13 |
+
|
| 14 |
+
from seafoam import Seafoam
|
| 15 |
+
|
| 16 |
warnings.filterwarnings("ignore")
|
| 17 |
|
| 18 |
+
import numpy as np
|
| 19 |
+
import matplotlib.ticker as ticker
|
| 20 |
+
|
| 21 |
+
os.makedirs("example_files", exist_ok=True)
|
| 22 |
+
|
| 23 |
+
def process_csv(file):
|
| 24 |
+
if file is None:
|
| 25 |
+
return None, gr.Dropdown(choices=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
if not file.name.endswith('.csv'):
|
| 28 |
+
raise gr.Error("Please upload a CSV file only")
|
| 29 |
+
df = pd.read_csv(file.name)
|
| 30 |
+
columns = df.columns.tolist()
|
| 31 |
+
transformed_columns = list(map(lambda x: ' '.join([word.capitalize() for word in x.split('_')]), columns))
|
| 32 |
+
data_columns = gr.Dropdown(choices=transformed_columns, value=None)
|
| 33 |
+
return df, data_columns, data_columns
|
| 34 |
|
| 35 |
|
| 36 |
+
def process_data(csv_file, date_column_value, target_column_value):
|
| 37 |
try:
|
| 38 |
+
if not csv_file:
|
| 39 |
+
return "Error: Upload Csv File"
|
| 40 |
+
|
| 41 |
+
if not date_column_value or not target_column_value:
|
| 42 |
+
return "Error: Both date and target columns must be selected"
|
| 43 |
+
|
| 44 |
+
date_column = date_column_value.lower().replace(" ", "_")
|
| 45 |
+
target_column = target_column_value.lower().replace(" ", "_")
|
| 46 |
+
|
| 47 |
# Read the CSV file
|
| 48 |
df = pd.read_csv(csv_file.name)
|
| 49 |
|
| 50 |
+
numeric_mask = df[date_column].apply(lambda x: isinstance(x, (int, float)))
|
| 51 |
+
if numeric_mask.any():
|
| 52 |
+
return "Error: Found numeric values in column '{date_column}'. Please provide dates in string format like 'YYYY-MM-DD'."
|
| 53 |
+
|
| 54 |
+
df['date'] = pd.to_datetime(df[date_column])
|
| 55 |
+
|
| 56 |
df['month'] = df['date'].dt.month
|
| 57 |
df['year'] = df['date'].dt.year
|
| 58 |
+
df['sold_qty'] = df[target_column]
|
| 59 |
|
| 60 |
monthly_sales = df.groupby(['year', 'month'])['sold_qty'].sum().reset_index()
|
| 61 |
monthly_sales = monthly_sales.rename(columns={'year': 'year', 'month': 'month', 'sold_qty': 'y'})
|
|
|
|
| 143 |
return None
|
| 144 |
|
| 145 |
# Create Gradio interface
|
| 146 |
+
with gr.Blocks(theme=Seafoam()) as demo:
|
| 147 |
gr.Markdown("# Chronos Forecasting - Tops infosolutions Pvt Ltd")
|
| 148 |
gr.Markdown("Upload a CSV file and click 'Forecast' to generate sales forecast for next 12 months .")
|
| 149 |
|
| 150 |
+
df_state = gr.State()
|
| 151 |
+
|
| 152 |
with gr.Row():
|
| 153 |
file_input = gr.File(label="Upload CSV File", file_types=[".csv"])
|
| 154 |
|
| 155 |
+
|
| 156 |
+
with gr.Row():
|
| 157 |
+
date_column = gr.Dropdown(
|
| 158 |
+
choices=[],
|
| 159 |
+
label="Select Date column",
|
| 160 |
+
multiselect=False,
|
| 161 |
+
value=None
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
target_column = gr.Dropdown(
|
| 165 |
+
choices=[],
|
| 166 |
+
label="Select Target column",
|
| 167 |
+
multiselect=False,
|
| 168 |
+
value=None
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
gr.Examples(
|
| 172 |
+
examples=[
|
| 173 |
+
["example_files/13dec_product_id96airaco.csv"],
|
| 174 |
+
["example_files/13dec_product_id346airaco.csv"],
|
| 175 |
+
["example_files/13dec_product_id567airaco.csv"],
|
| 176 |
+
["example_files/13dec_product_id856airaco.csv"],
|
| 177 |
+
["example_files/airaco_product_id215.csv"]
|
| 178 |
+
],
|
| 179 |
+
inputs=file_input,
|
| 180 |
+
outputs=[df_state, date_column, target_column],
|
| 181 |
+
fn=process_csv,
|
| 182 |
+
cache_examples=True
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
with gr.Row():
|
| 186 |
visualize_btn = gr.Button("Forecast", variant="primary")
|
| 187 |
|
|
|
|
| 191 |
with gr.Row():
|
| 192 |
pivot_plot_output = gr.Plot(label="Monthly Sales Pivot Table")
|
| 193 |
|
| 194 |
+
file_input.upload(
|
| 195 |
+
process_csv,
|
| 196 |
+
inputs=[file_input],
|
| 197 |
+
outputs=[df_state, date_column, target_column]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Column selection handler
|
| 201 |
+
date_column.change(
|
| 202 |
+
lambda x: x if x else "",
|
| 203 |
+
inputs=[date_column],
|
| 204 |
+
outputs=[]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
target_column.change(
|
| 208 |
+
lambda x: x if x else "",
|
| 209 |
+
inputs=[target_column],
|
| 210 |
+
outputs=[]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
visualize_btn.click(
|
| 214 |
fn=process_data,
|
| 215 |
+
inputs=[file_input, date_column, target_column],
|
| 216 |
outputs=[plot_output, pivot_plot_output]
|
| 217 |
)
|
| 218 |
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
|
| 223 |
# Launch the app
|
| 224 |
if __name__ == "__main__":
|
| 225 |
demo.launch()
|