Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,19 @@
|
|
| 1 |
import pickle
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
with open("tfidf_vectorizer (
|
| 12 |
vectorizer = pickle.load(f)
|
| 13 |
|
| 14 |
with open("model (1).pkl", "rb") as f:
|
|
@@ -17,34 +22,36 @@ try:
|
|
| 17 |
with open("mlb.pkl", "rb") as f:
|
| 18 |
mlb = pickle.load(f)
|
| 19 |
|
| 20 |
-
except FileNotFoundError as e:
|
| 21 |
-
st.error(f"β File not found: {e.filename}")
|
| 22 |
-
st.stop()
|
| 23 |
except Exception as e:
|
| 24 |
st.error(f"β Error loading model files: {str(e)}")
|
| 25 |
st.stop()
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
st.title("π Stack Overflow Tags Predictor")
|
| 29 |
-
st.markdown("Enter a
|
| 30 |
|
| 31 |
-
title = st.text_input("Enter Question Title")
|
| 32 |
-
description = st.text_area("Enter Question Description", height=150)
|
| 33 |
|
| 34 |
if st.button("Predict Tags"):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
else:
|
| 38 |
-
try:
|
| 39 |
-
input_text = title + " " + description
|
| 40 |
-
input_vector = vectorizer.transform([input_text])
|
| 41 |
-
prediction = model.predict(input_vector)
|
| 42 |
-
predicted_tags = mlb.inverse_transform(prediction)
|
| 43 |
-
|
| 44 |
-
if predicted_tags and predicted_tags[0]:
|
| 45 |
-
st.success("β
Predicted Tags: " + ", ".join(predicted_tags[0]))
|
| 46 |
-
else:
|
| 47 |
-
st.info("βΉοΈ No tags predicted. Try refining your question.")
|
| 48 |
-
|
| 49 |
-
except Exception as e:
|
| 50 |
-
st.error(f"β Error during prediction: {str(e)}")
|
|
|
|
| 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 |
+
# π§ Debug: Show current directory contents to verify files
|
| 12 |
+
st.write("π Files in current directory:", os.listdir())
|
| 13 |
+
|
| 14 |
+
# π Load model files
|
| 15 |
try:
|
| 16 |
+
with open("tfidf_vectorizer (1_.pkl", "rb") as f:
|
| 17 |
vectorizer = pickle.load(f)
|
| 18 |
|
| 19 |
with open("model (1).pkl", "rb") as f:
|
|
|
|
| 22 |
with open("mlb.pkl", "rb") as f:
|
| 23 |
mlb = pickle.load(f)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
st.error(f"β Error loading model files: {str(e)}")
|
| 27 |
st.stop()
|
| 28 |
|
| 29 |
+
# π§ Prediction function
|
| 30 |
+
def predict_tags(title, description):
|
| 31 |
+
try:
|
| 32 |
+
if not title.strip() or not description.strip():
|
| 33 |
+
return "β οΈ Please enter both title and description."
|
| 34 |
+
|
| 35 |
+
input_text = title + " " + description
|
| 36 |
+
input_vector = vectorizer.transform([input_text])
|
| 37 |
+
prediction = model.predict(input_vector)
|
| 38 |
+
predicted_tags = mlb.inverse_transform(prediction)
|
| 39 |
+
|
| 40 |
+
if predicted_tags and predicted_tags[0]:
|
| 41 |
+
return "β
Predicted Tags: " + ", ".join(predicted_tags[0])
|
| 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 |
+
# π Streamlit UI
|
| 49 |
st.title("π Stack Overflow Tags Predictor")
|
| 50 |
+
st.markdown("Enter a question title and description to predict relevant tags.")
|
| 51 |
|
| 52 |
+
title = st.text_input("π Enter Question Title")
|
| 53 |
+
description = st.text_area("π Enter Question Description", height=150)
|
| 54 |
|
| 55 |
if st.button("Predict Tags"):
|
| 56 |
+
result = predict_tags(title, description)
|
| 57 |
+
st.markdown(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|