jayebaku commited on
Commit
cf0c8b1
·
verified ·
1 Parent(s): 26e5625

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -15,16 +15,9 @@ 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
  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
 
@@ -46,11 +39,21 @@ def analyze_selected_texts(selections):
46
  result_df = pd.DataFrame({"Selected Text": selected_texts, "Analysis": analysis_results})
47
  return result_df
48
 
49
- def calculate_accuracy(flood_selections, fire_selections, none_selections, num_posts):
50
- incorrect = len(flood_selections) + len(fire_selections) + len(none_selections)
 
 
 
 
 
 
 
 
 
 
51
  correct = num_posts - incorrect
52
  accuracy = (correct/num_posts)*100
53
- return incorrect, correct, accuracy
54
 
55
 
56
 
@@ -117,12 +120,17 @@ with gr.Blocks() as demo:
117
 
118
  accuracy_button = gr.Button("Calculate Accuracy")
119
  num_posts = gr.Number(visible=False)
120
- data = gr.DataFrame() #visible=False
 
121
 
122
- predict_button.click(load_and_analyze_csv, inputs=[file_input, text_field, event_model],
123
- outputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, model_confidence, num_posts, data])
124
- accuracy_button.click(calculate_accuracy, inputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, num_posts],
125
- outputs=[incorrect, correct, accuracy])
 
 
 
 
126
 
127
  with gr.Tab("Question Answering"):
128
  # XXX Add some button disabling here, if the classification process is not completed first XXX
 
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
  labels, scores = [], []
19
  for post in df[text_field].to_list():
20
  res = classify(post, event_model, HFTOKEN)
 
 
 
 
 
 
21
  labels.append(res["event"])
22
  scores.append(res["score"])
23
 
 
39
  result_df = pd.DataFrame({"Selected Text": selected_texts, "Analysis": analysis_results})
40
  return result_df
41
 
42
+ def calculate_accuracy(flood_selections, fire_selections, none_selections, num_posts, text_field, data_df):
43
+ posts = data_df[text_field].to_list()
44
+ selections = flood_selections + fire_selections + none_selections
45
+ eval = []
46
+ for post in posts:
47
+ if post in selections:
48
+ eval.append("incorrect")
49
+ else:
50
+ eval.append("correct")
51
+
52
+ data_df["model_eval"] = eval
53
+ incorrect = len(selections)
54
  correct = num_posts - incorrect
55
  accuracy = (correct/num_posts)*100
56
+ return incorrect, correct, accuracy, data_df
57
 
58
 
59
 
 
120
 
121
  accuracy_button = gr.Button("Calculate Accuracy")
122
  num_posts = gr.Number(visible=False)
123
+ data = gr.DataFrame(visible=False)
124
+ data_eval = gr.DataFrame()
125
 
126
+ predict_button.click(
127
+ load_and_analyze_csv,
128
+ inputs=[file_input, text_field, event_model],
129
+ outputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, model_confidence, num_posts, data])
130
+ accuracy_button.click(
131
+ calculate_accuracy,
132
+ inputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, num_posts, text_field, data],
133
+ outputs=[incorrect, correct, accuracy, data_eval])
134
 
135
  with gr.Tab("Question Answering"):
136
  # XXX Add some button disabling here, if the classification process is not completed first XXX