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