Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,70 +0,0 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import joblib
|
| 6 |
-
import os
|
| 7 |
-
from transformers import pipeline
|
| 8 |
-
from sklearn.linear_model import LinearRegression
|
| 9 |
-
|
| 10 |
-
# Helper function to train if model doesn't exist
|
| 11 |
-
def train_models():
|
| 12 |
-
print("🔵 Training models because models/inventory_forecaster.pkl not found...")
|
| 13 |
-
data = pd.read_csv('data/sales_data_large.csv')
|
| 14 |
-
product_models = {}
|
| 15 |
-
|
| 16 |
-
for product_id, group in data.groupby('Product_ID'):
|
| 17 |
-
group = group.sort_values('Date')
|
| 18 |
-
X = group[['Units_Sold']].shift(1).fillna(0)
|
| 19 |
-
y = group['Units_Sold']
|
| 20 |
-
|
| 21 |
-
model = LinearRegression()
|
| 22 |
-
model.fit(X, y)
|
| 23 |
-
|
| 24 |
-
product_models[product_id] = model
|
| 25 |
-
|
| 26 |
-
os.makedirs('models', exist_ok=True)
|
| 27 |
-
joblib.dump(product_models, 'models/inventory_forecaster.pkl')
|
| 28 |
-
print("✅ Models trained and saved.")
|
| 29 |
-
|
| 30 |
-
# Auto-train if model doesn't exist
|
| 31 |
-
if not os.path.exists('models/inventory_forecaster.pkl'):
|
| 32 |
-
train_models()
|
| 33 |
-
|
| 34 |
-
# Load all ML models
|
| 35 |
-
product_models = joblib.load('models/inventory_forecaster.pkl')
|
| 36 |
-
|
| 37 |
-
# Load LLM pipeline
|
| 38 |
-
llm = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 39 |
-
|
| 40 |
-
# Inventory advisor function
|
| 41 |
-
def inventory_advisor(product_id, current_inventory, last_day_sales):
|
| 42 |
-
if product_id not in product_models:
|
| 43 |
-
return f"❌ Error: Product ID {product_id} not found."
|
| 44 |
-
|
| 45 |
-
forecast_model = product_models[product_id]
|
| 46 |
-
future_sales = forecast_model.predict([[last_day_sales]])[0]
|
| 47 |
-
|
| 48 |
-
prompt = (f"Current inventory is {current_inventory} units. "
|
| 49 |
-
f"Predicted sales for next week is {int(future_sales)} units. "
|
| 50 |
-
f"Should restocking be done? Suggest a human-readable restocking advice.")
|
| 51 |
-
|
| 52 |
-
response = llm(prompt, max_length=100)[0]['generated_text']
|
| 53 |
-
|
| 54 |
-
return f"🔮 Predicted Sales Next Week: {int(future_sales)} units\n\n🛒 Advice:\n{response}"
|
| 55 |
-
|
| 56 |
-
# Gradio UI
|
| 57 |
-
iface = gr.Interface(
|
| 58 |
-
fn=inventory_advisor,
|
| 59 |
-
inputs=[
|
| 60 |
-
gr.Number(label="Product ID"),
|
| 61 |
-
gr.Number(label="Current Inventory"),
|
| 62 |
-
gr.Number(label="Units Sold Yesterday")
|
| 63 |
-
],
|
| 64 |
-
outputs="text",
|
| 65 |
-
title="📦 Real-Time Inventory Management (Auto-Train)",
|
| 66 |
-
description="Enter product ID, current stock, and yesterday's sales. Get AI-based restocking advice!"
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|