Update app.py
Browse files
app.py
CHANGED
|
@@ -54,12 +54,21 @@ tfidf = TfidfVectorizer(max_features=5000)
|
|
| 54 |
X_train_tfidf = tfidf.fit_transform(X_train)
|
| 55 |
X_test_tfidf = tfidf.transform(X_test)
|
| 56 |
|
| 57 |
-
# Handle class imbalance using SMOTE
|
| 58 |
print("Applying SMOTE to handle class imbalance...")
|
| 59 |
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
except ValueError as e:
|
| 64 |
print(f"SMOTE error: {e}. Using original data.")
|
| 65 |
X_train_resampled, y_train_resampled = X_train_tfidf, y_train
|
|
@@ -187,13 +196,14 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 187 |
label="Example texts with similar tone"
|
| 188 |
)
|
| 189 |
|
| 190 |
-
# Set up event handlers
|
| 191 |
analyze_button.click(
|
| 192 |
fn=analyze_tone,
|
| 193 |
-
inputs=[text_input, None],
|
| 194 |
outputs=[result_message, all_probs_output, plot_output, examples_output]
|
| 195 |
)
|
| 196 |
|
|
|
|
| 197 |
tone_dropdown.change(
|
| 198 |
fn=get_tone_examples,
|
| 199 |
inputs=[tone_dropdown],
|
|
|
|
| 54 |
X_train_tfidf = tfidf.fit_transform(X_train)
|
| 55 |
X_test_tfidf = tfidf.transform(X_test)
|
| 56 |
|
| 57 |
+
# Handle class imbalance using SMOTE - Fix for SMOTE error
|
| 58 |
print("Applying SMOTE to handle class imbalance...")
|
| 59 |
try:
|
| 60 |
+
# Modify the SMOTE parameters to handle small sample sizes
|
| 61 |
+
# Use k_neighbors=min(5, n_samples-1) for classes with few samples
|
| 62 |
+
smallest_class_size = min(np.bincount(y_train)[np.bincount(y_train) > 0])
|
| 63 |
+
k_neighbors = min(5, smallest_class_size - 1)
|
| 64 |
+
|
| 65 |
+
if k_neighbors > 0:
|
| 66 |
+
smote = SMOTE(random_state=42, k_neighbors=k_neighbors)
|
| 67 |
+
X_train_resampled, y_train_resampled = smote.fit_resample(X_train_tfidf, y_train)
|
| 68 |
+
print(f"After SMOTE: {X_train_resampled.shape}")
|
| 69 |
+
else:
|
| 70 |
+
print("Classes too small for SMOTE, using original data.")
|
| 71 |
+
X_train_resampled, y_train_resampled = X_train_tfidf, y_train
|
| 72 |
except ValueError as e:
|
| 73 |
print(f"SMOTE error: {e}. Using original data.")
|
| 74 |
X_train_resampled, y_train_resampled = X_train_tfidf, y_train
|
|
|
|
| 196 |
label="Example texts with similar tone"
|
| 197 |
)
|
| 198 |
|
| 199 |
+
# Set up event handlers - Fix for NoneType error by providing proper arguments
|
| 200 |
analyze_button.click(
|
| 201 |
fn=analyze_tone,
|
| 202 |
+
inputs=[text_input, None], # Fixed: passing None for selected_tone parameter
|
| 203 |
outputs=[result_message, all_probs_output, plot_output, examples_output]
|
| 204 |
)
|
| 205 |
|
| 206 |
+
# Fix for tone_dropdown event handler
|
| 207 |
tone_dropdown.change(
|
| 208 |
fn=get_tone_examples,
|
| 209 |
inputs=[tone_dropdown],
|