Meghana-16/Stack_Over_Flow
0
1import streamlit as st2import pickle3import re4import numpy as np5 6 7# Streamlit page configuration8st.set_page_config(page_title="Stack Overflow Tags Predictor", layout="centered")9 10# ✅ Text preprocessing11def clean_text(text):12 text = re.sub(r"<.*?>", " ", text) # Remove HTML tags13 text = re.sub(r"\W", " ", text) # Remove special characters14 text = re.sub(r"\s+", " ", text.lower()).strip() # Normalize whitespace and lowercase15 return text16 17# ✅ Load pickled model, vectorizer, and label binarizer18@st.cache_resource19def load_artifacts():20 with open("model12.pkl", "rb") as f:21 model = pickle.load(f)22 with open("tfidf12.pkl", "rb") as f:23 vectorizer = pickle.load(f)24 with open("mlb12.pkl", "rb") as f:25 mlb = pickle.load(f)26 return model, vectorizer, mlb27 28# Load artifacts29model, vectorizer, mlb = load_artifacts()30 31# UI32st.title("🔖 Stack Overflow Tags Predictor")33st.markdown("Enter a question's *title* and *description*, and this app will suggest relevant tags.")34 35# User Inputs36title = st.text_input("📝 Question Title")37body = st.text_area("📄 Question Description", height=200)38 39# Optional: Add a threshold slider40threshold = st.slider("🔧 Tag Confidence Threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.05)41 42# Prediction Button43if st.button("🔍 Predict Tags"):44 if not title.strip() or not body.strip():45 st.warning("⚠ Please enter both a title and a description.")46 else:47 input_text = clean_text(title + " " + body)48 X_input = vectorizer.transform([input_text])49 50 try:51 # Use predict_proba and apply threshold52 y_prob = model.predict_proba(X_input)53 y_pred = (y_prob >= threshold).astype(int)54 except AttributeError:55 st.warning("⚠ Model does not support `predict_proba`. Using default `predict` method.")56 y_pred = model.predict(X_input)57 58 predicted_tags = mlb.inverse_transform(y_pred)59 60 if predicted_tags and predicted_tags[0]:61 st.success("✅ Predicted Tags:")62 st.write(", ".join(predicted_tags[0]))63 else:64 st.info("🤔 No tags predicted. Try refining your question or lowering the threshold.")