Upload 5 files
Browse files- .gitattributes +1 -0
- app.py +156 -0
- best_model.keras +3 -0
- requirement.txt +6 -0
- scaler.pkl +3 -0
- tokenizer.pkl +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
best_model.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import re
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 8 |
+
from nltk.corpus import stopwords
|
| 9 |
+
from sklearn.preprocessing import StandardScaler
|
| 10 |
+
import nltk
|
| 11 |
+
|
| 12 |
+
# Download required NLTK data (for stopwords only)
|
| 13 |
+
nltk.download('stopwords')
|
| 14 |
+
|
| 15 |
+
# Load the trained model
|
| 16 |
+
model = load_model("/home/petpooja-504/Desktop/lstm/best_model.keras")
|
| 17 |
+
|
| 18 |
+
# Load the tokenizer
|
| 19 |
+
with open("/home/petpooja-504/Desktop/lstm/tokenizer.pkl", "rb") as f:
|
| 20 |
+
tokenizer = pickle.load(f)
|
| 21 |
+
|
| 22 |
+
# Ensure the tokenizer is valid
|
| 23 |
+
if not hasattr(tokenizer, 'texts_to_sequences'):
|
| 24 |
+
raise ValueError("Loaded tokenizer is not a valid Keras Tokenizer object.")
|
| 25 |
+
|
| 26 |
+
# Define constants
|
| 27 |
+
MAX_SEQUENCE_LENGTH = 100
|
| 28 |
+
|
| 29 |
+
# Simple tokenizer function
|
| 30 |
+
def simple_tokenizer(text):
|
| 31 |
+
text = text.lower()
|
| 32 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text)
|
| 33 |
+
tokens = text.split() # Split by whitespace
|
| 34 |
+
return tokens
|
| 35 |
+
|
| 36 |
+
# Preprocessing function for input review
|
| 37 |
+
def preprocess_input_review(review_text):
|
| 38 |
+
tokens = simple_tokenizer(review_text)
|
| 39 |
+
stop_words = set(stopwords.words('english'))
|
| 40 |
+
tokens = [word for word in tokens if word not in stop_words]
|
| 41 |
+
processed_review = ' '.join(tokens)
|
| 42 |
+
return processed_review
|
| 43 |
+
|
| 44 |
+
# Convert simplified time strings (e.g., 2H30M or 15M) to minutes
|
| 45 |
+
def time_to_minutes(time_str):
|
| 46 |
+
if not time_str or pd.isna(time_str):
|
| 47 |
+
return 0
|
| 48 |
+
hours, minutes = 0, 0
|
| 49 |
+
if 'H' in time_str:
|
| 50 |
+
parts = time_str.split('H')
|
| 51 |
+
hours = int(parts[0]) if parts[0].isdigit() else 0
|
| 52 |
+
time_str = parts[1] if len(parts) > 1 else ''
|
| 53 |
+
if 'M' in time_str and time_str.split('M')[0].isdigit():
|
| 54 |
+
minutes = int(time_str.split('M')[0])
|
| 55 |
+
return hours * 60 + minutes
|
| 56 |
+
|
| 57 |
+
# Prediction function
|
| 58 |
+
def predict_menu_inclusion(food_name, review_text, cook_time, prep_time, total_time, protein, carbs, fat):
|
| 59 |
+
# Preprocess the review
|
| 60 |
+
processed_review = preprocess_input_review(review_text)
|
| 61 |
+
|
| 62 |
+
# Tokenize and pad the review
|
| 63 |
+
review_sequence = tokenizer.texts_to_sequences([processed_review])
|
| 64 |
+
padded_review = pad_sequences(review_sequence, maxlen=MAX_SEQUENCE_LENGTH)
|
| 65 |
+
|
| 66 |
+
# Prepare numerical features
|
| 67 |
+
numerical_features = np.array([[time_to_minutes(cook_time),
|
| 68 |
+
time_to_minutes(prep_time),
|
| 69 |
+
time_to_minutes(total_time),
|
| 70 |
+
protein, carbs, fat]])
|
| 71 |
+
|
| 72 |
+
# Normalize numerical features
|
| 73 |
+
scaler = StandardScaler()
|
| 74 |
+
numerical_features = scaler.fit_transform(numerical_features)
|
| 75 |
+
|
| 76 |
+
# Predict the rating
|
| 77 |
+
predicted_rating = model.predict([padded_review, numerical_features]).flatten()[0]
|
| 78 |
+
|
| 79 |
+
# Decide whether to include the item on the menu
|
| 80 |
+
include_on_menu = predicted_rating > 4 # Threshold for inclusion
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
"FoodName": food_name,
|
| 84 |
+
"PredictedRating": round(predicted_rating, 2),
|
| 85 |
+
"IncludeOnMenu": include_on_menu
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
# Streamlit app with enhanced UI
|
| 89 |
+
st.set_page_config(page_title="Recipe Rating & Menu Optimization", layout="wide")
|
| 90 |
+
|
| 91 |
+
st.markdown(
|
| 92 |
+
"""
|
| 93 |
+
<style>
|
| 94 |
+
.main {
|
| 95 |
+
background-color: #f5f5f5;
|
| 96 |
+
font-family: Arial, sans-serif;
|
| 97 |
+
}
|
| 98 |
+
h1 {
|
| 99 |
+
color: #3c3c7b;
|
| 100 |
+
}
|
| 101 |
+
.sidebar .sidebar-content {
|
| 102 |
+
background-color: #eef2f3;
|
| 103 |
+
}
|
| 104 |
+
.stButton>button {
|
| 105 |
+
background-color: #3c3c7b;
|
| 106 |
+
color: white;
|
| 107 |
+
border-radius: 5px;
|
| 108 |
+
font-size: 16px;
|
| 109 |
+
}
|
| 110 |
+
.stButton>button:hover {
|
| 111 |
+
background-color: #2c2c5a;
|
| 112 |
+
}
|
| 113 |
+
.prediction-box {
|
| 114 |
+
background-color: #eaf6f6;
|
| 115 |
+
padding: 20px;
|
| 116 |
+
border-radius: 10px;
|
| 117 |
+
box-shadow: 2px 2px 10px #ccc;
|
| 118 |
+
}
|
| 119 |
+
.success {
|
| 120 |
+
color: green;
|
| 121 |
+
font-weight: bold;
|
| 122 |
+
}
|
| 123 |
+
.error {
|
| 124 |
+
color: red;
|
| 125 |
+
font-weight: bold;
|
| 126 |
+
}
|
| 127 |
+
</style>
|
| 128 |
+
""",
|
| 129 |
+
unsafe_allow_html=True,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
st.title("๐ Recipe Rating & Menu Optimization")
|
| 133 |
+
|
| 134 |
+
st.sidebar.header("๐ Enter Recipe Details")
|
| 135 |
+
food_name = st.sidebar.text_input("๐ด Food Name", value="Paneer Butter Masala")
|
| 136 |
+
review_text = st.sidebar.text_area("๐ Review Text", value="This dish is delicious, rich in flavor, and a favorite among customers!")
|
| 137 |
+
cook_time = st.sidebar.text_input("โณ Cooking Time (e.g., 15M or 2H30M)", value="30M")
|
| 138 |
+
prep_time = st.sidebar.text_input("๐ Preparation Time (e.g., 15M or 1H15M)", value="15M")
|
| 139 |
+
total_time = st.sidebar.text_input("๐
Total Time (e.g., 45M or 1H45M)", value="45M")
|
| 140 |
+
protein = st.sidebar.number_input("๐ช Protein (grams)", min_value=0.0, step=1.0, value=20.0)
|
| 141 |
+
carbs = st.sidebar.number_input("๐ Carbohydrates (grams)", min_value=0.0, step=1.0, value=10.0)
|
| 142 |
+
fat = st.sidebar.number_input("๐ Fat (grams)", min_value=0.0, step=1.0, value=15.0)
|
| 143 |
+
|
| 144 |
+
if st.sidebar.button("๐ฎ Predict"):
|
| 145 |
+
result = predict_menu_inclusion(food_name, review_text, cook_time, prep_time, total_time, protein, carbs, fat)
|
| 146 |
+
st.markdown("<div class='prediction-box'>", unsafe_allow_html=True)
|
| 147 |
+
st.subheader("Prediction Results")
|
| 148 |
+
st.write(f"**๐ด Food Name:** {result['FoodName']}")
|
| 149 |
+
st.write(f"**โญ Predicted Rating:** {result['PredictedRating']}")
|
| 150 |
+
st.write(
|
| 151 |
+
f"**๐ฝ๏ธ Include on Menu:** "
|
| 152 |
+
f"<span class='{'success' if result['IncludeOnMenu'] else 'error'}'>"
|
| 153 |
+
f"{'Yes' if result['IncludeOnMenu'] else 'No'}</span>",
|
| 154 |
+
unsafe_allow_html=True,
|
| 155 |
+
)
|
| 156 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
best_model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:df82bb85028058a4aad29289fc19831402212df55c49a49ed16d54ad5ea6b9f0
|
| 3 |
+
size 396137083
|
requirement.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.19.0
|
| 2 |
+
tensorflow==2.13.0
|
| 3 |
+
nltk==3.8.1
|
| 4 |
+
scikit-learn==1.2.0
|
| 5 |
+
numpy==1.24.1
|
| 6 |
+
pandas==1.5.3
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1d59dc1db30af00e1a5c0a415eeb31b00872f32497b28a57404948c5576fab22
|
| 3 |
+
size 46
|
tokenizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5173d8221d42a1c0de8de69ffda2b8c5573554093225a621dd22991a888d21d8
|
| 3 |
+
size 15690211
|