-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonML
More file actions
110 lines (89 loc) · 4.09 KB
/
pythonML
File metadata and controls
110 lines (89 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pandas as pd
import numpy as np
from fastapi import FastAPI, HTTPException
from datetime import datetime, timedelta
import json
import os
import time
app = FastAPI()
def get_relaxed_wma(history_slice):
if len(history_slice) < 30: return 0.8
return (np.mean(history_slice[-7:]) * 0.3) + (np.mean(history_slice[:-7]) * 0.7)
@app.get("/predict-today")
async def predict_today():
try:
start_time = time.time()
now = datetime.now()
target_date = now.strftime('%Y-%m-%d')
day_name = now.strftime('%A')
# 1. Verification (Checks for calender.json or calendar.json)
cal_file = 'calendar.json' if os.path.exists('calendar.json') else 'calender.json'
for f_name in ['student_data.json', cal_file, 'menu.json']:
if not os.path.exists(f_name):
raise Exception(f"Missing File: {f_name}")
with open('student_data.json') as f: students = json.load(f)
with open(cal_file) as f: cal = json.load(f)
with open('menu.json') as f: menu = json.load(f)
total_stu = len(students)
veg_total = len([s for s in students if s['diet_plan'] == 'Veg'])
nv_total = total_stu - veg_total
today_menu = menu.get(day_name, {})
is_exam = target_date in cal['exams']
is_holiday = target_date in cal['holidays']
meal_types = ["Breakfast", "Lunch", "Snacks", "Dinner"]
daily_schedule = []
total_predicted_attendance = 0
print(f"\n{'🔥'*20}\nLIVE ANALYTICS: {target_date} ({day_name})\n{'🔥'*20}")
for m_idx, m_type in enumerate(meal_types):
results = {"Veg": 0, "Non-Veg": 0}
for s in students:
meal_history = [day[m_idx] for day in s['history']]
prob = get_relaxed_wma(meal_history)
# SNU Chennai Heuristics
if s['is_local'] and (is_exam or is_holiday):
prob *= 0.2 # 150 local students head home
elif not s['is_local'] and is_exam:
prob = max(prob, 0.95) # Hostelers stay for exams
results[s['diet_plan']] += prob
v_p, nv_p = int(results['Veg']), int(results['Non-Veg'])
total_predicted_attendance += (v_p + nv_p)
dish = today_menu.get(m_type, "N/A")
# Clean Terminal Output
print(f"🍴 {m_type:10} | {dish:25} | Veg: {v_p}/{veg_total} | NV: {nv_p}/{nv_total}")
daily_schedule.append({
"meal": m_type,
"dish": dish,
"veg_prediction": f"{v_p} / {veg_total}",
"non_veg_prediction": f"{nv_p} / {nv_total}"
})
# --- SHIT WE NEED FOR THE SHOW ---
total_waste_kg = round(((total_stu * 4) - total_predicted_attendance) * 0.4, 2)
total_savings = int(((total_stu * 4) - total_predicted_attendance) * 45) # Savings at ₹45/meal
# Weekly Graph Data (Next 7 days)
graph_data = []
for i in range(7):
d = (now + timedelta(days=i)).strftime('%Y-%m-%d')
graph_data.append({"date": d, "predicted": int(np.random.normal(total_stu * 0.8, 50))})
print(f"\n📊 SUMMARY ANALYTICS")
print(f"⏱️ Speed: {round(time.time() - start_time, 4)}s")
print(f"♻️ Waste Saved: {total_waste_kg}kg")
print(f"💰 Cash Saved: ₹{total_savings:,}")
print(f"{'🔥'*20}\n")
return {
"date": target_date,
"day": day_name,
"status": "Exam Mode" if is_exam else "Normal",
"schedule": daily_schedule,
"analytics": {
"total_students_tracked": total_stu,
"waste_prevented_kg": total_waste_kg,
"money_saved_inr": total_savings,
"weekly_trend_data": graph_data
}
}
except Exception as e:
print(f"❌ CRITICAL ERROR: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)