Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import re
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Streamlit page configuration
|
| 7 |
+
st.set_page_config(page_title="Stack Overflow Tags Predictor", layout="centered")
|
| 8 |
+
|
| 9 |
+
# β
Text preprocessing
|
| 10 |
+
def clean_text(text):
|
| 11 |
+
text = re.sub(r"<.*?>", " ", text) # Remove HTML tags
|
| 12 |
+
text = re.sub(r"\W", " ", text) # Remove special characters
|
| 13 |
+
text = re.sub(r"\s+", " ", text.lower()).strip() # Normalize whitespace and lowercase
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
# β
Load pickled model, vectorizer, and label binarizer
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_artifacts():
|
| 19 |
+
with open("model (1).pkl", "rb") as f:
|
| 20 |
+
model = pickle.load(f)
|
| 21 |
+
with open("tfidf (1).pkl", "rb") as f:
|
| 22 |
+
vectorizer = pickle.load(f)
|
| 23 |
+
with open("mlb (1).pkl", "rb") as f:
|
| 24 |
+
mlb = pickle.load(f)
|
| 25 |
+
return model, vectorizer, mlb
|
| 26 |
+
|
| 27 |
+
# Load artifacts
|
| 28 |
+
model, vectorizer, mlb = load_artifacts()
|
| 29 |
+
|
| 30 |
+
# UI
|
| 31 |
+
st.title("π Stack Overflow Tags Predictor")
|
| 32 |
+
st.markdown("Enter a question's *title* and *description*, and this app will suggest relevant tags.")
|
| 33 |
+
|
| 34 |
+
# User Inputs
|
| 35 |
+
title = st.text_input("π Question Title")
|
| 36 |
+
body = st.text_area("π Question Description", height=200)
|
| 37 |
+
|
| 38 |
+
# Prediction Button
|
| 39 |
+
if st.button("π Predict Tags"):
|
| 40 |
+
if not title.strip() or not body.strip():
|
| 41 |
+
st.warning("β Please enter both a title and a description.")
|
| 42 |
+
else:
|
| 43 |
+
input_text = clean_text(title + " " + body)
|
| 44 |
+
X_input = vectorizer.transform([input_text])
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
y_pred = model.predict(X_input)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"β Prediction failed: {e}")
|
| 50 |
+
y_pred = None
|
| 51 |
+
|
| 52 |
+
if y_pred is not None:
|
| 53 |
+
predicted_tags = mlb.inverse_transform(y_pred)
|
| 54 |
+
|
| 55 |
+
if predicted_tags and predicted_tags[0]:
|
| 56 |
+
st.success("β
Predicted Tags:")
|
| 57 |
+
st.write(", ".join(predicted_tags[0]))
|
| 58 |
+
else:
|
| 59 |
+
st.info("π€ No tags predicted. Try refining your question.")
|