Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +109 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,111 @@
|
|
| 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 |
-
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 pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
import string
|
| 6 |
+
import nltk
|
| 7 |
+
from nltk.corpus import stopwords as stp
|
| 8 |
+
from nltk import pos_tag, word_tokenize as w, sent_tokenize as s
|
| 9 |
+
from nltk.stem import WordNetLemmatizer as wl
|
| 10 |
+
|
| 11 |
+
# Download necessary NLTK data
|
| 12 |
+
nltk.download('punkt', quiet=True)
|
| 13 |
+
nltk.download('averaged_perceptron_tagger', quiet=True)
|
| 14 |
+
nltk.download('wordnet', quiet=True)
|
| 15 |
+
nltk.download('stopwords', quiet=True)
|
| 16 |
+
|
| 17 |
+
# === Cleaning Function ===
|
| 18 |
+
def sahi_karneka_function(x):
|
| 19 |
+
nouns=[]
|
| 20 |
+
li=[]
|
| 21 |
+
lem=wl()
|
| 22 |
+
l=s(x)
|
| 23 |
+
for i in l:
|
| 24 |
+
d=w(i.lower())
|
| 25 |
+
for k in d:
|
| 26 |
+
li.append(k)
|
| 27 |
+
lw=len(li)
|
| 28 |
+
j=0
|
| 29 |
+
while j<lw:
|
| 30 |
+
if li[j] in string.punctuation:
|
| 31 |
+
li.remove(li[j])
|
| 32 |
+
lw=len(li)
|
| 33 |
+
j=0
|
| 34 |
+
elif li[j] in stp.words("english"):
|
| 35 |
+
li.remove(li[j])
|
| 36 |
+
lw=len(li)
|
| 37 |
+
j=0
|
| 38 |
+
else:
|
| 39 |
+
j=j+1
|
| 40 |
+
tags=pos_tag(li)
|
| 41 |
+
for word,tag in tags:
|
| 42 |
+
if tag.startswith("NN") or tag.startswith("V"):
|
| 43 |
+
nouns.append(word)
|
| 44 |
+
semi_final_words=[lem.lemmatize(m,pos="n") if tagg.startswith("NN") else lem.lemmatize(m,pos="v") for m,tagg in pos_tag(nouns)]
|
| 45 |
+
final_sentence=" ".join(semi_final_words)
|
| 46 |
+
return final_sentence
|
| 47 |
+
|
| 48 |
+
# === Load Data and Models ===
|
| 49 |
+
df = pd.read_csv(r"src/c_d.csv")
|
| 50 |
+
model = joblib.load("src/logistic_models.pkl")
|
| 51 |
+
tfidf = joblib.load("src/tfidf.pkl")
|
| 52 |
+
ml = joblib.load("src/multilabels.pkl")
|
| 53 |
+
|
| 54 |
+
# === Streamlit UI ===
|
| 55 |
+
st.title("🧠 Multi-Label Question Tag Predictor")
|
| 56 |
+
|
| 57 |
+
# --- Select a URL for context ---
|
| 58 |
+
selected_url = st.selectbox("Select a question URL (for context):", df['questions_url'])
|
| 59 |
+
st.markdown(f"🔗 [Open selected question]({selected_url})")
|
| 60 |
+
|
| 61 |
+
# --- Session State ---
|
| 62 |
+
if "user_input" not in st.session_state:
|
| 63 |
+
st.session_state["user_input"] = ""
|
| 64 |
+
if "clear_input" not in st.session_state:
|
| 65 |
+
st.session_state["clear_input"] = False
|
| 66 |
+
|
| 67 |
+
# --- Clear input if flagged (AFTER rerun) ---
|
| 68 |
+
if st.session_state.clear_input:
|
| 69 |
+
st.session_state.user_input = ""
|
| 70 |
+
st.session_state.clear_input = False
|
| 71 |
+
|
| 72 |
+
# --- Input box ---
|
| 73 |
+
st.text_area("✍️ Type your question here:", key="user_input", height=150)
|
| 74 |
+
|
| 75 |
+
# --- Predict button ---
|
| 76 |
+
if st.button("Predict Tags"):
|
| 77 |
+
final_question = st.session_state.user_input.strip()
|
| 78 |
+
|
| 79 |
+
if not final_question:
|
| 80 |
+
st.warning("⚠️ Please enter a question.")
|
| 81 |
+
else:
|
| 82 |
+
with st.spinner("🔍 Predicting tags..."):
|
| 83 |
+
# Step 1: Clean input
|
| 84 |
+
cleaned = sahi_karneka_function(final_question)
|
| 85 |
+
|
| 86 |
+
# Step 2: TF-IDF
|
| 87 |
+
f=[]
|
| 88 |
+
f.append(cleaned)
|
| 89 |
+
x_tfidf = tfidf.transform(f)
|
| 90 |
+
|
| 91 |
+
# Step 3: Predict
|
| 92 |
+
y_probs = model.predict_proba(x_tfidf)
|
| 93 |
+
threshold = 0.55
|
| 94 |
+
y_predd=model.predict(x_tfidf)
|
| 95 |
+
probs_column1 = np.array([i[:, 1] for i in y_probs]).T
|
| 96 |
+
y_pred = (probs_column1 >= threshold).astype(int)
|
| 97 |
+
|
| 98 |
+
# Step 4: Decode
|
| 99 |
+
predicted_tags = ml.inverse_transform(y_predd)
|
| 100 |
+
|
| 101 |
+
# Step 5: Display results
|
| 102 |
+
st.success("✅ Predicted Tags:")
|
| 103 |
+
if predicted_tags and predicted_tags[0]:
|
| 104 |
+
for tag in predicted_tags[0]:
|
| 105 |
+
st.markdown(f"🔹 **`{tag}`**")
|
| 106 |
+
else:
|
| 107 |
+
st.info("No tags matched the threshold.")
|
| 108 |
|
| 109 |
+
# Step 6: Show a "Clear" button
|
| 110 |
+
if st.button("Clear Input"):
|
| 111 |
+
st.session_state.user_input = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|