Update pages/model.py
Browse files- pages/model.py +53 -33
pages/model.py
CHANGED
|
@@ -1,60 +1,80 @@
|
|
|
|
|
| 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 - Auto Tag Your Questions", layout="centered")
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
if st.button("π Predict Tags"):
|
| 39 |
-
if not
|
| 40 |
-
st.warning("β οΈ
|
| 41 |
else:
|
| 42 |
-
combined_input = clean_text(
|
| 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 |
-
|
| 49 |
else:
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
if
|
| 55 |
-
st.success("π·οΈ **Predicted Tags:**")
|
| 56 |
-
|
|
|
|
| 57 |
else:
|
| 58 |
-
st.info("π€ No tags predicted. Try
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as error:
|
| 60 |
st.error(f"π« Prediction failed: {error}")
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
import pickle
|
| 4 |
import re
|
| 5 |
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from io import StringIO
|
| 8 |
+
from streamlit_lottie import st_lottie
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
st.set_page_config(page_title="π TagGPT - Auto Tag Your Questions", layout="centered")
|
| 12 |
|
| 13 |
+
def load_lottie(filepath: str):
|
| 14 |
+
with open(filepath, "r") as f:
|
| 15 |
+
return json.load(f)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@st.cache_resource
|
| 18 |
+
|
| 19 |
def load_assets():
|
| 20 |
+
with open("model.pkl", "rb") as m:
|
| 21 |
+
tag_model = pickle.load(m)
|
| 22 |
+
with open("tfidf.pkl", "rb") as v:
|
| 23 |
+
text_vectorizer = pickle.load(v)
|
| 24 |
+
with open("mlb.pkl", "rb") as e:
|
| 25 |
+
tag_encoder = pickle.load(e)
|
| 26 |
+
return tag_model, text_vectorizer, tag_encoder
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
tag_model, text_vectorizer, tag_encoder = load_assets()
|
| 29 |
|
| 30 |
+
def clean_text(raw_text):
|
| 31 |
+
raw_text = re.sub(r"<.*?>", " ", raw_text) # Remove HTML tags
|
| 32 |
+
raw_text = re.sub(r"[^a-zA-Z0-9\s]", " ", raw_text) # Remove special characters
|
| 33 |
+
raw_text = re.sub(r"\s+", " ", raw_text.lower()).strip()
|
| 34 |
+
return raw_text
|
| 35 |
+
|
| 36 |
+
st_lottie(load_lottie("tag_animation.json"), height=150, key="intro") # Lottie animation (optional)
|
| 37 |
|
| 38 |
+
st.title("π·οΈ TagGPT - Smart Stack Overflow Tag Suggester")
|
| 39 |
+
st.markdown("**π Instantly generate relevant tags for your coding questions using AI.**")
|
| 40 |
+
|
| 41 |
+
q_title = st.text_input("π§ Enter your question title")
|
| 42 |
+
q_desc = st.text_area("π Describe your problem in detail", height=200)
|
| 43 |
|
| 44 |
if st.button("π Predict Tags"):
|
| 45 |
+
if not q_title.strip() or not q_desc.strip():
|
| 46 |
+
st.warning("β οΈ Please provide both title and description.")
|
| 47 |
else:
|
| 48 |
+
combined_input = clean_text(q_title + " " + q_desc)
|
| 49 |
transformed_input = text_vectorizer.transform([combined_input])
|
| 50 |
|
| 51 |
try:
|
| 52 |
if hasattr(tag_model, "predict_proba"):
|
| 53 |
+
probs = tag_model.predict_proba(transformed_input)[0]
|
| 54 |
+
pred_tags = (probs >= 0.3).astype(int) # Fixed threshold
|
| 55 |
else:
|
| 56 |
+
pred_tags = tag_model.predict(transformed_input)[0]
|
| 57 |
+
probs = pred_tags
|
| 58 |
|
| 59 |
+
tag_names = tag_encoder.classes_
|
| 60 |
+
selected_tags = [(tag, round(probs[i]*100, 2)) for i, tag in enumerate(tag_names) if pred_tags[i] == 1]
|
| 61 |
|
| 62 |
+
if selected_tags:
|
| 63 |
+
st.success("π·οΈ **Predicted Tags with Confidence:**")
|
| 64 |
+
for tag, score in selected_tags:
|
| 65 |
+
st.markdown(f"**{tag}** β `{score}%`")
|
| 66 |
else:
|
| 67 |
+
st.info("π€ No tags predicted. Try refining your input.")
|
| 68 |
+
|
| 69 |
+
# --- Download Option
|
| 70 |
+
tag_list = [tag for tag, _ in selected_tags]
|
| 71 |
+
df = pd.DataFrame({
|
| 72 |
+
"Title": [q_title],
|
| 73 |
+
"Description": [q_desc],
|
| 74 |
+
"Predicted Tags": [", ".join(tag_list)]
|
| 75 |
+
})
|
| 76 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 77 |
+
st.download_button("π₯ Download Tags as CSV", data=csv, file_name="tag_predictions.csv", mime='text/csv')
|
| 78 |
+
|
| 79 |
except Exception as error:
|
| 80 |
st.error(f"π« Prediction failed: {error}")
|