Update pages/model.py
Browse files- pages/model.py +53 -0
pages/model.py
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import re
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="TagGPT - Stack Overflow Tag Predictor", layout="centered")
|
| 7 |
+
|
| 8 |
+
def preprocess_input(text):
|
| 9 |
+
text = re.sub(r"<.*?>", " ", text)
|
| 10 |
+
text = re.sub(r"[^a-zA-Z0-9\s]", " ", text)
|
| 11 |
+
text = re.sub(r"\s+", " ", text.lower()).strip()
|
| 12 |
+
return text
|
| 13 |
+
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_model_assets():
|
| 16 |
+
with open("model.pkl", "rb") as f:
|
| 17 |
+
model = pickle.load(f)
|
| 18 |
+
with open("tfidf.pkl", "rb") as f:
|
| 19 |
+
vectorizer = pickle.load(f)
|
| 20 |
+
with open("mlb.pkl", "rb") as f:
|
| 21 |
+
encoder = pickle.load(f)
|
| 22 |
+
return model, vectorizer, encoder
|
| 23 |
+
|
| 24 |
+
model, vectorizer, encoder = load_model_assets()
|
| 25 |
+
|
| 26 |
+
st.title("π TagGPT: Predict Stack Overflow Tags")
|
| 27 |
+
st.markdown("Smartly auto-tag your programming questions using ML.")
|
| 28 |
+
|
| 29 |
+
q_title = st.text_input("π Enter Question Title")
|
| 30 |
+
q_body = st.text_area("π Describe Your Problem", height=200)
|
| 31 |
+
threshold = st.slider("π― Prediction Threshold", 0.1, 0.9, 0.3, 0.05)
|
| 32 |
+
|
| 33 |
+
if st.button("π Generate Tags"):
|
| 34 |
+
if not q_title.strip() or not q_body.strip():
|
| 35 |
+
st.warning("π¨ Please fill in both the title and description.")
|
| 36 |
+
else:
|
| 37 |
+
user_query = preprocess_input(q_title + " " + q_body)
|
| 38 |
+
X_transformed = vectorizer.transform([user_query])
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
tag_probs = model.predict_proba(X_transformed)
|
| 42 |
+
tag_preds = (tag_probs >= threshold).astype(int)
|
| 43 |
+
except AttributeError:
|
| 44 |
+
st.warning("β Model doesn't support probability output. Falling back to `predict`.")
|
| 45 |
+
tag_preds = model.predict(X_transformed)
|
| 46 |
+
|
| 47 |
+
predicted = encoder.inverse_transform(tag_preds)
|
| 48 |
+
|
| 49 |
+
if predicted and predicted[0]:
|
| 50 |
+
st.success("β
Predicted Tags:")
|
| 51 |
+
st.write(", ".join(predicted[0]))
|
| 52 |
+
else:
|
| 53 |
+
st.info("π€ No tags predicted. Try refining your input or lowering the threshold.")
|