Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,336 +1,645 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
from sklearn.
|
| 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 |
-
if
|
| 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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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.preprocessing import LabelEncoder
|
| 9 |
+
from sklearn.linear_model import LogisticRegression
|
| 10 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 11 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 12 |
+
from sklearn.svm import LinearSVC, SVC
|
| 13 |
+
from sklearn.naive_bayes import MultinomialNB, GaussianNB
|
| 14 |
+
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 15 |
+
import os
|
| 16 |
+
import pickle
|
| 17 |
+
import re
|
| 18 |
+
import string
|
| 19 |
+
from collections import Counter
|
| 20 |
+
import plotly.express as px
|
| 21 |
+
import plotly.graph_objects as go
|
| 22 |
+
|
| 23 |
+
# Configure Streamlit page
|
| 24 |
+
st.set_page_config(
|
| 25 |
+
page_title="Text Classification App",
|
| 26 |
+
page_icon="๐",
|
| 27 |
+
layout="wide"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Text preprocessing class
|
| 31 |
+
class TextCleaner:
|
| 32 |
+
def __init__(self):
|
| 33 |
+
self.stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by'])
|
| 34 |
+
|
| 35 |
+
def clean_text(self, text):
|
| 36 |
+
"""Clean and preprocess text"""
|
| 37 |
+
if pd.isna(text):
|
| 38 |
+
return ""
|
| 39 |
+
|
| 40 |
+
text = str(text).lower()
|
| 41 |
+
text = re.sub(r'http\S+', '', text) # Remove URLs
|
| 42 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text) # Remove non-alphabetic characters
|
| 43 |
+
text = re.sub(r'\s+', ' ', text) # Remove extra whitespace
|
| 44 |
+
text = text.strip()
|
| 45 |
+
|
| 46 |
+
# Remove stop words (optional)
|
| 47 |
+
words = text.split()
|
| 48 |
+
words = [word for word in words if word not in self.stop_words]
|
| 49 |
+
|
| 50 |
+
return ' '.join(words)
|
| 51 |
+
|
| 52 |
+
# Data analysis functions
|
| 53 |
+
def get_data_insights(df, text_col, target_col):
|
| 54 |
+
"""Get basic insights from the dataset"""
|
| 55 |
+
insights = {
|
| 56 |
+
'shape': df.shape,
|
| 57 |
+
'missing_values': df.isnull().sum().to_dict(),
|
| 58 |
+
'class_distribution': df[target_col].value_counts().to_dict(),
|
| 59 |
+
'text_length_stats': {
|
| 60 |
+
'mean': df[text_col].str.len().mean(),
|
| 61 |
+
'median': df[text_col].str.len().median(),
|
| 62 |
+
'min': df[text_col].str.len().min(),
|
| 63 |
+
'max': df[text_col].str.len().max()
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
return insights
|
| 67 |
+
|
| 68 |
+
# Model training functions
|
| 69 |
+
def train_model(model_name, X_train, X_test, y_train, y_test):
|
| 70 |
+
"""Train and evaluate a model"""
|
| 71 |
+
models = {
|
| 72 |
+
'Logistic Regression': LogisticRegression(random_state=42, max_iter=1000),
|
| 73 |
+
'Decision Tree': DecisionTreeClassifier(random_state=42),
|
| 74 |
+
'Random Forest': RandomForestClassifier(random_state=42, n_estimators=100),
|
| 75 |
+
'Linear SVC': LinearSVC(random_state=42, max_iter=1000),
|
| 76 |
+
'SVC': SVC(random_state=42, probability=True),
|
| 77 |
+
'Multinomial Naive Bayes': MultinomialNB(),
|
| 78 |
+
'Gaussian Naive Bayes': GaussianNB()
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
model = models[model_name]
|
| 82 |
+
|
| 83 |
+
# For Gaussian NB, convert sparse matrix to dense
|
| 84 |
+
if model_name == 'Gaussian Naive Bayes':
|
| 85 |
+
X_train = X_train.toarray()
|
| 86 |
+
X_test = X_test.toarray()
|
| 87 |
+
|
| 88 |
+
# Train model
|
| 89 |
+
model.fit(X_train, y_train)
|
| 90 |
+
|
| 91 |
+
# Make predictions
|
| 92 |
+
y_pred = model.predict(X_test)
|
| 93 |
+
|
| 94 |
+
# Calculate metrics
|
| 95 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 96 |
+
|
| 97 |
+
# Save model
|
| 98 |
+
os.makedirs("models", exist_ok=True)
|
| 99 |
+
model_filename = f"{model_name.replace(' ', '_').lower()}.pkl"
|
| 100 |
+
with open(os.path.join("models", model_filename), 'wb') as f:
|
| 101 |
+
pickle.dump(model, f)
|
| 102 |
+
|
| 103 |
+
return model, accuracy, y_pred, model_filename
|
| 104 |
+
|
| 105 |
+
# Utility functions
|
| 106 |
+
def save_artifacts(obj, folder_name, file_name):
|
| 107 |
+
"""Save artifacts like encoders and vectorizers"""
|
| 108 |
+
os.makedirs(folder_name, exist_ok=True)
|
| 109 |
+
with open(os.path.join(folder_name, file_name), 'wb') as f:
|
| 110 |
+
pickle.dump(obj, f)
|
| 111 |
+
|
| 112 |
+
def load_artifacts(folder_name, file_name):
|
| 113 |
+
"""Load saved artifacts"""
|
| 114 |
+
try:
|
| 115 |
+
with open(os.path.join(folder_name, file_name), 'rb') as f:
|
| 116 |
+
return pickle.load(f)
|
| 117 |
+
except FileNotFoundError:
|
| 118 |
+
st.error(f"File {file_name} not found in {folder_name} folder")
|
| 119 |
+
return None
|
| 120 |
+
|
| 121 |
+
def predict_text(model_filename, text, vectorizer_type="tfidf"):
|
| 122 |
+
"""Make prediction on new text"""
|
| 123 |
+
try:
|
| 124 |
+
# Load model
|
| 125 |
+
with open(os.path.join('models', model_filename), 'rb') as f:
|
| 126 |
+
model = pickle.load(f)
|
| 127 |
+
|
| 128 |
+
# Load vectorizer
|
| 129 |
+
vectorizer_file = f"{vectorizer_type}_vectorizer.pkl"
|
| 130 |
+
vectorizer = load_artifacts("artifacts", vectorizer_file)
|
| 131 |
+
if vectorizer is None:
|
| 132 |
+
return None, None
|
| 133 |
+
|
| 134 |
+
# Load label encoder
|
| 135 |
+
encoder = load_artifacts("artifacts", "encoder.pkl")
|
| 136 |
+
if encoder is None:
|
| 137 |
+
return None, None
|
| 138 |
+
|
| 139 |
+
# Clean and vectorize text
|
| 140 |
+
text_cleaner = TextCleaner()
|
| 141 |
+
clean_text = text_cleaner.clean_text(text)
|
| 142 |
+
|
| 143 |
+
# Transform text
|
| 144 |
+
text_vector = vectorizer.transform([clean_text])
|
| 145 |
+
|
| 146 |
+
# For Gaussian NB, convert to dense
|
| 147 |
+
if 'gaussian' in model_filename:
|
| 148 |
+
text_vector = text_vector.toarray()
|
| 149 |
+
|
| 150 |
+
# Make prediction
|
| 151 |
+
prediction = model.predict(text_vector)
|
| 152 |
+
prediction_proba = None
|
| 153 |
+
|
| 154 |
+
# Get prediction probabilities if available
|
| 155 |
+
if hasattr(model, 'predict_proba'):
|
| 156 |
+
try:
|
| 157 |
+
prediction_proba = model.predict_proba(text_vector)[0]
|
| 158 |
+
except:
|
| 159 |
+
pass
|
| 160 |
+
|
| 161 |
+
# Decode prediction
|
| 162 |
+
predicted_label = encoder.inverse_transform(prediction)[0]
|
| 163 |
+
|
| 164 |
+
return predicted_label, prediction_proba
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
st.error(f"Error during prediction: {str(e)}")
|
| 168 |
+
return None, None
|
| 169 |
+
|
| 170 |
+
# Streamlit App
|
| 171 |
+
st.title('๐ No Code Text Classification App')
|
| 172 |
+
st.markdown('---')
|
| 173 |
+
st.write('Analyze your text data and train machine learning models without coding!')
|
| 174 |
+
|
| 175 |
+
# Sidebar
|
| 176 |
+
st.sidebar.title("Navigation")
|
| 177 |
+
section = st.sidebar.radio("Choose Section", ["๐ Data Analysis", "๐ค Train Model", "๐ฎ Predictions"])
|
| 178 |
+
|
| 179 |
+
# Upload Data
|
| 180 |
+
st.sidebar.markdown("---")
|
| 181 |
+
st.sidebar.subheader("๐ Upload Your Dataset")
|
| 182 |
+
train_data = st.sidebar.file_uploader("Upload training data", type=["csv"])
|
| 183 |
+
test_data = st.sidebar.file_uploader("Upload test data (optional)", type=["csv"])
|
| 184 |
+
|
| 185 |
+
# Global variables to store data and settings
|
| 186 |
+
if 'vectorizer_type' not in st.session_state:
|
| 187 |
+
st.session_state.vectorizer_type = "tfidf"
|
| 188 |
+
|
| 189 |
+
if train_data is not None:
|
| 190 |
+
try:
|
| 191 |
+
# Try different encodings
|
| 192 |
+
encodings = ['utf-8', 'latin1', 'cp1252', 'iso-8859-1']
|
| 193 |
+
train_df = None
|
| 194 |
+
|
| 195 |
+
for encoding in encodings:
|
| 196 |
+
try:
|
| 197 |
+
train_df = pd.read_csv(train_data, encoding=encoding)
|
| 198 |
+
break
|
| 199 |
+
except UnicodeDecodeError:
|
| 200 |
+
continue
|
| 201 |
+
|
| 202 |
+
if train_df is None:
|
| 203 |
+
st.error("Unable to read the CSV file. Please check the file encoding.")
|
| 204 |
+
else:
|
| 205 |
+
if test_data is not None:
|
| 206 |
+
for encoding in encodings:
|
| 207 |
+
try:
|
| 208 |
+
test_df = pd.read_csv(test_data, encoding=encoding)
|
| 209 |
+
break
|
| 210 |
+
except UnicodeDecodeError:
|
| 211 |
+
continue
|
| 212 |
+
else:
|
| 213 |
+
test_df = None
|
| 214 |
+
|
| 215 |
+
# Show data preview
|
| 216 |
+
with st.sidebar.expander("๐ Data Preview", expanded=True):
|
| 217 |
+
st.write("Shape:", train_df.shape)
|
| 218 |
+
st.write(train_df.head(2))
|
| 219 |
+
|
| 220 |
+
columns = train_df.columns.tolist()
|
| 221 |
+
text_data = st.sidebar.selectbox("๐ Choose the text column:", columns)
|
| 222 |
+
target = st.sidebar.selectbox("๐ฏ Choose the target column:", columns)
|
| 223 |
+
|
| 224 |
+
# Process data
|
| 225 |
+
if text_data and target:
|
| 226 |
+
# Clean text
|
| 227 |
+
text_cleaner = TextCleaner()
|
| 228 |
+
train_df['clean_text'] = train_df[text_data].apply(text_cleaner.clean_text)
|
| 229 |
+
train_df['text_length'] = train_df[text_data].str.len()
|
| 230 |
+
|
| 231 |
+
# Handle label encoding
|
| 232 |
+
label_encoder = LabelEncoder()
|
| 233 |
+
train_df['target_encoded'] = label_encoder.fit_transform(train_df[target])
|
| 234 |
+
|
| 235 |
+
# Save label encoder
|
| 236 |
+
save_artifacts(label_encoder, "artifacts", "encoder.pkl")
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
st.error(f"Error loading data: {str(e)}")
|
| 240 |
+
train_df = None
|
| 241 |
+
|
| 242 |
+
# Data Analysis Section
|
| 243 |
+
if section == "๐ Data Analysis":
|
| 244 |
+
if train_data is not None and 'train_df' in locals() and train_df is not None:
|
| 245 |
+
st.header("๐ Data Analysis")
|
| 246 |
+
|
| 247 |
+
# Get insights
|
| 248 |
+
insights = get_data_insights(train_df, text_data, target)
|
| 249 |
+
|
| 250 |
+
# Display insights in columns
|
| 251 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 252 |
+
|
| 253 |
+
with col1:
|
| 254 |
+
st.metric("Total Samples", insights['shape'][0])
|
| 255 |
+
|
| 256 |
+
with col2:
|
| 257 |
+
st.metric("Features", insights['shape'][1])
|
| 258 |
+
|
| 259 |
+
with col3:
|
| 260 |
+
st.metric("Classes", len(insights['class_distribution']))
|
| 261 |
+
|
| 262 |
+
with col4:
|
| 263 |
+
st.metric("Avg Text Length", f"{insights['text_length_stats']['mean']:.1f}")
|
| 264 |
+
|
| 265 |
+
st.markdown("---")
|
| 266 |
+
|
| 267 |
+
# Data quality section
|
| 268 |
+
col1, col2 = st.columns(2)
|
| 269 |
+
|
| 270 |
+
with col1:
|
| 271 |
+
st.subheader("๐ Dataset Overview")
|
| 272 |
+
st.write("**Shape:**", insights['shape'])
|
| 273 |
+
st.write("**Missing Values:**")
|
| 274 |
+
missing_df = pd.DataFrame.from_dict(insights['missing_values'], orient='index', columns=['Count'])
|
| 275 |
+
st.dataframe(missing_df[missing_df['Count'] > 0])
|
| 276 |
+
|
| 277 |
+
st.write("**Sample Data:**")
|
| 278 |
+
st.dataframe(train_df[[text_data, target, 'text_length']].head())
|
| 279 |
+
|
| 280 |
+
with col2:
|
| 281 |
+
st.subheader("๐ Class Distribution")
|
| 282 |
+
class_dist = pd.DataFrame.from_dict(insights['class_distribution'], orient='index', columns=['Count'])
|
| 283 |
+
st.dataframe(class_dist)
|
| 284 |
+
|
| 285 |
+
# Plot class distribution
|
| 286 |
+
fig = px.bar(
|
| 287 |
+
x=class_dist.index,
|
| 288 |
+
y=class_dist['Count'],
|
| 289 |
+
title="Class Distribution",
|
| 290 |
+
labels={'x': 'Class', 'y': 'Count'}
|
| 291 |
+
)
|
| 292 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 293 |
+
|
| 294 |
+
st.markdown("---")
|
| 295 |
+
|
| 296 |
+
# Text analysis section
|
| 297 |
+
st.subheader("๐ Text Analysis")
|
| 298 |
+
|
| 299 |
+
col1, col2 = st.columns(2)
|
| 300 |
+
|
| 301 |
+
with col1:
|
| 302 |
+
# Text length distribution
|
| 303 |
+
fig = px.histogram(
|
| 304 |
+
train_df,
|
| 305 |
+
x='text_length',
|
| 306 |
+
title="Text Length Distribution",
|
| 307 |
+
nbins=30
|
| 308 |
+
)
|
| 309 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 310 |
+
|
| 311 |
+
with col2:
|
| 312 |
+
# Text length by class
|
| 313 |
+
fig = px.box(
|
| 314 |
+
train_df,
|
| 315 |
+
x=target,
|
| 316 |
+
y='text_length',
|
| 317 |
+
title="Text Length by Class"
|
| 318 |
+
)
|
| 319 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 320 |
+
|
| 321 |
+
# Word frequency analysis
|
| 322 |
+
st.subheader("๐ค Most Common Words")
|
| 323 |
+
all_text = ' '.join(train_df['clean_text'].astype(str))
|
| 324 |
+
word_freq = Counter(all_text.split())
|
| 325 |
+
top_words = word_freq.most_common(20)
|
| 326 |
+
|
| 327 |
+
if top_words:
|
| 328 |
+
words_df = pd.DataFrame(top_words, columns=['Word', 'Frequency'])
|
| 329 |
+
fig = px.bar(
|
| 330 |
+
words_df,
|
| 331 |
+
x='Frequency',
|
| 332 |
+
y='Word',
|
| 333 |
+
orientation='h',
|
| 334 |
+
title="Top 20 Most Common Words"
|
| 335 |
+
)
|
| 336 |
+
fig.update_layout(yaxis={'categoryorder': 'total ascending'})
|
| 337 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 338 |
+
|
| 339 |
+
else:
|
| 340 |
+
st.warning("๐ Please upload training data to perform analysis")
|
| 341 |
+
|
| 342 |
+
# Train Model Section
|
| 343 |
+
elif section == "๐ค Train Model":
|
| 344 |
+
if train_data is not None and 'train_df' in locals() and train_df is not None:
|
| 345 |
+
st.header("๐ค Train Machine Learning Model")
|
| 346 |
+
|
| 347 |
+
col1, col2 = st.columns(2)
|
| 348 |
+
|
| 349 |
+
with col1:
|
| 350 |
+
st.subheader("โ๏ธ Model Configuration")
|
| 351 |
+
model_name = st.selectbox("Choose Model", [
|
| 352 |
+
"Logistic Regression", "Decision Tree",
|
| 353 |
+
"Random Forest", "Linear SVC", "SVC",
|
| 354 |
+
"Multinomial Naive Bayes", "Gaussian Naive Bayes"
|
| 355 |
+
])
|
| 356 |
+
|
| 357 |
+
with col2:
|
| 358 |
+
st.subheader("๐ Vectorization Method")
|
| 359 |
+
vectorizer_choice = st.selectbox("Choose Vectorizer", ["TF-IDF", "Count Vectorizer"])
|
| 360 |
+
|
| 361 |
+
# Model parameters
|
| 362 |
+
st.subheader("๐ง Parameters")
|
| 363 |
+
col1, col2 = st.columns(2)
|
| 364 |
+
|
| 365 |
+
with col1:
|
| 366 |
+
max_features = st.slider("Max Features", 1000, 20000, 10000, step=1000)
|
| 367 |
+
test_size = st.slider("Test Size", 0.1, 0.4, 0.2, step=0.05)
|
| 368 |
+
|
| 369 |
+
with col2:
|
| 370 |
+
random_state = st.number_input("Random State", 0, 1000, 42)
|
| 371 |
+
min_df = st.slider("Min Document Frequency", 1, 10, 1)
|
| 372 |
+
|
| 373 |
+
# Initialize vectorizer
|
| 374 |
+
if vectorizer_choice == "TF-IDF":
|
| 375 |
+
vectorizer = TfidfVectorizer(
|
| 376 |
+
max_features=max_features,
|
| 377 |
+
min_df=min_df,
|
| 378 |
+
stop_words='english'
|
| 379 |
+
)
|
| 380 |
+
st.session_state.vectorizer_type = "tfidf"
|
| 381 |
+
else:
|
| 382 |
+
vectorizer = CountVectorizer(
|
| 383 |
+
max_features=max_features,
|
| 384 |
+
min_df=min_df,
|
| 385 |
+
stop_words='english'
|
| 386 |
+
)
|
| 387 |
+
st.session_state.vectorizer_type = "count"
|
| 388 |
+
|
| 389 |
+
# Show data info
|
| 390 |
+
st.subheader("๐ Training Data Info")
|
| 391 |
+
col1, col2, col3 = st.columns(3)
|
| 392 |
+
|
| 393 |
+
with col1:
|
| 394 |
+
st.metric("Total Samples", len(train_df))
|
| 395 |
+
|
| 396 |
+
with col2:
|
| 397 |
+
st.metric("Unique Classes", train_df[target].nunique())
|
| 398 |
+
|
| 399 |
+
with col3:
|
| 400 |
+
st.metric("Avg Text Length", f"{train_df['text_length'].mean():.1f}")
|
| 401 |
+
|
| 402 |
+
if st.button("๐ Start Training", type="primary"):
|
| 403 |
+
with st.spinner("Training model... This may take a few minutes."):
|
| 404 |
+
try:
|
| 405 |
+
# Vectorize text data
|
| 406 |
+
X = vectorizer.fit_transform(train_df['clean_text'])
|
| 407 |
+
y = train_df['target_encoded']
|
| 408 |
+
|
| 409 |
+
# Split data
|
| 410 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 411 |
+
X, y,
|
| 412 |
+
test_size=test_size,
|
| 413 |
+
random_state=random_state,
|
| 414 |
+
stratify=y
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
st.success(f"โ
Data split - Train: {X_train.shape}, Test: {X_test.shape}")
|
| 418 |
+
|
| 419 |
+
# Save vectorizer
|
| 420 |
+
vectorizer_filename = f"{st.session_state.vectorizer_type}_vectorizer.pkl"
|
| 421 |
+
save_artifacts(vectorizer, "artifacts", vectorizer_filename)
|
| 422 |
+
|
| 423 |
+
# Train model
|
| 424 |
+
model, accuracy, y_pred, model_filename = train_model(
|
| 425 |
+
model_name, X_train, X_test, y_train, y_test
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
st.success("๐ Model training completed!")
|
| 429 |
+
|
| 430 |
+
# Display results
|
| 431 |
+
col1, col2 = st.columns(2)
|
| 432 |
+
|
| 433 |
+
with col1:
|
| 434 |
+
st.metric("๐ฏ Test Accuracy", f"{accuracy:.4f}")
|
| 435 |
+
|
| 436 |
+
# Classification report
|
| 437 |
+
st.subheader("๐ Classification Report")
|
| 438 |
+
report = classification_report(
|
| 439 |
+
y_test, y_pred,
|
| 440 |
+
target_names=label_encoder.classes_,
|
| 441 |
+
output_dict=True
|
| 442 |
+
)
|
| 443 |
+
report_df = pd.DataFrame(report).transpose()
|
| 444 |
+
st.dataframe(report_df.round(4))
|
| 445 |
+
|
| 446 |
+
with col2:
|
| 447 |
+
# Confusion matrix
|
| 448 |
+
st.subheader("๐ Confusion Matrix")
|
| 449 |
+
cm = confusion_matrix(y_test, y_pred)
|
| 450 |
+
fig = px.imshow(
|
| 451 |
+
cm,
|
| 452 |
+
text_auto=True,
|
| 453 |
+
aspect="auto",
|
| 454 |
+
title="Confusion Matrix",
|
| 455 |
+
labels=dict(x="Predicted", y="Actual"),
|
| 456 |
+
x=label_encoder.classes_,
|
| 457 |
+
y=label_encoder.classes_
|
| 458 |
+
)
|
| 459 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 460 |
+
|
| 461 |
+
st.info(f"โ
Model saved as: {model_filename}")
|
| 462 |
+
st.info("๐ฎ You can now use the 'Predictions' section to classify new text!")
|
| 463 |
+
|
| 464 |
+
except Exception as e:
|
| 465 |
+
st.error(f"โ Error during training: {str(e)}")
|
| 466 |
+
|
| 467 |
+
else:
|
| 468 |
+
st.warning("๐ Please upload training data to train a model")
|
| 469 |
+
|
| 470 |
+
# Predictions Section
|
| 471 |
+
elif section == "๐ฎ Predictions":
|
| 472 |
+
st.header("๐ฎ Text Classification Predictions")
|
| 473 |
+
|
| 474 |
+
# Check if models exist
|
| 475 |
+
if os.path.exists("models") and os.listdir("models"):
|
| 476 |
+
available_models = [f for f in os.listdir("models") if f.endswith('.pkl')]
|
| 477 |
+
|
| 478 |
+
if available_models:
|
| 479 |
+
# Single prediction
|
| 480 |
+
st.subheader("๐ Single Text Classification")
|
| 481 |
+
|
| 482 |
+
col1, col2 = st.columns([2, 1])
|
| 483 |
+
|
| 484 |
+
with col1:
|
| 485 |
+
text_input = st.text_area("Enter text to classify:", height=150)
|
| 486 |
+
|
| 487 |
+
with col2:
|
| 488 |
+
selected_model = st.selectbox("Choose model:", available_models)
|
| 489 |
+
predict_button = st.button("๐ฎ Predict", type="primary")
|
| 490 |
+
|
| 491 |
+
if predict_button and text_input.strip():
|
| 492 |
+
with st.spinner("Making prediction..."):
|
| 493 |
+
predicted_label, prediction_proba = predict_text(
|
| 494 |
+
selected_model,
|
| 495 |
+
text_input,
|
| 496 |
+
st.session_state.get('vectorizer_type', 'tfidf')
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
+
if predicted_label is not None:
|
| 500 |
+
st.success("โ
Prediction completed!")
|
| 501 |
+
|
| 502 |
+
# Display results
|
| 503 |
+
col1, col2 = st.columns(2)
|
| 504 |
+
|
| 505 |
+
with col1:
|
| 506 |
+
st.markdown("### ๐ฏ Results")
|
| 507 |
+
st.markdown(f"**Input Text:** {text_input[:200]}{'...' if len(text_input) > 200 else ''}")
|
| 508 |
+
st.markdown(f"**Predicted Class:** `{predicted_label}`")
|
| 509 |
+
|
| 510 |
+
with col2:
|
| 511 |
+
# Display probabilities if available
|
| 512 |
+
if prediction_proba is not None:
|
| 513 |
+
st.markdown("### ๐ Class Probabilities")
|
| 514 |
+
|
| 515 |
+
encoder = load_artifacts("artifacts", "encoder.pkl")
|
| 516 |
+
if encoder is not None:
|
| 517 |
+
prob_df = pd.DataFrame({
|
| 518 |
+
'Class': encoder.classes_,
|
| 519 |
+
'Probability': prediction_proba
|
| 520 |
+
}).sort_values('Probability', ascending=False)
|
| 521 |
+
|
| 522 |
+
fig = px.bar(
|
| 523 |
+
prob_df,
|
| 524 |
+
x='Probability',
|
| 525 |
+
y='Class',
|
| 526 |
+
orientation='h',
|
| 527 |
+
title="Prediction Confidence"
|
| 528 |
+
)
|
| 529 |
+
fig.update_layout(yaxis={'categoryorder': 'total ascending'})
|
| 530 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 531 |
+
|
| 532 |
+
elif predict_button:
|
| 533 |
+
st.warning("โ ๏ธ Please enter some text to classify")
|
| 534 |
+
|
| 535 |
+
# Batch predictions
|
| 536 |
+
st.markdown("---")
|
| 537 |
+
st.subheader("๐ Batch Predictions")
|
| 538 |
+
|
| 539 |
+
uploaded_file = st.file_uploader("Upload CSV file with texts to classify", type=['csv'])
|
| 540 |
+
|
| 541 |
+
if uploaded_file is not None:
|
| 542 |
+
try:
|
| 543 |
+
# Try different encodings for batch file
|
| 544 |
+
encodings = ['utf-8', 'latin1', 'cp1252', 'iso-8859-1']
|
| 545 |
+
batch_df = None
|
| 546 |
+
|
| 547 |
+
for encoding in encodings:
|
| 548 |
+
try:
|
| 549 |
+
batch_df = pd.read_csv(uploaded_file, encoding=encoding)
|
| 550 |
+
break
|
| 551 |
+
except UnicodeDecodeError:
|
| 552 |
+
continue
|
| 553 |
+
|
| 554 |
+
if batch_df is not None:
|
| 555 |
+
st.write("๐ Uploaded data preview:")
|
| 556 |
+
st.dataframe(batch_df.head())
|
| 557 |
+
|
| 558 |
+
col1, col2 = st.columns(2)
|
| 559 |
+
|
| 560 |
+
with col1:
|
| 561 |
+
text_column = st.selectbox("Select text column:", batch_df.columns.tolist())
|
| 562 |
+
|
| 563 |
+
with col2:
|
| 564 |
+
batch_model = st.selectbox("Choose model:", available_models, key="batch_model")
|
| 565 |
+
|
| 566 |
+
if st.button("๐ Run Batch Predictions", type="primary"):
|
| 567 |
+
with st.spinner("Processing batch predictions..."):
|
| 568 |
+
predictions = []
|
| 569 |
+
confidences = []
|
| 570 |
+
|
| 571 |
+
progress_bar = st.progress(0)
|
| 572 |
+
total_texts = len(batch_df)
|
| 573 |
+
|
| 574 |
+
for i, text in enumerate(batch_df[text_column]):
|
| 575 |
+
pred, proba = predict_text(
|
| 576 |
+
batch_model,
|
| 577 |
+
str(text),
|
| 578 |
+
st.session_state.get('vectorizer_type', 'tfidf')
|
| 579 |
+
)
|
| 580 |
+
predictions.append(pred if pred is not None else "Error")
|
| 581 |
+
|
| 582 |
+
# Get confidence (max probability)
|
| 583 |
+
if proba is not None:
|
| 584 |
+
confidences.append(max(proba))
|
| 585 |
+
else:
|
| 586 |
+
confidences.append(0.0)
|
| 587 |
+
|
| 588 |
+
progress_bar.progress((i + 1) / total_texts)
|
| 589 |
+
|
| 590 |
+
batch_df['Predicted_Class'] = predictions
|
| 591 |
+
batch_df['Confidence'] = confidences
|
| 592 |
+
|
| 593 |
+
st.success("โ
Batch predictions completed!")
|
| 594 |
+
|
| 595 |
+
# Show results
|
| 596 |
+
st.subheader("๐ Results")
|
| 597 |
+
result_df = batch_df[[text_column, 'Predicted_Class', 'Confidence']]
|
| 598 |
+
st.dataframe(result_df)
|
| 599 |
+
|
| 600 |
+
# Summary statistics
|
| 601 |
+
st.subheader("๐ Summary")
|
| 602 |
+
col1, col2, col3 = st.columns(3)
|
| 603 |
+
|
| 604 |
+
with col1:
|
| 605 |
+
st.metric("Total Predictions", len(predictions))
|
| 606 |
+
|
| 607 |
+
with col2:
|
| 608 |
+
successful_preds = sum(1 for p in predictions if p != "Error")
|
| 609 |
+
st.metric("Successful", successful_preds)
|
| 610 |
+
|
| 611 |
+
with col3:
|
| 612 |
+
avg_confidence = sum(confidences) / len(confidences) if confidences else 0
|
| 613 |
+
st.metric("Avg Confidence", f"{avg_confidence:.3f}")
|
| 614 |
+
|
| 615 |
+
# Class distribution of predictions
|
| 616 |
+
pred_counts = pd.Series(predictions).value_counts()
|
| 617 |
+
if len(pred_counts) > 0:
|
| 618 |
+
fig = px.pie(
|
| 619 |
+
values=pred_counts.values,
|
| 620 |
+
names=pred_counts.index,
|
| 621 |
+
title="Distribution of Predictions"
|
| 622 |
+
)
|
| 623 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 624 |
+
|
| 625 |
+
# Download results
|
| 626 |
+
csv = batch_df.to_csv(index=False)
|
| 627 |
+
st.download_button(
|
| 628 |
+
label="๐ฅ Download Results as CSV",
|
| 629 |
+
data=csv,
|
| 630 |
+
file_name="batch_predictions.csv",
|
| 631 |
+
mime="text/csv"
|
| 632 |
+
)
|
| 633 |
+
else:
|
| 634 |
+
st.error("โ Unable to read the CSV file. Please check the file encoding.")
|
| 635 |
+
|
| 636 |
+
except Exception as e:
|
| 637 |
+
st.error(f"โ Error in batch prediction: {str(e)}")
|
| 638 |
+
else:
|
| 639 |
+
st.warning("โ ๏ธ No trained models found. Please train a model first.")
|
| 640 |
+
else:
|
| 641 |
+
st.warning("โ ๏ธ No models directory found. Please go to 'Train Model' section to train a model first.")
|
| 642 |
+
|
| 643 |
+
# Footer
|
| 644 |
+
st.markdown("---")
|
| 645 |
+
st.markdown("๐ Built with Streamlit | ๐ No-Code Text Classification")
|