File size: 4,746 Bytes
1c3213f
a69ce69
 
 
1c3213f
d53a13e
a69ce69
d53a13e
a69ce69
 
d53a13e
2d23204
 
a28183b
2d23204
 
a28183b
2d23204
 
a69ce69
2d23204
 
 
 
 
d53a13e
 
a69ce69
39d4583
a69ce69
 
 
 
 
 
 
39d4583
 
a69ce69
 
 
 
2d23204
a28183b
a69ce69
 
 
39d4583
a69ce69
39d4583
 
3086fce
39d4583
3086fce
39d4583
 
 
 
 
 
 
 
3086fce
39d4583
 
3086fce
39d4583
 
a69ce69
39d4583
 
a69ce69
39d4583
 
a69ce69
 
 
 
 
 
 
 
 
39d4583
a69ce69
 
a28183b
 
 
 
 
 
a69ce69
 
 
 
 
 
a28183b
2d23204
 
 
a69ce69
 
 
 
1c3213f
2d23204
a28183b
2d23204
 
a7bbfb1
a28183b
 
1c3213f
d53a13e
a69ce69
d53a13e
2d23204
a28183b
d53a13e
2d23204
d53a13e
2d23204
a69ce69
d53a13e
 
a7bbfb1
a28183b
 
 
 
 
 
d53a13e
a69ce69
2d23204
c710d69
a28183b
 
2d23204
 
a69ce69
2d23204
 
 
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
import gradio as gr
import requests
import json
import os

# -----------------------------
# CONFIG
# -----------------------------
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

DEVICE_PARAMS = {
    "Apple Watch": [
        "RMSSD", "lnRMSSD", "MeanRR", "HR", "SDNN",
        "StressScore", "baseline", "pNN50", "AMo50", "MxDMn"
    ],
    "Polar": [
        "RMSSD", "SDNN", "MeanRR", "HR", "pNN50", "StressScore"
    ],
    "Garmin": [
        "RMSSD", "SDNN", "MeanRR", "HR", "StressScore"
    ],
    "Other": [
        "RMSSD", "SDNN", "MeanRR", "HR", "pNN50", "AMo50",
        "StressScore", "baseline", "MxDMn"
    ],
}

THRESHOLDS = """
Use these physiological ranges for interpretation:

- RMSSD: Low <25, Moderate 25–60, High >60
- SDNN: Low <40, Moderate 40–70, High >70
- lnRMSSD: Low <3.0, Moderate 3.0–3.9, High >3.9
- HR: High stress >80 bpm, Normal 55–80, Excellent <55
- MeanRR: Low <700 (fast heart rate, higher stress), Moderate 700–950 (balanced rhythm), High >950 (good recovery)
- StressScore: Low <30, Moderate 30–60, High >60
- Baseline: <10 = low trend, 10–12 = stable, >12 = improving
- pNN50: Low <10%, Moderate 10–25%, High >25%
- AMo50: Low <1.5 (good adaptability), High >2.5 (rigidity)
- CV: Low <2, Moderate 2–6, High >6
"""

# -----------------------------
# Groq API Call
# -----------------------------
def call_groq_api(device, data):
    prompt = f"""
You are a Welltory-style health assistant who analyzes HRV data with empathy and scientific precision.

Here is the HRV dataset from a {device} device:
{json.dumps(data, indent=2)}

{THRESHOLDS}

Instructions:
1. Evaluate each parameter relative to its physiological range.
2. Explain what it means for stress, recovery, and adaptability.
3. If baseline > 0, describe it as a long-term recovery trend.
4. Maintain a calm, motivating, yet realistic tone β€” if HRV is low or stress is high, clearly say so.
5. Address the user directly using β€œyou” and β€œyour”.
6. Do not mention numeric thresholds in the output.
7. Output in Markdown format with these sections:

**Parameter Insights:**
[List of parameters, each explained clearly β€” what it measures, what the value means, and what it indicates.]

**Overall Interpretation:**
[A realistic summary of stress, recovery, and physiological state.]

**Actionable Suggestions:**
[3–4 specific, habit-based recommendations to improve HRV and stress recovery.]

If most parameters indicate stress or low HRV, the output tone should sound *concerned but encouraging*, not overly positive.
"""

    headers = {
        "Authorization": f"Bearer {GROQ_API_KEY}",
        "Content-Type": "application/json",
    }

    body = {
        "model": "llama-3.3-70b-versatile",
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.4,  # lower temperature = more consistent, realistic tone
    }

    response = requests.post(
        "https://api.groq.com/openai/v1/chat/completions",
        headers=headers,
        json=body
    )

    try:
        return response.json()["choices"][0]["message"]["content"]
    except Exception as e:
        return f"⚠️ Error: {e}\n\nRaw response: {response.text}"

# -----------------------------
# HRV Analyzer
# -----------------------------
def analyze_hrv(device, *values):
    params = DEVICE_PARAMS.get(device, [])
    data = {p: v for p, v in zip(params, values) if v is not None and v != 0}
    if not data:
        return "⚠️ Please input at least one HRV parameter."
    return call_groq_api(device, data)

# -----------------------------
# Dynamic Parameter Visibility
# -----------------------------
def update_params(device):
    visible_params = DEVICE_PARAMS.get(device, [])
    updates = [gr.update(visible=(p in visible_params)) for p in ALL_PARAM_NAMES]
    return updates

# -----------------------------
# BUILD GRADIO UI
# -----------------------------
with gr.Blocks() as demo:
    gr.Markdown("## πŸ’“ HRV Wellness Insight β€” Powered by Groq")

    device = gr.Dropdown(
        choices=list(DEVICE_PARAMS.keys()),
        value="Apple Watch",
        label="Select Your Device",
    )

    ALL_PARAM_NAMES = sorted({p for params in DEVICE_PARAMS.values() for p in params})

    with gr.Row():
        param_inputs = [
            gr.Number(label=p, visible=(p in DEVICE_PARAMS["Apple Watch"]))
            for p in ALL_PARAM_NAMES
        ]

    analyze_btn = gr.Button("πŸ” Analyze HRV")
    output = gr.Markdown()

    analyze_btn.click(fn=analyze_hrv, inputs=[device] + param_inputs, outputs=output)
    device.change(fn=update_params, inputs=device, outputs=param_inputs)

# -----------------------------
# RUN APP
# -----------------------------
if __name__ == "__main__":
    demo.launch()