Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,49 @@
|
|
| 1 |
import pickle
|
| 2 |
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return text.lower().split()
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# π Load model files
|
| 14 |
-
try:
|
| 15 |
-
with open("vectorizer (3).pkl", "rb") as f:
|
| 16 |
-
vectorizer = pickle.load(f)
|
| 17 |
-
|
| 18 |
-
with open("model (6).pkl", "rb") as f:
|
| 19 |
-
model = pickle.load(f)
|
| 20 |
-
|
| 21 |
-
with open("binarizer (3).pkl", "rb") as f:
|
| 22 |
-
mlb = pickle.load(f)
|
| 23 |
-
|
| 24 |
-
except Exception as e:
|
| 25 |
-
st.error(f"β Error loading model files: {str(e)}")
|
| 26 |
-
st.stop()
|
| 27 |
-
|
| 28 |
-
# π§ Prediction function
|
| 29 |
-
def predict_tags(title, description):
|
| 30 |
-
try:
|
| 31 |
-
if not title.strip() or not description.strip():
|
| 32 |
-
return "β οΈ Please enter both title and description."
|
| 33 |
-
|
| 34 |
-
input_text = title + " " + description
|
| 35 |
-
input_vector = vectorizer.transform([input_text])
|
| 36 |
-
prediction = model.predict(input_vector)
|
| 37 |
-
predicted_tags = mlb.inverse_transform(prediction)
|
| 38 |
-
st.write(predicted_tags)
|
| 39 |
-
if predicted_tags and predicted_tags[0]:
|
| 40 |
-
return "β
Predicted Tags: " + ", ".join(predicted_tags[0])
|
| 41 |
-
else:
|
| 42 |
-
return "βΉοΈ No tags predicted. Try refining your question."
|
| 43 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"β Error during prediction: {str(e)}"
|
| 46 |
-
|
| 47 |
-
# π Streamlit UI
|
| 48 |
st.title("π Stack Overflow Tags Predictor")
|
| 49 |
-
st.markdown("Enter a question title and description
|
| 50 |
|
| 51 |
title = st.text_input("π Enter Question Title")
|
| 52 |
description = st.text_area("π Enter Question Description", height=150)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if st.button("Predict Tags"):
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.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. 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 |
+
# Threshold for prediction
|
| 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 for each tag
|
| 29 |
+
probas = model.predict_proba(input_vector)
|
| 30 |
+
|
| 31 |
+
# Convert list of (1, n_classes) probs to array
|
| 32 |
+
probas = np.array([p[0] for p in probas])
|
| 33 |
+
|
| 34 |
+
# Apply threshold
|
| 35 |
+
predicted_binary = (probas >= threshold).astype(int)
|
| 36 |
+
|
| 37 |
+
# Convert binary vector to tags
|
| 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:
|
| 49 |
+
st.info("βΉοΈ No tags predicted. Try refining your question.")
|