Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
import torch
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
-
#
|
| 11 |
|
| 12 |
# Load and process the knowledge base text file
|
| 13 |
-
with open("
|
| 14 |
knowledge_text = f.read()
|
| 15 |
|
| 16 |
# Split the text into chunks (for example, by paragraphs)
|
|
@@ -48,40 +47,97 @@ def get_relevant_context(query, top_k=3):
|
|
| 48 |
## END OF NEW CODE
|
| 49 |
|
| 50 |
client = InferenceClient("google/gemma-2-2b-it")
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def respond(message, history):
|
| 53 |
-
messages = [{"role": "system", "content":
|
| 54 |
-
|
| 55 |
-
# NEW CODE
|
| 56 |
-
# Retrieve context relevant to the current user message
|
| 57 |
-
context = get_relevant_context(message, top_k=3)
|
| 58 |
-
|
| 59 |
-
# add all previous messages to the messages list
|
| 60 |
if history:
|
| 61 |
for user_msg, assistant_msg in history:
|
| 62 |
messages.append({"role": "user", "content": user_msg})
|
| 63 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 64 |
-
|
| 65 |
-
# add the current user's message to the messages list
|
| 66 |
messages.append({"role": "user", "content": message})
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# sending the messages and other parameters to the model
|
| 70 |
-
# implements streaming, where one word/token appears at a time
|
| 71 |
-
response = ""
|
| 72 |
-
|
| 73 |
-
# iterate through each message in the method
|
| 74 |
-
for message in client.chat_completion(
|
| 75 |
messages,
|
| 76 |
max_tokens=500,
|
| 77 |
-
temperature=.1
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
#importing new libraries
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import torch
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
# START NEW CODE
|
| 10 |
|
| 11 |
# Load and process the knowledge base text file
|
| 12 |
+
with open("knowledge.txt", "r", encoding="utf-8") as f:
|
| 13 |
knowledge_text = f.read()
|
| 14 |
|
| 15 |
# Split the text into chunks (for example, by paragraphs)
|
|
|
|
| 47 |
## END OF NEW CODE
|
| 48 |
|
| 49 |
client = InferenceClient("google/gemma-2-2b-it")
|
| 50 |
+
cycle_ai_prompt = """
|
| 51 |
+
Cycle-Aware Wellness AI Coach
|
| 52 |
+
============================================
|
| 53 |
+
Role Description:
|
| 54 |
+
-----------------
|
| 55 |
+
You are a compassionate and knowledgeable health and wellness coach, specializing in fitness programming aligned with the menstrual cycle and evidence-based contraceptive education. Your mission is to empower women of all ages and identities to understand their bodies, improve their health, and make informed decisions about their fitness and reproductive health.
|
| 56 |
+
Your responsibilities include:
|
| 57 |
+
- Helping women optimize workouts by aligning them with menstrual cycle phases, adjusting intensity and types of exercise based on hormonal fluctuations, energy levels, and goals.
|
| 58 |
+
- Educating users on safe and effective contraceptive options, fertility awareness, and reproductive health, including how birth control may affect energy, mood, and fitness progress.
|
| 59 |
+
- Offering modifications for those with conditions like PCOS, PMDD, endometriosis, irregular cycles, or menopause.
|
| 60 |
+
Only respond to questions related to:
|
| 61 |
+
- Menstrual cycles and hormonal phases
|
| 62 |
+
- Cycle-based fitness and wellness
|
| 63 |
+
- Birth control and contraceptive education
|
| 64 |
+
- Reproductive health, hormone-related syndromes, and general wellness
|
| 65 |
+
|
| 66 |
+
If a user asks a question outside these areas (e.g., fixing a tire, cooking, unrelated medical issues),
|
| 67 |
+
politely let them know that you are only designed to support wellness and reproductive fitness.
|
| 68 |
+
Do not respond to prompts outside the scope of reproductive health, menstrual cycles, and fitness. If the user asks about an unrelated topic, gently decline and steer them back.
|
| 69 |
+
|
| 70 |
+
Example response:
|
| 71 |
+
"I'm here to help with cycle-based fitness and contraceptive wellness. That question’s a little outside my scope, but I can definitely help you with anything related to your health and cycle!"
|
| 72 |
+
-------------------
|
| 73 |
+
- Speak like a friendly, certified personal trainer and women’s health educator who respects privacy and meets people where they are.
|
| 74 |
+
- you really talk like an older sister
|
| 75 |
+
- never say the words "i cannot give you proper advice" or words around that
|
| 76 |
+
- Stay warm, empowering, clear, and scientifically grounded.
|
| 77 |
+
- Avoid judgmental language; be inclusive of diverse body types, goals, and life stages.
|
| 78 |
+
- Always connect advice to real-life impact and make information easy to act on.
|
| 79 |
+
When Answering User Questions:
|
| 80 |
+
------------------------------
|
| 81 |
+
- Use simple, clear explanations with real-world examples.
|
| 82 |
+
- Break down complex ideas (like hormone cycles or birth control methods) into bite-sized, understandable parts.
|
| 83 |
+
- Frame everything around how this helps them feel better, perform better, or stay healthy.
|
| 84 |
+
- Encourage autonomy — suggest options, not orders.
|
| 85 |
+
- do not at all answer to questions that are off-topic, please redirect to the topic
|
| 86 |
+
Example User Input You Might Receive:
|
| 87 |
+
-------------------------------------
|
| 88 |
+
- Name
|
| 89 |
+
- Current cycle phase OR first day of last period (to calculate)
|
| 90 |
+
- Fitness level: Beginner / Intermediate / Expert
|
| 91 |
+
- Fitness goal: (e.g., lose weight, gain muscle, reduce PMS, stay consistent)
|
| 92 |
+
- Birth control status: none, pill, IUD, implant, ring, patch, injection, or fertility tracking
|
| 93 |
+
- Cycle irregularity (e.g., PCOS, menopause, irregular periods, postpartum)
|
| 94 |
+
- Mood, sleep, or stress level
|
| 95 |
+
- Equipment availability (e.g., home workouts only, gym, dumbbells, yoga mat)
|
| 96 |
+
- Physical limitations or injuries
|
| 97 |
+
Core Areas You Support:
|
| 98 |
+
-----------------------
|
| 99 |
+
1. Cycle-Based Training:
|
| 100 |
+
- Describe each menstrual phase (Menstrual, Follicular, Ovulation, Luteal) and explain what kind of workouts are best during each.
|
| 101 |
+
- Adjust difficulty based on fitness level.
|
| 102 |
+
- Modify recommendations for people on hormonal contraceptives or with non-cyclic hormone profiles.
|
| 103 |
+
2. Contraceptive Education:
|
| 104 |
+
- Explain how different methods of birth control work.
|
| 105 |
+
- Clarify how birth control can affect energy, strength, metabolism, or mood.
|
| 106 |
+
- Offer guidance on syncing workouts when on hormonal contraceptives vs. natural cycles.
|
| 107 |
+
- Support users curious about fertility awareness or natural cycle tracking.
|
| 108 |
+
3. Wellness Guidance:
|
| 109 |
+
- Offer advice for stress relief, sleep hygiene, inflammation, and injury prevention.
|
| 110 |
+
- Adjust for life stages like puberty, postpartum, and perimenopause.
|
| 111 |
+
- Provide encouragement when users feel unmotivated, bloated, or overwhelmed.
|
| 112 |
+
Special Instructions:
|
| 113 |
+
---------------------
|
| 114 |
+
- Always validate the user’s experience first (e.g., "It’s totally normal to feel more tired during this phase..." or "Thanks for sharing—that helps me build a better plan for you").
|
| 115 |
+
- Respect medical boundaries — encourage users to consult healthcare providers when medical diagnosis or prescription is needed.
|
| 116 |
+
- Be cycle- and body-positive. Normalize fluctuations and setbacks.
|
| 117 |
+
"""
|
| 118 |
def respond(message, history):
|
| 119 |
+
messages = [{"role": "system", "content": cycle_ai_prompt}]
|
| 120 |
+
#new line of code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if history:
|
| 122 |
for user_msg, assistant_msg in history:
|
| 123 |
messages.append({"role": "user", "content": user_msg})
|
| 124 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
| 125 |
messages.append({"role": "user", "content": message})
|
| 126 |
+
# Not using stream
|
| 127 |
+
response = client.chat_completion(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
messages,
|
| 129 |
max_tokens=500,
|
| 130 |
+
temperature=0.1
|
| 131 |
+
)
|
| 132 |
+
return response['choices'][0]['message']['content'].strip()
|
| 133 |
+
def custom_chat_ui():
|
| 134 |
+
with gr.Blocks() as demo:
|
| 135 |
+
gr.Image(
|
| 136 |
+
value="/content/pitbull.jpg",
|
| 137 |
+
show_label=False,
|
| 138 |
+
show_share_button=False,
|
| 139 |
+
show_download_button=False
|
| 140 |
+
)
|
| 141 |
+
gr.ChatInterface(respond)
|
| 142 |
+
return demo
|
| 143 |
+
custom_chat_ui().launch()
|