Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,54 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
import pandas as pd
|
|
|
4 |
import requests
|
5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
groq_client = Groq(api_key=groq_api_key)
|
18 |
-
except Exception as e:
|
19 |
-
st.error(f"Error initializing GROQ client: {e}")
|
20 |
-
st.stop()
|
21 |
|
22 |
# Function to load and preprocess data
|
23 |
@st.cache_data
|
24 |
def load_data(file):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
elif data['depression'] > 5 or data['anxiety'] > 5:
|
38 |
-
advice.append("You are showing moderate levels of depression or anxiety. It would be helpful to develop healthy coping strategies like maintaining a regular sleep schedule, engaging in physical activity, and reaching out to friends or family for support.")
|
39 |
-
|
40 |
-
# High isolation and low stress-relief activities
|
41 |
-
if data['isolation'] > 7 and data['stress_relief_activities'] < 5:
|
42 |
-
advice.append("It seems you are feeling isolated, and your engagement in stress-relief activities is low. It's important to connect with friends or join community groups. Incorporate activities that help alleviate stress, such as walking, journaling, or meditation.")
|
43 |
-
|
44 |
-
# High future insecurity
|
45 |
-
if data['future_insecurity'] > 7:
|
46 |
-
advice.append("You are feeling a significant amount of insecurity about the future. It can be helpful to break down your larger goals into smaller, manageable tasks. Seeking career counseling or mentorship could provide valuable guidance and reduce anxiety about the future.")
|
47 |
-
|
48 |
-
# Overall low engagement in stress-relief activities
|
49 |
-
if data['stress_relief_activities'] < 5:
|
50 |
-
advice.append("Your engagement in stress-relief activities is quite low. It's essential to engage in activities that reduce stress and promote mental wellness, such as hobbies, physical exercise, and relaxation techniques like deep breathing or yoga.")
|
51 |
-
|
52 |
-
return advice
|
53 |
-
|
54 |
-
# Function to fetch health articles from the GROC API based on the query
|
55 |
-
def get_health_articles(query):
|
56 |
-
url = f"https://api.groc.com/search?q={query}"
|
57 |
-
headers = {"Authorization": f"Bearer {groq_api_key}"} # Use the demo API key in the header
|
58 |
|
59 |
try:
|
60 |
-
response = requests.
|
61 |
-
response.raise_for_status()
|
62 |
-
|
63 |
-
if '
|
64 |
-
|
65 |
else:
|
66 |
-
|
67 |
-
return articles
|
68 |
-
except requests.exceptions.HTTPError as http_err:
|
69 |
-
st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
|
70 |
-
st.error(f"Response content: {response.text}")
|
71 |
-
return []
|
72 |
except requests.exceptions.RequestException as err:
|
73 |
-
|
74 |
-
|
|
|
75 |
|
76 |
# Streamlit app layout
|
77 |
def main():
|
@@ -99,56 +76,18 @@ def main():
|
|
99 |
)
|
100 |
|
101 |
# Title and header
|
102 |
-
st.title("π **
|
103 |
-
st.markdown("### **
|
104 |
-
|
105 |
-
#
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
st.
|
114 |
-
gender = st.selectbox("πΉ Gender", ["Male", "Female"], help="Select your gender.")
|
115 |
-
age = st.slider("πΉ Age", 18, 35, step=1)
|
116 |
-
depression = st.slider("πΉ Depression Level (1-10)", 1, 10)
|
117 |
-
anxiety = st.slider("πΉ Anxiety Level (1-10)", 1, 10)
|
118 |
-
isolation = st.slider("πΉ Isolation Level (1-10)", 1, 10)
|
119 |
-
future_insecurity = st.slider("πΉ Future Insecurity Level (1-10)", 1, 10)
|
120 |
-
stress_relief_activities = st.slider("πΉ Stress Relief Activities Level (1-10)", 1, 10)
|
121 |
-
|
122 |
-
# Data dictionary for advice
|
123 |
-
user_data = {
|
124 |
-
"gender": gender,
|
125 |
-
"age": age,
|
126 |
-
"depression": depression,
|
127 |
-
"anxiety": anxiety,
|
128 |
-
"isolation": isolation,
|
129 |
-
"future_insecurity": future_insecurity,
|
130 |
-
"stress_relief_activities": stress_relief_activities,
|
131 |
-
}
|
132 |
-
|
133 |
-
# Provide advice based on user inputs
|
134 |
-
if st.button("π Get Observed Advice", key="advice_btn"):
|
135 |
-
st.subheader("π **Health Advice Based on Observations** π")
|
136 |
-
advice = provide_observed_advice(user_data)
|
137 |
-
if advice:
|
138 |
-
for i, tip in enumerate(advice, 1):
|
139 |
-
st.write(f"π {i}. {tip}")
|
140 |
-
else:
|
141 |
-
st.warning("No advice available based on your inputs.")
|
142 |
-
|
143 |
-
# Fetch related health articles based on user input
|
144 |
-
st.subheader("π° **Related Health Articles** π°")
|
145 |
-
query = "mental health anxiety depression isolation stress relief"
|
146 |
-
articles = get_health_articles(query)
|
147 |
-
if articles:
|
148 |
-
for article in articles:
|
149 |
-
st.write(f"π [{article['title']}]({article['url']})")
|
150 |
-
else:
|
151 |
-
st.write("No articles found. Please check your API key or internet connection.")
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
main()
|
|
|
1 |
import os
|
2 |
+
import logging
|
3 |
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
import requests
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# Set up logging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
|
13 |
+
# Load environment variables from .env file
|
14 |
+
load_dotenv()
|
15 |
|
16 |
+
# Get the Hugging Face API key from environment variables
|
17 |
+
hf_api_key = os.getenv("HUGGINGFACE_API_KEY")
|
18 |
+
if not hf_api_key:
|
19 |
+
raise ValueError("HUGGINGFACE_API_KEY is not set. Please provide a valid API key.")
|
20 |
|
21 |
+
# Hugging Face API URL
|
22 |
+
hf_api_url = "https://api-inference.huggingface.co/models/{model_name}"
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Function to load and preprocess data
|
25 |
@st.cache_data
|
26 |
def load_data(file):
|
27 |
+
try:
|
28 |
+
df = pd.read_csv(file)
|
29 |
+
return df
|
30 |
+
except Exception as e:
|
31 |
+
logger.error(f"Error loading CSV file: {e}")
|
32 |
+
st.error("There was an issue loading the file. Please try again.")
|
33 |
+
return pd.DataFrame() # Return an empty DataFrame in case of error
|
34 |
+
|
35 |
+
# Function to call Hugging Face API for text generation
|
36 |
+
def generate_text_from_model(model_name, text_input):
|
37 |
+
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
38 |
+
data = {"inputs": text_input}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
try:
|
41 |
+
response = requests.post(hf_api_url.format(model_name=model_name), headers=headers, json=data)
|
42 |
+
response.raise_for_status()
|
43 |
+
result = response.json()
|
44 |
+
if 'generated_text' in result:
|
45 |
+
return result['generated_text']
|
46 |
else:
|
47 |
+
return "No result from model. Please try again."
|
|
|
|
|
|
|
|
|
|
|
48 |
except requests.exceptions.RequestException as err:
|
49 |
+
logger.error(f"Error interacting with Hugging Face API: {err}")
|
50 |
+
st.error(f"Error interacting with Hugging Face API: {err}")
|
51 |
+
return ""
|
52 |
|
53 |
# Streamlit app layout
|
54 |
def main():
|
|
|
76 |
)
|
77 |
|
78 |
# Title and header
|
79 |
+
st.title("π **Hugging Face Text Generation** π")
|
80 |
+
st.markdown("### **Generate text using Hugging Face Models**")
|
81 |
+
|
82 |
+
# User input for text generation
|
83 |
+
model_name = st.selectbox("πΉ Select Hugging Face Model", ["gpt2", "distilgpt2", "t5-small"])
|
84 |
+
text_input = st.text_area("πΉ Input Text", "Once upon a time...")
|
85 |
+
|
86 |
+
# Generate text based on input
|
87 |
+
if st.button("π Generate Text"):
|
88 |
+
st.subheader("π **Generated Text** π")
|
89 |
+
generated_text = generate_text_from_model(model_name, text_input)
|
90 |
+
st.write(f"π {generated_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
main()
|