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|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import re
|
| 5 |
+
from unidecode import unidecode
|
| 6 |
+
import emoji
|
| 7 |
+
import string
|
| 8 |
+
import contractions
|
| 9 |
+
from nltk.stem import PorterStemmer
|
| 10 |
+
import numpy as np
|
| 11 |
+
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
|
| 12 |
+
|
| 13 |
+
# Custom CSS Styling
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
.stApp {
|
| 17 |
+
background-color: #f9fbfc;
|
| 18 |
+
font-family: 'Segoe UI', sans-serif;
|
| 19 |
+
}
|
| 20 |
+
.custom-header {
|
| 21 |
+
background-color: #1e293b;
|
| 22 |
+
color: white;
|
| 23 |
+
padding: 2rem;
|
| 24 |
+
border-radius: 0.5rem;
|
| 25 |
+
text-align: center;
|
| 26 |
+
margin-bottom: 2rem;
|
| 27 |
+
}
|
| 28 |
+
.custom-header h1 {
|
| 29 |
+
font-size: 2rem;
|
| 30 |
+
margin-bottom: 0.5rem;
|
| 31 |
+
}
|
| 32 |
+
.custom-header p {
|
| 33 |
+
font-size: 1rem;
|
| 34 |
+
color: #cbd5e1;
|
| 35 |
+
}
|
| 36 |
+
.input-box, .output-box {
|
| 37 |
+
background-color: white;
|
| 38 |
+
padding: 1.5rem;
|
| 39 |
+
border-radius: 0.5rem;
|
| 40 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.04);
|
| 41 |
+
margin-bottom: 2rem;
|
| 42 |
+
}
|
| 43 |
+
.tag-pill {
|
| 44 |
+
display: inline-block;
|
| 45 |
+
background-color: #e0f2fe;
|
| 46 |
+
color: #0369a1;
|
| 47 |
+
padding: 0.4em 0.8em;
|
| 48 |
+
margin: 0.25em;
|
| 49 |
+
border-radius: 999px;
|
| 50 |
+
font-weight: 600;
|
| 51 |
+
font-size: 0.9rem;
|
| 52 |
+
}
|
| 53 |
+
.footer {
|
| 54 |
+
text-align: center;
|
| 55 |
+
font-size: 0.85rem;
|
| 56 |
+
color: #64748b;
|
| 57 |
+
margin-top: 2rem;
|
| 58 |
+
}
|
| 59 |
+
</style>
|
| 60 |
+
""", unsafe_allow_html=True)
|
| 61 |
+
|
| 62 |
+
# Header
|
| 63 |
+
st.markdown("""
|
| 64 |
+
<div class="custom-header">
|
| 65 |
+
<h1>๐ StackOverflow Tag Predictor</h1>
|
| 66 |
+
<p>Enter a programming question to see predicted tags</p>
|
| 67 |
+
</div>
|
| 68 |
+
""", unsafe_allow_html=True)
|
| 69 |
+
|
| 70 |
+
# Initialize components
|
| 71 |
+
stemmer = PorterStemmer()
|
| 72 |
+
stop_words = set(ENGLISH_STOP_WORDS)
|
| 73 |
+
chat_words = {
|
| 74 |
+
"brb": "be right back", "btw": "by the way", "lol": "laugh out loud",
|
| 75 |
+
"afaik": "as far as i know", "imo": "in my opinion", "tbh": "to be honest",
|
| 76 |
+
"idk": "i don't know", "asap": "as soon as possible", "np": "no problem",
|
| 77 |
+
"thx": "thanks", "pls": "please", "fyi": "for your information"
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
def preprocess_text(text):
|
| 81 |
+
if not isinstance(text, str) or not text.strip():
|
| 82 |
+
return ""
|
| 83 |
+
try:
|
| 84 |
+
text = re.sub(r'<[^>]+>', '', text)
|
| 85 |
+
text = re.sub(r'https?://\S+|www\.\S+', '', text)
|
| 86 |
+
text = emoji.demojize(text, delimiters=(" ", " "))
|
| 87 |
+
text = unidecode(text)
|
| 88 |
+
text = contractions.fix(text)
|
| 89 |
+
text = text.lower()
|
| 90 |
+
words = text.split()
|
| 91 |
+
text = " ".join([chat_words.get(word.lower(), word) for word in words])
|
| 92 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 93 |
+
tokens = re.findall(r'\b\w+\b', text)
|
| 94 |
+
tokens = [word for word in tokens if word not in stop_words]
|
| 95 |
+
tokens = [stemmer.stem(word) for word in tokens]
|
| 96 |
+
return " ".join(tokens)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
st.error(f"Preprocessing error: {e}")
|
| 99 |
+
return ""
|
| 100 |
+
|
| 101 |
+
@st.cache_resource
|
| 102 |
+
def load_models():
|
| 103 |
+
try:
|
| 104 |
+
model = joblib.load("tag_model.joblib")
|
| 105 |
+
mlb = joblib.load("tag_binarizer.joblib")
|
| 106 |
+
return model, mlb
|
| 107 |
+
except Exception as e:
|
| 108 |
+
st.error(f"Error loading model: {e}")
|
| 109 |
+
return None, None
|
| 110 |
+
|
| 111 |
+
model, mlb = load_models()
|
| 112 |
+
|
| 113 |
+
# Input
|
| 114 |
+
st.markdown('<div class="input-box">', unsafe_allow_html=True)
|
| 115 |
+
user_input = st.text_area("โ๏ธ Paste your programming question below:", height=200, placeholder="e.g., How to reverse a list in Python?")
|
| 116 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 117 |
+
|
| 118 |
+
# Prediction
|
| 119 |
+
if st.button("๐ Predict Tags"):
|
| 120 |
+
if not user_input.strip():
|
| 121 |
+
st.warning("Please enter your question to get predictions.")
|
| 122 |
+
elif model is None or mlb is None:
|
| 123 |
+
st.error("Model loading failed.")
|
| 124 |
+
else:
|
| 125 |
+
with st.spinner("Processing..."):
|
| 126 |
+
processed = preprocess_text(user_input)
|
| 127 |
+
if processed:
|
| 128 |
+
try:
|
| 129 |
+
input_df = pd.DataFrame({'processed_excerpt': [processed]})
|
| 130 |
+
if hasattr(model, "predict_proba"):
|
| 131 |
+
probs = model.predict_proba(input_df)[0]
|
| 132 |
+
top_idx = np.argsort(probs)[-5:][::-1]
|
| 133 |
+
tags = [mlb.classes_[i] for i in top_idx]
|
| 134 |
+
confs = [int(probs[i] * 100) for i in top_idx]
|
| 135 |
+
elif hasattr(model, "decision_function"):
|
| 136 |
+
scores = model.decision_function(input_df)[0]
|
| 137 |
+
top_idx = np.argsort(scores)[-5:][::-1]
|
| 138 |
+
tags = [mlb.classes_[i] for i in top_idx]
|
| 139 |
+
confs = [None] * 5
|
| 140 |
+
else:
|
| 141 |
+
preds = model.predict(input_df)
|
| 142 |
+
tags = mlb.inverse_transform(preds)[0]
|
| 143 |
+
confs = [None] * len(tags)
|
| 144 |
+
|
| 145 |
+
# Output
|
| 146 |
+
st.markdown('<div class="output-box"><h4>๐ท๏ธ Predicted Tags:</h4>', unsafe_allow_html=True)
|
| 147 |
+
for tag, conf in zip(tags, confs):
|
| 148 |
+
confidence = f" ({conf}%)" if conf is not None else ""
|
| 149 |
+
st.markdown(f'<span class="tag-pill">{tag}{confidence}</span>', unsafe_allow_html=True)
|
| 150 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 151 |
+
except Exception as e:
|
| 152 |
+
st.error(f"Prediction error: {e}")
|
| 153 |
+
|
| 154 |
+
# Footer
|
| 155 |
+
st.markdown("""
|
| 156 |
+
<div class="footer">
|
| 157 |
+
๐ This ML tool predicts tags based on programming question content.
|
| 158 |
+
</div>
|
| 159 |
+
""", unsafe_allow_html=True)
|