Spaces:
Sleeping
Sleeping
File size: 9,342 Bytes
fef8b06 5575829 fef8b06 8fca602 5575829 fef8b06 aeada86 f988ad7 6890e02 f988ad7 5575829 fef8b06 0c3391b fef8b06 0c3391b 8fca602 0c3391b 8fca602 0c3391b 8fca602 26e5625 cc1117d 5575829 cf0c8b1 5662680 475b809 ac89270 9865cf1 d6dd9fc 1d42bf8 d6dd9fc 9865cf1 5575829 f8d4a0e 5575829 8ce9236 de89642 8ce9236 de89642 8ce9236 5575829 aeada86 5575829 fef8b06 58cbd2d 5575829 069449a 5575829 069449a 5575829 069449a 8fca602 e5996e7 e3e91ee e5996e7 234945b 05d925b e5996e7 05d925b 234945b e5996e7 05d925b 4067906 6f5a3f7 e3e91ee 0c3391b f95341b 4067906 1085ea0 21731c5 ab2e768 cf0c8b1 ac89270 0c3391b cf0c8b1 aeada86 cf0c8b1 3c8dac4 21731c5 1d42bf8 410e6af 1d42bf8 5575829 de89642 946dac8 d6dd9fc cc1117d d6dd9fc cc1117d d6dd9fc 5575829 d6dd9fc 5575829 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
import os
import time
import gradio as gr
import pandas as pd
from classifier import classify
from statistics import mean
HFTOKEN = os.environ["HF_TOKEN"]
def load_and_classify_csv(file, text_field, event_model):
filepath = file.name
if ".csv" in filepath:
df = pd.read_csv(filepath)
else:
df = pd.read_table(filepath)
if text_field not in df.columns:
raise gr.Error(f"Error: Enter text column'{text_field}' not in CSV file.")
labels, scores = [], []
for post in df[text_field].to_list():
res = classify(post, event_model, HFTOKEN)
labels.append(res["event"])
scores.append(res["score"])
df["model_label"] = labels
df["model_score"] = scores
model_confidence = round(mean(scores), 5)
fire_related = gr.CheckboxGroup(choices=df[df["model_label"]=="fire"][text_field].to_list()) #fires
flood_related = gr.CheckboxGroup(choices=df[df["model_label"]=="flood"][text_field].to_list())
not_related = gr.CheckboxGroup(choices=df[df["model_label"]=="none"][text_field].to_list())
return flood_related, fire_related, not_related, model_confidence, len(df[text_field].to_list()), df
def qa_process(selections):
selected_texts = selections
analysis_results = [f"Word Count: {len(text.split())}" for text in selected_texts]
result_df = pd.DataFrame({"Selected Text": selected_texts, "Analysis": analysis_results})
return result_df
def calculate_accuracy(flood_selections, fire_selections, none_selections, num_posts, text_field, data_df):
posts = data_df[text_field].to_list()
selections = flood_selections + fire_selections + none_selections
eval = []
for post in posts:
if post in selections:
eval.append("incorrect")
else:
eval.append("correct")
data_df["model_eval"] = eval
incorrect = len(selections)
correct = num_posts - incorrect
accuracy = (correct/num_posts)*100
data_df.to_csv("output.csv")
return incorrect, correct, accuracy, data_df, gr.DownloadButton(label=f"Download CSV", value="output.csv", visible=True)
def get_queries(to_add, history):
history = history or [
"What areas are being evacuated?",
"What areas are predicted to be impacted?",
"What areas are without power?",
"What barriers are hindering response efforts?",
"What events have been canceled?",
"What preparations are being made?",
"What regions have announced a state of emergency?",
"What roads are blocked / closed?",
"What services have been closed?",
"What warnings are currently in effect?",
"Where are emergency services deployed?",
"Where are emergency services needed?",
"Where are evacuations needed?",
"Where are people needing rescued?",
"Where are recovery efforts taking place?",
"Where has building or infrastructure damage occurred?",
"Where has flooding occured?"
"Where are volunteers being requested?",
"Where has road damage occured?",
"What area has the wildfire burned?",
"Where have homes been damaged or destroyed?"]
# queries = [
# "What areas are being evacuated?",
# "What areas are predicted to be impacted?",
# "What areas are without power?",
# "What barriers are hindering response efforts?",
# "What events have been canceled?",
# "What preparations are being made?",
# "What regions have announced a state of emergency?",
# "What roads are blocked / closed?",
# "What services have been closed?",
# "What warnings are currently in effect?",
# "Where are emergency services deployed?",
# "Where are emergency services needed?",
# "Where are evacuations needed?",
# "Where are people needing rescued?",
# "Where are recovery efforts taking place?",
# "Where has building or infrastructure damage occurred?",
# "Where has flooding occured?"
# "Where are volunteers being requested?",
# "Where has road damage occured?",
# "What area has the wildfire burned?",
# "Where have homes been damaged or destroyed?"]
if to_add != "dummy":
history.append(to_add)
return gr.CheckboxGroup(choices=history), history
with gr.Blocks() as demo:
event_models = ["jayebaku/distilbert-base-multilingual-cased-crexdata-relevance-classifier"]
with gr.Tab("Event Type Classification"):
gr.Markdown(
"""
# T4.5 Relevance Classifier Demo
This is a demo created to explore floods and wildfire classification in social media posts.\n
Usage:\n
- Upload .tsv data file (must contain a text column with social media posts).\n
- Next, type the name of the text column.\n
- Then, choose a BERT classifier model from the drop down.\n
- Finally, click the 'start prediction' buttton.\n
Evaluation:\n
- To evaluate the model's accuracy select the INCORRECT classifications using the checkboxes in front of each post.\n
- Then, click on the 'Calculate Accuracy' button.\n
- Then, click on the 'Download data as CSV' to get the classifications and evaluation data as a .csv file.
""")
with gr.Row(equal_height=True):
with gr.Column(scale=4):
file_input = gr.File(label="Upload CSV or TSV File", file_types=['.tsv', '.csv'])
with gr.Column(scale=6):
text_field = gr.Textbox(label="Text field name", value="tweet_text")
event_model = gr.Dropdown(event_models, label="Select classification model")
predict_button = gr.Button("Start Prediction")
with gr.Row(): # XXX confirm this is not a problem later --equal_height=True
with gr.Column():
gr.Markdown("""### Flood-related""")
flood_checkbox_output = gr.CheckboxGroup(label="Select ONLY incorrect classifications", interactive=True)
with gr.Column():
gr.Markdown("""### Fire-related""")
fire_checkbox_output = gr.CheckboxGroup(label="Select ONLY incorrect classifications", interactive=True)
with gr.Column():
gr.Markdown("""### None""")
none_checkbox_output = gr.CheckboxGroup(label="Select ONLY incorrect classifications", interactive=True)
with gr.Row(equal_height=True):
with gr.Column(scale=5):
gr.Markdown(r"""
Accuracy: is the model's ability to make correct predicitons.
It is the fraction of correct prediction out of the total predictions.
$$
\text{Accuracy} = \frac{\text{Correct predictions}}{\text{All predictions}} * 100
$$
Model Confidence: is the mean probabilty of each case
belonging to their assigned classes. A value of 1 is best.
""", latex_delimiters=[{ "left": "$$", "right": "$$", "display": True }])
gr.Markdown("\n\n\n")
model_confidence = gr.Number(label="Model Confidence")
with gr.Column(scale=5):
correct = gr.Number(label="Number of correct classifications")
incorrect = gr.Number(label="Number of incorrect classifications")
accuracy = gr.Number(label="Model Accuracy (%)")
accuracy_button = gr.Button("Calculate Accuracy")
download_csv = gr.DownloadButton(visible=False)
num_posts = gr.Number(visible=False)
data = gr.DataFrame(visible=False)
data_eval = gr.DataFrame(visible=False)
predict_button.click(
load_and_classify_csv,
inputs=[file_input, text_field, event_model],
outputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, model_confidence, num_posts, data])
accuracy_button.click(
calculate_accuracy,
inputs=[flood_checkbox_output, fire_checkbox_output, none_checkbox_output, num_posts, text_field, data],
outputs=[incorrect, correct, accuracy, data_eval, download_csv])
qa_tab = gr.Tab("Question Answering")
with qa_tab:
# XXX Add some button disabling here, if the classification process is not completed first XXX
gr.Markdown(
"""
# Question Answering Demo
This section uses RAG to answer questions about the relevant social media posts identified by the relevance classifier\n
Usage:\n
- Select queries from predefined\n
- Parameters for QA can be editted in sidebar\n
""")
selected_queries = gr.CheckboxGroup(label="Select at least one query using the checkboxes", interactive=True)
dummy_arg = gr.Textbox(visible=False, value="dummy")
qa_tab.select(get_queries, inputs=[dummy_arg, "state"], outputs=selected_queries)
qa_button = gr.Button("Start QA")
analysis_output = gr.DataFrame(headers=["Selected Text", "Analysis"])
qa_button.click(qa_process, inputs=selected_queries, outputs=analysis_output)
demo.launch() |