Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,53 @@ import pickle
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
# Load saved
|
| 6 |
-
with open("vectorizer
|
| 7 |
vectorizer = pickle.load(f)
|
| 8 |
|
| 9 |
-
with open("model
|
| 10 |
model = pickle.load(f)
|
| 11 |
|
| 12 |
-
with open("binarizer
|
| 13 |
mlb = pickle.load(f)
|
| 14 |
|
| 15 |
st.title("π Stack Overflow Tags Predictor")
|
| 16 |
-
st.markdown("Enter a question title and description.
|
| 17 |
|
|
|
|
| 18 |
title = st.text_input("π Enter Question Title")
|
| 19 |
description = st.text_area("π Enter Question Description", height=150)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def predict_tags_auto(title, description, threshold=0.2):
|
| 25 |
input_text = title + " " + description
|
| 26 |
input_vector = vectorizer.transform([input_text])
|
| 27 |
|
| 28 |
-
# Get
|
| 29 |
probas = model.predict_proba(input_vector)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
probas_array = np.array([p[
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
predicted_binary = (probas_array
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Convert binary
|
| 38 |
tags = mlb.inverse_transform(predicted_binary)
|
| 39 |
return tags[0] if tags else []
|
| 40 |
|
| 41 |
-
|
| 42 |
if st.button("Predict Tags"):
|
| 43 |
if not title.strip() or not description.strip():
|
| 44 |
st.warning("β οΈ Please enter both title and description.")
|
| 45 |
else:
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
-
st.success("β
Predicted Tags: " + ", ".join(
|
| 49 |
else:
|
| 50 |
st.info("βΉοΈ No tags predicted. Try refining your question.")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Load saved model, vectorizer, and binarizer
|
| 6 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 7 |
vectorizer = pickle.load(f)
|
| 8 |
|
| 9 |
+
with open("model.pkl", "rb") as f:
|
| 10 |
model = pickle.load(f)
|
| 11 |
|
| 12 |
+
with open("binarizer.pkl", "rb") as f:
|
| 13 |
mlb = pickle.load(f)
|
| 14 |
|
| 15 |
st.title("π Stack Overflow Tags Predictor")
|
| 16 |
+
st.markdown("Enter a question title and description. The top 3 most relevant tags will be predicted automatically.")
|
| 17 |
|
| 18 |
+
# Input fields
|
| 19 |
title = st.text_input("π Enter Question Title")
|
| 20 |
description = st.text_area("π Enter Question Description", height=150)
|
| 21 |
|
| 22 |
+
# Function to predict top N tags (e.g., top 3)
|
| 23 |
+
def predict_tags_top_n(title, description, top_n=3):
|
|
|
|
|
|
|
| 24 |
input_text = title + " " + description
|
| 25 |
input_vector = vectorizer.transform([input_text])
|
| 26 |
|
| 27 |
+
# Get probability estimates from each classifier
|
| 28 |
probas = model.predict_proba(input_vector)
|
| 29 |
|
| 30 |
+
# Extract positive class probabilities for each tag
|
| 31 |
+
probas_array = np.array([p[0][1] for p in probas]) # shape: (n_classes,)
|
| 32 |
+
|
| 33 |
+
# Get indices of top N tags
|
| 34 |
+
top_indices = probas_array.argsort()[-top_n:][::-1]
|
| 35 |
|
| 36 |
+
# Build binary array for top tags
|
| 37 |
+
predicted_binary = np.zeros_like(probas_array, dtype=int)
|
| 38 |
+
predicted_binary[top_indices] = 1
|
| 39 |
+
predicted_binary = predicted_binary.reshape(1, -1)
|
| 40 |
|
| 41 |
+
# Convert binary to tag names
|
| 42 |
tags = mlb.inverse_transform(predicted_binary)
|
| 43 |
return tags[0] if tags else []
|
| 44 |
|
| 45 |
+
# Predict and display
|
| 46 |
if st.button("Predict Tags"):
|
| 47 |
if not title.strip() or not description.strip():
|
| 48 |
st.warning("β οΈ Please enter both title and description.")
|
| 49 |
else:
|
| 50 |
+
predicted_tags = predict_tags_top_n(title, description, top_n=3)
|
| 51 |
+
if predicted_tags:
|
| 52 |
+
st.success("β
Predicted Tags: " + ", ".join(predicted_tags))
|
| 53 |
else:
|
| 54 |
st.info("βΉοΈ No tags predicted. Try refining your question.")
|