EnYa32 commited on
Commit
604c8fd
·
verified ·
1 Parent(s): 5fc00b4

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)")