Files changed (1) hide show
  1. app.py +163 -47
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
  from sentence_transformers import SentenceTransformer
4
  import torch
5
  import numpy as np
 
6
 
7
- # Load and process the knowledge base
8
  with open("knowledge.txt", "r", encoding="utf-8") as f:
9
  knowledge_text = f.read()
10
  chunks = [chunk.strip() for chunk in knowledge_text.split("\n\n") if chunk.strip()]
@@ -17,7 +19,7 @@ def get_relevant_context(query, top_k=3):
17
  norm_chunk_embeddings = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
18
  similarities = torch.matmul(norm_chunk_embeddings, query_embedding)
19
  top_k_indices = torch.topk(similarities, k=top_k).indices.cpu().numpy()
20
- context = "\n\n".join([chunks[i] for i in top_k_indices])
21
  return context
22
 
23
  client = InferenceClient("google/gemma-2-2b-it")
@@ -28,14 +30,18 @@ Cycle-Aware Wellness AI Coach (Strict Enforcement Version)
28
  Mission:
29
  --------
30
  You are a compassionate and knowledgeable wellness coach who specializes in fitness aligned with the menstrual cycle and evidence-based contraceptive education. Your mission is to empower women to understand their bodies, support their fitness and reproductive health, and make informed, cycle-aware choices.
 
31
  You may *only* respond to questions related to:
32
  - Menstrual cycles and hormonal phases
33
  - Cycle-based fitness and wellness programming
34
  - Contraceptive methods and reproductive health education
35
  - Hormonal syndromes or life stages (PCOS, PMDD, menopause, irregular cycles, etc.)
36
- If the user asks something unrelated (e.g., fixing a car, meal prep, unrelated illnesses, tech support), immediately respond with the following message:
 
37
  "I'm here to help with cycle-based fitness and contraceptive wellness. That question’s outside my scope, but I’d love to support you with anything related to your body, cycle, or health goals!"
 
38
  You must *never* attempt to answer off-topic requests, even if the user insists or rephrases. Always redirect the conversation back to wellness, hormones, fitness, or reproductive health.
 
39
  Style and Voice Guidelines:
40
  ---------------------------
41
  - Speak like a friendly, knowledgeable older sister who’s also a certified personal trainer and women’s health educator.
@@ -43,59 +49,169 @@ Style and Voice Guidelines:
43
  - Validate the user’s experience before offering guidance (e.g., “That makes total sense—your energy might be shifting in this phase”).
44
  - Encourage autonomy by offering options, not orders.
45
  - Connect advice to real-life impact: how the cycle affects energy, mood, and performance.
46
- Core Support Areas (Scope You *Do* Cover):
47
- ------------------------------------------
48
- 1. Cycle-Based Training:
49
- - Explain hormonal phases (Menstrual, Follicular, Ovulation, Luteal).
50
- - Suggest workouts aligned to the user’s cycle and fitness level.
51
- - Adjust for users on birth control, with hormone conditions, or in life stages like menopause or postpartum.
52
- 2. Contraceptive Education:
53
- - Educate on birth control methods (pill, IUD, implant, etc.).
54
- - Explain how contraception affects workouts, mood, metabolism.
55
- - Support users interested in natural tracking or switching methods.
56
- 3. Wellness Support:
57
- - Help with stress, motivation, sleep, inflammation, and hormone-related fatigue or soreness.
58
- - Normalize fluctuations and setbacks.
59
- - Offer encouragement when users feel overwhelmed, bloated, or inconsistent.
60
  Final Boundary Rule:
61
  --------------------
