-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnlp_app.py
More file actions
75 lines (62 loc) · 3.43 KB
/
Copy pathnlp_app.py
File metadata and controls
75 lines (62 loc) · 3.43 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
import streamlit as st
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
# 1. Page Layout & UI Configuration
st.set_page_config(page_title="NLP Pipeline Tool", layout="centered")
st.title("📝 Natural Language Processing Pipeline")
st.write("This app demonstrates a complete text classification pipeline using Tokenisation, TF-IDF, and Machine Learning.")
# 2. The Internal Training Dataset
# (The model trains itself instantly on this data every time the app starts!)
training_data = [
"This is an amazing and fantastic tool, I love it!",
"Excellent quality, highly recommend this to everyone.",
"Brilliant customer care service and great experience.",
"The product works perfectly and smoothly.",
"Completely useless, a total waste of money and time.",
"Horrible experience, it broke immediately on day one.",
"I hate this application, it crashes constantly.",
"Worst service ever, deeply disappointed with the quality."
]
# Target Labels: 1 = Positive Sentiment, 0 = Negative Sentiment
training_labels = [1, 1, 1, 1, 0, 0, 0, 0]
# --- THE NLP PIPELINE IMPLEMENTATION ---
# Pipeline Steps 1 & 2: Tokenisation & TF-IDF Vectorisation
# The TfidfVectorizer handles tokenisation automatically by cleaning and splitting text
vectorizer = TfidfVectorizer(lowercase=True, stop_words='english')
X_train_tfidf = vectorizer.fit_transform(training_data)
# Pipeline Step 3: The Trained Classifier Model
# Training a Logistic Regression model brain on our numeric numeric dataset
classifier = LogisticRegression()
classifier.fit(X_train_tfidf, training_labels)
st.success("🤖 NLP Pipeline Model Trained Successfully!")
# --- USER INTERACTION INTERFACE ---
st.markdown("### 🔍 Test the Pipeline Live")
user_input = st.text_input("Enter a custom sentence or product review to classify:", "The build quality is absolutely horrible and broken.")
if user_input:
st.markdown("---")
st.subheader("🛠️ Pipeline Execution Breakdown")
# Showcase Pipeline Step 1: Tokenisation
# Emulating text cleanup to show raw tokens explicitly on screen
clean_text = re.sub(r'[^\w\s]', '', user_input.lower())
tokens = clean_text.split()
st.write("#### 1️⃣ Tokenisation Stage")
st.info(f"**Extracted Text Tokens (Individual Words):** `{tokens}`")
# Showcase Pipeline Step 2: TF-IDF Vectorisation
user_tfidf = vectorizer.transform([user_input])
feature_names = vectorizer.get_feature_names_out()
matched_features = [word for word in tokens if word in feature_names]
st.write("#### 2️⃣ TF-IDF Vectorisation Stage")
st.info(f"**Key Evaluated Features Found (Math Weights Applied):** `{matched_features}`")
# Showcase Pipeline Step 3: Trained Classifier Prediction
prediction = classifier.predict(user_tfidf)[0]
probabilities = classifier.predict_proba(user_tfidf)[0]
st.write("#### 3️⃣ Trained Classifier Prediction")
if prediction == 1:
st.markdown("### Final Sentiment Class: **🟢 POSITIVE**")
st.progress(float(probabilities[1]))
st.write(f"📊 Model Confidence Score: **{probabilities[1]*100:.2f}%**")
else:
st.markdown("### Final Sentiment Class: **🔴 NEGATIVE**")
st.progress(float(probabilities[0]))
st.write(f"📊 Model Confidence Score: **{probabilities[0]*100:.2f}%**")