File size: 13,019 Bytes
82dccf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
nlp_module.py  β€”  NLP Module (v2.1 Clean)
Models:
  - DistilBERT SST-2     β†’ sentiment analysis  (~250 MB, downloads on first use)
  - spaCy en_core_web_sm β†’ named entity recognition (~15 MB, auto-downloads)
  - TF-IDF              β†’ zero-shot classification (no download)
  - Extractive          β†’ summarization (no download)
  - Smart AI (built-in)  β†’ chatbot, zero downloads
"""
import warnings
warnings.filterwarnings("ignore")

import streamlit as st


# ══════════════════════════════════════════════════════════════════════════════
#  Cached pipeline loaders
# ══════════════════════════════════════════════════════════════════════════════

@st.cache_resource(show_spinner=False)
def load_sentiment_pipeline():
    """DistilBERT SST-2 β€” ~250 MB, fast and accurate."""
    from transformers import pipeline  # type: ignore[import-untyped]
    return pipeline(  # type: ignore[call-overload]
        "sentiment-analysis",
        model="distilbert-base-uncased-finetuned-sst-2-english",
    )


@st.cache_resource(show_spinner=False)
def load_ner_pipeline():
    """
    spaCy en_core_web_sm (~15 MB) for NER.
    Falls back to regex-based NER if spaCy is not installed.
    Install: pip install spacy && python -m spacy download en_core_web_sm
    """
    try:
        import spacy
        try:
            return ("spacy", spacy.load("en_core_web_sm"))
        except OSError:
            from spacy.cli.download import download as spacy_download  # type: ignore[import]
            spacy_download("en_core_web_sm")
            return ("spacy", spacy.load("en_core_web_sm"))
    except ImportError:
        return ("regex", None)


@st.cache_resource(show_spinner=False)
def load_zero_shot_pipeline():
    """
    Lightweight zero-shot classification using TF-IDF cosine similarity.
    Zero model downloads, zero RAM overhead β€” works on any machine.
    Falls back gracefully without any internet or large model requirement.
    """
    return "tfidf"   # sentinel value β€” actual logic is in run_text_classification


@st.cache_resource(show_spinner=False)
def load_summarization_pipeline():
    """
    Extractive summarizer β€” word-frequency scoring, zero model download.
    Picks the most informative sentences from the input text.
    """
    return "extractive"   # sentinel β€” actual logic in run_summarization


# ══════════════════════════════════════════════════════════════════════════════
#  Business logic
# ══════════════════════════════════════════════════════════════════════════════

def run_sentiment(texts: list) -> list:
    """
    Sentiment analysis on a list of strings.
    Returns list of dicts: Text, Sentiment, Confidence, Score.
    """
    pipe    = load_sentiment_pipeline()
    results = []
    for text in texts:
        if text.strip():
            r = pipe(text[:512], truncation=True, max_length=512)[0]
            results.append({
                "Text":       text[:80],
                "Sentiment":  r["label"],
                "Confidence": f"{r['score'] * 100:.1f}%",
                "Score":      round(r["score"], 4),
            })
    return results


def run_ner(text: str) -> list:
    """
    Named Entity Recognition using spaCy (15 MB) or regex fallback.
    Returns list of dicts: Entity, Type, Score, Start, End.
    """
    backend, model = load_ner_pipeline()

    if backend == "spacy" and model is not None:
        doc = model(text[:1000])
        return [
            {
                "Entity": ent.text,
                "Type":   ent.label_,
                "Score":  "100.0%",
                "Start":  ent.start_char,
                "End":    ent.end_char,
            }
            for ent in doc.ents
        ]

    # ── Regex fallback β€” works with zero extra installs ──────────────────────
    import re
    patterns = [
        (
            r'\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\s+'
            r'(?:Inc|Corp|Ltd|LLC|Co|Group|Foundation|Institute|University|'
            r'College|School|Hospital|Bank|Technologies|Solutions|Systems|Services)\.?)\b',
            "ORG",
        ),
        (
            r'\b([A-Z][a-zA-Z]+(?:\s+[A-Z][a-zA-Z]+)*)\b'
            r'(?=\s+(?:City|State|Country|Street|Avenue|Road|Park|Lake|River|'
            r'Mountain|Valley|Island|Bay|County|District|Province|Region))',
            "LOC",
        ),
        (
            r'\b([A-Z][a-z]{2,}\s+[A-Z][a-z]{2,}(?:\s+[A-Z][a-z]{2,})?)\b',
            "PER",
        ),
        (r'\b([A-Z]{2,6})\b', "ORG"),
    ]

    seen, results = set(), []
    for pattern, label in patterns:
        for m in re.finditer(pattern, text):
            entity = m.group(1).strip()
            key    = (entity, label)
            if key not in seen and len(entity) > 1:
                seen.add(key)
                results.append({
                    "Entity": entity,
                    "Type":   label,
                    "Score":  "~",
                    "Start":  m.start(),
                    "End":    m.end(),
                })

    return sorted(results, key=lambda x: x["Start"])


def _tfidf_cosine(text: str, label: str) -> float:
    """Compute TF-IDF cosine similarity between text and a label string."""
    import re
    from collections import Counter
    import math

    _stop = {"the","a","an","is","are","was","were","be","been","being","have",
             "has","had","do","does","did","will","would","could","should","may",
             "might","can","to","of","in","for","on","with","at","by","from","as",
             "and","but","or","not","it","its","this","that","i","we","you","he",
             "she","they","all","any","more","so","very","also","just","about"}

    def _tokens(s: str) -> list:
        return [w for w in re.findall(r"[a-z]+", s.lower()) if w not in _stop and len(w) > 1]

    t_tokens = _tokens(text)
    l_tokens = _tokens(label)
    if not t_tokens or not l_tokens:
        return 0.0

    # TF of text
    tf_t = Counter(t_tokens)
    tf_l = Counter(l_tokens)

    # Vocabulary union
    vocab = set(tf_t) | set(tf_l)

    # Simple IDF weight: log(1 + 1/freq_ratio) β€” single-doc approximation
    def vec(tf: Counter) -> dict:
        total = sum(tf.values()) or 1
        return {w: tf[w] / total for w in vocab}

    vt = vec(tf_t)
    vl = vec(tf_l)

    dot    = sum(vt[w] * vl[w] for w in vocab)
    norm_t = math.sqrt(sum(v * v for v in vt.values())) or 1e-9
    norm_l = math.sqrt(sum(v * v for v in vl.values())) or 1e-9
    return dot / (norm_t * norm_l)


def run_text_classification(text: str, labels: list) -> list:
    """
    Zero-shot text classification using TF-IDF cosine similarity.
    No model download required β€” works instantly on any machine.
    Returns list of dicts: Label, Score, Confidence β€” sorted by score desc.
    """
    if not labels:
        return []

    scores = []
    for label in labels:
        # Boost: also compare text against expanded label description
        sim = _tfidf_cosine(text, label)
        scores.append((label, sim))

    # Normalise scores so they sum to 1 (softmax-like)
    import math
    exp_scores = [(lbl, math.exp(s * 8)) for lbl, s in scores]   # temperature=8 sharpens
    total = sum(s for _, s in exp_scores) or 1.0
    normalised = sorted(
        [{"Label": lbl, "Score": round(s / total, 4), "Confidence": f"{s / total * 100:.1f}%"}
         for lbl, s in exp_scores],
        key=lambda x: x["Score"], reverse=True,
    )
    return normalised


def run_summarization(text: str) -> str:
    """
    Extractive summarization using word-frequency scoring.
    Zero model download β€” works on any machine, any RAM size.
    Picks the top 3 most informative sentences.
    """
    import re
    from collections import Counter

    text = text.strip()
    # Split into sentences
    sentences = re.split(r"(?<=[.!?])\s+", text)
    sentences = [s.strip() for s in sentences if len(s.split()) > 4]

    if len(sentences) <= 2:
        return text[:400] + ("…" if len(text) > 400 else "")

    # Stop words to ignore when computing importance
    stop = {"the","a","an","is","are","was","were","be","been","being","have",
            "has","had","do","does","did","will","would","could","should","may",
            "might","can","to","of","in","for","on","with","at","by","from",
            "as","into","and","but","or","not","it","its","this","that","i",
            "we","you","he","she","they","all","any","each","more","most","so",
            "very","also","just","about","than","other","such","when","which"}

    words  = re.findall(r"[a-z]+", text.lower())
    freq   = Counter(w for w in words if w not in stop and len(w) > 2)
    max_f  = max(freq.values(), default=1)
    freq   = {w: v / max_f for w, v in freq.items()}

    # Score sentences
    scores: dict = {}
    for i, sent in enumerate(sentences):
        score = sum(freq.get(w, 0) for w in re.findall(r"[a-z]+", sent.lower()))
        score = score / max(len(sent.split()), 1)
        if i == 0:
            score *= 1.3    # slight boost for the opening sentence
        scores[i] = score

    # Pick top N sentences (preserve original order)
    n   = max(1, min(4, len(sentences) // 3))
    top = sorted(sorted(scores, key=lambda k: scores[k], reverse=True)[:n])
    return " ".join(sentences[i] for i in top)


def chat_with_model(prompt: str, history: list) -> str:
    """
    Instant chatbot using Smart AI β€” no model download, zero RAM.
    Falls back to simple keyword responses if the import fails.
    """
    try:
        import sys
        from pathlib import Path
        # Support both flat and models/ directory layouts
        sys.path.insert(0, str(Path(__file__).parent))
        sys.path.insert(0, str(Path(__file__).parent.parent))
        from generative_ai import _smart_respond

        # Convert (user, bot) tuple history to dict format
        hist_dicts = []
        for u, b in history[-4:]:
            hist_dicts.append({"role": "user",      "content": u})
            hist_dicts.append({"role": "assistant",  "content": b})

        return _smart_respond(prompt, hist_dicts)

    except Exception:
        # Ultra-safe fallback if generative_ai import fails
        p = prompt.lower()
        if any(w in p for w in ["hello", "hi", "hey"]):
            return "Hello! Ask me anything about ML, data science, or AI. 😊"
        if "machine learning" in p or " ml " in p:
            return (
                "**Machine Learning** enables systems to learn patterns from data without "
                "explicit programming. Types: Supervised, Unsupervised, Reinforcement. "
                "Libraries: scikit-learn, XGBoost, LightGBM."
            )
        if "deep learning" in p or "neural" in p:
            return (
                "**Deep Learning** uses multi-layer neural networks to learn complex features. "
                "Best for images (CNNs), sequences (Transformers), and unstructured data. "
                "Frameworks: PyTorch, TensorFlow."
            )
        if "xgboost" in p or "gradient boosting" in p:
            return (
                "**XGBoost** builds trees sequentially, each correcting errors of the prior. "
                "Key params: n_estimators, max_depth, learning_rate. Extremely fast and accurate."
            )
        if "overfitting" in p:
            return (
                "**Overfitting** = model memorises training noise, fails on new data. "
                "Fixes: cross-validation, regularisation (L1/L2), dropout, more data, simpler model."
            )
        if "python" in p:
            return (
                "**Python** dominates AI/ML thanks to: NumPy, Pandas, scikit-learn, "
                "PyTorch, TensorFlow, HuggingFace Transformers. "
                "Use virtual environments to manage dependencies."
            )
        if "nlp" in p or "natural language" in p:
            return (
                "**NLP** (Natural Language Processing) enables machines to understand text. "
                "Key tasks: sentiment, NER, classification, summarisation, translation. "
                "Modern approach: HuggingFace Transformers (BERT, GPT, T5)."
            )
        return (
            "I'm your AI assistant. Try asking about: machine learning, neural networks, "
            "XGBoost, overfitting, Python, NLP, or data science topics!"
        )