Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,49 @@
|
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
vectorizer = pickle.load(f)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 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 |
-
# π Streamlit UI
|
| 48 |
-
st.title("π Stack Overflow Tags Predictor")
|
| 49 |
-
st.markdown("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(
|
| 56 |
-
st.markdown(result)
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import joblib
|
|
|
|
| 3 |
|
| 4 |
+
# β
MUST be first Streamlit command
|
| 5 |
+
st.set_page_config(page_title="Stack Overflow Tag Predictor", layout="centered")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# β
Define your custom tokenizer BEFORE loading the vectorizer
|
| 8 |
+
def customs_tokenization(text):
|
| 9 |
+
return text.split()
|
| 10 |
|
| 11 |
+
# --- Load model and vectorizers ---
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_pickle(path):
|
| 14 |
+
return joblib.load(path)
|
| 15 |
|
| 16 |
+
model = load_pickle("compressed_logistic_reg1 (1).pkl")
|
| 17 |
+
vectorizer = load_pickle("tfidf_vectorizer (2).pkl")
|
| 18 |
+
mlb = load_pickle("multilabel_binarizer (1).pkl")
|
|
|
|
| 19 |
|
| 20 |
+
# --- Prediction Function ---
|
| 21 |
+
def predict_tags(title, description):
|
| 22 |
+
title, description = title.strip(), description.strip()
|
| 23 |
|
| 24 |
+
if not title or not description:
|
| 25 |
+
return "β οΈ Please enter both title and description."
|
| 26 |
|
| 27 |
+
full_text = title + " " + description
|
| 28 |
+
vectorized = vectorizer.transform([full_text])
|
| 29 |
+
prediction = model.predict(vectorized)
|
| 30 |
+
tags = mlb.inverse_transform(prediction)
|
| 31 |
|
| 32 |
+
if tags and tags[0]:
|
| 33 |
+
return "β
Predicted Tags: " + ", ".join(tags[0])
|
| 34 |
+
else:
|
| 35 |
+
return "βΉοΈ No tags predicted. Try refining your question."
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# --- Streamlit UI ---
|
| 39 |
+
st.title("π Stack Overflow Tag Predictor")
|
| 40 |
+
st.markdown("Enter a question title and description to receive relevant tag suggestions.")
|
| 41 |
+
|
| 42 |
+
# Input fields
|
| 43 |
+
title_input = st.text_input("Question Title", placeholder="e.g. How to merge dictionaries in Python?")
|
| 44 |
+
desc_input = st.text_area("Question Description", placeholder="e.g. I have two dictionaries and I want to merge them...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Predict button
|
| 47 |
if st.button("Predict Tags"):
|
| 48 |
+
result = predict_tags(title_input, desc_input)
|
| 49 |
+
st.markdown(f"### Result:\n{result}")
|