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