Update app.py
Browse files
app.py
CHANGED
|
@@ -90,33 +90,25 @@ print(f"Recall: {(1 - recall) * 100:.2f}%")
|
|
| 90 |
print(f"F1 Score: {(1 - f1) * 100:.2f}%")
|
| 91 |
|
| 92 |
def predict_tone(text):
|
| 93 |
-
# Vectorize
|
| 94 |
text_tfidf = tfidf.transform([text])
|
| 95 |
|
| 96 |
-
# Get prediction probabilities
|
| 97 |
probs = model.predict_proba(text_tfidf)[0]
|
| 98 |
|
| 99 |
-
# Get predicted class and its probability
|
| 100 |
pred_class_encoded = model.classes_[np.argmax(probs)]
|
| 101 |
pred_class = label_encoder.inverse_transform([pred_class_encoded])[0]
|
| 102 |
|
| 103 |
-
# Get the labels used during training
|
| 104 |
trained_labels = model.classes_
|
| 105 |
|
| 106 |
-
# Decode to string (Labels)
|
| 107 |
trained_label_names = label_encoder.inverse_transform(trained_labels)
|
| 108 |
|
| 109 |
results = {label: float(prob) for label, prob in zip(trained_label_names, probs)}
|
| 110 |
|
| 111 |
-
# Sort results by probability (descending)
|
| 112 |
sorted_results = {k: v for k, v in sorted(results.items(), key=lambda item: item[1], reverse=True)}
|
| 113 |
|
| 114 |
-
# Create visualization
|
| 115 |
top_n = 5 # Top 5, adjust later if needed
|
| 116 |
top_labels = list(sorted_results.keys())[:top_n]
|
| 117 |
top_probs = list(sorted_results.values())[:top_n]
|
| 118 |
|
| 119 |
-
# OPTIONAL: color-code probabilities
|
| 120 |
colors = ["rgba(64, 128, 255, " + str(min(1.0, p + 0.3)) + ")" for p in top_probs]
|
| 121 |
|
| 122 |
fig = go.Figure()
|
|
|
|
| 90 |
print(f"F1 Score: {(1 - f1) * 100:.2f}%")
|
| 91 |
|
| 92 |
def predict_tone(text):
|
|
|
|
| 93 |
text_tfidf = tfidf.transform([text])
|
| 94 |
|
|
|
|
| 95 |
probs = model.predict_proba(text_tfidf)[0]
|
| 96 |
|
|
|
|
| 97 |
pred_class_encoded = model.classes_[np.argmax(probs)]
|
| 98 |
pred_class = label_encoder.inverse_transform([pred_class_encoded])[0]
|
| 99 |
|
|
|
|
| 100 |
trained_labels = model.classes_
|
| 101 |
|
|
|
|
| 102 |
trained_label_names = label_encoder.inverse_transform(trained_labels)
|
| 103 |
|
| 104 |
results = {label: float(prob) for label, prob in zip(trained_label_names, probs)}
|
| 105 |
|
|
|
|
| 106 |
sorted_results = {k: v for k, v in sorted(results.items(), key=lambda item: item[1], reverse=True)}
|
| 107 |
|
|
|
|
| 108 |
top_n = 5 # Top 5, adjust later if needed
|
| 109 |
top_labels = list(sorted_results.keys())[:top_n]
|
| 110 |
top_probs = list(sorted_results.values())[:top_n]
|
| 111 |
|
|
|
|
| 112 |
colors = ["rgba(64, 128, 255, " + str(min(1.0, p + 0.3)) + ")" for p in top_probs]
|
| 113 |
|
| 114 |
fig = go.Figure()
|