sree4411 commited on
Commit
1889170
Β·
verified Β·
1 Parent(s): 2324914

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (3).pkl", "rb") as f:
16
+ vectorizer = pickle.load(f)
17
+
18
+ with open("model (6).pkl", "rb") as f:
19
+ model = pickle.load(f)
20
+
21
+ with open("binarizer (3).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
+ st.write(predicted_tags)
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("πŸ”– 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(title, description)
56
+ st.markdown(result)