ravvasanthosh commited on
Commit
2f22d0f
·
verified ·
1 Parent(s): 2cc237c

Delete src

Browse files
Files changed (4) hide show
  1. src/7450_stack.csv +0 -0
  2. src/app .py +0 -159
  3. src/requirements .txt +0 -9
  4. src/streamlit_app.py +0 -40
src/7450_stack.csv DELETED
The diff for this file is too large to render. See raw diff
 
src/app .py DELETED
@@ -1,159 +0,0 @@
1
- import streamlit as st
2
- import joblib
3
- import pandas as pd
4
- import re
5
- from unidecode import unidecode
6
- import emoji
7
- import string
8
- import contractions
9
- from nltk.stem import PorterStemmer
10
- import numpy as np
11
- from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
12
-
13
- # Custom CSS Styling
14
- st.markdown("""
15
- <style>
16
- .stApp {
17
- background-color: #f9fbfc;
18
- font-family: 'Segoe UI', sans-serif;
19
- }
20
- .custom-header {
21
- background-color: #1e293b;
22
- color: white;
23
- padding: 2rem;
24
- border-radius: 0.5rem;
25
- text-align: center;
26
- margin-bottom: 2rem;
27
- }
28
- .custom-header h1 {
29
- font-size: 2rem;
30
- margin-bottom: 0.5rem;
31
- }
32
- .custom-header p {
33
- font-size: 1rem;
34
- color: #cbd5e1;
35
- }
36
- .input-box, .output-box {
37
- background-color: white;
38
- padding: 1.5rem;
39
- border-radius: 0.5rem;
40
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.04);
41
- margin-bottom: 2rem;
42
- }
43
- .tag-pill {
44
- display: inline-block;
45
- background-color: #e0f2fe;
46
- color: #0369a1;
47
- padding: 0.4em 0.8em;
48
- margin: 0.25em;
49
- border-radius: 999px;
50
- font-weight: 600;
51
- font-size: 0.9rem;
52
- }
53
- .footer {
54
- text-align: center;
55
- font-size: 0.85rem;
56
- color: #64748b;
57
- margin-top: 2rem;
58
- }
59
- </style>
60
- """, unsafe_allow_html=True)
61
-
62
- # Header
63
- st.markdown("""
64
- <div class="custom-header">
65
- <h1>📌 StackOverflow Tag Predictor</h1>
66
- <p>Enter a programming question to see predicted tags</p>
67
- </div>
68
- """, unsafe_allow_html=True)
69
-
70
- # Initialize components
71
- stemmer = PorterStemmer()
72
- stop_words = set(ENGLISH_STOP_WORDS)
73
- chat_words = {
74
- "brb": "be right back", "btw": "by the way", "lol": "laugh out loud",
75
- "afaik": "as far as i know", "imo": "in my opinion", "tbh": "to be honest",
76
- "idk": "i don't know", "asap": "as soon as possible", "np": "no problem",
77
- "thx": "thanks", "pls": "please", "fyi": "for your information"
78
- }
79
-
80
- def preprocess_text(text):
81
- if not isinstance(text, str) or not text.strip():
82
- return ""
83
- try:
84
- text = re.sub(r'<[^>]+>', '', text)
85
- text = re.sub(r'https?://\S+|www\.\S+', '', text)
86
- text = emoji.demojize(text, delimiters=(" ", " "))
87
- text = unidecode(text)
88
- text = contractions.fix(text)
89
- text = text.lower()
90
- words = text.split()
91
- text = " ".join([chat_words.get(word.lower(), word) for word in words])
92
- text = text.translate(str.maketrans('', '', string.punctuation))
93
- tokens = re.findall(r'\b\w+\b', text)
94
- tokens = [word for word in tokens if word not in stop_words]
95
- tokens = [stemmer.stem(word) for word in tokens]
96
- return " ".join(tokens)
97
- except Exception as e:
98
- st.error(f"Preprocessing error: {e}")
99
- return ""
100
-
101
- @st.cache_resource
102
- def load_models():
103
- try:
104
- model = joblib.load("tag_model.joblib")
105
- mlb = joblib.load("tag_binarizer.joblib")
106
- return model, mlb
107
- except Exception as e:
108
- st.error(f"Error loading model: {e}")
109
- return None, None
110
-
111
- model, mlb = load_models()
112
-
113
- # Input
114
- st.markdown('<div class="input-box">', unsafe_allow_html=True)
115
- user_input = st.text_area("✍️ Paste your programming question below:", height=200, placeholder="e.g., How to reverse a list in Python?")
116
- st.markdown('</div>', unsafe_allow_html=True)
117
-
118
- # Prediction
119
- if st.button("🚀 Predict Tags"):
120
- if not user_input.strip():
121
- st.warning("Please enter your question to get predictions.")
122
- elif model is None or mlb is None:
123
- st.error("Model loading failed.")
124
- else:
125
- with st.spinner("Processing..."):
126
- processed = preprocess_text(user_input)
127
- if processed:
128
- try:
129
- input_df = pd.DataFrame({'processed_excerpt': [processed]})
130
- if hasattr(model, "predict_proba"):
131
- probs = model.predict_proba(input_df)[0]
132
- top_idx = np.argsort(probs)[-5:][::-1]
133
- tags = [mlb.classes_[i] for i in top_idx]
134
- confs = [int(probs[i] * 100) for i in top_idx]
135
- elif hasattr(model, "decision_function"):
136
- scores = model.decision_function(input_df)[0]
137
- top_idx = np.argsort(scores)[-5:][::-1]
138
- tags = [mlb.classes_[i] for i in top_idx]
139
- confs = [None] * 5
140
- else:
141
- preds = model.predict(input_df)
142
- tags = mlb.inverse_transform(preds)[0]
143
- confs = [None] * len(tags)
144
-
145
- # Output
146
- st.markdown('<div class="output-box"><h4>🏷️ Predicted Tags:</h4>', unsafe_allow_html=True)
147
- for tag, conf in zip(tags, confs):
148
- confidence = f" ({conf}%)" if conf is not None else ""
149
- st.markdown(f'<span class="tag-pill">{tag}{confidence}</span>', unsafe_allow_html=True)
150
- st.markdown('</div>', unsafe_allow_html=True)
151
- except Exception as e:
152
- st.error(f"Prediction error: {e}")
153
-
154
- # Footer
155
- st.markdown("""
156
- <div class="footer">
157
- 🔎 This ML tool predicts tags based on programming question content.
158
- </div>
159
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/requirements .txt DELETED
@@ -1,9 +0,0 @@
1
- streamlit
2
- pandas
3
- numpy
4
- scikit-learn
5
- nltk
6
- joblib
7
- unidecode
8
- emoji
9
- contractions
 
 
 
 
 
 
 
 
 
 
src/streamlit_app.py DELETED
@@ -1,40 +0,0 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))