Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
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:
|
|
@@ -13,42 +12,25 @@ 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.
|
| 17 |
|
| 18 |
-
# Input fields
|
| 19 |
title = st.text_input("π Enter Question Title")
|
| 20 |
description = st.text_area("π Enter Question Description", height=150)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
input_text = title + " " + description
|
| 25 |
input_vector = vectorizer.transform([input_text])
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 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 |
-
|
| 48 |
-
|
|
|
|
| 49 |
else:
|
| 50 |
-
|
| 51 |
-
if predicted_tags:
|
| 52 |
-
st.success("β
Predicted Tags: " + ", ".join(predicted_tags))
|
| 53 |
-
else:
|
| 54 |
-
st.info("βΉοΈ No tags predicted. Try refining your question.")
|
|
|
|
| 1 |
import pickle
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
|
| 4 |
# Load saved model, vectorizer, and binarizer
|
| 5 |
with open("vectorizer (3).pkl", "rb") as f:
|
|
|
|
| 12 |
mlb = pickle.load(f)
|
| 13 |
|
| 14 |
st.title("π Stack Overflow Tags Predictor")
|
| 15 |
+
st.markdown("Enter a question title and description. Tags will be predicted automatically.")
|
| 16 |
|
|
|
|
| 17 |
title = st.text_input("π Enter Question Title")
|
| 18 |
description = st.text_area("π Enter Question Description", height=150)
|
| 19 |
|
| 20 |
+
def predict_tags(title, description):
|
| 21 |
+
if not title.strip() or not description.strip():
|
| 22 |
+
return []
|
| 23 |
input_text = title + " " + description
|
| 24 |
input_vector = vectorizer.transform([input_text])
|
| 25 |
+
|
| 26 |
+
# Predict tags using the model's internal default threshold
|
| 27 |
+
predicted_binary = model.predict(input_vector)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
tags = mlb.inverse_transform(predicted_binary)
|
| 29 |
return tags[0] if tags else []
|
| 30 |
|
|
|
|
| 31 |
if st.button("Predict Tags"):
|
| 32 |
+
tags = predict_tags(title, description)
|
| 33 |
+
if tags:
|
| 34 |
+
st.success("β
Predicted Tags: " + ", ".join(tags))
|
| 35 |
else:
|
| 36 |
+
st.info("βΉοΈ No tags predicted. Try refining your question.")
|
|
|
|
|
|
|
|
|
|
|
|