CoolFace
Apppublic

ytrsoymr/StackOverflow-Tag-Prediction

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py51 linesDownload Raw Back to root
1import streamlit as st2import joblib3import re4import nltk5from nltk.corpus import stopwords6from nltk.tokenize import word_tokenize7from sklearn.feature_extraction.text import TfidfVectorizer8from sklearn.preprocessing import MultiLabelBinarizer9 10nltk.download('punkt')11nltk.download('stopwords')12 13# --- Load artifacts ---14model=joblib.load(r"E:\Stack-Overflow\models\model.pkl")15vectorizer = joblib.load(r"E:\Stack-Overflow\models\vectorizer.pkl")  # TF-IDF vectorizer16mlb = joblib.load(r"E:\Stack-Overflow\models\mlb.pkl")             # MultiLabelBinarizer17# MultiLabelBinarizer18 19# --- Preprocessing function ---20def preprocess(text):21    text = text.lower()22    text = re.sub(r'[^a-z\s]', '', text)23    tokens = word_tokenize(text)24    stop_words = set(stopwords.words('english'))25    tokens = [t for t in tokens if t not in stop_words]26    return " ".join(tokens)27 28# --- Streamlit UI ---29st.set_page_config(page_title="Stack Overflow Tag Predictor", layout="centered")30 31st.title("💬 Stack Overflow Tag Predictor")32st.markdown("Enter a question (title + body) and get predicted tags.")33 34user_input = st.text_area("✍️ Question Title + Body", height=200)35 36top_k = st.slider("Number of tags to show", min_value=1, max_value=10, value=5)37 38if st.button("Predict Tags") and user_input.strip():39    cleaned = preprocess(user_input)40    X_vec = vectorizer.transform([cleaned])41    y_pred_proba = model.predict_proba(X_vec)42 43    # Get top-k tag predictions44    top_indices = y_pred_proba[0].argsort()[-top_k:][::-1]45    predicted_tags = [mlb.classes_[i] for i in top_indices]46    confidence = [y_pred_proba[0][i] for i in top_indices]47 48    st.markdown("### 🏷️ Predicted Tags:")49    for tag, conf in zip(predicted_tags, confidence):50        st.markdown(f"- **{tag}** (confidence: {conf:.2f})")51