victoria-latynina commited on
Commit
149df1f
Β·
verified Β·
1 Parent(s): 314f900

Update whoop_gradio_server.py

Browse files
Files changed (1) hide show
  1. whoop_gradio_server.py +69 -31
whoop_gradio_server.py CHANGED
@@ -14,6 +14,17 @@ logger = logging.getLogger(__name__)
14
  whoop_client = None
15
 
16
  def initialize_whoop_client_with_input(email, password):
 
 
 
 
 
 
 
 
 
 
 
17
  global whoop_client
18
 
19
  if not email or not password:
@@ -29,6 +40,12 @@ def initialize_whoop_client_with_input(email, password):
29
  return f"❌ Authentication failed: {e}"
30
 
31
  def get_latest_cycle_gr():
 
 
 
 
 
 
32
  if not whoop_client:
33
  return "❌ Not authenticated."
34
 
@@ -40,12 +57,22 @@ def get_latest_cycle_gr():
40
  return "⚠️ No cycle data available."
41
 
42
  latest = cycles[0]
43
- score = latest.get("score", {}).get("recovery", "N/A")
44
- return f"πŸŒ€ Latest Cycle:\nRecovery Score: {score}\n\nFull Data:\n{latest}"
 
45
  except Exception as e:
46
  return f"❌ Error: {e}"
47
 
48
  def get_average_strain_gr(days):
 
 
 
 
 
 
 
 
 
49
  if not whoop_client:
50
  return "❌ Not authenticated."
51
 
@@ -66,45 +93,56 @@ def get_average_strain_gr(days):
66
  except Exception as e:
67
  return f"❌ Error: {e}"
68
 
69
- def get_auth_status_gr():
70
- if not whoop_client:
71
- return "πŸ”’ Not authenticated."
72
 
 
 
 
 
 
 
 
 
73
  try:
74
- profile = whoop_client.get_profile()
75
- return f"πŸ”“ Authenticated.\nUser: {profile.get('name', 'Unknown')}\nEmail: {profile.get('email', 'Unknown')}"
 
 
76
  except Exception as e:
77
- return f"❌ Error checking auth: {e}"
78
 
79
  # UI definition
80
  with gr.Blocks(title="Whoop API Explorer") as demo:
81
  gr.Markdown("# 🧠 WHOOP API Dashboard")
82
- gr.Markdown("Authenticate and explore your recovery and strain data.")
83
-
84
- with gr.Row():
85
- email_input = gr.Textbox(label="WHOOP Email")
86
- password_input = gr.Textbox(label="WHOOP Password", type="password")
87
- auth_output = gr.Textbox(label="Auth Status", interactive=False)
88
- auth_button = gr.Button("Authenticate with Whoop")
89
-
90
- with gr.Row():
91
- cycle_output = gr.Textbox(label="Latest Cycle", lines=10, interactive=False)
92
- cycle_button = gr.Button("Get Latest Cycle")
93
-
94
- with gr.Row():
95
- days_input = gr.Number(value=7, label="Days for Avg Strain")
96
- strain_output = gr.Textbox(label="Average Strain", lines=4, interactive=False)
97
- strain_button = gr.Button("Get Average Strain")
98
-
99
- with gr.Row():
100
- status_output = gr.Textbox(label="Whoop Profile Info", lines=4, interactive=False)
101
- status_button = gr.Button("Check Auth Status")
 
 
102
 
103
  # Bind actions
104
  auth_button.click(fn=initialize_whoop_client_with_input, inputs=[email_input, password_input], outputs=auth_output)
105
- cycle_button.click(fn=get_latest_cycle_gr, outputs=cycle_output)
106
- strain_button.click(fn=get_average_strain_gr, inputs=days_input, outputs=strain_output)
107
- status_button.click(fn=get_auth_status_gr, outputs=status_output)
108
 
109
  # Launch app
110
  if __name__ == "__main__":
 
14
  whoop_client = None
15
 
16
  def initialize_whoop_client_with_input(email, password):
17
+ """
18
+ Authenticates a user using the provided WHOOP email and password.
19
+ Stores credentials in the environment variables and initializes the WhoopClient.
20
+
21
+ Args:
22
+ email (str): WHOOP account email.
23
+ password (str): WHOOP account password.
24
+
25
+ Returns:
26
+ str: Success or error message indicating authentication status.
27
+ """
28
  global whoop_client
