Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,9 +21,11 @@ DEVICE_PARAMS = {
|
|
| 21 |
# -----------------------------
|
| 22 |
# HRV ANALYSIS FUNCTION
|
| 23 |
# -----------------------------
|
| 24 |
-
def analyze_hrv(device,
|
|
|
|
| 25 |
payload = {"device": device}
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
thresholds = """
|
| 29 |
Use these physiological ranges:
|
|
@@ -41,7 +43,7 @@ def analyze_hrv(device, **kwargs):
|
|
| 41 |
|
| 42 |
prompt = f"""
|
| 43 |
You are a Welltory-style health assistant. Speak directly to the user in a warm, human tone.
|
| 44 |
-
|
| 45 |
|
| 46 |
Here is the HRV dataset:
|
| 47 |
{json.dumps(payload, indent=2)}
|
|
@@ -49,16 +51,13 @@ def analyze_hrv(device, **kwargs):
|
|
| 49 |
{thresholds}
|
| 50 |
|
| 51 |
Instructions:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
**Parameter Insights**
|
| 60 |
-
**Overall Interpretation**
|
| 61 |
-
**Actionable Suggestions**
|
| 62 |
"""
|
| 63 |
|
| 64 |
response = requests.post(
|
|
@@ -80,50 +79,50 @@ def analyze_hrv(device, **kwargs):
|
|
| 80 |
return f"β οΈ Error: {e}\n\nRaw response: {response.text}"
|
| 81 |
|
| 82 |
# -----------------------------
|
| 83 |
-
#
|
| 84 |
# -----------------------------
|
| 85 |
def get_param_inputs(device):
|
|
|
|
| 86 |
params = DEVICE_PARAMS.get(device, DEVICE_PARAMS["Other / Custom"])
|
| 87 |
-
|
| 88 |
-
return components
|
| 89 |
|
| 90 |
-
def
|
|
|
|
| 91 |
components = get_param_inputs(device)
|
| 92 |
-
return gr.
|
| 93 |
|
| 94 |
-
# -----------------------------
|
| 95 |
-
# BUILD INTERFACE
|
| 96 |
-
# -----------------------------
|
| 97 |
with gr.Blocks(title="π HRV Wellness Insight (Groq + Welltory Style)") as demo:
|
| 98 |
-
gr.Markdown("
|
| 99 |
|
| 100 |
device_dropdown = gr.Dropdown(
|
| 101 |
-
label="Select
|
| 102 |
choices=list(DEVICE_PARAMS.keys()),
|
| 103 |
value="Apple Watch"
|
| 104 |
)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
output = gr.Markdown()
|
| 110 |
|
|
|
|
|
|
|
| 111 |
def forward_call(device, *args):
|
| 112 |
-
|
| 113 |
-
params = DEVICE_PARAMS.get(device, [])
|
| 114 |
-
values = dict(zip(params, args))
|
| 115 |
-
return analyze_hrv(device, **values)
|
| 116 |
|
|
|
|
| 117 |
device_dropdown.change(
|
| 118 |
-
fn=lambda d: gr.
|
| 119 |
inputs=device_dropdown,
|
| 120 |
-
outputs=
|
| 121 |
)
|
| 122 |
|
| 123 |
-
|
| 124 |
analyze_btn.click(
|
| 125 |
fn=forward_call,
|
| 126 |
-
inputs=[
|
| 127 |
outputs=output
|
| 128 |
)
|
| 129 |
|
|
|
|
| 21 |
# -----------------------------
|
| 22 |
# HRV ANALYSIS FUNCTION
|
| 23 |
# -----------------------------
|
| 24 |
+
def analyze_hrv(device, param_values):
|
| 25 |
+
params = DEVICE_PARAMS.get(device, DEVICE_PARAMS["Other / Custom"])
|
| 26 |
payload = {"device": device}
|
| 27 |
+
for k, v in zip(params, param_values):
|
| 28 |
+
payload[k] = v
|
| 29 |
|
| 30 |
thresholds = """
|
| 31 |
Use these physiological ranges:
|
|
|
|
| 43 |
|
| 44 |
prompt = f"""
|
| 45 |
You are a Welltory-style health assistant. Speak directly to the user in a warm, human tone.
|
| 46 |
+
Skip parameters with missing or zero values.
|
| 47 |
|
| 48 |
Here is the HRV dataset:
|
| 49 |
{json.dumps(payload, indent=2)}
|
|
|
|
| 51 |
{thresholds}
|
| 52 |
|
| 53 |
Instructions:
|
| 54 |
+
- Evaluate each parameter relative to its range.
|
| 55 |
+
- Explain its meaning for stress, recovery, and adaptability.
|
| 56 |
+
- If baseline > 0, describe it as a long-term recovery trend.
|
| 57 |
+
- Use a calm, encouraging tone.
|
| 58 |
+
- Do NOT mention numerical thresholds.
|
| 59 |
+
- Output in Markdown with:
|
| 60 |
+
**Parameter Insights**, **Overall Interpretation**, **Actionable Suggestions**
|
|
|
|
|
|
|
|
|
|
| 61 |
"""
|
| 62 |
|
| 63 |
response = requests.post(
|
|
|
|
| 79 |
return f"β οΈ Error: {e}\n\nRaw response: {response.text}"
|
| 80 |
|
| 81 |
# -----------------------------
|
| 82 |
+
# BUILD UI
|
| 83 |
# -----------------------------
|
| 84 |
def get_param_inputs(device):
|
| 85 |
+
"""Return input components based on device."""
|
| 86 |
params = DEVICE_PARAMS.get(device, DEVICE_PARAMS["Other / Custom"])
|
| 87 |
+
return [gr.Number(label=p, value=0) for p in params]
|
|
|
|
| 88 |
|
| 89 |
+
def update_fields(device):
|
| 90 |
+
"""Dynamically rebuild parameter input fields when device changes."""
|
| 91 |
components = get_param_inputs(device)
|
| 92 |
+
return gr.Group(components), gr.State(device), gr.State(components)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Blocks(title="π HRV Wellness Insight (Groq + Welltory Style)") as demo:
|
| 95 |
+
gr.Markdown("## π HRV Wellness Insight\nSelect your wearable device and enter your HRV metrics to get a detailed interpretation.")
|
| 96 |
|
| 97 |
device_dropdown = gr.Dropdown(
|
| 98 |
+
label="Select Device",
|
| 99 |
choices=list(DEVICE_PARAMS.keys()),
|
| 100 |
value="Apple Watch"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Dynamic area for parameters
|
| 104 |
+
param_group = gr.Group(get_param_inputs("Apple Watch"))
|
| 105 |
+
device_state = gr.State("Apple Watch")
|
| 106 |
+
params_state = gr.State(get_param_inputs("Apple Watch"))
|
| 107 |
|
| 108 |
output = gr.Markdown()
|
| 109 |
|
| 110 |
+
analyze_btn = gr.Button("π Analyze HRV")
|
| 111 |
+
|
| 112 |
def forward_call(device, *args):
|
| 113 |
+
return analyze_hrv(device, args)
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# Update parameter fields when device changes
|
| 116 |
device_dropdown.change(
|
| 117 |
+
fn=lambda d: (gr.Group(get_param_inputs(d)), d, get_param_inputs(d)),
|
| 118 |
inputs=device_dropdown,
|
| 119 |
+
outputs=[param_group, device_state, params_state]
|
| 120 |
)
|
| 121 |
|
| 122 |
+
# Trigger analysis
|
| 123 |
analyze_btn.click(
|
| 124 |
fn=forward_call,
|
| 125 |
+
inputs=[device_state, *params_state],
|
| 126 |
outputs=output
|
| 127 |
)
|
| 128 |
|