sree4411 commited on
Commit
7bd1bd8
Β·
verified Β·
1 Parent(s): 9b1e220

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -29
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import pickle
2
  import streamlit as st
3
- import numpy as np
4
 
5
  # Load saved model, vectorizer, and binarizer
6
  with open("vectorizer (3).pkl", "rb") as f:
@@ -13,42 +12,25 @@ with open("binarizer (3).pkl", "rb") as f:
13
  mlb = pickle.load(f)
14
 
15
  st.title("πŸ”– Stack Overflow Tags Predictor")
16
- st.markdown("Enter a question title and description. The top 3 most relevant tags will be predicted automatically.")
17
 
18
- # Input fields
19
  title = st.text_input("πŸ“Œ Enter Question Title")
20
  description = st.text_area("πŸ“ Enter Question Description", height=150)
21
 
22
- # Function to predict top N tags (e.g., top 3)
23
- def predict_tags_top_n(title, description, top_n=3):
 
24
  input_text = title + " " + description
25
  input_vector = vectorizer.transform([input_text])
26
-
27
- # Get probability estimates from each classifier
28
- probas = model.predict_proba(input_vector)
29
-
30
- # Extract positive class probabilities for each tag
31
- probas_array = np.array([p[0][1] for p in probas]) # shape: (n_classes,)
32
-
33
- # Get indices of top N tags
34
- top_indices = probas_array.argsort()[-top_n:][::-1]
35
-
36
- # Build binary array for top tags
37
- predicted_binary = np.zeros_like(probas_array, dtype=int)
38
- predicted_binary[top_indices] = 1
39
- predicted_binary = predicted_binary.reshape(1, -1)
40
-
41
- # Convert binary to tag names
42
  tags = mlb.inverse_transform(predicted_binary)
43
  return tags[0] if tags else []
44
 
45
- # Predict and display
46
  if st.button("Predict Tags"):
47
- if not title.strip() or not description.strip():
48
- st.warning("⚠️ Please enter both title and description.")
 
49
  else:
50
- predicted_tags = predict_tags_top_n(title, description, top_n=3)
51
- if predicted_tags:
52
- st.success("βœ… Predicted Tags: " + ", ".join(predicted_tags))
53
- else:
54
- st.info("ℹ️ No tags predicted. Try refining your question.")
 
1
  import pickle
2
  import streamlit as st
 
3
 
4
  # Load saved model, vectorizer, and binarizer
5
  with open("vectorizer (3).pkl", "rb") as f:
 
12
  mlb = pickle.load(f)
13
 
14
  st.title("πŸ”– Stack Overflow Tags Predictor")
15
+ st.markdown("Enter a question title and description. Tags will be predicted automatically.")
16
 
 
17
  title = st.text_input("πŸ“Œ Enter Question Title")
18
  description = st.text_area("πŸ“ Enter Question Description", height=150)
19
 
20
+ def predict_tags(title, description):
21
+ if not title.strip() or not description.strip():
22
+ return []
23
  input_text = title + " " + description
24
  input_vector = vectorizer.transform([input_text])
25
+
26
+ # Predict tags using the model's internal default threshold
27
+ predicted_binary = model.predict(input_vector)
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  tags = mlb.inverse_transform(predicted_binary)
29
  return tags[0] if tags else []
30
 
 
31
  if st.button("Predict Tags"):
32
+ tags = predict_tags(title, description)
33
+ if tags:
34
+ st.success("βœ… Predicted Tags: " + ", ".join(tags))
35
  else:
36
+ st.info("ℹ️ No tags predicted. Try refining your question.")