saherPervaiz commited on
Commit
af0229e
Β·
verified Β·
1 Parent(s): e30d813

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -96
app.py CHANGED
@@ -1,114 +1,63 @@
1
  import streamlit as st
 
2
  from groq import Groq
3
 
4
- # Replace with your Groq API key
5
- groq_api_key = "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"
6
 
7
  # Initialize Groq client
8
- groq_client = Groq(api_key=groq_api_key)
9
 
10
- # Function to generate personalized advice based on user input
11
- def get_personalized_advice(user_data):
12
- query = f"""
13
- Based on the following information about the user:
14
- Anxiety Level: {user_data['anxiety_level']}
15
- Self-Esteem Level: {user_data['self_esteem']}
16
- Mental Health History: {user_data['mental_health_history']}
17
- Stress Level: {user_data['stress_level']}
18
-
19
- Please provide personalized advice for the user's well-being.
20
- """
21
  try:
22
- response = groq_client.chat.completions.create(
 
23
  messages=[{"role": "user", "content": query}],
24
- model="llama-3.3-70b-versatile" # Use the appropriate model
25
  )
26
- return response.choices[0].message.content
 
 
27
  except Exception as e:
28
- st.error(f"Error: {str(e)}")
29
- return "Sorry, I couldn't retrieve personalized advice at the moment."
30
-
31
- # Function to display the chatbot interface
32
- def chatbot_interface():
33
- st.set_page_config(page_title="Student Well-being Chatbot", page_icon="πŸŽ“", layout="centered")
34
- st.title("πŸŽ“ Professional Student Well-being Chatbot")
35
- st.markdown("""
36
- #### Welcome! Let's assess your well-being and provide some helpful advice.
37
- Fill in the details below, and we'll tailor our suggestions based on your inputs.
38
- """)
39
-
40
- # Initialize session state
41
- if 'step' not in st.session_state:
42
- st.session_state.step = 0
43
- st.session_state.user_data = {}
44
- st.session_state.chat_history = []
45
-
46
- # Display chat history (simulating chatbot-style interaction)
47
- for message in st.session_state.chat_history:
48
- st.chat_message(message["role"]).markdown(message["content"])
49
-
50
- def next_step():
51
- st.session_state.step += 1
52
-
53
- def restart_chat():
54
- st.session_state.step = 0
55
- st.session_state.user_data = {}
56
- st.session_state.chat_history = []
57
-
58
- # Step 1: Greet the user
59
- if st.session_state.step == 0:
60
- st.session_state.chat_history.append({"role": "assistant", "content": "Hello! How are you feeling today? 😊 Let's start your well-being assessment!"})
61
- user_input = st.text_input("You:", "")
62
- if user_input:
63
- st.session_state.chat_history.append({"role": "user", "content": user_input})
64
- next_step()
65
-
66
- # Step 2: Form to collect data (Anxiety Level, Self-Esteem, Mental Health, Stress)
67
- elif st.session_state.step == 1:
68
- st.session_state.chat_history.append({"role": "assistant", "content": "Please provide the following details to help me understand your well-being:"})
69
-
70
- with st.form("Well-being Form"):
71
- anxiety_level = st.slider("Anxiety Level (1 to 10)", 1, 10, 5)
72
- self_esteem = st.slider("Self-esteem Level (1 to 10)", 1, 10, 5)
73
- mental_health_history = st.selectbox("Mental Health History", ["None", "Minor Issues", "Moderate Issues", "Severe Issues"])
74
- stress_level = st.radio("Stress Level (0 to 2)", [0, 1, 2])
75
-
76
- submit_button = st.form_submit_button(label="Submit")
77
-
78
- if submit_button:
79
- # Collecting user input in session state
80
- st.session_state.user_data = {
81
- "anxiety_level": anxiety_level,
82
- "self_esteem": self_esteem,
83
- "mental_health_history": mental_health_history,
84
- "stress_level": stress_level
85
- }
86
- st.session_state.chat_history.append({"role": "user", "content": f"Form Submitted:\nAnxiety Level: {anxiety_level}\nSelf-esteem Level: {self_esteem}\nMental Health History: {mental_health_history}\nStress Level: {stress_level}"})
87
- next_step()
88
-
89
- # Step 3: Provide Personalized Advice
90
- elif st.session_state.step == 2:
91
- st.session_state.chat_history.append({"role": "assistant", "content": "Thank you for submitting your information! Here's a summary of your well-being:"})
92
- user_data = st.session_state.user_data
93
- st.write(f"Anxiety Level: {user_data['anxiety_level']}")
94
- st.write(f"Self-Esteem Level: {user_data['self_esteem']}")
95
- st.write(f"Mental Health History: {user_data['mental_health_history']}")
96
- st.write(f"Stress Level: {user_data['stress_level']}")
97
-
98
- # Provide personalized advice
99
- st.subheader("πŸ”” Personalized Well-being Advice:")
100
- advice = get_personalized_advice(user_data)
101
  if advice:
