Spaces:
Sleeping
Sleeping
| 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) |