STACKOVERFLOW2 / app.py
sree4411's picture
Update app.py
0040a68 verified
import streamlit as st
import joblib
# โœ… MUST be first Streamlit command
st.set_page_config(page_title="Stack Overflow Tag Predictor", layout="centered")
# โœ… Define your custom tokenizer BEFORE loading the vectorizer
def customs_tokenization(text):
return text.split()
# --- Load model and vectorizers ---
@st.cache_resource
def load_pickle(path):
return joblib.load(path)
model = load_pickle("compressed_logistic_reg1 (1).pkl")
vectorizer = load_pickle("tfidf_vectorizer (2).pkl")
mlb = load_pickle("multilabel_binarizer (1).pkl")
# --- Prediction Function ---
def predict_tags(title, description):
title, description = title.strip(), description.strip()
if not title or not description:
return "โš ๏ธ Please enter both title and description."
full_text = title + " " + description
vectorized = vectorizer.transform([full_text])
prediction = model.predict(vectorized)
tags = mlb.inverse_transform(prediction)
if tags and tags[0]:
return "โœ… Predicted Tags: " + ", ".join(tags[0])
else:
return "โ„น๏ธ No tags predicted. Try refining your question."
# --- Streamlit UI ---
st.title("๐Ÿ”– Stack Overflow Tag Predictor")
st.markdown("Enter a question title and description to receive relevant tag suggestions.")
# Input fields
title_input = st.text_input("Question Title", placeholder="e.g. How to merge dictionaries in Python?")
desc_input = st.text_area("Question Description", placeholder="e.g. I have two dictionaries and I want to merge them...")
# Predict button
if st.button("Predict Tags"):
result = predict_tags(title_input, desc_input)
st.markdown(f"### Result:\n{result}")