Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,146 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import requests
|
4 |
-
import os
|
5 |
-
from google.cloud import language_v1
|
6 |
-
from google.oauth2 import service_account
|
7 |
-
|
8 |
-
# Set the API key for Google AI API (if not set in the environment variable)
|
9 |
-
api_key = "AIzaSyAlvoXLqzqcZgVjhQeCNUsQgk6_SGHQNr8" # Ensure your credentials are set up
|
10 |
-
|
11 |
-
# Initialize Google AI Client
|
12 |
-
client = language_v1.LanguageServiceClient(credentials=service_account.Credentials.from_service_account_file("path_to_your_service_account_json"))
|
13 |
-
|
14 |
-
# Function to load and preprocess data
|
15 |
-
@st.cache_data
|
16 |
-
def load_data(file):
|
17 |
-
df = pd.read_csv(file)
|
18 |
-
return df
|
19 |
-
|
20 |
-
# Function to fetch and analyze text using Google AI's Natural Language API
|
21 |
-
def analyze_text_with_google_ai(text):
|
22 |
-
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
|
23 |
-
response = client.analyze_sentiment(document=document)
|
24 |
-
sentiment_score = response.document_sentiment.score
|
25 |
-
sentiment_magnitude = response.document_sentiment.magnitude
|
26 |
-
|
27 |
-
# Example: Based on sentiment, provide advice
|
28 |
-
if sentiment_score < -0.5:
|
29 |
-
return "You may want to focus on activities that improve your mood, such as physical exercise, talking with a counselor, or engaging in mindfulness practices."
|
30 |
-
elif sentiment_score > 0.5:
|
31 |
-
return "It seems you're in a positive emotional state. Keep nurturing these positive habits, such as engaging in social activities and continuing to practice stress-relief strategies."
|
32 |
-
else:
|
33 |
-
return "You are in a neutral emotional state. Consider exploring activities that help enhance your mood, such as engaging in hobbies or relaxation exercises."
|
34 |
-
|
35 |
-
# Function to provide health advice based on user data and Google AI analysis
|
36 |
-
def provide_google_ai_advice(data):
|
37 |
-
advice = []
|
38 |
-
|
39 |
-
# Example of analysis based on Google AI's sentiment analysis
|
40 |
-
if data['depression'] > 7 or data['anxiety'] > 7:
|
41 |
-
advice.append("It seems you're experiencing high levels of depression or anxiety. It might be helpful to talk to a professional or consider engaging in activities that can reduce stress, like mindfulness or physical exercise.")
|
42 |
-
|
43 |
-
# Call Google AI for sentiment-based advice
|
44 |
-
user_data_summary = f"User's depression: {data['depression']}, anxiety: {data['anxiety']}, isolation: {data['isolation']}, future insecurity: {data['future_insecurity']}, stress-relief activities: {data['stress_relief_activities']}"
|
45 |
-
google_ai_advice = analyze_text_with_google_ai(user_data_summary)
|
46 |
-
advice.append(google_ai_advice)
|
47 |
-
|
48 |
-
return advice
|
49 |
-
|
50 |
-
# Function to fetch related health articles from GROC API (optional, for RAG-style application)
|
51 |
-
def get_health_articles(query):
|
52 |
-
url = f"https://api.groc.com/search?q={query}"
|
53 |
-
headers = {"Authorization": f"Bearer {api_key}"} # Replace with actual Google API key if required
|
54 |
-
|
55 |
-
try:
|
56 |
-
response = requests.get(url, headers=headers)
|
57 |
-
response.raise_for_status()
|
58 |
-
data = response.json()
|
59 |
-
if 'results' in data:
|
60 |
-
articles = [{"title": item["title"], "url": item["url"]} for item in data['results']]
|
61 |
-
else:
|
62 |
-
articles = []
|
63 |
-
return articles
|
64 |
-
except requests.exceptions.RequestException as err:
|
65 |
-
st.error(f"Error fetching articles: {err}. Please check your internet connection.")
|
66 |
-
return []
|
67 |
-
|
68 |
-
# Streamlit app layout
|
69 |
-
def main():
|
70 |
-
# Set a background color and style
|
71 |
-
st.markdown(
|
72 |
-
"""
|
73 |
-
<style>
|
74 |
-
.stApp {
|
75 |
-
background-color: #F4F4F9;
|
76 |
-
}
|
77 |
-
.stButton>button {
|
78 |
-
background-color: #6200EE;
|
79 |
-
color: white;
|
80 |
-
font-size: 18px;
|
81 |
-
}
|
82 |
-
.stSlider>div>div>span {
|
83 |
-
color: #6200EE;
|
84 |
-
}
|
85 |
-
.stTextInput>div>div>input {
|
86 |
-
background-color: #E0E0E0;
|
87 |
-
}
|
88 |
-
</style>
|
89 |
-
""",
|
90 |
-
unsafe_allow_html=True
|
91 |
-
)
|
92 |
-
|
93 |
-
# Title and header
|
94 |
-
st.title("π **Student Health Advisory Assistant** π")
|
95 |
-
st.markdown("### **Analyze your well-being and get personalized advice**")
|
96 |
-
|
97 |
-
# File upload
|
98 |
-
uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
|
99 |
-
if uploaded_file:
|
100 |
-
df = load_data(uploaded_file)
|
101 |
-
st.write("### Dataset Preview:")
|
102 |
-
st.dataframe(df.head())
|
103 |
-
|
104 |
-
# User input for analysis
|
105 |
-
st.markdown("### **Input Your Details**")
|
106 |
-
gender = st.selectbox("πΉ Gender", ["Male", "Female"], help="Select your gender.")
|
107 |
-
age = st.slider("πΉ Age", 18, 35, step=1)
|
108 |
-
depression = st.slider("πΉ Depression Level (1-10)", 1, 10)
|
109 |
-
anxiety = st.slider("πΉ Anxiety Level (1-10)", 1, 10)
|
110 |
-
isolation = st.slider("πΉ Isolation Level (1-10)", 1, 10)
|
111 |
-
future_insecurity = st.slider("πΉ Future Insecurity Level (1-10)", 1, 10)
|
112 |
-
stress_relief_activities = st.slider("πΉ Stress Relief Activities Level (1-10)", 1, 10)
|
113 |
-
|
114 |
-
# Data dictionary for advice
|
115 |
-
user_data = {
|
116 |
-
"gender": gender,
|
117 |
-
"age": age,
|
118 |
-
"depression": depression,
|
119 |
-
"anxiety": anxiety,
|
120 |
-
"isolation": isolation,
|
121 |
-
"future_insecurity": future_insecurity,
|
122 |
-
"stress_relief_activities": stress_relief_activities,
|
123 |
-
}
|
124 |
-
|
125 |
-
# Provide advice based on user inputs
|
126 |
-
if st.button("π Get Observed Advice", key="advice_btn"):
|
127 |
-
st.subheader("π **Health Advice Based on Observations** π")
|
128 |
-
advice = provide_google_ai_advice(user_data)
|
129 |
-
if advice:
|
130 |
-
for i, tip in enumerate(advice, 1):
|
131 |
-
st.write(f"π {i}. {tip}")
|
132 |
-
else:
|
133 |
-
st.warning("No advice available based on your inputs.")
|
134 |
-
|
135 |
-
# Fetch related health articles based on user input
|
136 |
-
st.subheader("π° **Related Health Articles** π°")
|
137 |
-
query = "mental health anxiety depression isolation stress relief"
|
138 |
-
articles = get_health_articles(query)
|
139 |
-
if articles:
|
140 |
-
for article in articles:
|
141 |
-
st.write(f"π [{article['title']}]({article['url']})")
|
142 |
-
else:
|
143 |
-
st.write("No articles found. Please check your API key or internet connection.")
|
144 |
-
|
145 |
-
if __name__ == "__main__":
|
146 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|