Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,9 @@ import time
|
|
| 8 |
import pandas as pd
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
import torch
|
| 13 |
from chronos import ChronosPipeline
|
|
@@ -68,9 +71,6 @@ class Seafoam(Base):
|
|
| 68 |
seafoam = Seafoam()
|
| 69 |
|
| 70 |
|
| 71 |
-
import numpy as np
|
| 72 |
-
import matplotlib.ticker as ticker
|
| 73 |
-
|
| 74 |
def process_data(csv_file):
|
| 75 |
try:
|
| 76 |
# Read the CSV file
|
|
@@ -96,6 +96,48 @@ def process_data(csv_file):
|
|
| 96 |
forecast_index = range(len(monthly_sales), len(monthly_sales) + prediction_length)
|
| 97 |
low, median, high = np.quantile(forecast[0].numpy(), [0.1, 0.5, 0.9], axis=0)
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
# Visualization
|
| 100 |
plt.figure(figsize=(30, 10))
|
| 101 |
plt.plot(monthly_sales["y"], color="royalblue", label="Historical Data", linewidth=2)
|
|
@@ -117,7 +159,7 @@ def process_data(csv_file):
|
|
| 117 |
plt.grid(linestyle='--', linewidth=1.2, color='gray', alpha=0.7)
|
| 118 |
plt.tight_layout()
|
| 119 |
|
| 120 |
-
return plt.gcf()
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
print(f"Error: {str(e)}")
|
|
@@ -125,7 +167,7 @@ def process_data(csv_file):
|
|
| 125 |
|
| 126 |
# Create Gradio interface
|
| 127 |
with gr.Blocks(theme=seafoam) as demo:
|
| 128 |
-
gr.Markdown("# Chronos Forecasting - Tops
|
| 129 |
gr.Markdown("Upload a CSV file and click 'Forecast' to generate sales forecast for next 12 months .")
|
| 130 |
|
| 131 |
with gr.Row():
|
|
@@ -138,12 +180,12 @@ with gr.Blocks(theme=seafoam) as demo:
|
|
| 138 |
plot_output = gr.Plot(label="Chronos Forecasting Visualization")
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
-
|
| 142 |
|
| 143 |
visualize_btn.click(
|
| 144 |
fn=process_data,
|
| 145 |
inputs=[file_input],
|
| 146 |
-
outputs=[plot_output]
|
| 147 |
)
|
| 148 |
|
| 149 |
# Launch the app
|
|
|
|
| 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
|
|
|
|
| 71 |
seafoam = Seafoam()
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
def process_data(csv_file):
|
| 75 |
try:
|
| 76 |
# Read the CSV file
|
|
|
|
| 96 |
forecast_index = range(len(monthly_sales), len(monthly_sales) + prediction_length)
|
| 97 |
low, median, high = np.quantile(forecast[0].numpy(), [0.1, 0.5, 0.9], axis=0)
|
| 98 |
|
| 99 |
+
df['month_name'] = df['date'].dt.month_name()
|
| 100 |
+
month_order = [
|
| 101 |
+
'January', 'February', 'March', 'April', 'May', 'June',
|
| 102 |
+
'July', 'August', 'September', 'October', 'November', 'December'
|
| 103 |
+
]
|
| 104 |
+
df['month_name'] = pd.Categorical(df['month_name'], categories=month_order, ordered=True)
|
| 105 |
+
|
| 106 |
+
expanded_df = df.copy()
|
| 107 |
+
year_month_sum = expanded_df.groupby(['year', 'month_name'])['sold_qty'].sum().reset_index()
|
| 108 |
+
|
| 109 |
+
# Create a pivot table: sum of units sold per year and month
|
| 110 |
+
pivot_table = year_month_sum.pivot(index='year', columns='month_name', values='sold_qty')
|
| 111 |
+
|
| 112 |
+
new_data_list = [math.ceil(x) for x in median]
|
| 113 |
+
|
| 114 |
+
# Add the new data list for the next year (incrementing the year by 1)
|
| 115 |
+
next_year = pivot_table.index[-1] + 1 # Increment the year by 1
|
| 116 |
+
pivot_table.loc[next_year] = new_data_list # Add the new row for the next year
|
| 117 |
+
|
| 118 |
+
# Visualization: Pivot Table Data (Second Plot)
|
| 119 |
+
fig3, ax3 = plt.subplots(figsize=(18, 6))
|
| 120 |
+
|
| 121 |
+
# Create a table inside the plot
|
| 122 |
+
ax3.axis('off') # Turn off the axis
|
| 123 |
+
table = ax3.table(cellText=pivot_table.values, colLabels=pivot_table.columns, rowLabels=pivot_table.index, loc='center', cellLoc='center')
|
| 124 |
+
|
| 125 |
+
# Style the table
|
| 126 |
+
table.auto_set_font_size(False)
|
| 127 |
+
table.set_fontsize(12)
|
| 128 |
+
table.scale(1.2, 1.2) # Scale the table for better visibility
|
| 129 |
+
|
| 130 |
+
# Adjust table colors (optional)
|
| 131 |
+
for (i, j), cell in table.get_celld().items():
|
| 132 |
+
if i == 0:
|
| 133 |
+
cell.set_text_props(weight='bold')
|
| 134 |
+
cell.set_facecolor('#f2f2f2')
|
| 135 |
+
elif j == 0:
|
| 136 |
+
cell.set_text_props(weight='bold')
|
| 137 |
+
cell.set_facecolor('#f2f2f2')
|
| 138 |
+
else:
|
| 139 |
+
cell.set_facecolor('white')
|
| 140 |
+
|
| 141 |
# Visualization
|
| 142 |
plt.figure(figsize=(30, 10))
|
| 143 |
plt.plot(monthly_sales["y"], color="royalblue", label="Historical Data", linewidth=2)
|
|
|
|
| 159 |
plt.grid(linestyle='--', linewidth=1.2, color='gray', alpha=0.7)
|
| 160 |
plt.tight_layout()
|
| 161 |
|
| 162 |
+
return plt.gcf(), fig3
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
print(f"Error: {str(e)}")
|
|
|
|
| 167 |
|
| 168 |
# Create Gradio interface
|
| 169 |
with gr.Blocks(theme=seafoam) as demo:
|
| 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():
|
|
|
|
| 180 |
plot_output = gr.Plot(label="Chronos Forecasting Visualization")
|
| 181 |
|
| 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
|