Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,67 @@
|
|
| 1 |
import pickle
|
| 2 |
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
vectorizer
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
mlb = pickle.load(f)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
#
|
| 29 |
def predict_tags(title, description):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return "β οΈ Please enter both title and description."
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
input_vector = vectorizer.transform([input_text])
|
| 36 |
prediction = model.predict(input_vector)
|
| 37 |
predicted_tags = mlb.inverse_transform(prediction)
|
| 38 |
|
| 39 |
if predicted_tags and predicted_tags[0]:
|
| 40 |
-
|
|
|
|
| 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 |
-
#
|
| 48 |
-
st.
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
title = st.text_input("π
|
| 52 |
-
description = st.text_area("π
|
| 53 |
|
| 54 |
-
if st.button("Predict Tags"):
|
| 55 |
result = predict_tags(title, description)
|
| 56 |
-
st.markdown(result)
|
|
|
|
| 1 |
import pickle
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# --- Load Model Components ---
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_components():
|
| 7 |
+
try:
|
| 8 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 9 |
+
vectorizer = pickle.load(f)
|
| 10 |
+
with open("model (2).pkl", "rb") as f:
|
| 11 |
+
model = pickle.load(f)
|
| 12 |
+
with open("binarizer.pkl", "rb") as f:
|
| 13 |
+
mlb = pickle.load(f)
|
| 14 |
+
return vectorizer, model, mlb
|
| 15 |
+
except Exception as e:
|
| 16 |
+
st.error(f"π¨ Failed to load model files: {e}")
|
| 17 |
+
st.stop()
|
| 18 |
|
| 19 |
+
vectorizer, model, mlb = load_components()
|
|
|
|
| 20 |
|
| 21 |
+
# --- Custom Tokenizer (if used in training) ---
|
| 22 |
+
def custom_tokenizer(text):
|
| 23 |
+
return text.lower().split()
|
| 24 |
|
| 25 |
+
# --- Tag Prediction ---
|
| 26 |
def predict_tags(title, description):
|
| 27 |
+
if not title.strip() or not description.strip():
|
| 28 |
+
return "β οΈ Please enter both title and description."
|
|
|
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
input_text = title.strip() + " " + description.strip()
|
| 32 |
input_vector = vectorizer.transform([input_text])
|
| 33 |
prediction = model.predict(input_vector)
|
| 34 |
predicted_tags = mlb.inverse_transform(prediction)
|
| 35 |
|
| 36 |
if predicted_tags and predicted_tags[0]:
|
| 37 |
+
tag_list = ", ".join(predicted_tags[0])
|
| 38 |
+
return f"π― **Predicted Tags:** `{tag_list}`"
|
| 39 |
else:
|
| 40 |
return "βΉοΈ No tags predicted. Try refining your question."
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
return f"β Error during prediction: {str(e)}"
|
| 44 |
|
| 45 |
+
# --- Streamlit UI ---
|
| 46 |
+
st.set_page_config(page_title="Stack Overflow Tag Predictor", page_icon="π")
|
| 47 |
+
|
| 48 |
+
st.markdown(
|
| 49 |
+
"""
|
| 50 |
+
<style>
|
| 51 |
+
.title { font-size: 36px; font-weight: 700; color: #4A90E2; }
|
| 52 |
+
.desc { font-size: 18px; margin-bottom: 20px; }
|
| 53 |
+
.result-box { background-color: #f9f9f9; padding: 15px; border-radius: 8px; margin-top: 20px; }
|
| 54 |
+
</style>
|
| 55 |
+
""",
|
| 56 |
+
unsafe_allow_html=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
st.markdown('<div class="title">π Stack Overflow Tag Predictor</div>', unsafe_allow_html=True)
|
| 60 |
+
st.markdown('<div class="desc">Enter a Stack Overflow question title and description to get the most relevant tags.</div>', unsafe_allow_html=True)
|
| 61 |
|
| 62 |
+
title = st.text_input("π Question Title")
|
| 63 |
+
description = st.text_area("π Question Description", height=150)
|
| 64 |
|
| 65 |
+
if st.button("π Predict Tags"):
|
| 66 |
result = predict_tags(title, description)
|
| 67 |
+
st.markdown(f'<div class="result-box">{result}</div>', unsafe_allow_html=True)
|