Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from classifier import classify_toxic_comment
|
|
| 4 |
|
| 5 |
# Clear function for resetting the UI
|
| 6 |
def clear_inputs():
|
| 7 |
-
return "", 0, "", []
|
| 8 |
|
| 9 |
# Custom CSS for styling
|
| 10 |
custom_css = """
|
|
@@ -60,6 +60,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=2):
|
| 62 |
prediction_output = gr.Textbox(label="Prediction", placeholder="Prediction will appear here...")
|
|
|
|
|
|
|
| 63 |
with gr.Column(scale=1):
|
| 64 |
confidence_output = gr.Slider(
|
| 65 |
label="Confidence",
|
|
@@ -88,23 +90,31 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 88 |
def handle_classification(comment, history):
|
| 89 |
if history is None:
|
| 90 |
history = []
|
| 91 |
-
prediction, confidence, color = classify_toxic_comment(comment)
|
| 92 |
-
history.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
threshold_message = "High Confidence" if confidence >= 0.7 else "Low Confidence"
|
| 94 |
threshold_color = "green" if confidence >= 0.7 else "orange"
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
|
| 97 |
def handle_feedback(feedback, comment):
|
| 98 |
return f"Thank you for your feedback: {feedback}\nAdditional comment: {comment}"
|
| 99 |
|
| 100 |
submit_btn.click(
|
| 101 |
-
fn=lambda: ("Classifying...", 0, "", None, "", ""), # Show loading state
|
| 102 |
inputs=[],
|
| 103 |
-
outputs=[prediction_output, confidence_output, label_display, history_output, threshold_display, threshold_display]
|
| 104 |
).then(
|
| 105 |
fn=handle_classification,
|
| 106 |
inputs=[comment_input, history_output],
|
| 107 |
-
outputs=[prediction_output, confidence_output, label_display, history_output, threshold_display, threshold_display]
|
| 108 |
).then(
|
| 109 |
fn=lambda prediction, confidence, color: f"<span style='color: {color}; font-size: 20px; font-weight: bold;'>{prediction}</span>",
|
| 110 |
inputs=[prediction_output, confidence_output, label_display],
|
|
@@ -124,7 +134,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 124 |
clear_btn.click(
|
| 125 |
fn=clear_inputs,
|
| 126 |
inputs=[],
|
| 127 |
-
outputs=[comment_input, confidence_output, label_display, history_output]
|
| 128 |
)
|
| 129 |
|
| 130 |
gr.Markdown(
|
|
|
|
| 4 |
|
| 5 |
# Clear function for resetting the UI
|
| 6 |
def clear_inputs():
|
| 7 |
+
return "", 0, "", [], "", ""
|
| 8 |
|
| 9 |
# Custom CSS for styling
|
| 10 |
custom_css = """
|
|
|
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=2):
|
| 62 |
prediction_output = gr.Textbox(label="Prediction", placeholder="Prediction will appear here...")
|
| 63 |
+
toxicity_output = gr.Textbox(label="Toxicity Score", placeholder="Toxicity score will appear here...")
|
| 64 |
+
bias_output = gr.Textbox(label="Bias Score", placeholder="Bias score will appear here...")
|
| 65 |
with gr.Column(scale=1):
|
| 66 |
confidence_output = gr.Slider(
|
| 67 |
label="Confidence",
|
|
|
|
| 90 |
def handle_classification(comment, history):
|
| 91 |
if history is None:
|
| 92 |
history = []
|
| 93 |
+
prediction, confidence, color, toxicity_score, bias_score = classify_toxic_comment(comment)
|
| 94 |
+
history.append({
|
| 95 |
+
"comment": comment,
|
| 96 |
+
"prediction": prediction,
|
| 97 |
+
"confidence": confidence,
|
| 98 |
+
"toxicity_score": toxicity_score,
|
| 99 |
+
"bias_score": bias_score
|
| 100 |
+
})
|
| 101 |
threshold_message = "High Confidence" if confidence >= 0.7 else "Low Confidence"
|
| 102 |
threshold_color = "green" if confidence >= 0.7 else "orange"
|
| 103 |
+
toxicity_display = f"{toxicity_score} (Scale: 0 to 1, lower is less toxic)" if toxicity_score is not None else "N/A"
|
| 104 |
+
bias_display = f"{bias_score} (Scale: 0 to 1, lower indicates less bias)" if bias_score is not None else "N/A"
|
| 105 |
+
return prediction, confidence, color, history, threshold_message, threshold_color, toxicity_display, bias_display
|
| 106 |
|
| 107 |
def handle_feedback(feedback, comment):
|
| 108 |
return f"Thank you for your feedback: {feedback}\nAdditional comment: {comment}"
|
| 109 |
|
| 110 |
submit_btn.click(
|
| 111 |
+
fn=lambda: ("Classifying...", 0, "", None, "", "", "Calculating...", "Calculating..."), # Show loading state
|
| 112 |
inputs=[],
|
| 113 |
+
outputs=[prediction_output, confidence_output, label_display, history_output, threshold_display, threshold_display, toxicity_output, bias_output]
|
| 114 |
).then(
|
| 115 |
fn=handle_classification,
|
| 116 |
inputs=[comment_input, history_output],
|
| 117 |
+
outputs=[prediction_output, confidence_output, label_display, history_output, threshold_display, threshold_display, toxicity_output, bias_output]
|
| 118 |
).then(
|
| 119 |
fn=lambda prediction, confidence, color: f"<span style='color: {color}; font-size: 20px; font-weight: bold;'>{prediction}</span>",
|
| 120 |
inputs=[prediction_output, confidence_output, label_display],
|
|
|
|
| 134 |
clear_btn.click(
|
| 135 |
fn=clear_inputs,
|
| 136 |
inputs=[],
|
| 137 |
+
outputs=[comment_input, confidence_output, label_display, history_output, toxicity_output, bias_output]
|
| 138 |
)
|
| 139 |
|
| 140 |
gr.Markdown(
|