Spaces:
Build error
Build error
update app.py
Browse files
app.py
CHANGED
|
@@ -21,23 +21,23 @@ import utils
|
|
| 21 |
# Initialize configuration
|
| 22 |
config = get_config()
|
| 23 |
|
| 24 |
-
def initialize_wb_client():
|
| 25 |
"""Initialize Wildberries API client with error handling"""
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
return None
|
| 31 |
|
| 32 |
-
client = WildberriesAPI(
|
| 33 |
return client
|
| 34 |
except Exception as e:
|
| 35 |
gr.Error(f"Failed to initialize Wildberries client: {str(e)}")
|
| 36 |
return None
|
| 37 |
|
| 38 |
-
def get_sales_data(period, start_date=None, end_date=None):
|
| 39 |
"""Fetch sales data with fallback to demo data"""
|
| 40 |
-
wb_client = initialize_wb_client()
|
| 41 |
|
| 42 |
if wb_client is None:
|
| 43 |
# Use demo data when API is not available
|
|
@@ -58,10 +58,10 @@ def get_sales_data(period, start_date=None, end_date=None):
|
|
| 58 |
gr.Warning(f"API error: {str(e)}. Using demo data.")
|
| 59 |
return utils.load_demo_sales_data(period)
|
| 60 |
|
| 61 |
-
def analyze_sales_performance(period):
|
| 62 |
"""Analyze sales performance for the specified period"""
|
| 63 |
try:
|
| 64 |
-
data = get_sales_data(period)
|
| 65 |
|
| 66 |
if data.empty:
|
| 67 |
return "No sales data available for the selected period.", None
|
|
@@ -91,11 +91,11 @@ def analyze_sales_performance(period):
|
|
| 91 |
gr.Error(error_msg)
|
| 92 |
return error_msg, None
|
| 93 |
|
| 94 |
-
def calculate_stockout_forecast(method
|
| 95 |
"""Calculate days until stockout for products"""
|
| 96 |
try:
|
| 97 |
# Get current inventory (demo data if API unavailable)
|
| 98 |
-
wb_client = initialize_wb_client()
|
| 99 |
|
| 100 |
if wb_client:
|
| 101 |
inventory_data = wb_client.get_stocks()
|
|
@@ -103,7 +103,7 @@ def calculate_stockout_forecast(method="simple"):
|
|
| 103 |
inventory_data = utils.load_demo_inventory_data()
|
| 104 |
|
| 105 |
# Get sales data for forecasting
|
| 106 |
-
sales_data = get_sales_data("month")
|
| 107 |
|
| 108 |
# Initialize forecaster
|
| 109 |
forecaster = InventoryForecaster()
|
|
@@ -185,6 +185,13 @@ def calculate_stockout_forecast(method="simple"):
|
|
| 185 |
gr.Error(error_msg)
|
| 186 |
return error_msg, None, None
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
# Create Gradio interface
|
| 189 |
def create_interface():
|
| 190 |
"""Create the main Gradio interface"""
|
|
@@ -207,15 +214,29 @@ def create_interface():
|
|
| 207 |
- ๐ Interactive dashboards
|
| 208 |
""")
|
| 209 |
|
| 210 |
-
# API
|
| 211 |
with gr.Row():
|
| 212 |
-
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
api_status = gr.Textbox(
|
| 214 |
-
value="
|
| 215 |
label="Status",
|
| 216 |
interactive=False
|
| 217 |
)
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
with gr.Tabs():
|
| 220 |
# Sales Analytics Tab
|
| 221 |
with gr.TabItem("๐ Sales Analytics"):
|
|
@@ -229,13 +250,13 @@ def create_interface():
|
|
| 229 |
analyze_btn = gr.Button("๐ Analyze Sales", variant="primary")
|
| 230 |
|
| 231 |
with gr.Column(scale=3):
|
| 232 |
-
sales_summary = gr.Markdown("
|
| 233 |
sales_chart = gr.Plot(label="Sales Performance")
|
| 234 |
|
| 235 |
# Event handlers
|
| 236 |
analyze_btn.click(
|
| 237 |
fn=analyze_sales_performance,
|
| 238 |
-
inputs=[period_selector],
|
| 239 |
outputs=[sales_summary, sales_chart]
|
| 240 |
)
|
| 241 |
|
|
@@ -256,7 +277,7 @@ def create_interface():
|
|
| 256 |
forecast_btn = gr.Button("๐ฎ Calculate Forecast", variant="primary")
|
| 257 |
|
| 258 |
with gr.Column(scale=3):
|
| 259 |
-
forecast_summary = gr.Markdown("
|
| 260 |
forecast_table = gr.DataFrame(
|
| 261 |
headers=["Product", "Current Stock", "Days Until Stockout", "Risk Level"],
|
| 262 |
label="Inventory Forecast Results"
|
|
@@ -266,7 +287,7 @@ def create_interface():
|
|
| 266 |
# Event handlers
|
| 267 |
forecast_btn.click(
|
| 268 |
fn=calculate_stockout_forecast,
|
| 269 |
-
inputs=[forecast_method],
|
| 270 |
outputs=[forecast_summary, forecast_table, forecast_chart]
|
| 271 |
)
|
| 272 |
|
|
@@ -276,8 +297,9 @@ def create_interface():
|
|
| 276 |
## How to Use This Dashboard
|
| 277 |
|
| 278 |
### ๐ง Setup
|
| 279 |
-
1. **API Token**:
|
| 280 |
2. **Permissions**: Ensure your token has access to Analytics and Statistics APIs
|
|
|
|
| 281 |
|
| 282 |
### ๐ Sales Analytics
|
| 283 |
- **Week Analysis**: Shows sales data for the last 7 days
|
|
@@ -309,7 +331,7 @@ def create_interface():
|
|
| 309 |
|
| 310 |
gr.Markdown("""
|
| 311 |
---
|
| 312 |
-
๐ก **Tip**: This dashboard works in demo mode even without an API token.
|
| 313 |
""")
|
| 314 |
|
| 315 |
return demo
|
|
|
|
| 21 |
# Initialize configuration
|
| 22 |
config = get_config()
|
| 23 |
|
| 24 |
+
def initialize_wb_client(api_token=None):
|
| 25 |
"""Initialize Wildberries API client with error handling"""
|
| 26 |
try:
|
| 27 |
+
# Use provided token or fallback to environment variable
|
| 28 |
+
token = api_token or os.getenv("WILDBERRIES_API_TOKEN")
|
| 29 |
+
if not token:
|
| 30 |
return None
|
| 31 |
|
| 32 |
+
client = WildberriesAPI(token)
|
| 33 |
return client
|
| 34 |
except Exception as e:
|
| 35 |
gr.Error(f"Failed to initialize Wildberries client: {str(e)}")
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
def get_sales_data(period, api_token=None, start_date=None, end_date=None):
|
| 39 |
"""Fetch sales data with fallback to demo data"""
|
| 40 |
+
wb_client = initialize_wb_client(api_token)
|
| 41 |
|
| 42 |
if wb_client is None:
|
| 43 |
# Use demo data when API is not available
|
|
|
|
| 58 |
gr.Warning(f"API error: {str(e)}. Using demo data.")
|
| 59 |
return utils.load_demo_sales_data(period)
|
| 60 |
|
| 61 |
+
def analyze_sales_performance(period, api_token):
|
| 62 |
"""Analyze sales performance for the specified period"""
|
| 63 |
try:
|
| 64 |
+
data = get_sales_data(period, api_token)
|
| 65 |
|
| 66 |
if data.empty:
|
| 67 |
return "No sales data available for the selected period.", None
|
|
|
|
| 91 |
gr.Error(error_msg)
|
| 92 |
return error_msg, None
|
| 93 |
|
| 94 |
+
def calculate_stockout_forecast(method, api_token):
|
| 95 |
"""Calculate days until stockout for products"""
|
| 96 |
try:
|
| 97 |
# Get current inventory (demo data if API unavailable)
|
| 98 |
+
wb_client = initialize_wb_client(api_token)
|
| 99 |
|
| 100 |
if wb_client:
|
| 101 |
inventory_data = wb_client.get_stocks()
|
|
|
|
| 103 |
inventory_data = utils.load_demo_inventory_data()
|
| 104 |
|
| 105 |
# Get sales data for forecasting
|
| 106 |
+
sales_data = get_sales_data("month", api_token)
|
| 107 |
|
| 108 |
# Initialize forecaster
|
| 109 |
forecaster = InventoryForecaster()
|
|
|
|
| 185 |
gr.Error(error_msg)
|
| 186 |
return error_msg, None, None
|
| 187 |
|
| 188 |
+
def update_status(api_token):
|
| 189 |
+
"""Update API status based on token"""
|
| 190 |
+
if not api_token or api_token.strip() == "":
|
| 191 |
+
return "๐ด Demo Mode - No API token provided"
|
| 192 |
+
else:
|
| 193 |
+
return "๐ข API Token Configured - Ready for live data"
|
| 194 |
+
|
| 195 |
# Create Gradio interface
|
| 196 |
def create_interface():
|
| 197 |
"""Create the main Gradio interface"""
|
|
|
|
| 214 |
- ๐ Interactive dashboards
|
| 215 |
""")
|
| 216 |
|
| 217 |
+
# API Token Configuration
|
| 218 |
with gr.Row():
|
| 219 |
+
with gr.Column(scale=3):
|
| 220 |
+
api_token_input = gr.Textbox(
|
| 221 |
+
value="",
|
| 222 |
+
label="๐ Wildberries API Token",
|
| 223 |
+
placeholder="Enter your Wildberries API token here (optional - leave empty to use demo mode)",
|
| 224 |
+
type="password"
|
| 225 |
+
)
|
| 226 |
+
with gr.Column(scale=1):
|
| 227 |
api_status = gr.Textbox(
|
| 228 |
+
value="๐ด Demo Mode - No API token provided",
|
| 229 |
label="Status",
|
| 230 |
interactive=False
|
| 231 |
)
|
| 232 |
|
| 233 |
+
# Update status when token changes
|
| 234 |
+
api_token_input.change(
|
| 235 |
+
fn=update_status,
|
| 236 |
+
inputs=[api_token_input],
|
| 237 |
+
outputs=[api_status]
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
with gr.Tabs():
|
| 241 |
# Sales Analytics Tab
|
| 242 |
with gr.TabItem("๐ Sales Analytics"):
|
|
|
|
| 250 |
analyze_btn = gr.Button("๐ Analyze Sales", variant="primary")
|
| 251 |
|
| 252 |
with gr.Column(scale=3):
|
| 253 |
+
sales_summary = gr.Markdown("Enter your API token above and select a period, then click 'Analyze Sales' to get started.")
|
| 254 |
sales_chart = gr.Plot(label="Sales Performance")
|
| 255 |
|
| 256 |
# Event handlers
|
| 257 |
analyze_btn.click(
|
| 258 |
fn=analyze_sales_performance,
|
| 259 |
+
inputs=[period_selector, api_token_input],
|
| 260 |
outputs=[sales_summary, sales_chart]
|
| 261 |
)
|
| 262 |
|
|
|
|
| 277 |
forecast_btn = gr.Button("๐ฎ Calculate Forecast", variant="primary")
|
| 278 |
|
| 279 |
with gr.Column(scale=3):
|
| 280 |
+
forecast_summary = gr.Markdown("Enter your API token above and select a forecasting method, then click 'Calculate Forecast'.")
|
| 281 |
forecast_table = gr.DataFrame(
|
| 282 |
headers=["Product", "Current Stock", "Days Until Stockout", "Risk Level"],
|
| 283 |
label="Inventory Forecast Results"
|
|
|
|
| 287 |
# Event handlers
|
| 288 |
forecast_btn.click(
|
| 289 |
fn=calculate_stockout_forecast,
|
| 290 |
+
inputs=[forecast_method, api_token_input],
|
| 291 |
outputs=[forecast_summary, forecast_table, forecast_chart]
|
| 292 |
)
|
| 293 |
|
|
|
|
| 297 |
## How to Use This Dashboard
|
| 298 |
|
| 299 |
### ๐ง Setup
|
| 300 |
+
1. **API Token**: Enter your Wildberries API token in the field above
|
| 301 |
2. **Permissions**: Ensure your token has access to Analytics and Statistics APIs
|
| 302 |
+
3. **Demo Mode**: Leave token field empty to use demo data
|
| 303 |
|
| 304 |
### ๐ Sales Analytics
|
| 305 |
- **Week Analysis**: Shows sales data for the last 7 days
|
|
|
|
| 331 |
|
| 332 |
gr.Markdown("""
|
| 333 |
---
|
| 334 |
+
๐ก **Tip**: This dashboard works in demo mode even without an API token. Enter your Wildberries API token above for real-time data.
|
| 335 |
""")
|
| 336 |
|
| 337 |
return demo
|