shwetashweta05 commited on
Commit
55f74cf
Β·
verified Β·
1 Parent(s): 4682adb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -3,31 +3,38 @@ import joblib
3
  import re
4
  from bs4 import BeautifulSoup
5
 
6
- # Load saved models
7
  model = joblib.load("tag_predictor_model.pkl")
8
  vectorizer = joblib.load("tfidf_vectorizer.pkl")
9
  mlb = joblib.load("label_binarizer.pkl")
10
 
11
- # Clean user input
12
  def clean_text(text):
13
  soup = BeautifulSoup(text, "html.parser").get_text()
14
  text = re.sub(r'[^a-zA-Z\s]', '', text.lower())
15
  return " ".join([word for word in text.split() if len(word) > 2])
16
 
17
- # Streamlit UI
18
  st.title("πŸ€– Stack Overflow Tag Predictor")
19
- st.write("Enter a Stack Overflow question to predict relevant tags.")
20
 
21
- # Input form
22
- title = st.text_input("Enter Question Title")
23
- body = st.text_area("Enter Question Body")
24
 
 
25
  if st.button("Predict Tags"):
26
- input_text = title + " " + body
27
- cleaned = clean_text(input_text)
28
- vectorized = vectorizer.transform([cleaned])
29
- prediction = model.predict(vectorized)
30
- predicted_tags = mlb.inverse_transform(prediction)
 
 
 
 
 
 
 
 
31
 
32
- st.subheader("πŸ“Œ Predicted Tags:")
33
- st.write(", ".join(predicted_tags[0]) if predicted_tags else "No tags predicted.")
 
3
  import re
4
  from bs4 import BeautifulSoup
5
 
6
+ # Load models
7
  model = joblib.load("tag_predictor_model.pkl")
8
  vectorizer = joblib.load("tfidf_vectorizer.pkl")
9
  mlb = joblib.load("label_binarizer.pkl")
10
 
11
+ # Text cleaning function
12
  def clean_text(text):
13
  soup = BeautifulSoup(text, "html.parser").get_text()
14
  text = re.sub(r'[^a-zA-Z\s]', '', text.lower())
15
  return " ".join([word for word in text.split() if len(word) > 2])
16
 
17
+ # App layout
18
  st.title("πŸ€– Stack Overflow Tag Predictor")
19
+ st.write("Enter a Stack Overflow question (title + body) to predict relevant tags.")
20
 
21
+ # Input fields
22
+ title = st.text_input("πŸ“Œ Question Title")
23
+ body = st.text_area("πŸ“ Question Body")
24
 
25
+ # Predict
26
  if st.button("Predict Tags"):
27
+ if not title and not body:
28
+ st.warning("Please enter either a title or body.")
29
+ else:
30
+ with st.spinner("Predicting..."):
31
+ input_text = title + " " + body
32
+ cleaned = clean_text(input_text)
33
+ vectorized = vectorizer.transform([cleaned])
34
+ probas = model.predict_proba(vectorized)
35
+ prediction = (probas >= 0.3).astype(int)
36
+ predicted_tags = mlb.inverse_transform(prediction)
37
+
38
+ st.subheader("πŸ“ Predicted Tags:")
39
+ st.write(", ".join(predicted_tags[0]) if predicted_tags[0] else "No tags predicted.")
40