Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,441 +2,384 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
| 5 |
-
from NoCodeTextClassifier.EDA import Informations, Visualizations
|
| 6 |
-
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
| 7 |
-
from NoCodeTextClassifier.preprocessing import process, TextCleaner, Vectorization
|
| 8 |
-
from NoCodeTextClassifier.models import Models
|
| 9 |
import os
|
| 10 |
import pickle
|
| 11 |
-
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 12 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Set page config
|
| 15 |
-
st.set_page_config(page_title="Text Classification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Utility functions
|
| 18 |
def save_artifacts(obj, folder_name, file_name):
|
| 19 |
-
"""Save artifacts
|
|
|
|
| 20 |
try:
|
| 21 |
os.makedirs(folder_name, exist_ok=True)
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
pickle.dump(obj, f)
|
|
|
|
|
|
|
| 24 |
return True
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
return False
|
| 28 |
|
| 29 |
def load_artifacts(folder_name, file_name):
|
| 30 |
-
"""Load
|
|
|
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
return None
|
| 40 |
|
| 41 |
def load_model(model_name):
|
| 42 |
-
"""Load
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
return pickle.load(f)
|
| 46 |
-
except FileNotFoundError:
|
| 47 |
-
st.error(f"Model {model_name} not found. Please train a model first.")
|
| 48 |
-
return None
|
| 49 |
-
except Exception as e:
|
| 50 |
-
st.error(f"Error loading model {model_name}: {str(e)}")
|
| 51 |
-
return None
|
| 52 |
-
|
| 53 |
-
def safe_read_csv(uploaded_file, encoding_options=['utf-8', 'latin1', 'iso-8859-1', 'cp1252']):
|
| 54 |
-
"""Safely read CSV with multiple encoding options"""
|
| 55 |
-
for encoding in encoding_options:
|
| 56 |
-
try:
|
| 57 |
-
# Reset file pointer
|
| 58 |
-
uploaded_file.seek(0)
|
| 59 |
-
# Read as bytes first, then decode
|
| 60 |
-
content = uploaded_file.read()
|
| 61 |
-
if isinstance(content, bytes):
|
| 62 |
-
content = content.decode(encoding)
|
| 63 |
-
|
| 64 |
-
# Use StringIO to create a file-like object
|
| 65 |
-
df = pd.read_csv(io.StringIO(content))
|
| 66 |
-
st.success(f"File loaded successfully with {encoding} encoding")
|
| 67 |
-
return df
|
| 68 |
-
|
| 69 |
-
except UnicodeDecodeError:
|
| 70 |
-
continue
|
| 71 |
-
except Exception as e:
|
| 72 |
-
st.warning(f"Failed to read with {encoding} encoding: {str(e)}")
|
| 73 |
-
continue
|
| 74 |
-
|
| 75 |
-
# If all encodings fail, try pandas default
|
| 76 |
-
try:
|
| 77 |
-
uploaded_file.seek(0)
|
| 78 |
-
df = pd.read_csv(uploaded_file)
|
| 79 |
-
st.success("File loaded with default encoding")
|
| 80 |
-
return df
|
| 81 |
-
except Exception as e:
|
| 82 |
-
st.error(f"All encoding attempts failed. Error: {str(e)}")
|
| 83 |
-
return None
|
| 84 |
|
| 85 |
def predict_text(model_name, text, vectorizer_type="tfidf"):
|
| 86 |
-
"""Make prediction
|
|
|
|
|
|
|
| 87 |
try:
|
| 88 |
-
# Load
|
| 89 |
model = load_model(model_name)
|
| 90 |
if model is None:
|
| 91 |
return None, None
|
| 92 |
|
| 93 |
-
# Load vectorizer
|
| 94 |
vectorizer_file = f"{vectorizer_type}_vectorizer.pkl"
|
| 95 |
vectorizer = load_artifacts("artifacts", vectorizer_file)
|
| 96 |
if vectorizer is None:
|
| 97 |
return None, None
|
| 98 |
|
| 99 |
-
# Load label encoder
|
| 100 |
encoder = load_artifacts("artifacts", "encoder.pkl")
|
| 101 |
if encoder is None:
|
| 102 |
return None, None
|
| 103 |
|
| 104 |
-
|
| 105 |
text_cleaner = TextCleaner()
|
| 106 |
clean_text = text_cleaner.clean_text(text)
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
text_vector = vectorizer.transform([clean_text])
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
prediction = model.predict(text_vector)
|
| 113 |
prediction_proba = None
|
| 114 |
|
| 115 |
-
# Get prediction probabilities if available
|
| 116 |
if hasattr(model, 'predict_proba'):
|
| 117 |
try:
|
| 118 |
prediction_proba = model.predict_proba(text_vector)[0]
|
|
|
|
| 119 |
except:
|
| 120 |
-
|
| 121 |
|
| 122 |
-
# Decode prediction
|
| 123 |
predicted_label = encoder.inverse_transform(prediction)[0]
|
|
|
|
| 124 |
|
| 125 |
return predicted_label, prediction_proba
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return None, None
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
st.title('
|
| 133 |
-
st.write('
|
| 134 |
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
st.sidebar.
|
| 141 |
-
train_data = st.sidebar.file_uploader("Upload training data", type=["csv"], key="train_upload")
|
| 142 |
-
test_data = st.sidebar.file_uploader("Upload test data (optional)", type=["csv"], key="test_upload")
|
| 143 |
|
| 144 |
-
#
|
| 145 |
if 'vectorizer_type' not in st.session_state:
|
| 146 |
st.session_state.vectorizer_type = "tfidf"
|
| 147 |
if 'train_df' not in st.session_state:
|
| 148 |
st.session_state.train_df = None
|
| 149 |
-
if 'info' not in st.session_state:
|
| 150 |
-
st.session_state.info = None
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
if
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
st.session_state.test_df = None
|
| 166 |
-
|
| 167 |
-
st.sidebar.success("โ
Data loaded successfully!")
|
| 168 |
-
st.write("Training Data Preview:")
|
| 169 |
-
st.write(train_df.head(3))
|
| 170 |
-
|
| 171 |
-
columns = train_df.columns.tolist()
|
| 172 |
-
text_data = st.sidebar.selectbox("Choose the text column:", columns, key="text_col")
|
| 173 |
-
target = st.sidebar.selectbox("Choose the target column:", columns, key="target_col")
|
| 174 |
-
|
| 175 |
-
if text_data and target:
|
| 176 |
-
try:
|
| 177 |
-
# Process data
|
| 178 |
-
info = Informations(train_df, text_data, target)
|
| 179 |
-
train_df['clean_text'] = info.clean_text()
|
| 180 |
-
train_df['text_length'] = info.text_length()
|
| 181 |
-
|
| 182 |
-
# Handle label encoding manually
|
| 183 |
-
from sklearn.preprocessing import LabelEncoder
|
| 184 |
-
label_encoder = LabelEncoder()
|
| 185 |
-
train_df['target'] = label_encoder.fit_transform(train_df[target])
|
| 186 |
-
|
| 187 |
-
# Save label encoder for later use
|
| 188 |
-
if save_artifacts(label_encoder, "artifacts", "encoder.pkl"):
|
| 189 |
-
st.sidebar.success("โ
Data processed successfully!")
|
| 190 |
-
|
| 191 |
-
st.session_state.train_df = train_df
|
| 192 |
-
st.session_state.info = info
|
| 193 |
-
|
| 194 |
-
except Exception as e:
|
| 195 |
-
st.error(f"Error processing data: {str(e)}")
|
| 196 |
-
st.session_state.train_df = None
|
| 197 |
-
st.session_state.info = None
|
| 198 |
|
| 199 |
-
|
| 200 |
-
st.
|
| 201 |
-
|
| 202 |
-
st.
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
st.
|
| 213 |
-
|
| 214 |
-
col1, col2, col3 = st.columns(3)
|
| 215 |
-
with col1:
|
| 216 |
-
st.metric("Data Shape", f"{info.shape()[0]} rows ร {info.shape()[1]} cols")
|
| 217 |
-
with col2:
|
| 218 |
-
st.metric("Classes", len(train_df['target'].unique()))
|
| 219 |
-
with col3:
|
| 220 |
-
st.metric("Missing Values", info.missing_values())
|
| 221 |
-
|
| 222 |
-
st.write("**Class Distribution:**", info.class_imbalanced())
|
| 223 |
-
|
| 224 |
-
st.write("**Processed Data Preview:**")
|
| 225 |
-
st.write(train_df[['clean_text', 'text_length', 'target']].head(3))
|
| 226 |
|
| 227 |
-
|
| 228 |
-
st.
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
st.write(f"**Correlation between Text Length and Target:** {correlation:.4f}")
|
| 233 |
-
|
| 234 |
-
st.subheader("๐ Visualizations")
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
vis.text_length_distribution()
|
| 244 |
-
except Exception as e:
|
| 245 |
-
st.error(f"Error generating visualizations: {str(e)}")
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
else:
|
| 250 |
-
st.warning("
|
| 251 |
|
| 252 |
-
# Train Model Section
|
| 253 |
elif section == "Train Model":
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
with col1:
|
| 262 |
-
st.markdown("**Select Model:**")
|
| 263 |
-
model = st.radio("Choose the Model", [
|
| 264 |
-
"Logistic Regression", "Decision Tree",
|
| 265 |
-
"Random Forest", "Linear SVC", "SVC",
|
| 266 |
-
"Multinomial Naive Bayes", "Gaussian Naive Bayes"
|
| 267 |
-
])
|
| 268 |
-
|
| 269 |
-
with col2:
|
| 270 |
-
st.markdown("**Select Vectorizer:**")
|
| 271 |
-
vectorizer_choice = st.radio("Choose Vectorizer", ["Tfidf Vectorizer", "Count Vectorizer"])
|
| 272 |
-
|
| 273 |
-
# Initialize vectorizer
|
| 274 |
-
if vectorizer_choice == "Tfidf Vectorizer":
|
| 275 |
-
vectorizer = TfidfVectorizer(max_features=10000, stop_words='english')
|
| 276 |
-
st.session_state.vectorizer_type = "tfidf"
|
| 277 |
-
else:
|
| 278 |
-
vectorizer = CountVectorizer(max_features=10000, stop_words='english')
|
| 279 |
-
st.session_state.vectorizer_type = "count"
|
| 280 |
-
|
| 281 |
-
st.write("**Training Data Preview:**")
|
| 282 |
-
st.write(train_df[['clean_text', 'target']].head(3))
|
| 283 |
-
|
| 284 |
-
# Vectorize text data
|
| 285 |
-
with st.spinner("Vectorizing text data..."):
|
| 286 |
-
X = vectorizer.fit_transform(train_df['clean_text'])
|
| 287 |
-
y = train_df['target']
|
| 288 |
-
|
| 289 |
-
# Split data
|
| 290 |
-
X_train, X_test, y_train, y_test = process.split_data(X, y)
|
| 291 |
-
st.write(f"**Data split** - Train: {X_train.shape}, Test: {X_test.shape}")
|
| 292 |
-
|
| 293 |
-
# Save vectorizer for later use
|
| 294 |
-
vectorizer_filename = f"{st.session_state.vectorizer_type}_vectorizer.pkl"
|
| 295 |
-
save_artifacts(vectorizer, "artifacts", vectorizer_filename)
|
| 296 |
-
|
| 297 |
-
if st.button("๐ Start Training", type="primary"):
|
| 298 |
-
with st.spinner("Training model..."):
|
| 299 |
-
try:
|
| 300 |
-
models = Models(X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test)
|
| 301 |
-
|
| 302 |
-
# Train selected model
|
| 303 |
-
if model == "Logistic Regression":
|
| 304 |
-
models.LogisticRegression()
|
| 305 |
-
elif model == "Decision Tree":
|
| 306 |
-
models.DecisionTree()
|
| 307 |
-
elif model == "Linear SVC":
|
| 308 |
-
models.LinearSVC()
|
| 309 |
-
elif model == "SVC":
|
| 310 |
-
models.SVC()
|
| 311 |
-
elif model == "Multinomial Naive Bayes":
|
| 312 |
-
models.MultinomialNB()
|
| 313 |
-
elif model == "Random Forest":
|
| 314 |
-
models.RandomForestClassifier()
|
| 315 |
-
elif model == "Gaussian Naive Bayes":
|
| 316 |
-
models.GaussianNB()
|
| 317 |
-
|
| 318 |
-
st.success("๐ Model training completed!")
|
| 319 |
-
st.info("You can now use the 'Predictions' section to classify new text.")
|
| 320 |
-
|
| 321 |
-
except Exception as e:
|
| 322 |
-
st.error(f"Error during model training: {str(e)}")
|
| 323 |
-
|
| 324 |
-
except Exception as e:
|
| 325 |
-
st.error(f"Error in model training: {str(e)}")
|
| 326 |
else:
|
| 327 |
-
st.warning("
|
| 328 |
|
| 329 |
-
# Predictions Section
|
| 330 |
elif section == "Predictions":
|
| 331 |
-
st.subheader("๐ฎ
|
|
|
|
| 332 |
|
| 333 |
-
# Check
|
| 334 |
-
if os.path.exists("models")
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
# Model selection
|
| 339 |
-
available_models = [f for f in os.listdir("models") if f.endswith('.pkl')]
|
| 340 |
-
|
| 341 |
-
if available_models:
|
| 342 |
-
selected_model = st.selectbox("Choose the trained model:", available_models)
|
| 343 |
-
|
| 344 |
-
# Prediction button
|
| 345 |
-
if st.button("๐ฏ Predict", key="single_predict", type="primary"):
|
| 346 |
-
if text_input.strip():
|
| 347 |
-
with st.spinner("Making prediction..."):
|
| 348 |
-
predicted_label, prediction_proba = predict_text(
|
| 349 |
-
selected_model,
|
| 350 |
-
text_input,
|
| 351 |
-
st.session_state.get('vectorizer_type', 'tfidf')
|
| 352 |
-
)
|
| 353 |
-
|
| 354 |
-
if predicted_label is not None:
|
| 355 |
-
st.success("โ
Prediction completed!")
|
| 356 |
-
|
| 357 |
-
# Display results
|
| 358 |
-
st.markdown("### ๐ Prediction Results")
|
| 359 |
-
|
| 360 |
-
col1, col2 = st.columns([2, 1])
|
| 361 |
-
with col1:
|
| 362 |
-
st.markdown(f"**Input Text:** {text_input}")
|
| 363 |
-
with col2:
|
| 364 |
-
st.markdown(f"**Predicted Class:** `{predicted_label}`")
|
| 365 |
-
|
| 366 |
-
# Display probabilities if available
|
| 367 |
-
if prediction_proba is not None:
|
| 368 |
-
st.markdown("**Class Probabilities:**")
|
| 369 |
-
|
| 370 |
-
# Load encoder to get class names
|
| 371 |
-
encoder = load_artifacts("artifacts", "encoder.pkl")
|
| 372 |
-
if encoder is not None:
|
| 373 |
-
classes = encoder.classes_
|
| 374 |
-
prob_df = pd.DataFrame({
|
| 375 |
-
'Class': classes,
|
| 376 |
-
'Probability': prediction_proba
|
| 377 |
-
}).sort_values('Probability', ascending=False)
|
| 378 |
-
|
| 379 |
-
col1, col2 = st.columns(2)
|
| 380 |
-
with col1:
|
| 381 |
-
st.bar_chart(prob_df.set_index('Class'))
|
| 382 |
-
with col2:
|
| 383 |
-
st.dataframe(prob_df, use_container_width=True)
|
| 384 |
-
else:
|
| 385 |
-
st.warning("โ ๏ธ Please enter some text to classify")
|
| 386 |
else:
|
| 387 |
-
st.
|
| 388 |
else:
|
| 389 |
-
st.
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
st.
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
batch_df = safe_read_csv(uploaded_file)
|
| 400 |
-
|
| 401 |
-
if batch_df is not None:
|
| 402 |
-
st.write("**Uploaded data preview:**")
|
| 403 |
-
st.write(batch_df.head())
|
| 404 |
-
|
| 405 |
-
# Select text column
|
| 406 |
-
text_column = st.selectbox("Select the text column:", batch_df.columns.tolist())
|
| 407 |
-
|
| 408 |
-
if os.path.exists("models") and os.listdir("models"):
|
| 409 |
-
available_models = [f for f in os.listdir("models") if f.endswith('.pkl')]
|
| 410 |
-
batch_model = st.selectbox("Choose model for batch prediction:", available_models, key="batch_model")
|
| 411 |
-
|
| 412 |
-
if st.button("๐ Run Batch Predictions", key="batch_predict", type="primary"):
|
| 413 |
-
with st.spinner("Processing batch predictions..."):
|
| 414 |
-
predictions = []
|
| 415 |
-
progress_bar = st.progress(0)
|
| 416 |
-
|
| 417 |
-
for idx, text in enumerate(batch_df[text_column]):
|
| 418 |
-
pred, _ = predict_text(
|
| 419 |
-
batch_model,
|
| 420 |
-
str(text),
|
| 421 |
-
st.session_state.get('vectorizer_type', 'tfidf')
|
| 422 |
-
)
|
| 423 |
-
predictions.append(pred if pred is not None else "Error")
|
| 424 |
-
progress_bar.progress((idx + 1) / len(batch_df))
|
| 425 |
-
|
| 426 |
-
batch_df['Predicted_Class'] = predictions
|
| 427 |
-
|
| 428 |
-
st.success("โ
Batch predictions completed!")
|
| 429 |
-
st.write("**Results:**")
|
| 430 |
-
st.write(batch_df[[text_column, 'Predicted_Class']])
|
| 431 |
-
|
| 432 |
-
# Download results
|
| 433 |
-
csv = batch_df.to_csv(index=False)
|
| 434 |
-
st.download_button(
|
| 435 |
-
label="๐ฅ Download predictions as CSV",
|
| 436 |
-
data=csv,
|
| 437 |
-
file_name="batch_predictions.csv",
|
| 438 |
-
mime="text/csv"
|
| 439 |
-
)
|
| 440 |
-
|
| 441 |
-
except Exception as e:
|
| 442 |
-
st.error(f"Error in batch prediction: {str(e)}")
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import pickle
|
|
|
|
| 7 |
import io
|
| 8 |
+
import traceback
|
| 9 |
+
import sys
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# Import ML libraries with error handling
|
| 13 |
+
try:
|
| 14 |
+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
| 15 |
+
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 16 |
+
from sklearn.preprocessing import LabelEncoder
|
| 17 |
+
st.success("โ
Sklearn imported successfully")
|
| 18 |
+
except ImportError as e:
|
| 19 |
+
st.error(f"โ Sklearn import error: {e}")
|
| 20 |
+
|
| 21 |
+
# Import custom modules with error handling
|
| 22 |
+
try:
|
| 23 |
+
from NoCodeTextClassifier.EDA import Informations, Visualizations
|
| 24 |
+
from NoCodeTextClassifier.preprocessing import process, TextCleaner, Vectorization
|
| 25 |
+
from NoCodeTextClassifier.models import Models
|
| 26 |
+
st.success("โ
NoCodeTextClassifier imported successfully")
|
| 27 |
+
except ImportError as e:
|
| 28 |
+
st.error(f"โ NoCodeTextClassifier import error: {e}")
|
| 29 |
+
st.info("Please ensure NoCodeTextClassifier package is installed")
|
| 30 |
|
| 31 |
# Set page config
|
| 32 |
+
st.set_page_config(page_title="Debug Text Classification", page_icon="๐", layout="wide")
|
| 33 |
+
|
| 34 |
+
# Debug section
|
| 35 |
+
st.sidebar.header("๐ Debug Information")
|
| 36 |
+
debug_mode = st.sidebar.checkbox("Enable Debug Mode", value=True)
|
| 37 |
+
|
| 38 |
+
def debug_log(message, level="INFO"):
|
| 39 |
+
"""Debug logging function"""
|
| 40 |
+
if debug_mode:
|
| 41 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 42 |
+
st.sidebar.write(f"**{timestamp} [{level}]:** {message}")
|
| 43 |
+
|
| 44 |
+
def detailed_error_info(e):
|
| 45 |
+
"""Get detailed error information"""
|
| 46 |
+
error_type = type(e).__name__
|
| 47 |
+
error_message = str(e)
|
| 48 |
+
error_traceback = traceback.format_exc()
|
| 49 |
+
|
| 50 |
+
return {
|
| 51 |
+
'type': error_type,
|
| 52 |
+
'message': error_message,
|
| 53 |
+
'traceback': error_traceback
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
def inspect_uploaded_file(uploaded_file):
|
| 57 |
+
"""Inspect uploaded file properties"""
|
| 58 |
+
debug_log("๐ Inspecting uploaded file...")
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
file_info = {
|
| 62 |
+
'name': uploaded_file.name,
|
| 63 |
+
'type': uploaded_file.type,
|
| 64 |
+
'size': uploaded_file.size,
|
| 65 |
+
'file_id': getattr(uploaded_file, 'file_id', 'Not available')
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
debug_log(f"File name: {file_info['name']}")
|
| 69 |
+
debug_log(f"File type: {file_info['type']}")
|
| 70 |
+
debug_log(f"File size: {file_info['size']} bytes")
|
| 71 |
+
debug_log(f"File ID: {file_info['file_id']}")
|
| 72 |
+
|
| 73 |
+
# Try to read first few bytes
|
| 74 |
+
uploaded_file.seek(0)
|
| 75 |
+
first_bytes = uploaded_file.read(100)
|
| 76 |
+
debug_log(f"First 100 bytes type: {type(first_bytes)}")
|
| 77 |
+
debug_log(f"First 100 bytes preview: {first_bytes[:50]}...")
|
| 78 |
+
|
| 79 |
+
# Reset file pointer
|
| 80 |
+
uploaded_file.seek(0)
|
| 81 |
+
|
| 82 |
+
return file_info
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
error_info = detailed_error_info(e)
|
| 86 |
+
debug_log(f"โ Error inspecting file: {error_info['type']}: {error_info['message']}", "ERROR")
|
| 87 |
+
st.sidebar.error(f"File inspection error: {error_info['message']}")
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
def safe_read_csv_debug(uploaded_file, encoding_options=['utf-8', 'latin1', 'iso-8859-1', 'cp1252']):
|
| 91 |
+
"""Safely read CSV with extensive debugging"""
|
| 92 |
+
debug_log("๐ Starting CSV read process...")
|
| 93 |
+
|
| 94 |
+
# Inspect file first
|
| 95 |
+
file_info = inspect_uploaded_file(uploaded_file)
|
| 96 |
+
if file_info is None:
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
# Try different reading methods
|
| 100 |
+
methods = [
|
| 101 |
+
("Direct pandas read", lambda f: pd.read_csv(f)),
|
| 102 |
+
("BytesIO method", lambda f: pd.read_csv(io.BytesIO(f.read()))),
|
| 103 |
+
("StringIO method", lambda f: pd.read_csv(io.StringIO(f.read().decode('utf-8')))),
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
for method_name, method_func in methods:
|
| 107 |
+
debug_log(f"๐ Trying method: {method_name}")
|
| 108 |
+
|
| 109 |
+
for encoding in encoding_options:
|
| 110 |
+
try:
|
| 111 |
+
debug_log(f" - Attempting encoding: {encoding}")
|
| 112 |
+
uploaded_file.seek(0)
|
| 113 |
+
|
| 114 |
+
if method_name == "Direct pandas read":
|
| 115 |
+
df = pd.read_csv(uploaded_file, encoding=encoding)
|
| 116 |
+
elif method_name == "BytesIO method":
|
| 117 |
+
uploaded_file.seek(0)
|
| 118 |
+
content = uploaded_file.read()
|
| 119 |
+
df = pd.read_csv(io.BytesIO(content), encoding=encoding)
|
| 120 |
+
elif method_name == "StringIO method":
|
| 121 |
+
uploaded_file.seek(0)
|
| 122 |
+
content = uploaded_file.read()
|
| 123 |
+
if isinstance(content, bytes):
|
| 124 |
+
content = content.decode(encoding)
|
| 125 |
+
df = pd.read_csv(io.StringIO(content))
|
| 126 |
+
|
| 127 |
+
debug_log(f"โ
Success with {method_name} + {encoding}")
|
| 128 |
+
debug_log(f"DataFrame shape: {df.shape}")
|
| 129 |
+
debug_log(f"Columns: {list(df.columns)}")
|
| 130 |
+
|
| 131 |
+
st.success(f"File loaded successfully using {method_name} with {encoding} encoding")
|
| 132 |
+
return df
|
| 133 |
+
|
| 134 |
+
except UnicodeDecodeError as e:
|
| 135 |
+
debug_log(f" - Unicode error with {encoding}: {str(e)}", "WARNING")
|
| 136 |
+
continue
|
| 137 |
+
except Exception as e:
|
| 138 |
+
error_info = detailed_error_info(e)
|
| 139 |
+
debug_log(f" - Error with {method_name} + {encoding}: {error_info['type']}: {error_info['message']}", "ERROR")
|
| 140 |
+
|
| 141 |
+
# Show detailed error for 403 or permission errors
|
| 142 |
+
if "403" in str(e) or "permission" in str(e).lower():
|
| 143 |
+
st.error("๐จ PERMISSION ERROR DETECTED!")
|
| 144 |
+
st.error(f"Method: {method_name}, Encoding: {encoding}")
|
| 145 |
+
st.error(f"Error type: {error_info['type']}")
|
| 146 |
+
st.error(f"Error message: {error_info['message']}")
|
| 147 |
+
st.code(error_info['traceback'])
|
| 148 |
+
|
| 149 |
+
continue
|
| 150 |
+
|
| 151 |
+
debug_log("โ All reading methods failed", "ERROR")
|
| 152 |
+
st.error("All CSV reading methods failed. Check debug log for details.")
|
| 153 |
+
return None
|
| 154 |
|
| 155 |
+
# Utility functions with debugging
|
| 156 |
def save_artifacts(obj, folder_name, file_name):
|
| 157 |
+
"""Save artifacts with debugging"""
|
| 158 |
+
debug_log(f"๐พ Saving {file_name} to {folder_name}")
|
| 159 |
try:
|
| 160 |
os.makedirs(folder_name, exist_ok=True)
|
| 161 |
+
full_path = os.path.join(folder_name, file_name)
|
| 162 |
+
|
| 163 |
+
with open(full_path, 'wb') as f:
|
| 164 |
pickle.dump(obj, f)
|
| 165 |
+
|
| 166 |
+
debug_log(f"โ
Successfully saved {file_name}")
|
| 167 |
return True
|
| 168 |
+
|
| 169 |
except Exception as e:
|
| 170 |
+
error_info = detailed_error_info(e)
|
| 171 |
+
debug_log(f"โ Error saving {file_name}: {error_info['message']}", "ERROR")
|
| 172 |
+
st.error(f"Save error: {error_info['message']}")
|
| 173 |
return False
|
| 174 |
|
| 175 |
def load_artifacts(folder_name, file_name):
|
| 176 |
+
"""Load artifacts with debugging"""
|
| 177 |
+
debug_log(f"๐ Loading {file_name} from {folder_name}")
|
| 178 |
try:
|
| 179 |
+
full_path = os.path.join(folder_name, file_name)
|
| 180 |
+
|
| 181 |
+
if not os.path.exists(full_path):
|
| 182 |
+
debug_log(f"โ File not found: {full_path}", "ERROR")
|
| 183 |
+
return None
|
| 184 |
+
|
| 185 |
+
with open(full_path, 'rb') as f:
|
| 186 |
+
obj = pickle.load(f)
|
| 187 |
+
|
| 188 |
+
debug_log(f"โ
Successfully loaded {file_name}")
|
| 189 |
+
return obj
|
| 190 |
+
|
| 191 |
except Exception as e:
|
| 192 |
+
error_info = detailed_error_info(e)
|
| 193 |
+
debug_log(f"โ Error loading {file_name}: {error_info['message']}", "ERROR")
|
| 194 |
+
st.error(f"Load error: {error_info['message']}")
|
| 195 |
return None
|
| 196 |
|
| 197 |
def load_model(model_name):
|
| 198 |
+
"""Load model with debugging"""
|
| 199 |
+
debug_log(f"๐ค Loading model: {model_name}")
|
| 200 |
+
return load_artifacts("models", model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
def predict_text(model_name, text, vectorizer_type="tfidf"):
|
| 203 |
+
"""Make prediction with debugging"""
|
| 204 |
+
debug_log(f"๐ฎ Starting prediction with {model_name}")
|
| 205 |
+
|
| 206 |
try:
|
| 207 |
+
# Load components
|
| 208 |
model = load_model(model_name)
|
| 209 |
if model is None:
|
| 210 |
return None, None
|
| 211 |
|
|
|
|
| 212 |
vectorizer_file = f"{vectorizer_type}_vectorizer.pkl"
|
| 213 |
vectorizer = load_artifacts("artifacts", vectorizer_file)
|
| 214 |
if vectorizer is None:
|
| 215 |
return None, None
|
| 216 |
|
|
|
|
| 217 |
encoder = load_artifacts("artifacts", "encoder.pkl")
|
| 218 |
if encoder is None:
|
| 219 |
return None, None
|
| 220 |
|
| 221 |
+
debug_log("๐งน Cleaning text...")
|
| 222 |
text_cleaner = TextCleaner()
|
| 223 |
clean_text = text_cleaner.clean_text(text)
|
| 224 |
+
debug_log(f"Cleaned text preview: {clean_text[:50]}...")
|
| 225 |
|
| 226 |
+
debug_log("๐ข Vectorizing text...")
|
| 227 |
text_vector = vectorizer.transform([clean_text])
|
| 228 |
+
debug_log(f"Vector shape: {text_vector.shape}")
|
| 229 |
|
| 230 |
+
debug_log("๐ฏ Making prediction...")
|
| 231 |
prediction = model.predict(text_vector)
|
| 232 |
prediction_proba = None
|
| 233 |
|
|
|
|
| 234 |
if hasattr(model, 'predict_proba'):
|
| 235 |
try:
|
| 236 |
prediction_proba = model.predict_proba(text_vector)[0]
|
| 237 |
+
debug_log(f"Prediction probabilities: {prediction_proba}")
|
| 238 |
except:
|
| 239 |
+
debug_log("No prediction probabilities available", "WARNING")
|
| 240 |
|
|
|
|
| 241 |
predicted_label = encoder.inverse_transform(prediction)[0]
|
| 242 |
+
debug_log(f"โ
Prediction complete: {predicted_label}")
|
| 243 |
|
| 244 |
return predicted_label, prediction_proba
|
| 245 |
|
| 246 |
except Exception as e:
|
| 247 |
+
error_info = detailed_error_info(e)
|
| 248 |
+
debug_log(f"โ Prediction error: {error_info['message']}", "ERROR")
|
| 249 |
+
st.error(f"Prediction error: {error_info['message']}")
|
| 250 |
+
if debug_mode:
|
| 251 |
+
st.code(error_info['traceback'])
|
| 252 |
return None, None
|
| 253 |
|
| 254 |
+
# Main App
|
| 255 |
+
st.title('๐ Debug Text Classification App')
|
| 256 |
+
st.write('Debug version to identify and fix issues')
|
| 257 |
|
| 258 |
+
# Environment info
|
| 259 |
+
if debug_mode:
|
| 260 |
+
st.sidebar.subheader("๐ฅ๏ธ Environment Info")
|
| 261 |
+
st.sidebar.write(f"Python version: {sys.version}")
|
| 262 |
+
st.sidebar.write(f"Streamlit version: {st.__version__}")
|
| 263 |
+
st.sidebar.write(f"Pandas version: {pd.__version__}")
|
| 264 |
+
st.sidebar.write(f"Current working directory: {os.getcwd()}")
|
| 265 |
+
|
| 266 |
+
# Check directory permissions
|
| 267 |
+
try:
|
| 268 |
+
test_dir = "test_permissions"
|
| 269 |
+
os.makedirs(test_dir, exist_ok=True)
|
| 270 |
+
test_file = os.path.join(test_dir, "test.txt")
|
| 271 |
+
with open(test_file, 'w') as f:
|
| 272 |
+
f.write("test")
|
| 273 |
+
os.remove(test_file)
|
| 274 |
+
os.rmdir(test_dir)
|
| 275 |
+
st.sidebar.success("โ
File system permissions OK")
|
| 276 |
+
except Exception as e:
|
| 277 |
+
st.sidebar.error(f"โ File system permission issue: {e}")
|
| 278 |
|
| 279 |
+
# Sidebar navigation
|
| 280 |
+
section = st.sidebar.radio("Choose Section", ["File Upload Debug", "Data Analysis", "Train Model", "Predictions"])
|
|
|
|
|
|
|
| 281 |
|
| 282 |
+
# Session state initialization
|
| 283 |
if 'vectorizer_type' not in st.session_state:
|
| 284 |
st.session_state.vectorizer_type = "tfidf"
|
| 285 |
if 'train_df' not in st.session_state:
|
| 286 |
st.session_state.train_df = None
|
|
|
|
|
|
|
| 287 |
|
| 288 |
+
# File Upload Debug Section
|
| 289 |
+
if section == "File Upload Debug":
|
| 290 |
+
st.subheader("๐ File Upload Debugging")
|
| 291 |
+
|
| 292 |
+
st.info("This section helps debug file upload issues. Upload your file and see detailed error information.")
|
| 293 |
+
|
| 294 |
+
train_data = st.file_uploader("Upload training data (DEBUG MODE)", type=["csv"], key="debug_upload")
|
| 295 |
+
|
| 296 |
+
if train_data is not None:
|
| 297 |
+
st.write("### File Upload Detected!")
|
| 298 |
|
| 299 |
+
# Show raw file info
|
| 300 |
+
st.write("**Raw File Information:**")
|
| 301 |
+
st.json({
|
| 302 |
+
"name": train_data.name,
|
| 303 |
+
"type": train_data.type if hasattr(train_data, 'type') else "Unknown",
|
| 304 |
+
"size": train_data.size if hasattr(train_data, 'size') else "Unknown"
|
| 305 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
+
# Try to read the file
|
| 308 |
+
st.write("### Attempting to Read File...")
|
| 309 |
+
|
| 310 |
+
with st.spinner("Reading file with debug mode..."):
|
| 311 |
+
df = safe_read_csv_debug(train_data)
|
| 312 |
+
|
| 313 |
+
if df is not None:
|
| 314 |
+
st.success("๐ File successfully loaded!")
|
| 315 |
+
st.write("**Data Preview:**")
|
| 316 |
+
st.dataframe(df.head())
|
| 317 |
+
st.write(f"**Shape:** {df.shape}")
|
| 318 |
+
st.write(f"**Columns:** {list(df.columns)}")
|
| 319 |
+
st.write(f"**Data Types:**")
|
| 320 |
+
st.write(df.dtypes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
+
# Store in session state
|
| 323 |
+
st.session_state.train_df = df
|
| 324 |
|
| 325 |
+
else:
|
| 326 |
+
st.error("โ Failed to load file. Check the debug log for details.")
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
+
# Additional troubleshooting
|
| 329 |
+
st.write("### ๐ง Troubleshooting Steps:")
|
| 330 |
+
st.write("1. Check if your file is a valid CSV")
|
| 331 |
+
st.write("2. Try saving your CSV with different encoding (UTF-8 recommended)")
|
| 332 |
+
st.write("3. Check if file size is within limits")
|
| 333 |
+
st.write("4. Ensure no special characters in filename")
|
| 334 |
+
st.write("5. Try uploading from a different location")
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
+
# Other sections (simplified for debugging)
|
| 337 |
+
elif section == "Data Analysis":
|
| 338 |
+
st.subheader("๐ Data Analysis")
|
| 339 |
+
|
| 340 |
+
if st.session_state.train_df is not None:
|
| 341 |
+
df = st.session_state.train_df
|
| 342 |
+
st.write("Using loaded data from debug session:")
|
| 343 |
+
st.dataframe(df.head())
|
| 344 |
+
|
| 345 |
+
# Basic analysis without custom modules if they fail
|
| 346 |
+
st.write(f"**Shape:** {df.shape}")
|
| 347 |
+
st.write(f"**Columns:** {list(df.columns)}")
|
| 348 |
+
st.write(f"**Missing values:**")
|
| 349 |
+
st.write(df.isnull().sum())
|
| 350 |
+
|
| 351 |
else:
|
| 352 |
+
st.warning("No data loaded. Please use 'File Upload Debug' section first.")
|
| 353 |
|
|
|
|
| 354 |
elif section == "Train Model":
|
| 355 |
+
st.subheader("๐ค Train Model")
|
| 356 |
+
st.info("Use this section after successfully loading data in debug mode.")
|
| 357 |
+
|
| 358 |
+
if st.session_state.train_df is not None:
|
| 359 |
+
st.success("Data available for training!")
|
| 360 |
+
# Add your training logic here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
else:
|
| 362 |
+
st.warning("No data loaded. Please use 'File Upload Debug' section first.")
|
| 363 |
|
|
|
|
| 364 |
elif section == "Predictions":
|
| 365 |
+
st.subheader("๐ฎ Predictions")
|
| 366 |
+
st.info("Use this section after training a model.")
|
| 367 |
|
| 368 |
+
# Check for trained models
|
| 369 |
+
if os.path.exists("models"):
|
| 370 |
+
models = [f for f in os.listdir("models") if f.endswith('.pkl')]
|
| 371 |
+
if models:
|
| 372 |
+
st.write(f"Available models: {models}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
else:
|
| 374 |
+
st.info("No trained models found.")
|
| 375 |
else:
|
| 376 |
+
st.info("Models directory not found.")
|
| 377 |
+
|
| 378 |
+
# Debug summary
|
| 379 |
+
if debug_mode:
|
| 380 |
+
st.sidebar.markdown("---")
|
| 381 |
+
st.sidebar.subheader("๐ Debug Summary")
|
| 382 |
+
if st.session_state.train_df is not None:
|
| 383 |
+
st.sidebar.success("โ
Data loaded successfully")
|
| 384 |
+
else:
|
| 385 |
+
st.sidebar.warning("โ ๏ธ No data loaded")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|