Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,16 @@
|
|
| 1 |
-
import pickle
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
# Modify this function to match your original tokenizer logic
|
| 9 |
-
return text.lower().split()
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
try:
|
| 15 |
with open("vectorizer.pkl", "rb") as f:
|
| 16 |
vectorizer = pickle.load(f)
|
| 17 |
|
| 18 |
-
with open("model
|
| 19 |
model = pickle.load(f)
|
| 20 |
|
| 21 |
with open("binarizer.pkl", "rb") as f:
|
|
@@ -25,33 +20,40 @@ except Exception as e:
|
|
| 25 |
st.error(f"β Error loading model files: {str(e)}")
|
| 26 |
st.stop()
|
| 27 |
|
| 28 |
-
# π§ Prediction
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if predicted_tags and predicted_tags[0]:
|
| 41 |
-
return "β
Predicted Tags
|
| 42 |
else:
|
| 43 |
-
return "βΉοΈ No tags predicted. Try refining your
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
return f"β Error during prediction: {str(e)}"
|
| 47 |
|
| 48 |
# π Streamlit UI
|
| 49 |
-
st.title("
|
| 50 |
-
st.markdown("
|
| 51 |
|
| 52 |
-
title = st.text_input("π
|
| 53 |
-
description = st.text_area("π
|
|
|
|
| 54 |
|
| 55 |
if st.button("Predict Tags"):
|
| 56 |
-
result = predict_tags(title, description)
|
| 57 |
-
st.markdown(result)
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# βοΈ Set Streamlit Page Config (this MUST be first)
|
| 6 |
+
st.set_page_config(page_title="Multi-Tag Stack Overflow Predictor", page_icon="π§ ")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# π§ͺ Load Model Files
|
| 9 |
try:
|
| 10 |
with open("vectorizer.pkl", "rb") as f:
|
| 11 |
vectorizer = pickle.load(f)
|
| 12 |
|
| 13 |
+
with open("model.pkl", "rb") as f:
|
| 14 |
model = pickle.load(f)
|
| 15 |
|
| 16 |
with open("binarizer.pkl", "rb") as f:
|
|
|
|
| 20 |
st.error(f"β Error loading model files: {str(e)}")
|
| 21 |
st.stop()
|
| 22 |
|
| 23 |
+
# π§ Prediction Function (Multi-tag with Threshold)
|
| 24 |
+
def predict_tags(title, description, threshold=0.3):
|
| 25 |
try:
|
| 26 |
if not title.strip() or not description.strip():
|
| 27 |
return "β οΈ Please enter both title and description."
|
| 28 |
|
| 29 |
input_text = title + " " + description
|
| 30 |
input_vector = vectorizer.transform([input_text])
|
| 31 |
+
|
| 32 |
+
# Predict probabilities
|
| 33 |
+
probas = model.predict_proba(input_vector)
|
| 34 |
+
|
| 35 |
+
# Apply threshold to get binary predictions
|
| 36 |
+
multi_pred = (probas >= threshold).astype(int)
|
| 37 |
+
|
| 38 |
+
# Convert binary predictions to tags
|
| 39 |
+
predicted_tags = mlb.inverse_transform(multi_pred)
|
| 40 |
|
| 41 |
if predicted_tags and predicted_tags[0]:
|
| 42 |
+
return "β
**Predicted Tags:** " + ", ".join(predicted_tags[0])
|
| 43 |
else:
|
| 44 |
+
return "βΉοΈ No tags predicted. Try lowering the threshold or refining your input."
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
return f"β Error during prediction: {str(e)}"
|
| 48 |
|
| 49 |
# π Streamlit UI
|
| 50 |
+
st.title("π Stack Overflow Multi-Tag Predictor")
|
| 51 |
+
st.markdown("Enter a question title and description to predict relevant tags using ML.")
|
| 52 |
|
| 53 |
+
title = st.text_input("π Question Title")
|
| 54 |
+
description = st.text_area("π Question Description", height=150)
|
| 55 |
+
threshold = st.slider("π― Prediction Threshold", 0.1, 0.9, 0.3, step=0.05)
|
| 56 |
|
| 57 |
if st.button("Predict Tags"):
|
| 58 |
+
result = predict_tags(title, description, threshold)
|
| 59 |
+
st.markdown(result)
|