try / chatbot_wellness.py
asiaao's picture
Create chatbot_wellness.py
17af28e verified
import json
import os
# File to store user preferences
PREFERENCES_FILE = "user_preferences.json"
# Load or create user preferences file
def load_preferences():
if os.path.exists(PREFERENCES_FILE):
with open(PREFERENCES_FILE, "r") as file:
return json.load(file)
return {}
def save_preferences(preferences):
with open(PREFERENCES_FILE, "w") as file:
json.dump(preferences, file, indent=4)
# Initialize preferences
user_preferences = load_preferences()
# Function to get user preferences
def get_user_preferences(user_id):
return user_preferences.get(user_id, {"name": None, "style": None})
# Function to update user preferences
def update_user_preferences(user_id, name=None, style=None):
if user_id not in user_preferences:
user_preferences[user_id] = {}
if name:
user_preferences[user_id]["name"] = name
if style:
user_preferences[user_id]["style"] = style
save_preferences(user_preferences)
# Function to provide advice based on tone
def generate_advice(user_style, user_name):
advice_pool = {
"gentleness": f"{user_name}, remember that progress takes time. Be kind to yourself as you grow.",
"tough love": f"Listen, {user_name}, no excuses! You’ve got what it takes—so get to it!",
"words of affirmation": f"You’re amazing, {user_name}. Believe in yourself; you’re capable of incredible things.",
}
return advice_pool.get(user_style, "I'm here to support you in the way that works best for you!")
# Main chatbot response function
def wellness_bot_response(user_id, user_input):
preferences = get_user_preferences(user_id)
if not preferences["name"] or not preferences["style"]:
# Collect user preferences if not set
if not preferences["name"]:
preferences["name"] = user_input # Assume input is the user's name
update_user_preferences(user_id, name=preferences["name"])
return f"Hi {preferences['name']}! What motivational style do you prefer? (e.g., gentleness, tough love, words of affirmation)"
elif not preferences["style"]:
preferences["style"] = user_input.lower() # Assume input is the style
update_user_preferences(user_id, style=preferences["style"])
return f"Got it, {preferences['name']}! I’ll use a {preferences['style']} approach from now on. How can I support you today?"
else:
# Provide advice based on preferences
advice = generate_advice(preferences["style"], preferences["name"])
return f"{advice}\nLet me know if there’s anything specific on your mind!"
# Example usage
if __name__ == "__main__":
user_id = "user123" # Replace with a unique identifier for each user
print(wellness_bot_response(user_id, "Alice")) # First input (name)
print(wellness_bot_response(user_id, "gentleness")) # Second input (style)
print(wellness_bot_response(user_id, "I need help staying motivated.")) # Third input (advice request)