Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# Fichier: app.py
|
| 2 |
|
| 3 |
# ===================================================================================
|
| 4 |
-
# WAHIS SCRAPER - VERSION FINALE
|
| 5 |
# ===================================================================================
|
| 6 |
|
| 7 |
import gradio as gr
|
|
@@ -16,7 +16,6 @@ import subprocess
|
|
| 16 |
import zipfile
|
| 17 |
from playwright.async_api import async_playwright
|
| 18 |
from playwright_stealth import stealth_async
|
| 19 |
-
import plotly.express as px
|
| 20 |
|
| 21 |
# Installation du navigateur (ne change pas)
|
| 22 |
def install_playwright_browsers():
|
|
@@ -82,87 +81,100 @@ class WAHISScraper:
|
|
| 82 |
if browser and browser.is_connected(): await browser.close()
|
| 83 |
|
| 84 |
def process_data_and_create_zip(reports, outbreaks, additional_infos):
|
| 85 |
-
if not reports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
valid_additional_infos = [info for info in additional_infos if isinstance(info, dict)]
|
| 87 |
additional_info_map = {info.get('outbreakId'): info for info in valid_additional_infos}
|
| 88 |
for outbreak in outbreaks:
|
| 89 |
outbreak_id = outbreak.get('outbreakId')
|
| 90 |
if outbreak_id in additional_info_map: outbreak.update(additional_info_map[outbreak_id])
|
|
|
|
| 91 |
df_outbreaks = pd.DataFrame(outbreaks)
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 94 |
zip_path = OUTPUT_DIR / f"wahis_package_{timestamp}.zip"
|
| 95 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
|
|
|
| 96 |
df_summary.to_excel(OUTPUT_DIR / "1_summary_events.xlsx", index=False)
|
| 97 |
df_outbreaks.to_excel(OUTPUT_DIR / "2_outbreaks_full_details.xlsx", index=False)
|
| 98 |
zipf.write(OUTPUT_DIR / "1_summary_events.xlsx", arcname="1_summary_events.xlsx")
|
| 99 |
zipf.write(OUTPUT_DIR / "2_outbreaks_full_details.xlsx", arcname="2_outbreaks_full_details.xlsx")
|
| 100 |
-
return df_summary, df_outbreaks, str(zip_path)
|
| 101 |
-
|
| 102 |
-
def create_map_figure(df, title="Carte des Foyers"):
|
| 103 |
-
if df.empty or 'latitude' not in df.columns or 'longitude' not in df.columns:
|
| 104 |
-
fig = px.scatter_mapbox(title="Aucune donnée à afficher")
|
| 105 |
-
fig.update_layout(mapbox_style="open-street-map")
|
| 106 |
-
return fig
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
size_col_name = None
|
| 111 |
-
if 'totalAnimalsAffected' in df_plot.columns: size_col_name = 'totalAnimalsAffected'
|
| 112 |
-
elif 'cases' in df_plot.columns: size_col_name = 'cases'
|
| 113 |
-
elif 'deaths' in df_plot.columns: size_col_name = 'deaths'
|
| 114 |
-
|
| 115 |
-
# CORRECTION : Utiliser 'locationName' qui est le bon nom de colonne
|
| 116 |
-
hover_name_col = 'locationName' if 'locationName' in df_plot.columns else None
|
| 117 |
|
| 118 |
-
|
| 119 |
-
df_plot['size_for_plotting'] = pd.to_numeric(df_plot[size_col_name], errors='coerce').fillna(1)
|
| 120 |
-
fig = px.scatter_mapbox(df_plot, lat="latitude", lon="longitude", color="diseaseName",
|
| 121 |
-
size='size_for_plotting', size_max=30,
|
| 122 |
-
hover_name=hover_name_col, title=title, zoom=1, height=600)
|
| 123 |
-
else:
|
| 124 |
-
fig = px.scatter_mapbox(df_plot, lat="latitude", lon="longitude", color="diseaseName",
|
| 125 |
-
hover_name=hover_name_col, title=title, zoom=1, height=600)
|
| 126 |
-
|
| 127 |
-
fig.update_layout(mapbox_style="open-street-map", margin={"r":0,"t":40,"l":0,"b":0})
|
| 128 |
-
return fig
|
| 129 |
|
| 130 |
async def run_and_update_ui():
|
| 131 |
scraper = WAHISScraper()
|
| 132 |
reports, outbreaks, additional_infos, logs = await scraper.run_extraction_async()
|
| 133 |
if not reports:
|
| 134 |
-
return logs,
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
with gr.Blocks(theme=gr.themes.Soft(), title="WAHIS Scraper") as demo:
|
|
|
|
| 140 |
gr.Markdown("# 🤖 Scraper pour WAHIS (WOAH) - Version Robuste")
|
| 141 |
run_button = gr.Button("🚀 Lancer l'extraction des données", variant="primary")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
status_textbox = gr.Textbox(lines=15, label="📜 Logs", interactive=False)
|
| 147 |
-
|
| 148 |
-
gr.Markdown("---")
|
| 149 |
-
gr.Markdown("### 🗺️ Carte des Foyers (Vue d'ensemble)")
|
| 150 |
-
map_plot = gr.Plot()
|
| 151 |
-
|
| 152 |
-
gr.Markdown("### 📊 Tableau Résumé des Rapports")
|
| 153 |
-
summary_table = gr.DataFrame()
|
| 154 |
-
|
| 155 |
-
gr.Markdown("### 📋 Tableau Détaillé des Foyers (avec Coordonnées GPS)")
|
| 156 |
-
outbreaks_table = gr.DataFrame()
|
| 157 |
-
|
| 158 |
-
gr.Markdown("### 💾 Télécharger le Package Complet")
|
| 159 |
-
download_section = gr.File(label="Package de Données (.zip)")
|
| 160 |
|
| 161 |
run_button.click(
|
| 162 |
fn=run_and_update_ui,
|
| 163 |
inputs=[],
|
| 164 |
-
outputs=[status_textbox,
|
| 165 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
demo.launch()
|
|
|
|
| 1 |
# Fichier: app.py
|
| 2 |
|
| 3 |
# ===================================================================================
|
| 4 |
+
# WAHIS SCRAPER - VERSION FINALE, SIMPLE ET ROBUSTE (FILTRES ET TABLEAU)
|
| 5 |
# ===================================================================================
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
| 16 |
import zipfile
|
| 17 |
from playwright.async_api import async_playwright
|
| 18 |
from playwright_stealth import stealth_async
|
|
|
|
| 19 |
|
| 20 |
# Installation du navigateur (ne change pas)
|
| 21 |
def install_playwright_browsers():
|
|
|
|
| 81 |
if browser and browser.is_connected(): await browser.close()
|
| 82 |
|
| 83 |
def process_data_and_create_zip(reports, outbreaks, additional_infos):
|
| 84 |
+
if not reports: return pd.DataFrame(), [], [], [], None
|
| 85 |
+
|
| 86 |
+
# Enrichir les données des foyers avec le nom de la maladie et du pays
|
| 87 |
+
report_map = {report['eventId']: {'disease': report['disease'], 'country': report['country']} for report in reports}
|
| 88 |
+
for outbreak in outbreaks:
|
| 89 |
+
event_info = report_map.get(outbreak.get('eventId'), {})
|
| 90 |
+
outbreak['disease'] = event_info.get('disease')
|
| 91 |
+
outbreak['country'] = event_info.get('country')
|
| 92 |
+
|
| 93 |
valid_additional_infos = [info for info in additional_infos if isinstance(info, dict)]
|
| 94 |
additional_info_map = {info.get('outbreakId'): info for info in valid_additional_infos}
|
| 95 |
for outbreak in outbreaks:
|
| 96 |
outbreak_id = outbreak.get('outbreakId')
|
| 97 |
if outbreak_id in additional_info_map: outbreak.update(additional_info_map[outbreak_id])
|
| 98 |
+
|
| 99 |
df_outbreaks = pd.DataFrame(outbreaks)
|
| 100 |
+
|
| 101 |
+
# Créer les listes pour les filtres à partir des données complètes
|
| 102 |
+
all_countries = sorted(df_outbreaks['country'].dropna().unique()) if 'country' in df_outbreaks else []
|
| 103 |
+
all_diseases = sorted(df_outbreaks['disease'].dropna().unique()) if 'disease' in df_outbreaks else []
|
| 104 |
+
all_species = sorted(df_outbreaks['species'].dropna().unique()) if 'species' in df_outbreaks else []
|
| 105 |
+
|
| 106 |
+
# Créer le package ZIP
|
| 107 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 108 |
zip_path = OUTPUT_DIR / f"wahis_package_{timestamp}.zip"
|
| 109 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 110 |
+
df_summary = pd.DataFrame(reports)
|
| 111 |
df_summary.to_excel(OUTPUT_DIR / "1_summary_events.xlsx", index=False)
|
| 112 |
df_outbreaks.to_excel(OUTPUT_DIR / "2_outbreaks_full_details.xlsx", index=False)
|
| 113 |
zipf.write(OUTPUT_DIR / "1_summary_events.xlsx", arcname="1_summary_events.xlsx")
|
| 114 |
zipf.write(OUTPUT_DIR / "2_outbreaks_full_details.xlsx", arcname="2_outbreaks_full_details.xlsx")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
return df_outbreaks, ["Tous"] + all_countries, ["Toutes"] + all_diseases, ["Toutes"] + all_species, str(zip_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# --- Fonctions de l'interface Gradio ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
async def run_and_update_ui():
|
| 121 |
scraper = WAHISScraper()
|
| 122 |
reports, outbreaks, additional_infos, logs = await scraper.run_extraction_async()
|
| 123 |
if not reports:
|
| 124 |
+
return {status_textbox: logs, ui_visibility_group: gr.Group(visible=False)}
|
| 125 |
+
|
| 126 |
+
df_outbreaks, countries, diseases, species, zip_path = process_data_and_create_zip(reports, outbreaks, additional_infos)
|
| 127 |
+
|
| 128 |
+
return {
|
| 129 |
+
status_textbox: logs,
|
| 130 |
+
outbreak_data_state: df_outbreaks,
|
| 131 |
+
filter_country: gr.Dropdown(choices=countries, value="Tous"),
|
| 132 |
+
filter_disease: gr.Dropdown(choices=diseases, value="Toutes"),
|
| 133 |
+
filter_species: gr.Dropdown(choices=species, value="Toutes"),
|
| 134 |
+
outbreaks_table: df_outbreaks,
|
| 135 |
+
download_section: gr.File(value=zip_path, visible=True),
|
| 136 |
+
ui_visibility_group: gr.Group(visible=True)
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
def update_table(country, disease, species, df_outbreaks):
|
| 140 |
+
if df_outbreaks is None or df_outbreaks.empty: return pd.DataFrame()
|
| 141 |
+
filtered_df = df_outbreaks.copy()
|
| 142 |
+
if country != "Tous": filtered_df = filtered_df[filtered_df['country'] == country]
|
| 143 |
+
if disease != "Toutes": filtered_df = filtered_df[filtered_df['disease'] == disease]
|
| 144 |
+
if species != "Toutes": filtered_df = filtered_df[filtered_df['species'] == species]
|
| 145 |
+
return filtered_df
|
| 146 |
|
| 147 |
with gr.Blocks(theme=gr.themes.Soft(), title="WAHIS Scraper") as demo:
|
| 148 |
+
outbreak_data_state = gr.State()
|
| 149 |
gr.Markdown("# 🤖 Scraper pour WAHIS (WOAH) - Version Robuste")
|
| 150 |
run_button = gr.Button("🚀 Lancer l'extraction des données", variant="primary")
|
| 151 |
|
| 152 |
+
with gr.Group(visible=False) as ui_visibility_group:
|
| 153 |
+
gr.Markdown("### 🔍 Filtrez les données")
|
| 154 |
+
with gr.Row():
|
| 155 |
+
filter_country = gr.Dropdown(label="Pays")
|
| 156 |
+
filter_disease = gr.Dropdown(label="Maladie")
|
| 157 |
+
filter_species = gr.Dropdown(label="Espèce")
|
| 158 |
+
gr.Markdown("### 📋 Tableau Détaillé des Foyers (avec Coordonnées GPS)")
|
| 159 |
+
outbreaks_table = gr.DataFrame(wrap=True)
|
| 160 |
+
|
| 161 |
+
with gr.Accordion("Journal d'exécution et Téléchargement", open=False):
|
| 162 |
status_textbox = gr.Textbox(lines=15, label="📜 Logs", interactive=False)
|
| 163 |
+
download_section = gr.File(label="💾 Télécharger le Package Complet (.zip)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
run_button.click(
|
| 166 |
fn=run_and_update_ui,
|
| 167 |
inputs=[],
|
| 168 |
+
outputs=[status_textbox, ui_visibility_group, outbreak_data_state, filter_country, filter_disease, filter_species, outbreaks_table, download_section]
|
| 169 |
)
|
| 170 |
+
|
| 171 |
+
filters = [filter_country, filter_disease, filter_species]
|
| 172 |
+
for f in filters:
|
| 173 |
+
f.change(
|
| 174 |
+
fn=update_table,
|
| 175 |
+
inputs=[filter_country, filter_disease, filter_species, outbreak_data_state],
|
| 176 |
+
outputs=[outbreaks_table]
|
| 177 |
+
)
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
demo.launch()
|