shani67's picture
Upload 7 files
fabbbff verified
Raw History Blame Contribute Delete
2.08 kB
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}")