Harika22 commited on
Commit
24c4109
Β·
verified Β·
1 Parent(s): ead514c

Update pages/model.py

Browse files
Files changed (1) hide show
  1. pages/model.py +39 -43
pages/model.py CHANGED
@@ -3,62 +3,58 @@ import pickle
3
  import re
4
  import numpy as np
5
 
6
- st.set_page_config(page_title="TagGPT - Stack Overflow Tag Predictor", layout="centered")
7
 
8
- # ---------- Text Preprocessing ----------
9
- def preprocess_input(text):
10
- text = re.sub(r"<.*?>", " ", text) # Remove HTML tags
11
- text = re.sub(r"[^a-zA-Z0-9\s]", " ", text) # Remove special characters
12
- text = re.sub(r"\s+", " ", text.lower()).strip() # Lowercase and clean whitespace
13
- return text
14
 
15
- # ---------- Load Model Assets ----------
16
  @st.cache_resource
17
- def load_model_assets():
18
  try:
19
- with open("model.pkl", "rb") as f:
20
- model = pickle.load(f)
21
- with open("tfidf.pkl", "rb") as f:
22
- vectorizer = pickle.load(f)
23
- with open("mlb.pkl", "rb") as f:
24
- encoder = pickle.load(f)
25
- return model, vectorizer, encoder
26
- except Exception as e:
27
- st.error(f"❌ Error loading model assets: {e}")
28
  st.stop()
29
 
30
- model, vectorizer, encoder = load_model_assets()
31
 
32
- # ---------- UI ----------
33
- st.title("πŸ”– TagGPT: Predict Stack Overflow Tags")
34
- st.markdown("Smartly auto-tag your programming questions using machine learning.")
35
 
36
- q_title = st.text_input("πŸ“ Enter Question Title")
37
- q_body = st.text_area("πŸ“„ Describe Your Problem", height=200)
38
- threshold = st.slider("🎯 Prediction Threshold", 0.1, 0.9, 0.3, 0.05)
39
 
40
- # ---------- Prediction ----------
41
- if st.button("πŸ” Generate Tags"):
42
- if not q_title.strip() or not q_body.strip():
43
- st.warning("🚨 Please fill in both the title and description.")
44
  else:
45
- user_query = preprocess_input(q_title + " " + q_body)
46
- X_transformed = vectorizer.transform([user_query])
47
 
48
  try:
49
- # Handle both predict_proba and predict cases
50
- if hasattr(model, "predict_proba"):
51
- tag_probs = model.predict_proba(X_transformed)
52
- tag_preds = (tag_probs >= threshold).astype(int)
53
  else:
54
- tag_preds = model.predict(X_transformed)
55
 
56
- predicted = encoder.inverse_transform(tag_preds)
57
 
58
- if predicted and predicted[0]:
59
- st.success("βœ… Predicted Tags:")
60
- st.write(", ".join(predicted[0]))
61
  else:
62
- st.info("πŸ€– No tags predicted. Try refining your input or lowering the threshold.")
63
- except Exception as e:
64
- st.error(f"⚠️ Prediction failed: {e}")
 
3
  import re
4
  import numpy as np
5
 
6
+ st.set_page_config(page_title="πŸ”– TagGPT - Auto Tag Your Questions", layout="centered")
7
 
8
+ def clean_text(raw_text):
9
+ raw_text = re.sub(r"<.*?>", " ", raw_text) # Remove HTML tags
10
+ raw_text = re.sub(r"[^a-zA-Z0-9\s]", " ", raw_text) # Remove special characters
11
+ raw_text = re.sub(r"\s+", " ", raw_text.lower()).strip() # Lowercase & strip spaces
12
+ return raw_text
 
13
 
 
14
  @st.cache_resource
15
+ def load_assets():
16
  try:
17
+ with open("model.pkl", "rb") as m:
18
+ tag_model = pickle.load(m)
19
+ with open("tfidf.pkl", "rb") as v:
20
+ text_vectorizer = pickle.load(v)
21
+ with open("mlb.pkl", "rb") as e:
22
+ tag_encoder = pickle.load(e)
23
+ return tag_model, text_vectorizer, tag_encoder
24
+ except Exception as error:
25
+ st.error(f"❌ Failed to load model components: {error}")
26
  st.stop()
27
 
28
+ tag_model, text_vectorizer, tag_encoder = load_assets()
29
 
30
+ st.title("🎯 TagGPT - Smart Stack Overflow Tag Suggester")
31
+ st.markdown("πŸš€ **Automatically generate relevant tags for your coding questions.**")
32
+ st.markdown("Just enter your question title and description, and let AI do the tagging!")
33
 
34
+ question_title = st.text_input("🧠 Enter the **Question Title**")
35
+ question_description = st.text_area("πŸ“ Provide a **Detailed Description**", height=200)
36
+ confidence_threshold = st.slider("πŸ“ˆ Select Tag Confidence Threshold", 0.1, 0.9, 0.3, 0.05)
37
 
38
+ if st.button("πŸ” Predict Tags"):
39
+ if not question_title.strip() or not question_description.strip():
40
+ st.warning("⚠️ Both title and description are required!")
 
41
  else:
42
+ combined_input = clean_text(question_title + " " + question_description)
43
+ transformed_input = text_vectorizer.transform([combined_input])
44
 
45
  try:
46
+ if hasattr(tag_model, "predict_proba"):
47
+ probs = tag_model.predict_proba(transformed_input)
48
+ tag_preds = (probs >= confidence_threshold).astype(int)
 
49
  else:
50
+ tag_preds = tag_model.predict(transformed_input)
51
 
52
+ final_tags = tag_encoder.inverse_transform(tag_preds)
53
 
54
+ if final_tags and final_tags[0]:
55
+ st.success("🏷️ **Predicted Tags:**")
56
+ st.markdown("πŸ”Έ " + ", ".join(final_tags[0]))
57
  else:
58
+ st.info("πŸ€– No tags predicted. Try adjusting the threshold or refining your input.")
59
+ except Exception as error:
60
+ st.error(f"🚫 Prediction failed: {error}")