29
 
30
  if not email or not password:
 
40
  return f"❌ Authentication failed: {e}"
41
 
42
  def get_latest_cycle_gr():
43
+ """
44
+ Retrieves the most recent WHOOP cycle (recovery data) for the authenticated user.
45
+
46
+ Returns:
47
+ str: Summary of latest recovery data or an error message.
48
+ """
49
  if not whoop_client:
50
  return "❌ Not authenticated."
51
 
 
57
  return "⚠️ No cycle data available."
58
 
59
  latest = cycles[0]
60
+ score = latest.get("score", {}).get("recovery")
61
+ recovery_score = f"{score}" if score is not None else "Not Available"
62
+ return f"πŸŒ€ Latest Cycle:\nRecovery Score: {recovery_score}\n\nFull Data:\n{latest}"
63
  except Exception as e:
64
  return f"❌ Error: {e}"
65
 
66
  def get_average_strain_gr(days):
67
+ """
68
+ Calculates the average strain over a given number of past days.
69
+
70
+ Args:
71
+ days (int): Number of days to include in the average.
72
+
73
+ Returns:
74
+ str: Average strain value or an error message.
75
+ """
76
  if not whoop_client:
77
  return "❌ Not authenticated."
78
 
 
93
  except Exception as e:
94
  return f"❌ Error: {e}"
95
 
96
+ def format_latest_cycle(raw_text):
97
+ """
98
+ Formats the raw text returned by get_latest_cycle_gr for display.
99
 
100
+ Args:
101
+ raw_text (str): Raw output from get_latest_cycle_gr.
102
+
103
+ Returns:
104
+ Tuple[str, str]: A short summary and detailed cycle data.
105
+ """
106
+ if raw_text.startswith("❌") or raw_text.startswith("⚠️"):
107
+ return raw_text, ""
108
  try:
109
+ lines = raw_text.splitlines()
110
+ recovery_score_line = next((line for line in lines if "Recovery Score" in line), "Recovery Score: Not Available")
111
+ full_data = "\n".join(lines[2:]) # skip title and recovery line
112
+ return recovery_score_line, full_data
113
  except Exception as e:
114
+ return f"❌ Failed to parse cycle: {e}", ""
115
 
116
  # UI definition
117
  with gr.Blocks(title="Whoop API Explorer") as demo:
118
  gr.Markdown("# 🧠 WHOOP API Dashboard")
119
+ gr.Markdown("Easily authenticate and explore your WHOOP recovery and strain data.")
120
+
121
+ with gr.Group():
122
+ gr.Markdown("## πŸ” Authentication")
123
+ with gr.Row():
124
+ email_input = gr.Textbox(label="WHOOP Email", placeholder="[email protected]")
125
+ password_input = gr.Textbox(label="WHOOP Password", type="password", placeholder="β€’β€’β€’β€’β€’β€’β€’β€’")
126
+ auth_button = gr.Button("Authenticate")
127
+ auth_output = gr.Label(label="Auth Status")
128
+
129
+ with gr.Group():
130
+ gr.Markdown("## πŸ” Latest Recovery Cycle")
131
+ cycle_button = gr.Button("Fetch Latest Cycle")
132
+ latest_recovery = gr.Label(label="Recovery Score")
133
+ cycle_details = gr.Textbox(label="Full Cycle Data", visible=False, lines=6)
134
+
135
+ with gr.Group():
136
+ gr.Markdown("## πŸ“Š Strain Insights")
137
+ with gr.Row():
138
+ days_input = gr.Number(value=7, label="Number of Days", precision=0)
139
+ strain_button = gr.Button("Calculate Average Strain")
140
+ average_strain = gr.Label(label="Average Strain")
141
 
142
  # Bind actions
143
  auth_button.click(fn=initialize_whoop_client_with_input, inputs=[email_input, password_input], outputs=auth_output)
144
+ cycle_button.click(fn=lambda: format_latest_cycle(get_latest_cycle_gr()), outputs=[latest_recovery, cycle_details])
145
+ strain_button.click(fn=get_average_strain_gr, inputs=days_input, outputs=average_strain)
 
146
 
147
  # Launch app
148
  if __name__ == "__main__":