Team Ai
Apppublic

Harshitha-01/Stack_Overflow

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py70 linesDownload Raw Back to root
1import streamlit as st2import pickle3import re4import numpy as np5 6# --- Page Configuration ---7st.set_page_config(page_title="Stack Overflow Tags Predictor", layout="centered")8 9# --- Text Preprocessing ---10def clean_text(text):11    text = re.sub(r"<.*?>", " ", text)  # Remove HTML tags12    text = re.sub(r"\W", " ", text)     # Remove special characters13    text = re.sub(r"\s+", " ", text.lower())  # Normalize whitespace and lowercase14    return text.strip()15 16# --- Load Model and Artifacts ---17@st.cache_resource18def load_artifacts():19    with open("model.pkl", "rb") as f:20        model = pickle.load(f)21    with open("tfidf.pkl", "rb") as f:22        vectorizer = pickle.load(f)23    with open("mlb.pkl", "rb") as f:24        mlb = pickle.load(f)25    return model, vectorizer, mlb26 27# Load once28model, vectorizer, mlb = load_artifacts()29 30# --- Sidebar ---31st.sidebar.header("๐Ÿ“˜ Instructions")32st.sidebar.markdown("""331. Enter a relevant **question title** and **description**.  342. Click **Predict Tags** to see suggestions.  353. The model works best on programming-related questions.36""")37st.sidebar.markdown("---")38 39# --- App Title and Description ---40st.title("๐Ÿ”– Stack Overflow Tags Predictor")41st.markdown("Enter a question's **title** and **description**, and this app will suggest relevant programming tags based on the content.")42 43# --- User Input ---44title = st.text_input("๐Ÿ“ Question Title", placeholder="E.g. How to fix null pointer exception in Java?")45body = st.text_area("๐Ÿ“„ Question Description", placeholder="Describe your issue with details like code, errors, or what you've tried.", height=200)46 47# --- Predict Button ---48if st.button("๐Ÿ” Predict Tags"):49    if not title.strip() or not body.strip():50        st.warning("โš ๏ธ Please enter both a question title and description.")51    else:52        input_text = clean_text(title + " " + body)53        X_input = vectorizer.transform([input_text])54 55        # Predict without threshold56        y_pred = model.predict(X_input)57        predicted_tags = mlb.inverse_transform(y_pred)58 59        st.markdown("---")60        st.subheader("๐Ÿท๏ธ Suggested Tags")61 62        if predicted_tags and predicted_tags[0]:63            st.success("โœ… " + ", ".join(predicted_tags[0]))64        else:65            st.info("๐Ÿค” No tags were predicted. Try rephrasing your question.")66 67# --- Footer ---68st.markdown("---")69st.markdown("<p style='text-align:center; font-size:14px;'>Made with โค๏ธ using Scikit-learn and Streamlit</p>", unsafe_allow_html=True)70