File size: 17,382 Bytes
8433bea
 
 
 
509afe3
 
8433bea
 
509afe3
c5d67d0
8433bea
 
444c673
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
c5d67d0
 
 
8433bea
 
c5d67d0
 
444c673
 
8433bea
 
 
444c673
 
8433bea
444c673
 
8433bea
 
 
444c673
 
 
 
 
 
 
 
 
 
8433bea
 
 
 
 
 
 
444c673
8433bea
 
444c673
 
 
 
 
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d67d0
 
 
8433bea
 
444c673
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509afe3
8433bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
 
 
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d67d0
8433bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d67d0
 
b42ecfb
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import os
from pathlib import Path
from typing import Optional, Tuple, List, Dict

import gradio as gr
import pandas as pd
import numpy as np
import plotly.express as px
import joblib

# ZeroGPU hooks (safe on CPU Spaces too)
import spaces
import torch

# Optional micro-model to "polish" text when GPU window is available
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline

# ---------------------
# Constants & storage
# ---------------------
DATA_DIR = Path("data"); DATA_DIR.mkdir(exist_ok=True)
TS_FMT = "%Y-%m-%d %H:%M:%S"

# Load your regressor
DT_PATH = "./decision_tree_regressor.joblib"
decision_tree_regressor = joblib.load(DT_PATH)

# Lightweight text model (CPU ok, faster on GPU)
GEN_MODEL = os.getenv("PLAN_POLISH_MODEL", "google/flan-t5-small")
_tokenizer = AutoTokenizer.from_pretrained(GEN_MODEL)
_model = AutoModelForSeq2SeqLM.from_pretrained(GEN_MODEL)
_generate_cpu = pipeline("text2text-generation", model=_model, tokenizer=_tokenizer, device=-1)

# --------------
# ZeroGPU fns
# --------------
@spaces.GPU
def gpu_warmup() -> str:
    return f"cuda={torch.cuda.is_available()}"

@spaces.GPU
def polish_on_gpu(text: str, lang: str = "en") -> str:
    """Polish/translate the already-generated plan inside a GPU window.
    Falls back to CPU gracefully if needed.
    """
    try:
        if torch.cuda.is_available():
            gen = pipeline(
                "text2text-generation",
                model=_model.to("cuda"),
                tokenizer=_tokenizer,
                device=0,
            )
        else:
            gen = _generate_cpu
        prompt = (
            "Rewrite the following fasting plan in a friendly coaching tone, keep markdown structure, "
            f"and output language '{lang}'. Keep tables and numbered lists concise.\n\n" + text
        )
        out = gen(prompt, max_new_tokens=700)
        return out[0]["generated_text"].strip()
    except Exception as e:
        out = _generate_cpu(text, max_new_tokens=10)
        return text + f"\n\n(Polish step skipped: {e})"

try:
    _ = gpu_warmup()
except Exception:
    pass

# ---------------------
# Utilities (metrics)
# ---------------------
ACTIVITY = {
    "Sedentary": 1.2,
    "Lightly active": 1.375,
    "Moderately active": 1.55,
    "Very active": 1.725,
    "Athlete": 1.9,
}

GOAL_CAL_ADJ = {  # % change to TDEE
    "Fat loss": -0.15,
    "Recomp/Maintenance": 0.0,
    "Muscle gain": 0.10,
}

def bmi(weight_kg: float, height_cm: float) -> float:
    return weight_kg / ((height_cm / 100) ** 2)


def bmr_mifflin(sex: str, weight_kg: float, height_cm: float, age: float) -> float:
    s = 5 if sex == "Male" else -161
    return 10 * weight_kg + 6.25 * height_cm - 5 * age + s


def tdee(bmr: float, activity: str) -> float:
    return bmr * ACTIVITY.get(activity, 1.2)


def parse_hhmm(hhmm: str) -> Tuple[int, int]:
    h, m = hhmm.split(":")
    h = int(h); m = int(m)
    if not (0 <= h <= 23 and 0 <= m <= 59):
        raise ValueError("Time must be HH:MM in 24h format.")
    return h, m


