File size: 6,254 Bytes
b873bf2
a88b37a
b29b783
67d7fa5
80acb63
 
67d7fa5
80acb63
 
b29b783
80acb63
 
b29b783
 
 
 
a88b37a
 
 
80acb63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a88b37a
 
80acb63
 
 
 
 
 
 
 
a88b37a
 
 
80acb63
a88b37a
 
80acb63
b29b783
 
a88b37a
80acb63
 
a88b37a
 
b29b783
a88b37a
 
b29b783
a88b37a
 
b29b783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a88b37a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80acb63
a88b37a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b29b783
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
@st.cache_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()