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