62
- 🔒 Strictly decline all unrelated questions. Your only purpose is cycle-aware fitness and reproductive wellness coaching. Do not give general medical, tech, legal, or life advice. You can still give advice relating to helping symptoms like home remedies. No actual medical advice, but recipes are fine.
63
  """
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def respond(message, history):
 
 
 
66
  messages = [{"role": "system", "content": cycle_ai_prompt}]
67
- if history:
68
- for user_msg, assistant_msg in history:
69
- messages.append({"role": "user", "content": user_msg})
70
- messages.append({"role": "assistant", "content": assistant_msg})
71
  messages.append({"role": "user", "content": message})
 
72
  response = client.chat_completion(
73
  messages,
74
  max_tokens=500,
75
  temperature=0.1
76
  )
77
- return response['choices'][0]['message']['content'].strip()
78
-
79
- def custom_chat_ui():
80
- with gr.Blocks(
81
- theme=gr.themes.Soft(
82
- primary_hue="pink",
83
- secondary_hue="orange",
84
- neutral_hue="yellow",
85
- spacing_size="lg",
86
- radius_size="lg", # FIXED: was "xl"
87
- font=[gr.themes.GoogleFont("Quicksand"), "sans-serif"],
88
- font_mono=[gr.themes.GoogleFont("IBM Plex Mono"), "monospace"]
89
- )
90
- ) as demo:
91
- gr.Image(
92
- show_label=False,
93
- show_share_button=False,
94
- show_download_button=False,
95
- container=False,
96
- height=200
97
- )
98
- gr.ChatInterface(respond, title="🌸 Cycle-Aware Wellness Coach 🌞")
99
- return demo
100
-
101
- custom_chat_ui().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import os
4
  from sentence_transformers import SentenceTransformer
5
  import torch
6
  import numpy as np
7
+ from datetime import datetime, timedelta
8
 
9
+ # Load knowledge base
10
  with open("knowledge.txt", "r", encoding="utf-8") as f:
11
  knowledge_text = f.read()
12
  chunks = [chunk.strip() for chunk in knowledge_text.split("\n\n") if chunk.strip()]
 
19
  norm_chunk_embeddings = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
20
  similarities = torch.matmul(norm_chunk_embeddings, query_embedding)
21
  top_k_indices = torch.topk(similarities, k=top_k).indices.cpu().numpy()
22
+ context = "\n\n".join([chunks[i]] for i in top_k_indices)
23
  return context
24
 
25
  client = InferenceClient("google/gemma-2-2b-it")
 
30
  Mission:
31
  --------
32
  You are a compassionate and knowledgeable wellness coach who specializes in fitness aligned with the menstrual cycle and evidence-based contraceptive education. Your mission is to empower women to understand their bodies, support their fitness and reproductive health, and make informed, cycle-aware choices.
33
+
34
  You may *only* respond to questions related to:
35
  - Menstrual cycles and hormonal phases
36
  - Cycle-based fitness and wellness programming
37
  - Contraceptive methods and reproductive health education
38
  - Hormonal syndromes or life stages (PCOS, PMDD, menopause, irregular cycles, etc.)
39
+
40
+ If the user asks something unrelated (e.g., fixing a car, meal prep, unrelated illnesses, tech support), immediately respond with:
41
  "I'm here to help with cycle-based fitness and contraceptive wellness. That question’s outside my scope, but I’d love to support you with anything related to your body, cycle, or health goals!"
42
+
43
  You must *never* attempt to answer off-topic requests, even if the user insists or rephrases. Always redirect the conversation back to wellness, hormones, fitness, or reproductive health.
44
+
45
  Style and Voice Guidelines:
46
  ---------------------------
47
  - Speak like a friendly, knowledgeable older sister who’s also a certified personal trainer and women’s health educator.
 
49
  - Validate the user’s experience before offering guidance (e.g., “That makes total sense—your energy might be shifting in this phase”).
50
  - Encourage autonomy by offering options, not orders.
51
  - Connect advice to real-life impact: how the cycle affects energy, mood, and performance.
52
+
53
+ When the user asks about workouts, always ask a follow-up:
54
+ "What kind of equipment or space do you have access to—like a gym, home weights, or just bodyweight? I’ll tailor a workout for you based on that!"
55
+
 
 
 
 
 
 
 
 
 
 
56
  Final Boundary Rule:
57
  --------------------
58
+ Strictly decline all unrelated questions. Your only purpose is cycle-aware fitness and reproductive wellness coaching. Do not give general medical, tech, cooking, legal, or life advice.
59
  """
60
 
61
+ def determine_cycle_phase(start_date_str):
62
+ try:
63
+ start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
64
+ days_since = (datetime.now() - start_date).days % 28
65
+ if days_since < 5:
66
+ return "Menstrual Phase", "💗 Time to rest and recover. Gentle movement like stretching or walking is great."
67
+ elif days_since < 13:
68
+ return "Follicular Phase", "💪 Your energy’s building—go for strength training or cardio!"
69
+ elif days_since < 16:
70
+ return "Ovulation Phase", "🔥 Peak power! Try high-intensity workouts or social activities."
71
+ else:
72
+ return "Luteal Phase", "�� Wind down. Opt for lighter training, yoga, or bodyweight exercises."
73
+ except:
74
+ return "Unknown Phase", "Couldn't parse the date. Please use YYYY-MM-DD."
75
+
76
  def respond(message, history):
77
+ if not isinstance(history, list):
78
+ history = []
79
+
80
  messages = [{"role": "system", "content": cycle_ai_prompt}]
81
+ for entry in history:
82
+ if isinstance(entry, dict):
83
+ messages.append(entry)
 
84
  messages.append({"role": "user", "content": message})
85
+
86
  response = client.chat_completion(
87
  messages,
88
  max_tokens=500,
89
  temperature=0.1
90
  )
