dss107 commited on
Commit
311e9d1
Β·
verified Β·
1 Parent(s): 2a85a1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -21,9 +21,11 @@ DEVICE_PARAMS = {
21
  # -----------------------------
22
  # HRV ANALYSIS FUNCTION
23
  # -----------------------------
24
- def analyze_hrv(device, **kwargs):
 
25
  payload = {"device": device}
26
- payload.update(kwargs)
 
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
- If any parameter has a value of 0 or missing, skip analyzing or mentioning it entirely.
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
- 1. Evaluate each parameter relative to its range.
53
- 2. Explain what it means for stress, recovery, and adaptability.
54
- 3. If baseline > 0, describe it as a long-term recovery trend.
55
- 4. Maintain a calm, motivating tone like a professional wellness coach.
56
- 5. Address the user directly (use β€œyou”, β€œyour”).
57
- 6. Don’t mention numerical thresholds.
58
- 7. Output your answer in Markdown with:
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
- # DYNAMIC UI BUILDER
84
  # -----------------------------
85
  def get_param_inputs(device):
 
86
  params = DEVICE_PARAMS.get(device, DEVICE_PARAMS["Other / Custom"])
87
- components = [gr.Number(label=p) for p in params]
88
- return components
89
 
90
- def update_param_fields(device):
 
91
  components = get_param_inputs(device)
92
- return gr.update(visible=True), components
93
 
94
- # -----------------------------
95
- # BUILD INTERFACE
96
- # -----------------------------
97
  with gr.Blocks(title="πŸ’“ HRV Wellness Insight (Groq + Welltory Style)") as demo:
98
- gr.Markdown("### πŸ’“ HRV Wellness Insight\nSelect your wearable device and enter your HRV metrics below to get a detailed interpretation.")
99
 
100
  device_dropdown = gr.Dropdown(
101
- label="Select Your Device",
102
  choices=list(DEVICE_PARAMS.keys()),
103
  value="Apple Watch"
104
  )
105
 
106
- param_inputs = gr.Group(visible=True)
107
- dynamic_params = gr.Column(get_param_inputs("Apple Watch"))
 
 
108
 
109
  output = gr.Markdown()
110
 
 
 
111
  def forward_call(device, *args):
112
- # Prepare kwargs for parameters dynamically
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.Column(get_param_inputs(d)),
119
  inputs=device_dropdown,
120
- outputs=dynamic_params
121
  )
122
 
123
- analyze_btn = gr.Button("πŸ” Analyze HRV")
124
  analyze_btn.click(
125
  fn=forward_call,
126
- inputs=[device_dropdown] + get_param_inputs("Apple Watch"),
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