Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
import zipfile, os, re
|
5 |
+
import spacy
|
6 |
+
|
7 |
+
# ================================
|
8 |
+
# Cargar spaCy (modelo espa帽ol)
|
9 |
+
# ================================
|
10 |
+
try:
|
11 |
+
nlp = spacy.load("es_core_news_sm")
|
12 |
+
except OSError:
|
13 |
+
import subprocess
|
14 |
+
subprocess.run(["python", "-m", "spacy", "download", "es_core_news_sm"])
|
15 |
+
nlp = spacy.load("es_core_news_sm")
|
16 |
+
|
17 |
+
# ================================
|
18 |
+
# Funci贸n de extracci贸n
|
19 |
+
# ================================
|
20 |
+
def extraer_info_pregunta1(text, nombre_reporte="reporte.pdf"):
|
21 |
+
doc = nlp(text)
|
22 |
+
|
23 |
+
# Ubicaciones
|
24 |
+
ubicaciones = [ent.text.strip() for ent in doc.ents if ent.label_ in ["LOC", "GPE"]]
|
25 |
+
ubicaciones_final = list(set([re.sub(r"\n", " ", u) for u in ubicaciones]))
|
26 |
+
|
27 |
+
# Fechas
|
28 |
+
regex_patterns = [
|
29 |
+
r"\b\d{1,2}\s+de\s+(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)\s+\d{4}\b",
|
30 |
+
r"\b(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)\s+\d{4}\b",
|
31 |
+
r"\b\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b"
|
32 |
+
]
|
33 |
+
fechas_final = []
|
34 |
+
for pattern in regex_patterns:
|
35 |
+
fechas_final.extend(re.findall(pattern, text, flags=re.IGNORECASE))
|
36 |
+
|
37 |
+
# Tipos de incidente
|
38 |
+
keywords_incidentes = {
|
39 |
+
"Inundaci贸n": ["inundaci贸n", "lluvia", "r铆o", "desbordamiento"],
|
40 |
+
"Desplazamiento": ["desplazamiento", "huida", "migraci贸n"],
|
41 |
+
"Salud": ["epidemia", "enfermedad", "hospital", "salud"],
|
42 |
+
"Seguridad": ["conflicto", "violencia", "ataque", "amenaza"]
|
43 |
+
}
|
44 |
+
tipos_detectados = []
|
45 |
+
for tipo, palabras in keywords_incidentes.items():
|
46 |
+
if any(p in text.lower() for p in palabras):
|
47 |
+
tipos_detectados.append(tipo)
|
48 |
+
|
49 |
+
return {
|
50 |
+
"Reporte": nombre_reporte,
|
51 |
+
"Ubicaciones": ", ".join(ubicaciones_final) if ubicaciones_final else "No detectadas",
|
52 |
+
"Fechas": ", ".join(set(fechas_final)) if fechas_final else "No detectadas",
|
53 |
+
"Tipos de incidente": ", ".join(tipos_detectados) if tipos_detectados else "No detectados"
|
54 |
+
}
|
55 |
+
|
56 |
+
# ================================
|
57 |
+
# Procesar PDFs o ZIP
|
58 |
+
# ================================
|
59 |
+
def analizar_reportes(file):
|
60 |
+
resultados = []
|
61 |
+
|
62 |
+
# ZIP
|
63 |
+
if file.name.endswith(".zip"):
|
64 |
+
with zipfile.ZipFile(file.name, 'r') as zip_ref:
|
65 |
+
zip_ref.extractall("tmp")
|
66 |
+
for fname in os.listdir("tmp"):
|
67 |
+
if fname.endswith(".pdf"):
|
68 |
+
pdf = PdfReader(os.path.join("tmp", fname))
|
69 |
+
text = "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
|
70 |
+
resultados.append(extraer_info_pregunta1(text, nombre_reporte=fname))
|
71 |
+
else:
|
72 |
+
pdf = PdfReader(file.name)
|
73 |
+
text = "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
|
74 |
+
resultados.append(extraer_info_pregunta1(text, nombre_reporte=os.path.basename(file.name)))
|
75 |
+
|
76 |
+
df = pd.DataFrame(resultados)
|
77 |
+
csv_path = "resultados.csv"
|
78 |
+
df.to_csv(csv_path, index=False)
|
79 |
+
return df, csv_path
|
80 |
+
|
81 |
+
# ================================
|
82 |
+
# Interfaz Gradio
|
83 |
+
# ================================
|
84 |
+
iface = gr.Interface(
|
85 |
+
fn=analizar_reportes,
|
86 |
+
inputs=gr.File(type="file", label="Sube un PDF o ZIP"),
|
87 |
+
outputs=[
|
88 |
+
gr.Dataframe(headers=["Reporte", "Ubicaciones", "Fechas", "Tipos de incidente"], label="Resultados"),
|
89 |
+
gr.File(label="馃摜 Descargar CSV")
|
90 |
+
],
|
91 |
+
title="馃搼 Analizador de Reportes Humanitarios",
|
92 |
+
description="Sube tus reportes en PDF o ZIP y obt茅n una tabla con Ubicaciones, Fechas y Tipos de incidente."
|
93 |
+
)
|
94 |
+
|
95 |
+
iface.launch()
|