Update pages/model.py
Browse files- pages/model.py +39 -43
pages/model.py
CHANGED
|
@@ -3,62 +3,58 @@ import pickle
|
|
| 3 |
import re
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
st.set_page_config(page_title="TagGPT -
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
return text
|
| 14 |
|
| 15 |
-
# ---------- Load Model Assets ----------
|
| 16 |
@st.cache_resource
|
| 17 |
-
def
|
| 18 |
try:
|
| 19 |
-
with open("model.pkl", "rb") as
|
| 20 |
-
|
| 21 |
-
with open("tfidf.pkl", "rb") as
|
| 22 |
-
|
| 23 |
-
with open("mlb.pkl", "rb") as
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
-
except Exception as
|
| 27 |
-
st.error(f"β
|
| 28 |
st.stop()
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
st.
|
| 34 |
-
st.markdown("
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
if
|
| 42 |
-
|
| 43 |
-
st.warning("π¨ Please fill in both the title and description.")
|
| 44 |
else:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
tag_preds = (tag_probs >= threshold).astype(int)
|
| 53 |
else:
|
| 54 |
-
tag_preds =
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
if
|
| 59 |
-
st.success("
|
| 60 |
-
st.
|
| 61 |
else:
|
| 62 |
-
st.info("π€ No tags predicted. Try
|
| 63 |
-
except Exception as
|
| 64 |
-
st.error(f"
|
|
|
|
| 3 |
import re
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="π TagGPT - Auto Tag Your Questions", layout="centered")
|
| 7 |
|
| 8 |
+
def clean_text(raw_text):
|
| 9 |
+
raw_text = re.sub(r"<.*?>", " ", raw_text) # Remove HTML tags
|
| 10 |
+
raw_text = re.sub(r"[^a-zA-Z0-9\s]", " ", raw_text) # Remove special characters
|
| 11 |
+
raw_text = re.sub(r"\s+", " ", raw_text.lower()).strip() # Lowercase & strip spaces
|
| 12 |
+
return raw_text
|
|
|
|
| 13 |
|
|
|
|
| 14 |
@st.cache_resource
|
| 15 |
+
def load_assets():
|
| 16 |
try:
|
| 17 |
+
with open("model.pkl", "rb") as m:
|
| 18 |
+
tag_model = pickle.load(m)
|
| 19 |
+
with open("tfidf.pkl", "rb") as v:
|
| 20 |
+
text_vectorizer = pickle.load(v)
|
| 21 |
+
with open("mlb.pkl", "rb") as e:
|
| 22 |
+
tag_encoder = pickle.load(e)
|
| 23 |
+
return tag_model, text_vectorizer, tag_encoder
|
| 24 |
+
except Exception as error:
|
| 25 |
+
st.error(f"β Failed to load model components: {error}")
|
| 26 |
st.stop()
|
| 27 |
|
| 28 |
+
tag_model, text_vectorizer, tag_encoder = load_assets()
|
| 29 |
|
| 30 |
+
st.title("π― TagGPT - Smart Stack Overflow Tag Suggester")
|
| 31 |
+
st.markdown("π **Automatically generate relevant tags for your coding questions.**")
|
| 32 |
+
st.markdown("Just enter your question title and description, and let AI do the tagging!")
|
| 33 |
|
| 34 |
+
question_title = st.text_input("π§ Enter the **Question Title**")
|
| 35 |
+
question_description = st.text_area("π Provide a **Detailed Description**", height=200)
|
| 36 |
+
confidence_threshold = st.slider("π Select Tag Confidence Threshold", 0.1, 0.9, 0.3, 0.05)
|
| 37 |
|
| 38 |
+
if st.button("π Predict Tags"):
|
| 39 |
+
if not question_title.strip() or not question_description.strip():
|
| 40 |
+
st.warning("β οΈ Both title and description are required!")
|
|
|
|
| 41 |
else:
|
| 42 |
+
combined_input = clean_text(question_title + " " + question_description)
|
| 43 |
+
transformed_input = text_vectorizer.transform([combined_input])
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
if hasattr(tag_model, "predict_proba"):
|
| 47 |
+
probs = tag_model.predict_proba(transformed_input)
|
| 48 |
+
tag_preds = (probs >= confidence_threshold).astype(int)
|
|
|
|
| 49 |
else:
|
| 50 |
+
tag_preds = tag_model.predict(transformed_input)
|
| 51 |
|
| 52 |
+
final_tags = tag_encoder.inverse_transform(tag_preds)
|
| 53 |
|
| 54 |
+
if final_tags and final_tags[0]:
|
| 55 |
+
st.success("π·οΈ **Predicted Tags:**")
|
| 56 |
+
st.markdown("πΈ " + ", ".join(final_tags[0]))
|
| 57 |
else:
|
| 58 |
+
st.info("π€ No tags predicted. Try adjusting the threshold or refining your input.")
|
| 59 |
+
except Exception as error:
|
| 60 |
+
st.error(f"π« Prediction failed: {error}")
|