File size: 2,084 Bytes
fabbbff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import joblib
import re

# -------------------------------
# Load models
# -------------------------------
tfidf = joblib.load("tfidf.pkl")
model = joblib.load("Mental_Health_Text.pkl")
le = joblib.load("label_encoder.pkl")

# -------------------------------
# Background Image CSS
# -------------------------------
def set_bg():
    bg_img = """

    <style>

    .stApp {

        background-image: url("https://images.unsplash.com/photo-1506126613408-eca07ce68773");

        background-size: cover;

        background-position: center;

        background-repeat: no-repeat;

    }



    /* Optional: make text readable */

    .block-container {

        background-color: rgba(0, 0, 0, 0.6);

        padding: 2rem;

        border-radius: 15px;

    }



    h1, h2, h3, p, label {

        color: white !important;

    }

    </style>

    """
    st.markdown(bg_img, unsafe_allow_html=True)

set_bg()

# -------------------------------
# Preprocess
# -------------------------------
def preprocess_text(text):
    text = re.sub(r"[^a-zA-Z\s]", "", text)
    return text.lower()

# -------------------------------
# Prediction
# -------------------------------
def predict(text):
    text = preprocess_text(text)
    vec = tfidf.transform([text])

    if vec.nnz < 3:
        return "Not related to mental health"

    pred = model.predict(vec)[0]
    return le.inverse_transform([pred])[0]

# -------------------------------
# UI
# -------------------------------
st.set_page_config(page_title="Mental Health Detector", page_icon="🧠")

st.title("Mental Health Detection")
st.write("Analyze text to detect mental health signals")

user_input = st.text_area("Enter your text here:", height=150)

if st.button("Predict"):
    if user_input.strip() == "":
        st.warning("Please enter some text")
    else:
        result = predict(user_input)

        if result == "Not related to mental health":
            st.info(result)
        else:
            st.success(f"Prediction: {result}")