102
- st.write(f"Bot: {advice}")
103
- else:
104
- st.write("Bot: I couldn't retrieve personalized advice at the moment.")
105
 
106
- if st.button("Start Over"):
107
- restart_chat()
 
108
 
109
  # Main function to run the chatbot
110
  def main():
111
- chatbot_interface()
112
 
113
  if __name__ == "__main__":
114
  main()
 
1
  import streamlit as st
2
+ import requests
3
  from groq import Groq
4
 
5
+ # Groq API Key (replace with your actual API key)
6
+ groq_api_key = "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" # Replace with your Groq API key
7
 
8
  # Initialize Groq client
9
+ client = Groq(api_key=groq_api_key)
10
 
11
+ # Function to send a request to the Groq API and fetch health advice
12
+ def get_health_advice_from_groq(anxiety_level, self_esteem, stress_level):
13
+ """Fetch health advice from Groq API based on user input."""
14
+ query = f"Provide personalized health advice based on the following data: anxiety level: {anxiety_level}, self-esteem: {self_esteem}, stress level: {stress_level}."
15
+
 
 
 
 
 
 
16
  try:
17
+ # Request to Groq API
18
+ response = client.chat.completions.create(
19
  messages=[{"role": "user", "content": query}],
20
+ model="llama-3.3-70b-versatile", # You can use the model that suits your needs
21
  )
22
+ # Extract and return the health advice from the response
23
+ advice = response.choices[0].message.content
24
+ return advice
25
  except Exception as e:
26
+ st.error(f"Error fetching advice from Groq: {e}")
27
+ return None
28
+
29
+ # Function to create the chatbot interface
30
+ def health_advice_chatbot():
31
+ st.title("Health Advice Chatbot")
32
+ st.write("Welcome! I'm here to help you with some basic health advice based on your well-being.")
33
+
34
+ # User input
35
+ st.write("Please answer the following questions to receive personalized health advice.")
36
+
37
+ # Anxiety Level (Slider)
38
+ anxiety_level = st.slider("On a scale of 1 to 10, how would you rate your anxiety level?", 1, 10, 5)
39
+
40
+ # Self-esteem Level (Slider)
41
+ self_esteem = st.slider("On a scale of 1 to 10, how would you rate your self-esteem?", 1, 10, 5)
42
+
43
+ # Stress Level (Radio Buttons)
44
+ stress_level = st.radio("On a scale of 0 to 2, how would you rate your stress level?", [0, 1, 2])
45
+
46
+ # Submit button to get health advice
47
+ if st.button("Get Health Advice"):
48
+ # Fetch health advice from Groq API
49
+ advice = get_health_advice_from_groq(anxiety_level, self_esteem, stress_level)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  if advice:
51
+ st.subheader("Here is your personalized health advice:")
52
+ st.write(advice)
 
53
 
54
+ # Option to restart the conversation
55
+ if st.button("Start Over"):
56
+ st.experimental_rerun()
57
 
58
  # Main function to run the chatbot
59
  def main():
60
+ health_advice_chatbot()
61
 
62
  if __name__ == "__main__":
63
  main()