File size: 6,298 Bytes
7166038 fe90954 3b3718a 7166038 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
from typing import Dict, List
import os
import google.generativeai as genai
import re
class HealthRecommendations:
def __init__(self):
# Configure Google GenAI
api_key = os.getenv("gemini_api") # Fetch from environment variable
genai.configure(api_key=api_key)
def get_recommendations(self, patient_data: Dict, prediction: Dict) -> Dict[str, List[str]]:
"""Generate personalized health recommendations based on patient data and prediction"""
# Create a prompt for the LLM
prompt = self._create_prompt(patient_data, prediction)
try:
print("Generated prompt:", prompt) # Debugging: Check the prompt
model = genai.GenerativeModel("gemini-1.5-flash")
print("Model initialized successfully") # Debugging
response = model.generate_content(
contents=[{"parts": [{"text": prompt}]}],
generation_config=genai.types.GenerationConfig(temperature=0.7, max_output_tokens=500)
)
print("Response from API:", response) # Debugging: Check the response
if response.candidates and response.candidates[0].content.parts: # Check if response and parts exist
response_text = response.candidates[0].content.parts[0].text
recommendations = self._parse_recommendations(response_text)
return recommendations
else:
print("Unexpected response format from the API.")
return self._get_fallback_recommendations(prediction['is_diabetic'])
except Exception as e:
print(f"Error generating recommendations: {e}")
return self._get_fallback_recommendations(prediction['is_diabetic'])
def _create_prompt(self, patient_data: Dict, prediction: Dict) -> str:
"""Create a prompt for the LLM based on patient data"""
risk_level = "high" if prediction['probability'] > 0.7 else "moderate" if prediction['probability'] > 0.3 else "low"
prompt = f"""
Based on the following patient data:
- Glucose Level: {patient_data['Glucose']}
- Blood Pressure: {patient_data['BloodPressure']}
- BMI: {patient_data['BMI']}
- Age: {patient_data['Age']}
- Diabetes Risk Level: {risk_level}
Please provide specific recommendations in the following categories:
1. Diet and Nutrition
2. Physical Activity
3. Lifestyle Changes
4. Monitoring and Prevention
Make the recommendations specific to this patient's condition and risk level.
"""
return prompt
def _parse_recommendations(self, response: str) -> Dict[str, List[str]]:
"""Parse the LLM response into structured recommendations and remove all asterisks."""
# Categories we expect in the response
categories = ['Diet and Nutrition', 'Physical Activity', 'Lifestyle Changes', 'Monitoring and Prevention']
recommendations = {category: [] for category in categories}
# Assuming the text is extracted from the API response
api_response_text = response # This would be the text from the API, adjust based on the actual response structure
# Regex patterns to match categories and extract their associated recommendations
current_category = None
lines = api_response_text.split("\n")
for line in lines:
line = line.strip()
# Check if the line is a category
if any(category in line for category in categories):
for category in categories:
if category in line:
current_category = category
break
elif line and current_category:
# Remove all asterisks from the line
cleaned_line = re.sub(r'\*+', '', line).strip() # Remove all asterisks and leading/trailing spaces
if cleaned_line: # Add only non-empty lines
recommendations[current_category].append(cleaned_line)
return recommendations
def _get_fallback_recommendations(self, is_diabetic: bool) -> Dict[str, List[str]]:
"""Provide fallback recommendations if API call fails"""
if is_diabetic:
return {
'1.Diet and Nutrition': [
'Monitor carbohydrate intake and follow a balanced diet',
'Eat plenty of vegetables and whole grains',
'Limit sugary foods and beverages'
],
'Physical Activity': [
'Aim for 150 minutes of moderate exercise per week',
'Include both aerobic and strength training exercises',
'Take regular walking breaks during the day'
],
'Lifestyle Changes': [
'Monitor blood sugar regularly',
'Maintain a healthy sleep schedule',
'Manage stress through relaxation techniques'
],
'Monitoring and Prevention': [
'Regular check-ups with healthcare provider',
'Keep track of blood sugar levels',
'Monitor blood pressure and weight.1'
]
}
else:
return {
'2.Diet and Nutrition': [
'Follow a balanced diet rich in whole foods',
'Limit processed foods and added sugars',
'Stay hydrated with water'
],
'Physical Activity': [
'Regular exercise for 30 minutes daily',
'Include variety in your workout routine',
'Stay active throughout the day'
],
'Lifestyle Changes': [
'Maintain a healthy weight',
'Get adequate sleep',
'Practice stress management'
],
'Monitoring and Prevention': [
'Regular health check-ups',
'Annual blood sugar screening',
'Monitor weight and blood pressure.2'
]
}
|