Harika22 commited on
Commit
17762b2
ยท
verified ยท
1 Parent(s): 4dbdd7c

Update pages/model.py

Browse files
Files changed (1) hide show
  1. pages/model.py +67 -67
pages/model.py CHANGED
@@ -1,80 +1,80 @@
1
-
2
  import streamlit as st
3
  import pickle
4
  import re
5
  import numpy as np
6
  import pandas as pd
7
- from io import StringIO
8
- from streamlit_lottie import st_lottie
9
- import json
10
 
11
- st.set_page_config(page_title="๐Ÿ”– TagGPT - Auto Tag Your Questions", layout="centered")
12
 
13
- def load_lottie(filepath: str):
14
- with open(filepath, "r") as f:
15
- return json.load(f)
 
 
16
 
17
  @st.cache_resource
18
-
19
- def load_assets():
20
- with open("model.pkl", "rb") as m:
21
- tag_model = pickle.load(m)
22
- with open("tfidf.pkl", "rb") as v:
23
- text_vectorizer = pickle.load(v)
24
- with open("mlb.pkl", "rb") as e:
25
- tag_encoder = pickle.load(e)
26
- return tag_model, text_vectorizer, tag_encoder
27
-
28
- tag_model, text_vectorizer, tag_encoder = load_assets()
29
-
30
- def clean_text(raw_text):
31
- raw_text = re.sub(r"<.*?>", " ", raw_text) # Remove HTML tags
32
- raw_text = re.sub(r"[^a-zA-Z0-9\s]", " ", raw_text) # Remove special characters
33
- raw_text = re.sub(r"\s+", " ", raw_text.lower()).strip()
34
- return raw_text
35
-
36
- st_lottie(load_lottie("tag_animation.json"), height=150, key="intro") # Lottie animation (optional)
37
-
38
- st.title("๐Ÿท๏ธ TagGPT - Smart Stack Overflow Tag Suggester")
39
- st.markdown("**๐Ÿš€ Instantly generate relevant tags for your coding questions using AI.**")
40
-
41
- q_title = st.text_input("๐Ÿง  Enter your question title")
42
- q_desc = st.text_area("๐Ÿ“ Describe your problem in detail", height=200)
43
-
44
- if st.button("๐Ÿ” Predict Tags"):
45
- if not q_title.strip() or not q_desc.strip():
46
- st.warning("โš ๏ธ Please provide both title and description.")
47
  else:
48
- combined_input = clean_text(q_title + " " + q_desc)
49
- transformed_input = text_vectorizer.transform([combined_input])
50
 
51
  try:
52
- if hasattr(tag_model, "predict_proba"):
53
- probs = tag_model.predict_proba(transformed_input)[0]
54
- pred_tags = (probs >= 0.3).astype(int) # Fixed threshold
55
- else:
56
- pred_tags = tag_model.predict(transformed_input)[0]
57
- probs = pred_tags
58
-
59
- tag_names = tag_encoder.classes_
60
- selected_tags = [(tag, round(probs[i]*100, 2)) for i, tag in enumerate(tag_names) if pred_tags[i] == 1]
61
-
62
- if selected_tags:
63
- st.success("๐Ÿท๏ธ **Predicted Tags with Confidence:**")
64
- for tag, score in selected_tags:
65
- st.markdown(f"**{tag}** โ€” `{score}%`")
 
 
 
 
 
 
66
  else:
67
- st.info("๐Ÿค– No tags predicted. Try refining your input.")
68
-
69
- # --- Download Option
70
- tag_list = [tag for tag, _ in selected_tags]
71
- df = pd.DataFrame({
72
- "Title": [q_title],
73
- "Description": [q_desc],
74
- "Predicted Tags": [", ".join(tag_list)]
75
- })
76
- csv = df.to_csv(index=False).encode('utf-8')
77
- st.download_button("๐Ÿ“ฅ Download Tags as CSV", data=csv, file_name="tag_predictions.csv", mime='text/csv')
78
-
79
- except Exception as error:
80
- st.error(f"๐Ÿšซ Prediction failed: {error}")
 
 
 
