Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
|
6 |
+
def load_and_analyze_csv(file, text_field):
|
7 |
+
df = pd.read_csv(file.name)
|
8 |
+
|
9 |
+
if text_field not in df.columns:
|
10 |
+
raise gr.Error(f"Error: Enter text column'{text_field}' not in CSV file.")
|
11 |
+
|
12 |
+
fire_related = gr.CheckboxGroup(choices=df['text'].to_list()[:5])
|
13 |
+
flood_related = gr.CheckboxGroup(choices=df['text'].to_list()[:7])
|
14 |
+
not_related = gr.CheckboxGroup(choices=df['text'].to_list())
|
15 |
+
time.sleep(5)
|
16 |
+
return fire_related, flood_related, not_related
|
17 |
+
|
18 |
+
def analyze_selected_texts(selections):
|
19 |
+
selected_texts = selections
|
20 |
+
|
21 |
+
analysis_results = [f"Word Count: {len(text.split())}" for text in selected_texts]
|
22 |
+
|
23 |
+
result_df = pd.DataFrame({"Selected Text": selected_texts, "Analysis": analysis_results})
|
24 |
+
return result_df
|
25 |
+
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
event_models = ["jayebaku/distilbert-base-multilingual-cased-crexdata-relevance-classifier"]
|
29 |
+
|
30 |
+
with gr.Tab("Event Type Classification"):
|
31 |
+
with gr.Row(equal_height=True):
|
32 |
+
with gr.Column(scale=4):
|
33 |
+
file_input = gr.File(label="Upload CSV File")
|
34 |
+
|
35 |
+
with gr.Column(scale=6):
|
36 |
+
text_field = gr.Textbox(label="Text field name", value="text")
|
37 |
+
event_model = gr.Dropdown(event_models, label="Select classification model")
|
38 |
+
predict_button = gr.Button("Start Prediction")
|
39 |
+
|
40 |
+
with gr.Row(): # XXX confirm this is not a problem later --equal_height=True
|
41 |
+
with gr.Column():
|
42 |
+
gr.Markdown("""### Flood-related""")
|
43 |
+
fire_checkbox_output = gr.CheckboxGroup(label="Select ONLY incorrect classifications")
|
44 |
+
|
45 |
+
with gr.Column():
|
46 |
+
gr.Markdown("""### Fire-related""")
|
47 |
+
flood_checkbox_output = gr.CheckboxGroup(label="Select ONLY incorrect classifications")
|
48 |
+
|
49 |
+
with gr.Column():
|
50 |
+
gr.Markdown("""### None""")
|
51 |
+
none_checkbox_output = gr.CheckboxGroup(label="Select ONLY incorrect classifications")
|
52 |
+
|
53 |
+
predict_button.click(load_and_analyze_csv, inputs=[file_input, text_field], outputs=[fire_checkbox_output, flood_checkbox_output, none_checkbox_output])
|
54 |
+
|
55 |
+
with gr.Tab("Question Answering"):
|
56 |
+
# XXX Add some button disabling here, if the classification process is not completed first XXX
|
57 |
+
|
58 |
+
analysis_button = gr.Button("Analyze Selected Texts")
|
59 |
+
analysis_output = gr.DataFrame(headers=["Selected Text", "Analysis"])
|
60 |
+
analysis_button.click(analyze_selected_texts, inputs=fire_checkbox_output, outputs=analysis_output)
|
61 |
+
|
62 |
+
demo.launch()
|