def fmt_hhmm(h: int, m: int) -> str:
    return f"{h:02d}:{m:02d}"

# ---------------------
# Plan generator (deterministic, rich)
# ---------------------
DIET_STYLES = ["Omnivore", "Mediterranean", "Vegetarian", "Vegan", "Low-carb"]

MEAL_IDEAS = {
    "Omnivore": [
        "Greek yogurt + berries + nuts",
        "Chicken bowl (rice, veggies, olive oil)",
        "Eggs, avocado, sourdough",
        "Salmon, quinoa, asparagus",
        "Lean beef, sweet potato, salad",
        "Tuna whole-grain wrap",
        "Cottage cheese + fruit + seeds",
    ],
    "Mediterranean": [
        "Oats with dates, walnuts, olive oil drizzle",
        "Grilled fish, lentil salad, greens",
        "Hummus platter, wholegrain pita, veg",
        "Chickpea tomato stew",
        "Feta + olive salad, quinoa",
        "Shakshuka + side salad",
        "Lentils, roasted veg, tahini",
    ],
    "Vegetarian": [
        "Tofu scramble, toast, avocado",
        "Paneer tikka bowl",
        "Bean chili + brown rice",
        "Halloumi, couscous, veg",
        "Greek salad + eggs",
        "Tempeh stir-fry",
        "Yogurt parfait + granola",
    ],
    "Vegan": [
        "Tofu scramble, avocado toast",
        "Lentil curry + basmati",
        "Burrito bowl (beans, corn, salsa)",
        "Seitan, roasted potatoes, veg",
        "Tofu poke bowl",
        "Chickpea pasta + marinara",
        "Overnight oats + banana + peanut butter",
    ],
    "Low-carb": [
        "Eggs, smoked salmon, salad",
        "Chicken Caesar (no croutons)",
        "Beef & greens stir-fry",
        "Omelette + veg + cheese",
        "Zoodles + turkey bolognese",
        "Tofu salad w/ tahini",
        "Yogurt + nuts (moderate)",
    ],
}

WORKOUTS = {
    "Fat loss": [
        "3× LISS cardio 30–40min",
        "2× full‑body strength 45min",
        "1× intervals 12–16min",
        "Daily 8–10k steps"
    ],
    "Recomp/Maintenance": [
        "3× full‑body strength 45–60min",
        "1–2× LISS cardio 30min",
        "Mobility 10min daily",
        "8–10k steps"
    ],
    "Muscle gain": [
        "4× strength split 45–60min",
        "Optional 1× LISS 20–30min",
        "Mobility 10min",
        "7–9k steps"
    ],
}


