Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pickle
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.set_page_config(page_title="
|
| 7 |
|
| 8 |
-
#
|
| 9 |
try:
|
| 10 |
with open("vectorizer.pkl", "rb") as f:
|
| 11 |
vectorizer = pickle.load(f)
|
| 12 |
|
| 13 |
-
with open("model
|
| 14 |
model = pickle.load(f)
|
| 15 |
|
| 16 |
with open("binarizer.pkl", "rb") as f:
|
|
@@ -20,40 +19,33 @@ except Exception as e:
|
|
| 20 |
st.error(f"β Error loading model files: {str(e)}")
|
| 21 |
st.stop()
|
| 22 |
|
| 23 |
-
# π§ Prediction
|
| 24 |
-
def predict_tags(title, description
|
| 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 |
-
|
| 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
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
return f"β Error during prediction: {str(e)}"
|
| 48 |
|
| 49 |
# π Streamlit UI
|
| 50 |
-
st.title("
|
| 51 |
-
st.markdown("Enter a question title and description to
|
| 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
|
| 59 |
st.markdown(result)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pickle
|
|
|
|
| 3 |
|
| 4 |
+
# β
Set Streamlit page config (must be first command)
|
| 5 |
+
st.set_page_config(page_title="π Stack Overflow Multi-Tag Predictor", page_icon="π§ ")
|
| 6 |
|
| 7 |
+
# π Load model files
|
| 8 |
try:
|
| 9 |
with open("vectorizer.pkl", "rb") as f:
|
| 10 |
vectorizer = pickle.load(f)
|
| 11 |
|
| 12 |
+
with open("model.pkl", "rb") as f:
|
| 13 |
model = pickle.load(f)
|
| 14 |
|
| 15 |
with open("binarizer.pkl", "rb") as f:
|
|
|
|
| 19 |
st.error(f"β Error loading model files: {str(e)}")
|
| 20 |
st.stop()
|
| 21 |
|
| 22 |
+
# π§ Prediction function
|
| 23 |
+
def predict_tags(title, description):
|
| 24 |
try:
|
| 25 |
if not title.strip() or not description.strip():
|
| 26 |
return "β οΈ Please enter both title and description."
|
| 27 |
|
| 28 |
input_text = title + " " + description
|
| 29 |
input_vector = vectorizer.transform([input_text])
|
| 30 |
+
prediction = model.predict(input_vector)
|
| 31 |
|
| 32 |
+
predicted_tags = mlb.inverse_transform(prediction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if predicted_tags and predicted_tags[0]:
|
| 35 |
return "β
**Predicted Tags:** " + ", ".join(predicted_tags[0])
|
| 36 |
else:
|
| 37 |
+
return "βΉοΈ No tags predicted. Try refining your question."
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
return f"β Error during prediction: {str(e)}"
|
| 41 |
|
| 42 |
# π Streamlit UI
|
| 43 |
+
st.title("π¬ Stack Overflow Multi-Tag Predictor")
|
| 44 |
+
st.markdown("Enter a question title and description to get multiple relevant tags.")
|
| 45 |
|
| 46 |
+
title = st.text_input("π Enter Question Title")
|
| 47 |
+
description = st.text_area("π Enter Question Description", height=150)
|
|
|
|
| 48 |
|
| 49 |
+
if st.button("π― Predict Tags"):
|
| 50 |
+
result = predict_tags(title, description)
|
| 51 |
st.markdown(result)
|