File size: 4,707 Bytes
8e315dc
c4587cb
8e315dc
c4587cb
e3e8ff9
c4587cb
 
 
8e315dc
7877a46
c4587cb
7877a46
c4587cb
 
 
 
 
 
 
 
 
 
 
8e315dc
982b8ff
 
 
 
 
 
 
 
 
7877a46
b30668d
 
7877a46
e3e8ff9
7877a46
01ec88d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b30668d
01ec88d
 
 
 
 
 
b30668d
01ec88d
 
7877a46
b30668d
01ec88d
b30668d
 
8e315dc
b30668d
9e67cb5
8e315dc
 
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
import streamlit as st
from groq import Groq

# Groq API Key (replace with your actual API key)
groq_api_key = "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"

# Initialize Groq client
client = Groq(api_key=groq_api_key)

# Function to fetch personalized advice using Groq API
def get_personalized_advice(user_data):
    """Fetch personalized advice using the Groq API based on user data."""
    query = f"Based on the user's data: anxiety level: {user_data['anxiety_level']}, self esteem: {user_data['self_esteem']}, mental health history: {user_data['mental_health_history']}, stress level: {user_data['stress_level']}, provide personalized mental health advice."
    
    try:
        response = client.chat.completions.create(
            messages=[{"role": "user", "content": query}],
            model="llama-3.3-70b-versatile",
        )
        return response.choices[0].message.content
    except Exception as e:
        st.error(f"Error fetching advice from Groq: {str(e)}")
        return None

def stress_level_to_string(stress_level):
    """Convert numerical stress level (0, 1, 2) to a string representation."""
    if stress_level == 0:
        return "Low"
    elif stress_level == 1:
        return "Moderate"
    else:
        return "High"

# Chatbot interface for conversation-like experience
def chatbot_interface():
    st.title("πŸŽ“ Student Well-being Chatbot")
    st.write("Hello! I’m here to assist you with well-being advice based on your responses.")

    # Initialize session state
    if 'messages' not in st.session_state:
        st.session_state.messages = [
            {"role": "bot", "content": "Hi there! I'm here to help with your well-being. Let's start! 😊"}
        ]
    
    def next_step(user_input=None):
        # Save the user's message to the chat history
        if user_input:
            st.session_state.messages.append({"role": "user", "content": user_input})
        
        # Proceed to next step in the conversation
        if len(st.session_state.messages) == 1:
            st.session_state.messages.append({"role": "bot", "content": "On a scale of 1 to 10, how would you rate your anxiety level?"})
        elif len(st.session_state.messages) == 2:
            anxiety_level = int(user_input)
            st.session_state.user_data = {'anxiety_level': anxiety_level}
            st.session_state.messages.append({"role": "bot", "content": f"Got it! Your anxiety level is {anxiety_level}. Now, on a scale of 1 to 10, how would you rate your self-esteem?"})
        elif len(st.session_state.messages) == 3:
            self_esteem = int(user_input)
            st.session_state.user_data['self_esteem'] = self_esteem
            st.session_state.messages.append({"role": "bot", "content": f"Got it! Your self-esteem level is {self_esteem}. How would you describe your mental health history?"})
        elif len(st.session_state.messages) == 4:
            st.session_state.user_data['mental_health_history'] = user_input
            st.session_state.messages.append({"role": "bot", "content": "Thanks! Now, on a scale of 0 to 2, how would you rate your stress level?"})
        elif len(st.session_state.messages) == 5:
            stress_level = int(user_input)
            st.session_state.user_data['stress_level'] = stress_level
            st.session_state.messages.append({"role": "bot", "content": "Thank you for providing the information! Let me get your personalized advice..."})
            # Get the advice from Groq API
            advice = get_personalized_advice(st.session_state.user_data)
            st.session_state.messages.append({"role": "bot", "content": advice or "I couldn't retrieve specific advice right now, but please check back later."})
        elif len(st.session_state.messages) == 6:
            st.session_state.messages.append({"role": "bot", "content": "Would you like to start over?"})

    # Display conversation history
    for message in st.session_state.messages:
        if message['role'] == 'bot':
            st.markdown(f"**Bot**: {message['content']}")
        else:
            st.markdown(f"**You**: {message['content']}")

    # User input field for chat interaction
    user_input = st.text_input("Your message:", "")
    if user_input:
        next_step(user_input)

    # Option to restart the conversation
    if len(st.session_state.messages) >= 6:
        restart_button = st.button("Start Over")
        if restart_button:
            st.session_state.messages = [{"role": "bot", "content": "Hi there! I'm here to help with your well-being. Let's start! 😊"}]

# Main function to run the chatbot
def main():
    chatbot_interface()

if __name__ == "__main__":
    main()