Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +46 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,48 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
| 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 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
|
| 4 |
+
st.set_page_config(page_title="Stress Detection (TF-IDF)", page_icon="🧠", layout="centered")
|
| 5 |
+
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_pipeline():
|
| 8 |
+
return joblib.load("src/tfidf_lr_pipeline.pkl")
|
| 9 |
+
|
| 10 |
+
pipeline = load_pipeline()
|
| 11 |
+
|
| 12 |
+
st.title("🧠 Stress Detection from Text")
|
| 13 |
+
st.write("This app uses a **TF-IDF + Logistic Regression** pipeline to classify text as **Stress (1)** or **No Stress (0)**.")
|
| 14 |
+
|
| 15 |
+
text = st.text_area("Enter a text (e.g., a social media post):", height=180, placeholder="Type or paste text here...")
|
| 16 |
+
|
| 17 |
+
col1, col2 = st.columns([1, 1])
|
| 18 |
+
|
| 19 |
+
with col1:
|
| 20 |
+
predict_btn = st.button("Predict", use_container_width=True)
|
| 21 |
+
|
| 22 |
+
with col2:
|
| 23 |
+
clear_btn = st.button("Clear", use_container_width=True)
|
| 24 |
+
|
| 25 |
+
if clear_btn:
|
| 26 |
+
st.session_state["text_area"] = ""
|
| 27 |
+
st.rerun()
|
| 28 |
+
|
| 29 |
+
if predict_btn:
|
| 30 |
+
if not text.strip():
|
| 31 |
+
st.warning("Please enter some text.")
|
| 32 |
+
else:
|
| 33 |
+
pred = int(pipeline.predict([text])[0])
|
| 34 |
+
proba = pipeline.predict_proba([text])[0] # [P(class0), P(class1)]
|
| 35 |
+
|
| 36 |
+
st.subheader("Result")
|
| 37 |
+
if pred == 1:
|
| 38 |
+
st.error("Prediction: **Stress (1)**")
|
| 39 |
+
else:
|
| 40 |
+
st.success("Prediction: **No Stress (0)**")
|
| 41 |
+
|
| 42 |
+
st.write(f"Confidence (No Stress): **{proba[0]*100:.2f}%**")
|
| 43 |
+
st.write(f"Confidence (Stress): **{proba[1]*100:.2f}%**")
|
| 44 |
+
|
| 45 |
+
st.progress(float(proba[1])) # show stress probability as a bar
|
| 46 |
+
|
| 47 |
+
st.markdown("---")
|
| 48 |
+
st.caption("Model: TF-IDF Vectorizer + Logistic Regression (saved as tfidf_lr_pipeline.pkl)")
|