def feeding_schedule(first_meal_hhmm: str, fasting_hours: float) -> List[Tuple[str, str]]:
    """Return 7 (start,end) strings for the eating window each day."""
    h, m = parse_hhmm(first_meal_hhmm)
    window = max(0.0, 24 - float(fasting_hours))
    start_minutes = h * 60 + m
    end_minutes = int((start_minutes + window * 60) % (24 * 60))

    sched = []
    for _ in range(7):
        start = fmt_hhmm(h, m)
        end = fmt_hhmm(end_minutes // 60, end_minutes % 60)
        sched.append((start, end))
    return sched


def weekly_plan(diet: str, sched: List[Tuple[str, str]], kcal: int, protein_g: int) -> pd.DataFrame:
    ideas = MEAL_IDEAS[diet]
    rows = []
    for i in range(7):
        day = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"][i]
        start, end = sched[i]
        meal1 = ideas[i % len(ideas)]
        meal2 = ideas[(i+3) % len(ideas)]
        snack = "Fruit or nuts (optional)"
        rows.append({
            "Day": day,
            "Feeding window": f"{start}{end}",
            "Meal 1": meal1,
            "Meal 2": meal2,
            "Protein target": f"≥ {protein_g} g",
            "Daily kcal": kcal,
            "Snack": snack,
        })
    return pd.DataFrame(rows)


def shopping_list(diet: str) -> List[str]:
    core = [
        "Leafy greens, mixed veg, berries",
        "Olive oil, nuts/seeds, herbs & spices",
        "Coffee/tea, mineral water, electrolytes",
    ]
    extras = {
        "Omnivore": ["Chicken, fish, eggs, yogurt, cottage cheese", "Rice/quinoa/sourdough", "Beans/lentils"],
        "Mediterranean": ["Fish, feta, olives", "Whole grains (bulgur, farro)", "Chickpeas/lentils"],
        "Vegetarian": ["Eggs, dairy, paneer", "Legumes", "Tofu/tempeh"],
        "Vegan": ["Tofu/tempeh/seitan", "Beans/lentils", "Plant yogurt/milk"],
        "Low-carb": ["Eggs, fish, meat", "Green veg", "Greek yogurt, cheese"],
    }
    return core + extras[diet]

# ---------------------
# Tracker (history)
# ---------------------
active_fasts: Dict[str, pd.Timestamp] = {}

def _csv(u: str) -> Path:
    safe = "".join(ch for ch in (u or "default") if ch.isalnum() or ch in ("_","-"))
    return DATA_DIR / f"{safe}.csv"

def hist_load(u: str) -> pd.DataFrame:
    p = _csv(u)
    if p.exists():
        d = pd.read_csv(p)
        for c in ["start_time","end_time"]:
            if c in d: d[c] = pd.to_datetime(d[c], errors="coerce")
        return d
    return pd.DataFrame(columns=["start_time","end_time","duration_hours","note"])

def hist_save(u: str, d: pd.DataFrame):
    d.to_csv(_csv(u), index=False)

# ---------------------
# Core actions
# ---------------------

def predict_and_plan(fasting_duration, meal_timing, weight, age, gender, height,
                     activity, goal, diet, lang, ai_polish) -> Tuple[Optional[float], str, str, pd.DataFrame, object, str]:
    try:
        # Validation
        if fasting_duration < 0 or fasting_duration > 72:
            raise ValueError("Fasting duration must be 0–72h.")
        parse_hhmm(meal_timing)
        if weight <= 0 or height <= 0 or age < 0:
            raise ValueError("Check weight/height/age values.")

        # Model score
        df = pd.DataFrame({
            "Fasting Duration (hours)": [float(fasting_duration)],
            "Meal Timing (hour:minute)": [lambda t=meal_timing: int(t.split(":")[0]) + int(t.split(":")[1]) / 60.0][0](),
            "Body Weight (kg)": [float(weight)],
            "Age (years)": [float(age)],
            "Height (cm)": [float(height)],
            "Gender_Male": [1 if gender == "Male" else 0],
            "Gender_Other": [1 if gender == "Other" else 0],
        })
        score = float(decision_tree_regressor.predict(df)[0])

        # Metrics
        bmr = bmr_mifflin(gender, weight, height, age)
        tdee_kcal = tdee(bmr, activity)
        adj = GOAL_CAL_ADJ[goal]
        target_kcal = int(round(tdee_kcal * (1 + adj)))
        protein_g = int(round(max(1.6 * weight, 90 if goal == "Muscle gain" else 80)))
        bmi_val = round(bmi(weight, height), 1)

        # Schedule & tables
        sched = feeding_schedule(meal_timing, float(fasting_duration))
        plan_df = weekly_plan(diet, sched, target_kcal, protein_g)

        # Chart (Gantt-style feeding window)
        chart_df = pd.DataFrame({
            "Day": ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],
            "start": [int(s.split(":")[0])*60 + int(s.split(":")[1]) for s,_ in sched],
            "length": [max(0, int((24 - float(fasting_duration))*60))]*7,
        })
        fig = px.bar(chart_df, y="Day", x="length", base="start", orientation="h", title="Feeding window each day (minutes)")
        fig.update_layout(xaxis=dict(range=[0,1440], tickvals=[0,360,720,1080,1440], ticktext=["00:00","06:00","12:00","18:00","24:00"]))

        # Markdown plan
        hdr = {
            "en": "## Your 7‑day intermittent fasting plan",
            "es": "## Tu plan de ayuno intermitente de 7 días",
        }[lang]
        kpis = (
            f"**Score:** {score:.1f}  •  **BMI:** {bmi_val}  •  **BMR:** {int(bmr)} kcal  •  **TDEE:** {int(tdee_kcal)} kcal  •  "
            f"**Target:** {target_kcal} kcal  •  **Protein:** ≥ {protein_g} g  •  **Diet:** {diet}\n"
        )
        sched_md = "\n".join([f"- **{d}**: {s}{e}" for d,(s,e) in zip(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"], sched)])
        workouts = "\n".join([f"- {w}" for w in WORKOUTS[goal]])
        shop = "\n".join([f"- {x}" for x in shopping_list(diet)])

        plan_md = f"""
{hdr}

{kpis}

### Feeding window (daily)
{sched_md}

### Weekly training
{workouts}

### Daily meals (example week)
(See table below for details.)

### Shopping list
{shop}

> Hydration & electrolytes during the fast, protein at each meal, whole foods, and 7–9 hours sleep.
""".strip()

        # Optional AI polish (ZeroGPU window)
        if ai_polish:
            try:
                plan_md = polish_on_gpu(plan_md, lang)
            except Exception:
                pass

        # Export file path (Markdown)
        md_path = DATA_DIR / "plan.md"
        md_path.write_text(plan_md, encoding="utf-8")

        return score, kpis, plan_md, plan_df, fig, str(md_path)
    except Exception as e:
        return None, "", f"⚠️ {e}", pd.DataFrame(), None, ""

# ---------------------
# Tracker actions
# ---------------------

def start_fast(user: str, note: str):
    if not user: return "Enter username in Settings.", None
    if user in active_fasts: return f"Already fasting since {active_fasts[user]}.", None
    active_fasts[user] = pd.Timestamp.now()
    return f"✅ Fast started at {active_fasts[user].strftime(TS_FMT)}.", None


def end_fast(user: str):
    if not user: return "Enter username in Settings.", None, None, None
    if user not in active_fasts: return "No active fast.", None, None, None
    end = pd.Timestamp.now(); start = active_fasts.pop(user)
    dur = round((end - start).total_seconds()/3600, 2)
    df = hist_load(user)
    df.loc[len(df)] = [start, end, dur, ""]
    hist_save(user, df)
    chart = make_hist_chart(df)
    return f"✅ Fast ended at {end.strftime(TS_FMT)}{dur} h", df.tail(12), chart, hist_stats(df)


def refresh_hist(user: str):
    df = hist_load(user)
    return df.tail(12), make_hist_chart(df), hist_stats(df)


def make_hist_chart(df: pd.DataFrame):
    if df.empty: return None
    d = df.dropna(subset=["end_time"]).copy()
    d["date"] = pd.to_datetime(d["end_time"]).dt.date
    fig = px.bar(d, x="date", y="duration_hours", title="Fasting duration by day (h)")
    fig.update_layout(height=300, margin=dict(l=10,r=10,t=40,b=10))
    return fig


def hist_stats(df: pd.DataFrame) -> str:
    if df.empty: return "No history yet."
    last7 = df.tail(7)
    avg = last7["duration_hours"].mean()
    streak = compute_streak(df)
    return f"Total fasts: {len(df)}\nAvg (last 7): {avg:.2f} h\nCurrent streak: {streak} day(s)"


def compute_streak(df: pd.DataFrame) -> int:
    d = df.dropna(subset=["end_time"]).copy()
    if d.empty: return 0
    days = set(pd.to_datetime(d["end_time"]).dt.date)
    cur = pd.Timestamp.now().date(); streak=0
    while cur in days:
        streak+=1; cur = cur - pd.Timedelta(days=1)
    return streak

# ---------------------
# UI
# ---------------------
with gr.Blocks(
    title="Intermittent Fasting Coach — Pro",
    theme=gr.themes.Soft(primary_hue=gr.themes.colors.orange, neutral_hue=gr.themes.colors.gray),
) as demo:
    gr.Markdown("""
    # 🥣 Intermittent Fasting — Pro
    Detailed coaching plans + tracker. ZeroGPU‑ready (with CPU fallback). All data stored locally in this Space.
    """)

    with gr.Tabs():
        # --- Coach tab
        with gr.TabItem("Coach"):
            with gr.Row():
                with gr.Column():
                    fasting_duration = gr.Number(label="Fasting Duration (h)", value=16, minimum=0, maximum=72, step=0.5)
                    meal_timing = gr.Textbox(label="First meal time (HH:MM)", value="12:30")
                    weight = gr.Number(label="Body Weight (kg)", value=70, step=0.5)
                with gr.Column():
                    age = gr.Slider(label="Age (years)", minimum=18, maximum=100, value=35)
                    gender = gr.Radio(["Male","Female","Other"], label="Gender", value="Male")
                    height = gr.Number(label="Height (cm)", value=175)
            with gr.Row():
                activity = gr.Dropdown(choices=list(ACTIVITY.keys()), value="Lightly active", label="Activity")
                goal = gr.Dropdown(choices=list(GOAL_CAL_ADJ.keys()), value="Recomp/Maintenance", label="Goal")
                diet = gr.Dropdown(choices=DIET_STYLES, value="Mediterranean", label="Diet style")
                lang = gr.Radio(["en","es"], value="en", label="Language")
                ai_polish = gr.Checkbox(value=True, label="AI polish (uses ZeroGPU)")

            btn = gr.Button("Predict & Build Plan", variant="primary")

            score_out = gr.Number(label="Predicted score")
            kpi_out = gr.Markdown()
            plan_md = gr.Markdown()
            plan_tbl = gr.Dataframe(headers=["Day","Feeding window","Meal 1","Meal 2","Protein target","Daily kcal","Snack"], interactive=False)
            fig = gr.Plot()
            dl = gr.DownloadButton(label="Download plan (.md)")

            btn.click(
                predict_and_plan,
                inputs=[fasting_duration, meal_timing, weight, age, gender, height, activity, goal, diet, lang, ai_polish],
                outputs=[score_out, kpi_out, plan_md, plan_tbl, fig, dl],
                api_name="coach_plan"
            )

        # --- Tracker tab
        with gr.TabItem("Tracker"):
            with gr.Row():
                user = gr.Textbox(label="Username", value="")
                note = gr.Textbox(label="Note (optional)")
            with gr.Row():
                b1 = gr.Button("Start fast", variant="primary")
                b2 = gr.Button("End fast")
                b3 = gr.Button("Reload history")
            status = gr.Markdown("Not fasting.")
            hist = gr.Dataframe(interactive=False)
            hist_fig = gr.Plot()
            stats = gr.Markdown()

            b1.click(start_fast, inputs=[user, note], outputs=[status, note])
            b2.click(end_fast, inputs=[user], outputs=[status, hist, hist_fig, stats])
            b3.click(refresh_hist, inputs=[user], outputs=[hist, hist_fig, stats])
            demo.load(refresh_hist, inputs=[user], outputs=[hist, hist_fig, stats])

        # --- About tab
        with gr.TabItem("About"):
            gr.Markdown("""
            **How it works**  
            • Your predictor estimates a health score from inputs.  
            • The coach builds a 7‑day schedule matching your fasting window, goal, activity and diet style.  
            • Optional AI polish refines wording using a tiny model (ZeroGPU window).  
            • Tracker stores CSVs under `/data/` and never sends data elsewhere.
            """)

if __name__ == "__main__":
    demo.queue().launch()