1
  import streamlit as st
2
  import pickle
3
  import re
4
  import numpy as np
5
  import pandas as pd
 
 
 
6
 
7
+ st.set_page_config(page_title="๐Ÿ”– TagGPT - Stack Overflow Tag Generator", layout="centered")
8
 
9
+ def clean_text(text):
10
+ text = re.sub(r"<.*?>", " ", text)
11
+ text = re.sub(r"[^a-zA-Z0-9\s]", " ", text)
12
+ text = re.sub(r"\s+", " ", text.lower()).strip()
13
+ return text
14
 
15
  @st.cache_resource
16
+ def load_model_assets():
17
+ try:
18
+ with open("model.pkl", "rb") as m:
19
+ model = pickle.load(m)
20
+ with open("tfidf.pkl", "rb") as v:
21
+ vectorizer = pickle.load(v)
22
+ with open("mlb.pkl", "rb") as e:
23
+ encoder = pickle.load(e)
24
+ return model, vectorizer, encoder
25
+ except Exception as err:
26
+ st.error(f"โŒ Failed to load model assets: {err}")
27
+ st.stop()
28
+
29
+ model, vectorizer, encoder = load_model_assets()
30
+
31
+ st.title("๐Ÿ’ก TagGPT")
32
+ st.markdown("โœจ _Auto-tag your Stack Overflow questions using AI_")
33
+
34
+ q_title = st.text_input("๐Ÿ“ **Question Title**")
35
+ q_body = st.text_area("๐Ÿ“„ **Detailed Description**", height=200)
36
+
37
+ if st.button("๐Ÿ”ฎ Generate Tags"):
38
+ if not q_title.strip() or not q_body.strip():
39
+ st.warning("โš ๏ธ Please enter both title and description.")
 
 
 
 
 
40
  else:
41
+ user_input = clean_text(q_title + " " + q_body)
42
+ transformed = vectorizer.transform([user_input])
43
 
44
  try:
45
+ if hasattr(model, "predict_proba"):
46
+ probs = model.predict_proba(transformed)[0]
47
+ tag_names = encoder.classes_
48
+ tag_conf_df = pd.DataFrame({
49
+ "Tag": tag_names,
50
+ "Confidence": probs
51
+ }).sort_values("Confidence", ascending=False)
52
+
53
+ top_tags = tag_conf_df[tag_conf_df["Confidence"] > 0.3] # adjustable threshold
54
+
55
+ if not top_tags.empty:
56
+ st.success("๐Ÿท๏ธ **Top Suggested Tags with Confidence:**")
57
+ st.dataframe(top_tags, use_container_width=True)
58
+
59
+ # Download Button
60
+ csv = top_tags.to_csv(index=False).encode('utf-8')
61
+ st.download_button("โฌ‡๏ธ Download Tags as CSV", csv, "predicted_tags.csv", "text/csv")
62
+
63
+ else:
64
+ st.info("๐Ÿค– No high-confidence tags predicted. Try improving the input.")
65
  else:
66
+ preds = model.predict(transformed)
67
+ tags = encoder.inverse_transform(preds)
68
+
69
+ if tags and tags[0]:
70
+ st.success("๐Ÿท๏ธ **Predicted Tags:**")
71
+ tag_list = ", ".join(tags[0])
72
+ st.markdown("๐Ÿ”ธ " + tag_list)
73
+
74
+ # Download plain text
75
+ txt = tag_list.encode('utf-8')
76
+ st.download_button("โฌ‡๏ธ Download Tags as TXT", txt, "predicted_tags.txt", "text/plain")
77
+ else:
78
+ st.info("๐Ÿค– No tags predicted.")
79
+ except Exception as e:
80
+ st.error(f"๐Ÿšซ Prediction failed: {e}")