Update whoop_gradio_server.py
Browse files- 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"
|
44 |
-
|
|
|
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
|
70 |
-
|
71 |
-
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
try:
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
except Exception as e:
|
77 |
-
return f"β
|
78 |
|
79 |
# UI definition
|
80 |
with gr.Blocks(title="Whoop API Explorer") as demo:
|
81 |
gr.Markdown("# π§ WHOOP API Dashboard")
|
82 |
-
gr.Markdown("
|
83 |
-
|
84 |
-
with gr.
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
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=
|
106 |
-
strain_button.click(fn=get_average_strain_gr, inputs=days_input, outputs=
|
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__":
|