91
+
92
+ print("DEBUG RESPONSE:", response) # <-- Add this to inspect
93
+
94
+ # Now adapt based on actual response
95
+ try:
96
+ assistant_reply = response['choices'][0]['message']['content'].strip()
97
+ except Exception as e:
98
+ assistant_reply = f"⚠️ There was an error processing the response: {e}"
99
+
100
+ new_history = history + [
101
+ {"role": "user", "content": message},
102
+ {"role": "assistant", "content": assistant_reply}
103
+ ]
104
+ return new_history, new_history, ""
105
+
106
+ def update_chatbot(user_message, history):
107
+ return respond(user_message, history)
108
+
109
+ def set_user_info(name, age, level, period_start_date, period_end_date):
110
+ phase, tip = determine_cycle_phase(period_start_date)
111
+ greeting = f"Hi {name}! I'm here to help you with cycle-aware fitness and wellness.\n\nYou’re {age} years old, training at a {level.lower()} level, and your last period started on {period_start_date} and ended on {period_end_date}.\n\nRight now, you’re likely in your **{phase}**. {tip} 💞\n\nAsk me anything about your body, cycle, or contraceptive health!"
112
+ return name, [{"role": "assistant", "content": greeting}]
113
+
114
+ def button_click(question, history):
115
+ new_history, updated_history, _ = respond(question, history)
116
+ return new_history, updated_history
117
+
118
+ with gr.Blocks(
119
+ css="""
120
+ .gradio-container {
121
+ background: linear-gradient(135deg, #F6D365 0%, #FDA085 50%, #FF8FA0 100%);
122
+ font-family: 'Quicksand', sans-serif;
123
+ }
124
+ .message.user {
125
+ background-color: lightpink;
126
+ border-radius: 20px;
127
+ padding: 10px;
128
+ margin: 5px;
129
+ max-width: 75%;
130
+ align-self: flex-end;
131
+ }
132
+ .message.bot {
133
+ background-color: lightcoral;
134
+ border-radius: 20px;
135
+ padding: 10px;
136
+ margin: 5px;
137
+ max-width: 75%;
138
+ align-self: flex-start;
139
+ }
140
+ .chat-interface {
141
+ background-color: peachpuff;
142
+ border-radius: 12px;
143
+ padding: 15px;
144
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
145
+ }
146
+
147
+ #banner-image {
148
+ background-color: transparent;
149
+ margin-bottom: -100px;
150
+
151
+ }
152
+ """,
153
+ theme=gr.themes.Soft(
154
+ primary_hue="pink",
155
+ secondary_hue="orange",
156
+ neutral_hue="yellow",
157
+ spacing_size="lg",
158
+ radius_size="lg",
159
+ font=[gr.themes.GoogleFont("Quicksand"), "sans-serif"],
160
+ font_mono=[gr.themes.GoogleFont("IBM Plex Mono"), "monospace"]
161
+ )
162
+ ) as demo:
163
+ gr.Image(
164
+ value="/content/Untitled design.png",
165
+ show_label=False,
166
+ show_share_button = False,
167
+ show_download_button = False,
168
+ elem_id="banner-image")
169
+
170
+ name_state = gr.State("")
171
+ chat_history = gr.State([])
172
+
173
+ with gr.Row():
174
+ with gr.Column(scale=1):
175
+ gr.Markdown("### Tell me about yourself")
176
+ name_input = gr.Textbox(label="Name", placeholder="Your name…")
177
+ age_input = gr.Textbox(label="Age", placeholder="Your age…")
178
+ level_input = gr.Dropdown(choices=["Beginner", "Intermediate", "Expert"], label="Training Level")
179
+ period_start_input = gr.Textbox(label="Last Period Start Date", placeholder="YYYY-MM-DD")
180
+ period_end_input = gr.Textbox(label="Last Period End Date", placeholder="YYYY-MM-DD")
181
+ set_btn = gr.Button("Set Info")
182
+ gr.Markdown("_After the greeting appears, start chatting →_")
183
+
184
+ with gr.Column(scale=2):
185
+ chatbot = gr.Chatbot(label="Chat", type="messages")
186
+ user_text = gr.Textbox(placeholder="Ask me something…", label="")
187
+ with gr.Row():
188
+ workout_btn = gr.Button("Prompt: Workouts")
189
+ contraceptive_btn = gr.Button("Prompt: Contraceptives")
190
+
191
+ set_btn.click(
192
+ fn=set_user_info,
193
+ inputs=[name_input, age_input, level_input, period_start_input, period_end_input],
194
+ outputs=[name_state, chatbot],
195
+ show_progress=False,
196
+ )
197
+
198
+ user_text.submit(
199
+ fn=update_chatbot,
200
+ inputs=[user_text, chat_history],
201
+ outputs=[chatbot, chat_history, user_text]
202
+ )
203
+
204
+ workout_btn.click(
205
+ fn=lambda history: button_click("What workout routine would you recommend based on my cycle phase?", history),
206
+ inputs=[chat_history],
207
+ outputs=[chatbot, chat_history]
208
+ )
209
+
210
+ contraceptive_btn.click(
211
+ fn=lambda history: button_click("Can you explain the different contraceptive options and their benefits?", history),
212
+ inputs=[chat_history],
213
+ outputs=[chatbot, chat_history]
214
+ )
215
+
216
+ if __name__ == '__main__':
217
+ demo.launch()