Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,23 +15,28 @@ def load_and_analyze_csv(file, text_field, event_model):
|
|
| 15 |
if text_field not in df.columns:
|
| 16 |
raise gr.Error(f"Error: Enter text column'{text_field}' not in CSV file.")
|
| 17 |
|
| 18 |
-
floods, fires, nones, scores = [], [], [], []
|
|
|
|
| 19 |
for post in df[text_field].to_list():
|
| 20 |
res = classify(post, event_model, HFTOKEN)
|
| 21 |
-
if res["event"] == 'flood':
|
| 22 |
-
|
| 23 |
-
elif res["event"] == 'fire':
|
| 24 |
-
|
| 25 |
-
else:
|
| 26 |
-
|
|
|
|
| 27 |
scores.append(res["score"])
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
model_confidence = round(mean(scores), 5)
|
| 30 |
-
fire_related = gr.CheckboxGroup(choices=fires
|
| 31 |
-
flood_related = gr.CheckboxGroup(choices=
|
| 32 |
-
not_related = gr.CheckboxGroup(choices=
|
| 33 |
|
| 34 |
-
return flood_related, fire_related, not_related, model_confidence, len(df[text_field].to_list())
|
| 35 |
|
| 36 |
def analyze_selected_texts(selections):
|
| 37 |
selected_texts = selections
|
|
@@ -105,14 +110,16 @@ with gr.Blocks() as demo:
|
|
| 105 |
gr.Markdown("\n\n\n")
|
| 106 |
model_confidence = gr.Number(label="Model Confidence")
|
| 107 |
with gr.Column(scale=5):
|
| 108 |
-
correct = gr.Number(label="Number of correct classifications")
|
| 109 |
incorrect = gr.Number(label="Number of incorrect classifications")
|
| 110 |
accuracy = gr.Number(label="Model Accuracy (%)")
|
| 111 |
|
| 112 |
accuracy_button = gr.Button("Calculate Accuracy")
|
| 113 |
num_posts = gr.Number(visible=False)
|
|
|
|
|
|
|
| 114 |
predict_button.click(load_and_analyze_csv, inputs=[file_input, text_field, event_model],
|
| 115 |
-
outputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, model_confidence, num_posts])
|
| 116 |
accuracy_button.click(calculate_accuracy, inputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, num_posts],
|
| 117 |
outputs=[incorrect, correct, accuracy])
|
| 118 |
|
|
|
|
| 15 |
if text_field not in df.columns:
|
| 16 |
raise gr.Error(f"Error: Enter text column'{text_field}' not in CSV file.")
|
| 17 |
|
| 18 |
+
# floods, fires, nones, scores = [], [], [], []
|
| 19 |
+
labels, scores = [], []
|
| 20 |
for post in df[text_field].to_list():
|
| 21 |
res = classify(post, event_model, HFTOKEN)
|
| 22 |
+
# if res["event"] == 'flood':
|
| 23 |
+
# floods.append(post)
|
| 24 |
+
# elif res["event"] == 'fire':
|
| 25 |
+
# fires.append(post)
|
| 26 |
+
# else:
|
| 27 |
+
# nones.append(post)
|
| 28 |
+
labels.append(res["event"])
|
| 29 |
scores.append(res["score"])
|
| 30 |
|
| 31 |
+
df["model_label"] = labels
|
| 32 |
+
df["model_score"] = scores
|
| 33 |
+
|
| 34 |
model_confidence = round(mean(scores), 5)
|
| 35 |
+
fire_related = gr.CheckboxGroup(choices=df[df["model_label"]=="fire"][text_field].to_list()) #fires
|
| 36 |
+
flood_related = gr.CheckboxGroup(choices=df[df["model_label"]=="flood"][text_field].to_list())
|
| 37 |
+
not_related = gr.CheckboxGroup(choices=df[df["model_label"]=="none"][text_field].to_list())
|
| 38 |
|
| 39 |
+
return flood_related, fire_related, not_related, model_confidence, len(df[text_field].to_list()), df, df.columns.tolist()
|
| 40 |
|
| 41 |
def analyze_selected_texts(selections):
|
| 42 |
selected_texts = selections
|
|
|
|
| 110 |
gr.Markdown("\n\n\n")
|
| 111 |
model_confidence = gr.Number(label="Model Confidence")
|
| 112 |
with gr.Column(scale=5):
|
| 113 |
+
correct = gr.Number(label="Number of correct classifications")
|
| 114 |
incorrect = gr.Number(label="Number of incorrect classifications")
|
| 115 |
accuracy = gr.Number(label="Model Accuracy (%)")
|
| 116 |
|
| 117 |
accuracy_button = gr.Button("Calculate Accuracy")
|
| 118 |
num_posts = gr.Number(visible=False)
|
| 119 |
+
data = gr.DataFrame(headers=datacols) #visible=False
|
| 120 |
+
|
| 121 |
predict_button.click(load_and_analyze_csv, inputs=[file_input, text_field, event_model],
|
| 122 |
+
outputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, model_confidence, num_posts, data, datacols])
|
| 123 |
accuracy_button.click(calculate_accuracy, inputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, num_posts],
|
| 124 |
outputs=[incorrect, correct, accuracy])
|
| 125 |
|