Update backend/app.py
Browse files- backend/app.py +18 -47
backend/app.py
CHANGED
|
@@ -13,6 +13,7 @@ from backend.decay import (
|
|
| 13 |
ROOM_SHELF,
|
| 14 |
HIGH_HUMIDITY_SHELF
|
| 15 |
)
|
|
|
|
| 16 |
|
| 17 |
# --------------------------------
|
| 18 |
# Flask App Configuration
|
|
@@ -109,56 +110,20 @@ def predict():
|
|
| 109 |
status = "SPOILED"
|
| 110 |
color = "#ef4444"
|
| 111 |
|
| 112 |
-
# return jsonify({
|
| 113 |
-
# "success": True,
|
| 114 |
-
# "fruit": fruit.capitalize(),
|
| 115 |
-
# "initial_freshness": initial,
|
| 116 |
-
# "decay": decay,
|
| 117 |
-
# "status": status,
|
| 118 |
-
# "status_color": color
|
| 119 |
-
# })
|
| 120 |
return jsonify({
|
| 121 |
"success": True,
|
| 122 |
"fruit": fruit.capitalize(),
|
| 123 |
"initial_freshness": initial,
|
| 124 |
-
|
| 125 |
-
# -------------------------
|
| 126 |
-
# Decay (existing)
|
| 127 |
-
# -------------------------
|
| 128 |
"decay": decay,
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# Shelf life (frontend expects this)
|
| 132 |
-
# -------------------------
|
| 133 |
"shelf_life": {
|
| 134 |
"ideal": IDEAL_SHELF[fruit],
|
| 135 |
"room": ROOM_SHELF[fruit],
|
| 136 |
"humid": HIGH_HUMIDITY_SHELF[fruit]
|
| 137 |
},
|
| 138 |
-
|
| 139 |
-
# -------------------------
|
| 140 |
-
# Conditions (frontend expects this)
|
| 141 |
-
# -------------------------
|
| 142 |
-
"conditions": {
|
| 143 |
-
"ideal": {
|
| 144 |
-
"freshness": decay["ideal_final"],
|
| 145 |
-
"days_left": decay["ideal_days_left"]
|
| 146 |
-
},
|
| 147 |
-
"room": {
|
| 148 |
-
"freshness": decay["room_final"],
|
| 149 |
-
"days_left": decay["room_days_left"]
|
| 150 |
-
},
|
| 151 |
-
"humid": {
|
| 152 |
-
"freshness": decay["humid_final"],
|
| 153 |
-
"days_left": decay["humid_days_left"]
|
| 154 |
-
}
|
| 155 |
-
},
|
| 156 |
-
|
| 157 |
-
# -------------------------
|
| 158 |
-
# 🔥 ADD THIS: chart_data (FIXES CURRENT ERROR)
|
| 159 |
-
# -------------------------
|
| 160 |
"chart_data": {
|
| 161 |
-
"labels": ["Ideal Storage", "Room
|
| 162 |
"freshness": [
|
| 163 |
decay["ideal_final"],
|
| 164 |
decay["room_final"],
|
|
@@ -170,20 +135,26 @@ def predict():
|
|
| 170 |
decay["humid_days_left"]
|
| 171 |
]
|
| 172 |
},
|
| 173 |
-
|
| 174 |
-
# -------------------------
|
| 175 |
-
# Status
|
| 176 |
-
# -------------------------
|
| 177 |
-
"status": status,
|
| 178 |
-
"status_color": color
|
| 179 |
})
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
finally:
|
| 184 |
if os.path.exists(filepath):
|
| 185 |
os.remove(filepath)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
# --------------------------------
|
| 188 |
# Serve React (Vite build)
|
| 189 |
# --------------------------------
|
|
|
|
| 13 |
ROOM_SHELF,
|
| 14 |
HIGH_HUMIDITY_SHELF
|
| 15 |
)
|
| 16 |
+
from backend.tips import STORAGE_TIPS
|
| 17 |
|
| 18 |
# --------------------------------
|
| 19 |
# Flask App Configuration
|
|
|
|
| 110 |
status = "SPOILED"
|
| 111 |
color = "#ef4444"
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return jsonify({
|
| 114 |
"success": True,
|
| 115 |
"fruit": fruit.capitalize(),
|
| 116 |
"initial_freshness": initial,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
"decay": decay,
|
| 118 |
+
"status": status,
|
| 119 |
+
"status_color": color,
|
|
|
|
|
|
|
| 120 |
"shelf_life": {
|
| 121 |
"ideal": IDEAL_SHELF[fruit],
|
| 122 |
"room": ROOM_SHELF[fruit],
|
| 123 |
"humid": HIGH_HUMIDITY_SHELF[fruit]
|
| 124 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
"chart_data": {
|
| 126 |
+
"labels": ["Ideal Storage", "Room Temp", "High Humidity"],
|
| 127 |
"freshness": [
|
| 128 |
decay["ideal_final"],
|
| 129 |
decay["room_final"],
|
|
|
|
| 135 |
decay["humid_days_left"]
|
| 136 |
]
|
| 137 |
},
|
| 138 |
+
"tips": STORAGE_TIPS.get(fruit, {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
})
|
| 140 |
|
|
|
|
|
|
|
| 141 |
finally:
|
| 142 |
if os.path.exists(filepath):
|
| 143 |
os.remove(filepath)
|
| 144 |
|
| 145 |
+
# --------------------------------
|
| 146 |
+
# Tips endpoint
|
| 147 |
+
# --------------------------------
|
| 148 |
+
@app.route("/api/tips/<fruit>")
|
| 149 |
+
def get_tips(fruit):
|
| 150 |
+
fruit = fruit.lower()
|
| 151 |
+
if fruit not in STORAGE_TIPS:
|
| 152 |
+
return jsonify({"error": "No tips available for this item"}), 400
|
| 153 |
+
return jsonify({
|
| 154 |
+
"fruit": fruit.capitalize(),
|
| 155 |
+
"tips": STORAGE_TIPS[fruit]
|
| 156 |
+
})
|
| 157 |
+
|
| 158 |
# --------------------------------
|
| 159 |
# Serve React (Vite build)
|
| 160 |
# --------------------------------
|