Create chatbot_wellness.py
Browse files- chatbot_wellness.py +67 -0
chatbot_wellness.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# File to store user preferences
|
| 5 |
+
PREFERENCES_FILE = "user_preferences.json"
|
| 6 |
+
|
| 7 |
+
# Load or create user preferences file
|
| 8 |
+
def load_preferences():
|
| 9 |
+
if os.path.exists(PREFERENCES_FILE):
|
| 10 |
+
with open(PREFERENCES_FILE, "r") as file:
|
| 11 |
+
return json.load(file)
|
| 12 |
+
return {}
|
| 13 |
+
|
| 14 |
+
def save_preferences(preferences):
|
| 15 |
+
with open(PREFERENCES_FILE, "w") as file:
|
| 16 |
+
json.dump(preferences, file, indent=4)
|
| 17 |
+
|
| 18 |
+
# Initialize preferences
|
| 19 |
+
user_preferences = load_preferences()
|
| 20 |
+
|
| 21 |
+
# Function to get user preferences
|
| 22 |
+
def get_user_preferences(user_id):
|
| 23 |
+
return user_preferences.get(user_id, {"name": None, "style": None})
|
| 24 |
+
|
| 25 |
+
# Function to update user preferences
|
| 26 |
+
def update_user_preferences(user_id, name=None, style=None):
|
| 27 |
+
if user_id not in user_preferences:
|
| 28 |
+
user_preferences[user_id] = {}
|
| 29 |
+
if name:
|
| 30 |
+
user_preferences[user_id]["name"] = name
|
| 31 |
+
if style:
|
| 32 |
+
user_preferences[user_id]["style"] = style
|
| 33 |
+
save_preferences(user_preferences)
|
| 34 |
+
|
| 35 |
+
# Function to provide advice based on tone
|
| 36 |
+
def generate_advice(user_style, user_name):
|
| 37 |
+
advice_pool = {
|
| 38 |
+
"gentleness": f"{user_name}, remember that progress takes time. Be kind to yourself as you grow.",
|
| 39 |
+
"tough love": f"Listen, {user_name}, no excuses! You’ve got what it takes—so get to it!",
|
| 40 |
+
"words of affirmation": f"You’re amazing, {user_name}. Believe in yourself; you’re capable of incredible things.",
|
| 41 |
+
}
|
| 42 |
+
return advice_pool.get(user_style, "I'm here to support you in the way that works best for you!")
|
| 43 |
+
|
| 44 |
+
# Main chatbot response function
|
| 45 |
+
def wellness_bot_response(user_id, user_input):
|
| 46 |
+
preferences = get_user_preferences(user_id)
|
| 47 |
+
if not preferences["name"] or not preferences["style"]:
|
| 48 |
+
# Collect user preferences if not set
|
| 49 |
+
if not preferences["name"]:
|
| 50 |
+
preferences["name"] = user_input # Assume input is the user's name
|
| 51 |
+
update_user_preferences(user_id, name=preferences["name"])
|
| 52 |
+
return f"Hi {preferences['name']}! What motivational style do you prefer? (e.g., gentleness, tough love, words of affirmation)"
|
| 53 |
+
elif not preferences["style"]:
|
| 54 |
+
preferences["style"] = user_input.lower() # Assume input is the style
|
| 55 |
+
update_user_preferences(user_id, style=preferences["style"])
|
| 56 |
+
return f"Got it, {preferences['name']}! I’ll use a {preferences['style']} approach from now on. How can I support you today?"
|
| 57 |
+
else:
|
| 58 |
+
# Provide advice based on preferences
|
| 59 |
+
advice = generate_advice(preferences["style"], preferences["name"])
|
| 60 |
+
return f"{advice}\nLet me know if there’s anything specific on your mind!"
|
| 61 |
+
|
| 62 |
+
# Example usage
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
user_id = "user123" # Replace with a unique identifier for each user
|
| 65 |
+
print(wellness_bot_response(user_id, "Alice")) # First input (name)
|
| 66 |
+
print(wellness_bot_response(user_id, "gentleness")) # Second input (style)
|
| 67 |
+
print(wellness_bot_response(user_id, "I need help staying motivated.")) # Third input (advice request)
|