Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import requests | |
import os | |
from google.cloud import language_v1 | |
from google.oauth2 import service_account | |
# Set the API key for Google AI API (if not set in the environment variable) | |
api_key = "AIzaSyAlvoXLqzqcZgVjhQeCNUsQgk6_SGHQNr8" # Ensure your credentials are set up | |
# Initialize Google AI Client | |
client = language_v1.LanguageServiceClient(credentials=service_account.Credentials.from_service_account_file("path_to_your_service_account_json")) | |
# Function to load and preprocess data | |
def load_data(file): | |
df = pd.read_csv(file) | |
return df | |
# Function to fetch and analyze text using Google AI's Natural Language API | |
def analyze_text_with_google_ai(text): | |
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT) | |
response = client.analyze_sentiment(document=document) | |
sentiment_score = response.document_sentiment.score | |
sentiment_magnitude = response.document_sentiment.magnitude | |
# Example: Based on sentiment, provide advice | |
if sentiment_score < -0.5: | |
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." | |
elif sentiment_score > 0.5: | |
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." | |
else: | |
return "You are in a neutral emotional state. Consider exploring activities that help enhance your mood, such as engaging in hobbies or relaxation exercises." | |
# Function to provide health advice based on user data and Google AI analysis | |
def provide_google_ai_advice(data): | |
advice = [] | |
# Example of analysis based on Google AI's sentiment analysis | |
if data['depression'] > 7 or data['anxiety'] > 7: | |
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.") | |
# Call Google AI for sentiment-based advice | |
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']}" | |
google_ai_advice = analyze_text_with_google_ai(user_data_summary) | |
advice.append(google_ai_advice) | |
return advice | |
# Function to fetch related health articles from GROC API (optional, for RAG-style application) | |
def get_health_articles(query): | |
url = f"https://api.groc.com/search?q={query}" | |
headers = {"Authorization": f"Bearer {api_key}"} # Replace with actual Google API key if required | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() | |
data = response.json() | |
if 'results' in data: | |
articles = [{"title": item["title"], "url": item["url"]} for item in data['results']] | |
else: | |
articles = [] | |
return articles | |
except requests.exceptions.RequestException as err: | |
st.error(f"Error fetching articles: {err}. Please check your internet connection.") | |
return [] | |
# Streamlit app layout | |
def main(): | |
# Set a background color and style | |
st.markdown( | |
""" | |
<style> | |
.stApp { | |
background-color: #F4F4F9; | |
} | |
.stButton>button { | |
background-color: #6200EE; | |
color: white; | |
font-size: 18px; | |
} | |
.stSlider>div>div>span { | |
color: #6200EE; | |
} | |
.stTextInput>div>div>input { | |
background-color: #E0E0E0; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
# Title and header | |
st.title("π **Student Health Advisory Assistant** π") | |
st.markdown("### **Analyze your well-being and get personalized advice**") | |
# File upload | |
uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"]) | |
if uploaded_file: | |
df = load_data(uploaded_file) | |
st.write("### Dataset Preview:") | |
st.dataframe(df.head()) | |
# User input for analysis | |
st.markdown("### **Input Your Details**") | |
gender = st.selectbox("πΉ Gender", ["Male", "Female"], help="Select your gender.") | |
age = st.slider("πΉ Age", 18, 35, step=1) | |
depression = st.slider("πΉ Depression Level (1-10)", 1, 10) | |
anxiety = st.slider("πΉ Anxiety Level (1-10)", 1, 10) | |
isolation = st.slider("πΉ Isolation Level (1-10)", 1, 10) | |
future_insecurity = st.slider("πΉ Future Insecurity Level (1-10)", 1, 10) | |
stress_relief_activities = st.slider("πΉ Stress Relief Activities Level (1-10)", 1, 10) | |
# Data dictionary for advice | |
user_data = { | |
"gender": gender, | |
"age": age, | |
"depression": depression, | |
"anxiety": anxiety, | |
"isolation": isolation, | |
"future_insecurity": future_insecurity, | |
"stress_relief_activities": stress_relief_activities, | |
} | |
# Provide advice based on user inputs | |
if st.button("π Get Observed Advice", key="advice_btn"): | |
st.subheader("π **Health Advice Based on Observations** π") | |
advice = provide_google_ai_advice(user_data) | |
if advice: | |
for i, tip in enumerate(advice, 1): | |
st.write(f"π {i}. {tip}") | |
else: | |
st.warning("No advice available based on your inputs.") | |
# Fetch related health articles based on user input | |
st.subheader("π° **Related Health Articles** π°") | |
query = "mental health anxiety depression isolation stress relief" | |
articles = get_health_articles(query) | |
if articles: | |
for article in articles: | |
st.write(f"π [{article['title']}]({article['url']})") | |
else: | |
st.write("No articles found. Please check your API key or internet connection.") | |
if __name__ == "__main__": | |
main() | |