Spaces:
Build error
Build error
Update pages/model.py
Browse files- pages/model.py +67 -67
pages/model.py
CHANGED
|
@@ -1,80 +1,80 @@
|
|
| 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 -
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@st.cache_resource
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
st.
|
| 40 |
-
|
| 41 |
-
|
| 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 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
if hasattr(
|
| 53 |
-
probs =
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pickle
|
| 3 |
import re
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="๐ TagGPT - Stack Overflow Tag Generator", layout="centered")
|
| 8 |
|
| 9 |
+
def clean_text(text):
|
| 10 |
+
text = re.sub(r"<.*?>", " ", text)
|
| 11 |
+
text = re.sub(r"[^a-zA-Z0-9\s]", " ", text)
|
| 12 |
+
text = re.sub(r"\s+", " ", text.lower()).strip()
|
| 13 |
+
return text
|
| 14 |
|
| 15 |
@st.cache_resource
|
| 16 |
+
def load_model_assets():
|
| 17 |
+
try:
|
| 18 |
+
with open("model.pkl", "rb") as m:
|
| 19 |
+
model = pickle.load(m)
|
| 20 |
+
with open("tfidf.pkl", "rb") as v:
|
| 21 |
+
vectorizer = pickle.load(v)
|
| 22 |
+
with open("mlb.pkl", "rb") as e:
|
| 23 |
+
encoder = pickle.load(e)
|
| 24 |
+
return model, vectorizer, encoder
|
| 25 |
+
except Exception as err:
|
| 26 |
+
st.error(f"โ Failed to load model assets: {err}")
|
| 27 |
+
st.stop()
|
| 28 |
+
|
| 29 |
+
model, vectorizer, encoder = load_model_assets()
|
| 30 |
+
|
| 31 |
+
st.title("๐ก TagGPT")
|
| 32 |
+
st.markdown("โจ _Auto-tag your Stack Overflow questions using AI_")
|
| 33 |
+
|
| 34 |
+
q_title = st.text_input("๐ **Question Title**")
|
| 35 |
+
q_body = st.text_area("๐ **Detailed Description**", height=200)
|
| 36 |
+
|
| 37 |
+
if st.button("๐ฎ Generate Tags"):
|
| 38 |
+
if not q_title.strip() or not q_body.strip():
|
| 39 |
+
st.warning("โ ๏ธ Please enter both title and description.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
+
user_input = clean_text(q_title + " " + q_body)
|
| 42 |
+
transformed = vectorizer.transform([user_input])
|
| 43 |
|
| 44 |
try:
|
| 45 |
+
if hasattr(model, "predict_proba"):
|
| 46 |
+
probs = model.predict_proba(transformed)[0]
|
| 47 |
+
tag_names = encoder.classes_
|
| 48 |
+
tag_conf_df = pd.DataFrame({
|
| 49 |
+
"Tag": tag_names,
|
| 50 |
+
"Confidence": probs
|
| 51 |
+
}).sort_values("Confidence", ascending=False)
|
| 52 |
+
|
| 53 |
+
top_tags = tag_conf_df[tag_conf_df["Confidence"] > 0.3] # adjustable threshold
|
| 54 |
+
|
| 55 |
+
if not top_tags.empty:
|
| 56 |
+
st.success("๐ท๏ธ **Top Suggested Tags with Confidence:**")
|
| 57 |
+
st.dataframe(top_tags, use_container_width=True)
|
| 58 |
+
|
| 59 |
+
# Download Button
|
| 60 |
+
csv = top_tags.to_csv(index=False).encode('utf-8')
|
| 61 |
+
st.download_button("โฌ๏ธ Download Tags as CSV", csv, "predicted_tags.csv", "text/csv")
|
| 62 |
+
|
| 63 |
+
else:
|
| 64 |
+
st.info("๐ค No high-confidence tags predicted. Try improving the input.")
|
| 65 |
else:
|
| 66 |
+
preds = model.predict(transformed)
|
| 67 |
+
tags = encoder.inverse_transform(preds)
|
| 68 |
+
|
| 69 |
+
if tags and tags[0]:
|
| 70 |
+
st.success("๐ท๏ธ **Predicted Tags:**")
|
| 71 |
+
tag_list = ", ".join(tags[0])
|
| 72 |
+
st.markdown("๐ธ " + tag_list)
|
| 73 |
+
|
| 74 |
+
# Download plain text
|
| 75 |
+
txt = tag_list.encode('utf-8')
|
| 76 |
+
st.download_button("โฌ๏ธ Download Tags as TXT", txt, "predicted_tags.txt", "text/plain")
|
| 77 |
+
else:
|
| 78 |
+
st.info("๐ค No tags predicted.")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error(f"๐ซ Prediction failed: {e}")
|