Spaces:
Running
Running
File size: 15,589 Bytes
6413247 83834cd 6413247 |
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
import os
import pandas as pd
from datetime import date
import gradio as gr
from pyvis.network import Network
import ast
# Load the CSV file
df = pd.read_csv("https://jeodpp.jrc.ec.europa.eu/ftp/jrc-opendata/ETOHA/storylines/emdat2.csv", sep=',', header=0, dtype=str, encoding='utf-8')
def try_parse_date(y, m, d):
try:
if not y or not m or not d:
return None
return date(int(float(y)), int(float(m)), int(float(d)))
except (ValueError, TypeError):
return None
def plot_cgraph_pyvis(grp):
if not grp:
return "<div>No data available to plot.</div>"
net = Network(notebook=False, directed=True)
edge_colors_dict = {"causes": "red", "prevents": "green"}
for src, rel, tgt in grp:
src = str(src)
tgt = str(tgt)
rel = str(rel)
net.add_node(src, shape="circle", label=src)
net.add_node(tgt, shape="circle", label=tgt)
edge_color = edge_colors_dict.get(rel, 'black')
net.add_edge(src, tgt, title=rel, label=rel, color=edge_color)
net.repulsion(
node_distance=200,
central_gravity=0.2,
spring_length=200,
spring_strength=0.05,
damping=0.09
)
net.set_edge_smooth('dynamic')
html = net.generate_html()
html = html.replace("'", "\"")
html_s = f"""<iframe style="width: 200%; height: 800px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
allow-scripts allow-same-origin allow-popups
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
return html_s
def display_info(selected_row_str, country, year, month, day, graph_type):
additional_fields = [
"Country", "ISO", "Subregion", "Region", "Location", "Origin",
"Disaster Group", "Disaster Subgroup", "Disaster Type", "Disaster Subtype", "External IDs",
"Event Name", "Associated Types", "OFDA/BHA Response", "Appeal", "Declaration",
"AID Contribution ('000 US$)", "Magnitude", "Magnitude Scale", "Latitude",
"Longitude", "River Basin", "Total Deaths", "No. Injured",
"No. Affected", "No. Homeless", "Total Affected",
"Reconstruction Costs ('000 US$)", "Reconstruction Costs, Adjusted ('000 US$)",
"Insured Damage ('000 US$)", "Insured Damage, Adjusted ('000 US$)",
"Total Damage ('000 US$)", "Total Damage, Adjusted ('000 US$)", "CPI",
"Admin Units",
]
if selected_row_str is None or selected_row_str == '':
print("No row selected.")
return ('', '', '', '', '', '', '', None, '', '') + tuple([''] * len(additional_fields))
print(f"Selected Country: {country}, Selected Row: {selected_row_str}, Date: {year}-{month}-{day}")
filtered_df = df
if country:
filtered_df = filtered_df[filtered_df['Country'] == country]
# Date filtering logic remains the same...
# Use the "DisNo." column for selecting the row
row_data = filtered_df[filtered_df['DisNo.'] == selected_row_str].squeeze()
if not row_data.empty:
print(f"Row data: {row_data}")
key_information = row_data.get('key information', '')
severity = row_data.get('severity', '')
key_drivers = row_data.get('key drivers', '')
impacts_exposure_vulnerability = row_data.get('main impacts, exposure, and vulnerability', '')
likelihood_multi_hazard = row_data.get('likelihood of multi-hazard risks', '')
best_practices = row_data.get('best practices for managing this risk', '')
recommendations = row_data.get('recommendations and supportive measures for recovery', '')
if graph_type == "LLaMA Graph":
causal_graph_caption = row_data.get('llama graph', '')
elif graph_type == "Mixtral Graph":
causal_graph_caption = row_data.get('mixtral graph', '')
elif graph_type == "Ensemble Graph":
causal_graph_caption = row_data.get('ensemble graph', '')
else:
causal_graph_caption = ''
grp = ast.literal_eval(causal_graph_caption) if causal_graph_caption else []
causal_graph_html = plot_cgraph_pyvis(grp)
# Parse and format the start date
start_date = try_parse_date(row_data['Start Year'], row_data['Start Month'], row_data['Start Day'])
start_date_str = start_date.strftime('%Y-%m-%d') if start_date else str(row_data['Start Year'])+"-"+str(row_data['Start Month'])+"-"+str(row_data['Start Day'])
# Parse and format the end date
end_date = try_parse_date(row_data['End Year'], row_data['End Month'], row_data['End Day'])
end_date_str = end_date.strftime('%Y-%m-%d') if end_date else str(row_data['End Year'])+"-"+str(row_data['End Month'])+"-"+str(row_data['End Day'])
additional_data = [row_data.get(field, '') for field in additional_fields]
return (
key_information,
severity,
key_drivers,
impacts_exposure_vulnerability,
likelihood_multi_hazard,
best_practices,
recommendations,
causal_graph_html,
start_date_str,
end_date_str
) + tuple(additional_data)
else:
print("No valid data found for the selection.")
return ('', '', '', '', '', '', '', None, '', '') + tuple([''] * len(additional_fields))
def update_row_dropdown(country, year, month, day):
filtered_df = df
if country:
filtered_df = filtered_df[filtered_df['Country'] == country]
selected_date = try_parse_date(year, month, day)
if selected_date:
# filtered_rows = []
# for idx, row in filtered_df.iterrows():
# if (try_parse_date(row['Start Year'], row['Start Month'], row['Start Day']) is not None) and \
# (try_parse_date(row['End Year'], row['End Month'], row['End Day']) is not None) and \
# (try_parse_date(row['Start Year'], row['Start Month'], row['Start Day']) <= selected_date <= \
# try_parse_date(row['End Year'], row['End Month'], row['End Day'])):
# filtered_rows.append(row)
#
# filtered_df = pd.DataFrame(filtered_rows)
filtered_df = filtered_df[filtered_df.apply(
lambda row: (
(try_parse_date(row['Start Year'], "01" if row['Start Month'] == "" else row['Start Month'], "01" if row['Start Day'] == "" else row['Start Day']) is not None) and
(try_parse_date(row['End Year'], "01" if row['End Month'] == "" else row['End Month'], "01" if row['End Day'] == "" else row['End Day']) is not None) and
(try_parse_date(row['Start Year'], "01" if row['Start Month'] == "" else row['Start Month'], "01" if row['Start Day'] == "" else row['Start Day']) <= selected_date <=
try_parse_date(row['End Year'], "01" if row['End Month'] == "" else row['End Month'], "01" if row['End Day'] == "" else row['End Day']))
), axis=1)]
else:
if year:
sstart = None
eend = None
if month:
try:
sstart = try_parse_date(year, month, "01")
eend = try_parse_date(year, int(float(month)) + 1, "01")
except Exception as err:
print("Invalid selected date.")
sstart = None
eend = None
if sstart and eend:
filtered_df = filtered_df[filtered_df.apply(
lambda row: (
(try_parse_date(row['Start Year'], "01" if row['Start Month'] == "" else row['Start Month'], "01" if row['Start Day'] == "" else row['Start Day']) is not None) and
(sstart <= try_parse_date(row['Start Year'], "01" if row['Start Month'] == "" else row['Start Month'], "01" if row['Start Day'] == "" else row['Start Day']) < eend)
), axis=1)]
else:
try:
sstart = try_parse_date(year, "01", "01")
eend = try_parse_date(year, "12", "31")
except Exception as err:
print("Invalid selected date.")
sstart = None
eend = None
if sstart and eend:
filtered_df = filtered_df[filtered_df.apply(
lambda row: (
(try_parse_date(row['Start Year'], "01" if row['Start Month'] == "" else row['Start Month'], "01" if row['Start Day'] == "" else row['Start Day']) is not None) and
(sstart <= try_parse_date(row['Start Year'], "01" if row['Start Month'] == "" else row['Start Month'], "01" if row['Start Day'] == "" else row['Start Day']) <= eend)
), axis=1)]
else:
print("Invalid selected date.")
# Use the "DisNo." column for choices
choices = filtered_df['DisNo.'].tolist() if not filtered_df.empty else []
print(f"Available rows for {country} on {year}-{month}-{day}: {choices}")
return gr.update(choices=choices, value=choices[0] if choices else None)
def build_interface():
with gr.Blocks() as interface:
gr.Markdown("## From Data to Narratives: AI-Enhanced Disaster and Health Threats Storylines")
gr.Markdown(
"This Gradio app complements Health Threats and Disaster event data through generative AI techniques, including the use of Retrieval Augmented Generation (RAG) with the [Europe Media Monitoring (EMM)](https://emm.newsbrief.eu/overview.html) service, "
"and Large Language Models (LLMs) from the [GPT@JRC](https://gpt.jrc.ec.europa.eu/) portfolio. <br>"
"The app leverages the EMM RAG service to retrieve relevant news chunks for each event data, transforms the unstructured news chunks into structured narratives and causal knowledge graphs using LLMs and text-to-graph techniques, linking health threats and disaster events to their causes and impacts. "
"Drawing data from sources like the [EM-DAT](https://www.emdat.be/) database, it augments each event with news-derived information in a storytelling fashion. <br>"
"This tool enables decision-makers to better explore health threats and disaster dynamics, identify patterns, and simulate scenarios for improved response and readiness. <br><br>"
"Select an event data below. You can filter by country and date period. Below, you will see the AI-generated storyline and causal knowledge graph, while on the right you can see the related EM-DAT data record. <br><br>") # Description -, and constructs disaster-specific ontologies. "
# Extract and prepare unique years from "Start Year" and "End Year"
if not df.empty:
start_years = df["Start Year"].dropna().unique()
end_years = df["End Year"].dropna().unique()
years = set(start_years.astype(int).tolist() + end_years.astype(int).tolist())
year_choices = sorted(years)
else:
year_choices = []
country_dropdown = gr.Dropdown(choices=[''] + df['Country'].unique().tolist(), label="Select Country")
year_dropdown = gr.Dropdown(choices=[""] + [str(year) for year in year_choices], label="Select Year")
month_dropdown = gr.Dropdown(choices=[""] + [f"{i:02d}" for i in range(1, 13)], label="Select Month")
day_dropdown = gr.Dropdown(choices=[""] + [f"{i:02d}" for i in range(1, 32)], label="Select Day")
row_dropdown = gr.Dropdown(choices=[], label="Select Disaster Event #", interactive=True)
graph_type_dropdown = gr.Dropdown(
choices=["LLaMA Graph", "Mixtral Graph", "Ensemble Graph"],
label="Select Graph Type"
)
additional_fields = [
"Country", "ISO", "Subregion", "Region", "Location", "Origin",
"Disaster Group", "Disaster Subgroup", "Disaster Type", "Disaster Subtype", "External IDs",
"Event Name", "Associated Types", "OFDA/BHA Response", "Appeal", "Declaration",
"AID Contribution ('000 US$)", "Magnitude", "Magnitude Scale", "Latitude",
"Longitude", "River Basin", "Total Deaths", "No. Injured",
"No. Affected", "No. Homeless", "Total Affected",
"Reconstruction Costs ('000 US$)", "Reconstruction Costs, Adjusted ('000 US$)",
"Insured Damage ('000 US$)", "Insured Damage, Adjusted ('000 US$)",
"Total Damage ('000 US$)", "Total Damage, Adjusted ('000 US$)", "CPI",
"Admin Units",
]
with gr.Column():
#with gr.Row():
#with gr.Column():
country_dropdown
year_dropdown
month_dropdown
day_dropdown
row_dropdown
graph_type_dropdown
gr.Markdown("### AI-Generated Storyline:"), # Title
outputs = [
gr.Textbox(label="Key Information", interactive=False),
gr.Textbox(label="Severity", interactive=False),
gr.Textbox(label="Key Drivers", interactive=False),
gr.Textbox(label="Main Impacts, Exposure, and Vulnerability", interactive=False),
gr.Textbox(label="Likelihood of Multi-Hazard Risks", interactive=False),
gr.Textbox(label="Best Practices for Managing This Risk", interactive=False),
gr.Textbox(label="Recommendations and Supportive Measures for Recovery", interactive=False),
#gr.Markdown("### Causal Graph:"), # Title
gr.HTML(label="Causal Graph") # Change from gr.Plot to gr.HTML
]
#with gr.Column():
gr.Markdown("### EMDAT2 Original Record:") # Title
outputs.extend([
gr.Textbox(label="Start Date", interactive=False),
gr.Textbox(label="End Date", interactive=False)
])
for field in additional_fields:
outputs.append(gr.Textbox(label=field, interactive=False))
country_dropdown.change(
fn=update_row_dropdown,
inputs=[country_dropdown, year_dropdown, month_dropdown, day_dropdown],
outputs=row_dropdown
)
year_dropdown.change(
fn=update_row_dropdown,
inputs=[country_dropdown, year_dropdown, month_dropdown, day_dropdown],
outputs=row_dropdown
)
month_dropdown.change(
fn=update_row_dropdown,
inputs=[country_dropdown, year_dropdown, month_dropdown, day_dropdown],
outputs=row_dropdown
)
day_dropdown.change(
fn=update_row_dropdown,
inputs=[country_dropdown, year_dropdown, month_dropdown, day_dropdown],
outputs=row_dropdown
)
row_dropdown.change(
fn=display_info,
inputs=[row_dropdown, country_dropdown, year_dropdown, month_dropdown, day_dropdown, graph_type_dropdown],
outputs=outputs
)
graph_type_dropdown.change(
fn=display_info,
inputs=[row_dropdown, country_dropdown, year_dropdown, month_dropdown, day_dropdown, graph_type_dropdown],
outputs=outputs
)
return interface
app = build_interface()
app.launch()
|