Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import pickle
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
# Load saved model, vectorizer, and binarizer
|
| 6 |
with open("vectorizer (3).pkl", "rb") as f:
|
| 7 |
vectorizer = pickle.load(f)
|
| 8 |
|
|
@@ -12,37 +12,37 @@ with open("model (6).pkl", "rb") as f:
|
|
| 12 |
with open("binarizer (3).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. Tags will be predicted automatically based on model confidence.")
|
| 17 |
|
| 18 |
title = st.text_input("π Enter Question Title")
|
| 19 |
description = st.text_area("π Enter Question Description", height=150)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
threshold = 0.4 # You can tweak this value to make it more or less strict
|
| 23 |
-
|
| 24 |
def predict_tags_auto(title, description, threshold=0.4):
|
| 25 |
input_text = title + " " + description
|
| 26 |
input_vector = vectorizer.transform([input_text])
|
| 27 |
-
|
| 28 |
-
# Get probabilities
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Convert list of (1, n_classes)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
# Apply threshold
|
| 35 |
-
predicted_binary = (
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
tags = mlb.inverse_transform(predicted_binary)
|
| 39 |
-
return tags[0] if tags else []
|
| 40 |
|
|
|
|
| 41 |
if st.button("Predict Tags"):
|
| 42 |
if not title.strip() or not description.strip():
|
| 43 |
st.warning("β οΈ Please enter both title and description.")
|
| 44 |
else:
|
| 45 |
-
tags = predict_tags_auto(title, description)
|
| 46 |
if tags:
|
| 47 |
st.success("β
Predicted Tags: " + ", ".join(tags))
|
| 48 |
else:
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# --- Load saved model, vectorizer, and binarizer ---
|
| 6 |
with open("vectorizer (3).pkl", "rb") as f:
|
| 7 |
vectorizer = pickle.load(f)
|
| 8 |
|
|
|
|
| 12 |
with open("binarizer (3).pkl", "rb") as f:
|
| 13 |
mlb = pickle.load(f)
|
| 14 |
|
| 15 |
+
# --- Streamlit App UI ---
|
| 16 |
st.title("π Stack Overflow Tags Predictor")
|
| 17 |
st.markdown("Enter a question title and description. Tags will be predicted automatically based on model confidence.")
|
| 18 |
|
| 19 |
title = st.text_input("π Enter Question Title")
|
| 20 |
description = st.text_area("π Enter Question Description", height=150)
|
| 21 |
|
| 22 |
+
# --- Prediction Logic ---
|
|
|
|
|
|
|
| 23 |
def predict_tags_auto(title, description, threshold=0.4):
|
| 24 |
input_text = title + " " + description
|
| 25 |
input_vector = vectorizer.transform([input_text])
|
| 26 |
+
|
| 27 |
+
# Get class probabilities from model
|
| 28 |
+
probas_list = model.predict_proba(input_vector) # List of arrays
|
| 29 |
+
|
| 30 |
+
# Convert list of (1, n_classes) to shape (1, n_classes)
|
| 31 |
+
proba_matrix = np.hstack([p[:, 1] if p.shape[1] == 2 else p[:, 0] for p in probas_list])
|
| 32 |
+
|
| 33 |
# Apply threshold
|
| 34 |
+
predicted_binary = (proba_matrix >= threshold).astype(int).reshape(1, -1)
|
| 35 |
+
|
| 36 |
+
# Decode binary predictions to tag labels
|
| 37 |
tags = mlb.inverse_transform(predicted_binary)
|
| 38 |
+
return tags[0] if tags and len(tags[0]) > 0 else []
|
| 39 |
|
| 40 |
+
# --- Predict Button ---
|
| 41 |
if st.button("Predict Tags"):
|
| 42 |
if not title.strip() or not description.strip():
|
| 43 |
st.warning("β οΈ Please enter both title and description.")
|
| 44 |
else:
|
| 45 |
+
tags = predict_tags_auto(title, description, threshold=0.4)
|
| 46 |
if tags:
|
| 47 |
st.success("β
Predicted Tags: " + ", ".join(tags))
|
| 48 |
else:
|