Stack_overflow / app.py
sree4411's picture
Update app.py
6fbaf6c verified
import pickle
import streamlit as st
import os
import numpy as np
# πŸ’‘ Define the custom tokenizer exactly as used during training
def custom_tokenizer(text):
# Modify this function to match your original tokenizer logic
return text.lower().split()
# πŸ”ƒ Load model files
try:
with open("tfidf.pkl", "rb") as f:
vectorizer = pickle.load(f)
with open("model (3).pkl", "rb") as f:
model = pickle.load(f)
with open("mlb (1).pkl", "rb") as f:
mlb = pickle.load(f)
except Exception as e:
st.error(f"❌ Error loading model files: {str(e)}")
st.stop()
# 🧠 Prediction function
def predict_tags(title, description):
try:
if not title.strip() or not description.strip():
return "⚠️ Please enter both title and description."
input_text = title + " " + description
input_vector = vectorizer.transform([input_text])
prediction = model.predict(input_vector)
predicted_tags = mlb.inverse_transform(prediction)
st.write(predicted_tags)
if predicted_tags and predicted_tags[0]:
return "βœ… Predicted Tags: " + ", ".join(predicted_tags[0])
else:
return "ℹ️ No tags predicted. Try refining your question."
except Exception as e:
return f"❌ Error during prediction: {str(e)}"
# πŸš€ Streamlit UI
st.title("πŸ”– Stack Overflow Tags Predictor")
st.markdown("Enter a question title and description to predict relevant tags.")
title = st.text_input("πŸ“Œ Enter Question Title")
description = st.text_area("πŸ“ Enter Question Description", height=150)
if st.button("Predict Tags"):
result = predict_tags(title, description)
st.markdown(result)