-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
397 lines (321 loc) · 13.5 KB
/
streamlit_app.py
File metadata and controls
397 lines (321 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
IntelliTag - Stack Overflow Tag Suggestion System
Streamlit Web Application
This app provides an interactive interface for predicting tags
for Stack Overflow questions using NLP and machine learning.
"""
import sys
import time
from pathlib import Path
import streamlit as st
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
# Page configuration
st.set_page_config(
page_title="IntelliTag - Tag Predictor",
page_icon="🏷️",
layout="wide",
initial_sidebar_state="expanded",
)
# =============================================================================
# MODEL LOADING
# =============================================================================
@st.cache_resource
def load_intellitag_models():
"""Load IntelliTag models if available."""
try:
from intellitag.config.settings import settings
from intellitag.data.preprocessor import TextPreprocessor
from intellitag.features import BowExtractor
from intellitag.models.classifier import TagClassifier
preprocessor = TextPreprocessor.for_bow()
extractor_path = settings.model_path / "bow_extractor.pkl"
classifier_path = settings.model_path / "classifier.pkl"
if extractor_path.exists() and classifier_path.exists():
extractor = BowExtractor.load(extractor_path)
classifier = TagClassifier.load(classifier_path)
return {
"preprocessor": preprocessor,
"extractor": extractor,
"classifier": classifier,
"loaded": True,
}
except Exception as e:
st.sidebar.warning(f"Model loading: {e}")
return {"loaded": False}
def predict_tags_demo(title: str, body: str, top_k: int = 5) -> list:
"""
Demo prediction based on keyword matching.
Used when ML models are not available.
"""
text = f"{title} {body}".lower()
# Keyword-based tag suggestions (demo mode)
tag_keywords = {
"python": ["python", "pandas", "numpy", "django", "flask", "pip"],
"javascript": ["javascript", "js", "node", "react", "vue", "angular"],
"java": ["java", "spring", "maven", "gradle", "jvm"],
"c#": ["c#", "csharp", ".net", "asp.net", "unity"],
"html": ["html", "html5", "dom", "webpage"],
"css": ["css", "css3", "flexbox", "grid", "sass", "bootstrap"],
"sql": ["sql", "mysql", "postgresql", "database", "query"],
"json": ["json", "parse", "serialize", "api"],
"api": ["api", "rest", "endpoint", "request", "response"],
"pandas": ["pandas", "dataframe", "series", "csv"],
"numpy": ["numpy", "array", "matrix", "ndarray"],
"machine-learning": ["machine learning", "ml", "model", "train", "predict"],
"tensorflow": ["tensorflow", "keras", "neural", "deep learning"],
"django": ["django", "orm", "views", "templates"],
"flask": ["flask", "route", "blueprint"],
"react": ["react", "jsx", "component", "hooks", "redux"],
"node.js": ["node", "npm", "express", "async"],
"git": ["git", "commit", "branch", "merge", "github"],
"docker": ["docker", "container", "image", "dockerfile"],
"regex": ["regex", "regular expression", "pattern", "match"],
"list": ["list", "array", "append", "iterate"],
"dictionary": ["dictionary", "dict", "key", "value", "hash"],
"string": ["string", "text", "substring", "split", "join"],
"file": ["file", "read", "write", "open", "path"],
"error": ["error", "exception", "traceback", "debug"],
}
scores = {}
for tag, keywords in tag_keywords.items():
score = sum(1 for kw in keywords if kw in text)
if score > 0:
# Normalize score
scores[tag] = min(0.95, 0.3 + score * 0.15)
# Sort by score and return top_k
sorted_tags = sorted(scores.items(), key=lambda x: x[1], reverse=True)[:top_k]
# If no matches, return generic tags
if not sorted_tags:
sorted_tags = [("python", 0.5), ("programming", 0.3), ("code", 0.2)][:top_k]
return sorted_tags
def predict_tags_ml(
models: dict, title: str, body: str, top_k: int = 5, threshold: float = 0.1
) -> list:
"""Predict tags using loaded ML models."""
text = f"{title} {body}"
# Preprocess
preprocessed = models["preprocessor"].process(text)
# Extract features
features = models["extractor"].transform([preprocessed])
# Predict
models["classifier"].threshold = threshold
models["classifier"].top_k = top_k
predictions = models["classifier"].predict_with_proba(features)
return predictions[0]
# =============================================================================
# UI COMPONENTS
# =============================================================================
def render_sidebar():
"""Render sidebar with settings and info."""
st.sidebar.title("🏷️ IntelliTag")
st.sidebar.markdown("*Intelligent Tag Suggestions*")
st.sidebar.markdown("---")
# Settings
st.sidebar.header("⚙️ Settings")
top_k = st.sidebar.slider(
"Number of tags",
min_value=1,
max_value=10,
value=5,
help="Maximum number of tags to suggest",
)
threshold = st.sidebar.slider(
"Confidence threshold",
min_value=0.0,
max_value=1.0,
value=0.1,
step=0.05,
help="Minimum confidence score for tag suggestions",
)
st.sidebar.markdown("---")
# Model status
st.sidebar.header("📊 Model Status")
models = load_intellitag_models()
if models["loaded"]:
st.sidebar.success("✅ ML Models Loaded")
n_tags = (
models["classifier"].n_classes if models["classifier"]._is_fitted else "N/A"
)
st.sidebar.metric("Supported Tags", n_tags)
else:
st.sidebar.info("🎮 Demo Mode (keyword-based)")
st.sidebar.caption("ML models not available. Using demo predictions.")
st.sidebar.markdown("---")
# About
st.sidebar.header("ℹ️ About")
st.sidebar.markdown("""
**IntelliTag** is an intelligent tag suggestion system
for Stack Overflow questions.
- 🧠 Multi-model NLP pipeline
- 📊 78% Precision@5 achieved
- ⚡ <200ms latency
[GitHub](https://github.com/ThomasMeb/Classifier_Questions_StackOverflow) |
[API Docs](/docs)
""")
return top_k, threshold, models
def render_example_questions():
"""Render example question buttons."""
st.markdown("##### 💡 Try an example:")
examples = [
{
"title": "How to parse JSON in Python?",
"body": "I have a JSON string and I want to convert it to a Python dictionary. What is the best way to do this? I tried using the json module but I'm getting errors when the JSON contains special characters.",
},
{
"title": "React useState not updating immediately",
"body": "I'm using useState in my React component but when I call setState, the value doesn't update immediately. I need to use the updated value right after setting it. How can I solve this? Should I use useEffect?",
},
{
"title": "SQL query to find duplicates in a table",
"body": "I have a MySQL database table with millions of rows and I need to find all duplicate entries based on the email column. What's the most efficient query to do this without affecting performance?",
},
{
"title": "How to train a neural network with TensorFlow",
"body": "I'm new to machine learning and want to build a simple neural network for image classification. I have a dataset of images and labels. Can someone explain how to structure the model and train it using TensorFlow/Keras?",
},
]
cols = st.columns(len(examples))
for i, (col, example) in enumerate(zip(cols, examples)):
with col:
if st.button(
f"Example {i+1}", key=f"example_{i}", use_container_width=True
):
st.session_state.title = example["title"]
st.session_state.body = example["body"]
st.rerun()
def render_results(predictions: list, processing_time: float, is_demo: bool):
"""Render prediction results."""
st.markdown("### 🏷️ Suggested Tags")
if is_demo:
st.caption("🎮 Demo mode - Results based on keyword matching")
else:
st.caption("🤖 ML model predictions")
# Metrics row
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Tags Found", len(predictions))
with col2:
st.metric("Processing Time", f"{processing_time:.0f}ms")
with col3:
avg_conf = (
sum(p[1] for p in predictions) / len(predictions) if predictions else 0
)
st.metric("Avg. Confidence", f"{avg_conf:.1%}")
st.markdown("---")
# Tags display
if predictions:
# Visual tags
tag_html = " ".join(
[
f'<span style="background-color: {"#2ecc71" if conf > 0.7 else "#3498db" if conf > 0.4 else "#95a5a6"}; '
f"color: white; padding: 5px 12px; border-radius: 15px; margin: 3px; display: inline-block; "
f'font-size: 14px;">{tag} <small>({conf:.0%})</small></span>'
for tag, conf in predictions
]
)
st.markdown(
f'<div style="line-height: 2.5;">{tag_html}</div>', unsafe_allow_html=True
)
st.markdown("")
# Detailed table
with st.expander("📊 Detailed Confidence Scores"):
for tag, conf in predictions:
col1, col2 = st.columns([3, 1])
with col1:
st.progress(conf, text=tag)
with col2:
st.write(f"**{conf:.2%}**")
else:
st.warning(
"No tags predicted. Try adjusting the threshold or adding more detail to your question."
)
# =============================================================================
# MAIN APPLICATION
# =============================================================================
def main():
"""Main application entry point."""
# Initialize session state
if "title" not in st.session_state:
st.session_state.title = ""
if "body" not in st.session_state:
st.session_state.body = ""
# Render sidebar and get settings
top_k, threshold, models = render_sidebar()
# Main content
st.title("🏷️ IntelliTag")
st.markdown("### Intelligent Tag Suggestion for Stack Overflow Questions")
st.markdown("""
> Enter your Stack Overflow question below and get AI-powered tag suggestions
> to help categorize your question effectively.
""")
# Example questions
render_example_questions()
st.markdown("---")
# Input form
st.markdown("### 📝 Your Question")
title = st.text_input(
"Question Title",
value=st.session_state.title,
max_chars=300,
placeholder="e.g., How to parse JSON in Python?",
help="Enter a clear, specific title (10-300 characters)",
)
body = st.text_area(
"Question Body",
value=st.session_state.body,
height=200,
max_chars=30000,
placeholder="Describe your question in detail. Include code snippets, error messages, and what you've tried...",
help="Provide detailed context (30-30,000 characters)",
)
# Validation
title_valid = len(title) >= 10
body_valid = len(body) >= 30
col1, col2 = st.columns(2)
with col1:
if title and not title_valid:
st.warning("⚠️ Title must be at least 10 characters")
with col2:
if body and not body_valid:
st.warning("⚠️ Body must be at least 30 characters")
# Predict button
st.markdown("")
predict_button = st.button(
"🔮 Predict Tags",
type="primary",
disabled=not (title_valid and body_valid),
use_container_width=True,
)
# Prediction
if predict_button and title_valid and body_valid:
with st.spinner("Analyzing your question..."):
start_time = time.time()
if models["loaded"]:
predictions = predict_tags_ml(models, title, body, top_k, threshold)
is_demo = False
else:
predictions = predict_tags_demo(title, body, top_k)
# Filter by threshold
predictions = [(t, c) for t, c in predictions if c >= threshold]
is_demo = True
processing_time = (time.time() - start_time) * 1000
st.markdown("---")
render_results(predictions, processing_time, is_demo)
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: gray; padding: 20px;'>
<p>🏷️ <strong>IntelliTag</strong> - Stack Overflow Tag Suggestion System</p>
<p>
<a href='https://github.com/ThomasMeb/Classifier_Questions_StackOverflow'>GitHub</a> |
Built with Streamlit & FastAPI |
© 2026 Thomas Mebarki
</p>
</div>
""",
unsafe_allow_html=True,
)
if __name__ == "__main__":
main()