import streamlit as st
import pickle
import re
import numpy as np
# ✅ Page Config
st.set_page_config(page_title="Stack Overflow Tags Predictor", layout="wide")
# ✅ Enhanced CSS for a modern UI
st.markdown("""
""", unsafe_allow_html=True)
# ✅ Text Preprocessing
def clean_text(text):
text = re.sub(r"<.*?>", " ", text)
text = re.sub(r"\W", " ", text)
text = re.sub(r"\s+", " ", text.lower()).strip()
return text
# ✅ Load Pickled Artifacts
@st.cache_resource
def load_artifacts():
with open("model12.pkl", "rb") as f:
model = pickle.load(f)
with open("tfidf12.pkl", "rb") as f:
vectorizer = pickle.load(f)
with open("mlb12.pkl", "rb") as f:
mlb = pickle.load(f)
return model, vectorizer, mlb
model, vectorizer, mlb = load_artifacts()
# ✅ UI Container
with st.container():
st.markdown("
", unsafe_allow_html=True)
st.markdown("
🔖 Stack Overflow Tags Predictor
", unsafe_allow_html=True)
st.markdown("
Enter your question title and description to generate the most relevant tags using Machine Learning.
", unsafe_allow_html=True)
with st.form(key="tag_prediction_form"):
title = st.text_input("📝 Enter Question Title")
body = st.text_area("📄 Enter Question Description", height=180)
threshold = st.slider("🔧 Tag Confidence Threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.05)
submitted = st.form_submit_button("🔍 Predict Tags")
if submitted:
if not title.strip() or not body.strip():
st.warning("⚠️ Please fill in both the title and description.")
else:
with st.spinner("🔍 Predicting the most relevant tags..."):
input_text = clean_text(title + " " + body)
X_input = vectorizer.transform([input_text])
try:
y_prob = model.predict_proba(X_input)
y_pred = (y_prob >= threshold).astype(int)
except AttributeError:
y_pred = model.predict(X_input)
predicted_tags = mlb.inverse_transform(y_pred)
with st.container():
st.markdown("
", unsafe_allow_html=True)
if predicted_tags and predicted_tags[0]:
st.success("✅ Tags Predicted Successfully!")
tag_html = "".join([f"{tag}" for tag in predicted_tags[0]])
st.markdown(tag_html, unsafe_allow_html=True)
else:
st.info("🤔 No tags predicted. Try refining your input or lowering the threshold.")
st.markdown("
", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)