| import pandas as pd
|
| from sklearn.feature_extraction.text import TfidfVectorizer
|
| from sklearn.naive_bayes import MultinomialNB
|
| from sklearn.pipeline import make_pipeline
|
| import pickle
|
| import os
|
|
|
|
|
|
|
| data = [
|
|
|
| ("Milk", 7), ("Yogurt", 14), ("Cheese", 30), ("Butter", 60), ("Cream", 10),
|
|
|
| ("Spinach", 5), ("Lettuce", 5), ("Tomato", 7), ("Carrot", 21), ("Potato", 30), ("Onion", 30),
|
|
|
| ("Apple", 21), ("Banana", 5), ("Orange", 14), ("Grapes", 7), ("Strawberry", 4),
|
|
|
| ("Chicken", 2), ("Beef", 3), ("Pork", 3), ("Fish", 2), ("Ham", 5),
|
|
|
| ("Bread", 5), ("Rice", 365), ("Pasta", 365), ("Cereal", 180),
|
|
|
| ("Eggs", 21), ("Juice", 10), ("Sauce", 90), ("Canned Beans", 700)
|
| ]
|
|
|
| MODEL_PATH = "expiry_model.pkl"
|
|
|
| def train_model():
|
| print("Training Expiry Prediction Model...")
|
| df = pd.DataFrame(data, columns=["item", "days"])
|
|
|
|
|
| model = make_pipeline(TfidfVectorizer(), MultinomialNB())
|
| model.fit(df["item"], df["days"])
|
|
|
| with open(MODEL_PATH, "wb") as f:
|
| pickle.dump(model, f)
|
| print("Model saved to", MODEL_PATH)
|
| return model
|
|
|
| def load_model():
|
| if os.path.exists(MODEL_PATH):
|
| with open(MODEL_PATH, "rb") as f:
|
| return pickle.load(f)
|
| else:
|
| return train_model()
|
|
|
| def predict_days(item_name):
|
| model = load_model()
|
| try:
|
|
|
| predicted_days = model.predict([item_name])[0]
|
| return int(predicted_days)
|
| except Exception as e:
|
| print(f"Prediction Error: {e}")
|
| return 7
|
|
|