sree4411 commited on
Commit
f703295
Β·
verified Β·
1 Parent(s): a856a6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -28
app.py CHANGED
@@ -1,14 +1,19 @@
1
  import pickle
2
  import streamlit as st
3
  import os
 
4
 
5
- # Show current working directory and files for debugging
6
- st.write("πŸ“‚ Current directory:", os.getcwd())
7
- st.write("πŸ“ Files found:", os.listdir())
 
8
 
9
- # Load the pickle files safely
 
 
 
10
  try:
11
- with open("tfidf_vectorizer (1).pkl", "rb") as f:
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
- # Streamlit UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  st.title("πŸ”– Stack Overflow Tags Predictor")
29
- st.markdown("Enter a Stack Overflow question title and description to get predicted tags.")
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
- if not title.strip() or not description.strip():
36
- st.warning("⚠️ Please enter both title and description.")
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)