Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
import re
|
| 3 |
import unicodedata
|
| 4 |
import io
|
|
@@ -10,6 +9,7 @@ from transformers import (
|
|
| 10 |
AutoTokenizer,
|
| 11 |
AutoModelForTokenClassification,
|
| 12 |
AutoModelForCausalLM,
|
|
|
|
| 13 |
)
|
| 14 |
from peft import PeftModel
|
| 15 |
|
|
@@ -36,37 +36,44 @@ ID2LABEL = {0: "O", 1: "B-TIMEX", 2: "I-TIMEX"}
|
|
| 36 |
BASE_ID = "google/gemma-2b-it"
|
| 37 |
ADAPTER_ID = "Rhulli/gemma-2b-it-TIMEX3"
|
| 38 |
|
| 39 |
-
# ---
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def load_models():
|
| 43 |
-
#
|
| 44 |
-
ner_tok = AutoTokenizer.from_pretrained(NER_ID
|
| 45 |
-
ner_mod = AutoModelForTokenClassification.from_pretrained(NER_ID
|
| 46 |
ner_mod.eval()
|
| 47 |
if torch.cuda.is_available():
|
| 48 |
ner_mod.to("cuda")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
norm_tok = AutoTokenizer.from_pretrained(ADAPTER_ID, use_fast=True, token=HF_TOKEN)
|
| 59 |
norm_mod = PeftModel.from_pretrained(
|
| 60 |
base_mod,
|
| 61 |
ADAPTER_ID,
|
| 62 |
-
device_map="auto"
|
| 63 |
-
token=HF_TOKEN
|
| 64 |
)
|
| 65 |
norm_mod.eval()
|
| 66 |
|
| 67 |
return ner_tok, ner_mod, norm_tok, norm_mod
|
| 68 |
|
| 69 |
-
# Carga inicial de los modelos
|
| 70 |
ner_tok, ner_mod, norm_tok, norm_mod = load_models()
|
| 71 |
eos_id = norm_tok.convert_tokens_to_ids("<end_of_turn>")
|
| 72 |
|
|
@@ -89,7 +96,7 @@ def read_file(file_obj) -> str:
|
|
| 89 |
except:
|
| 90 |
return data.decode('latin-1', errors='ignore')
|
| 91 |
|
| 92 |
-
# --- Procesamiento
|
| 93 |
def extract_timex(text: str):
|
| 94 |
text_norm = _normalise_spaces(_normalise_apostrophes(text))
|
| 95 |
inputs = ner_tok(text_norm, return_tensors="pt", truncation=True)
|
|
@@ -176,20 +183,69 @@ with gr.Blocks() as demo:
|
|
| 176 |
|
| 177 |
Esta aplicaci贸n permite extraer expresiones temporales de textos o archivos (.txt, .pdf)
|
| 178 |
y normalizarlas a formato TIMEX3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
"""
|
| 180 |
)
|
| 181 |
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column(scale=1):
|
| 184 |
-
files = gr.File(
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
run_btn = gr.Button("Procesar")
|
|
|
|
| 187 |
with gr.Column(scale=2):
|
| 188 |
-
raw_text = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import unicodedata
|
| 3 |
import io
|
|
|
|
| 9 |
AutoTokenizer,
|
| 10 |
AutoModelForTokenClassification,
|
| 11 |
AutoModelForCausalLM,
|
| 12 |
+
BitsAndBytesConfig,
|
| 13 |
)
|
| 14 |
from peft import PeftModel
|
| 15 |
|
|
|
|
| 36 |
BASE_ID = "google/gemma-2b-it"
|
| 37 |
ADAPTER_ID = "Rhulli/gemma-2b-it-TIMEX3"
|
| 38 |
|
| 39 |
+
# --- Configuraci贸n de cuantizaci贸n ---
|
| 40 |
+
quant_config = BitsAndBytesConfig(
|
| 41 |
+
load_in_4bit=True,
|
| 42 |
+
bnb_4bit_quant_type="nf4",
|
| 43 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 44 |
+
)
|
| 45 |
|
| 46 |
def load_models():
|
| 47 |
+
# Modelo NER
|
| 48 |
+
ner_tok = AutoTokenizer.from_pretrained(NER_ID)
|
| 49 |
+
ner_mod = AutoModelForTokenClassification.from_pretrained(NER_ID)
|
| 50 |
ner_mod.eval()
|
| 51 |
if torch.cuda.is_available():
|
| 52 |
ner_mod.to("cuda")
|
| 53 |
|
| 54 |
+
# Modelo de normalizaci贸n (solo 4bit si hay GPU)
|
| 55 |
+
if torch.cuda.is_available():
|
| 56 |
+
base_mod = AutoModelForCausalLM.from_pretrained(
|
| 57 |
+
BASE_ID,
|
| 58 |
+
quantization_config=quant_config,
|
| 59 |
+
device_map="auto"
|
| 60 |
+
)
|
| 61 |
+
else:
|
| 62 |
+
base_mod = AutoModelForCausalLM.from_pretrained(
|
| 63 |
+
BASE_ID,
|
| 64 |
+
device_map="auto"
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
norm_tok = AutoTokenizer.from_pretrained(ADAPTER_ID, use_fast=True)
|
|
|
|
| 68 |
norm_mod = PeftModel.from_pretrained(
|
| 69 |
base_mod,
|
| 70 |
ADAPTER_ID,
|
| 71 |
+
device_map="auto"
|
|
|
|
| 72 |
)
|
| 73 |
norm_mod.eval()
|
| 74 |
|
| 75 |
return ner_tok, ner_mod, norm_tok, norm_mod
|
| 76 |
|
|
|
|
| 77 |
ner_tok, ner_mod, norm_tok, norm_mod = load_models()
|
| 78 |
eos_id = norm_tok.convert_tokens_to_ids("<end_of_turn>")
|
| 79 |
|
|
|
|
| 96 |
except:
|
| 97 |
return data.decode('latin-1', errors='ignore')
|
| 98 |
|
| 99 |
+
# --- Procesamiento ---
|
| 100 |
def extract_timex(text: str):
|
| 101 |
text_norm = _normalise_spaces(_normalise_apostrophes(text))
|
| 102 |
inputs = ner_tok(text_norm, return_tensors="pt", truncation=True)
|
|
|
|
| 183 |
|
| 184 |
Esta aplicaci贸n permite extraer expresiones temporales de textos o archivos (.txt, .pdf)
|
| 185 |
y normalizarlas a formato TIMEX3.
|
| 186 |
+
|
| 187 |
+
**C贸mo usar:**
|
| 188 |
+
- Sube uno o varios archivos en la columna izquierda.
|
| 189 |
+
- Ajusta la *Fecha de Anclaje (DCT)* justo debajo de los archivos.
|
| 190 |
+
- Escribe o pega tu texto en la columna derecha.
|
| 191 |
+
- Pulsa **Procesar** para ver los resultados en la tabla debajo.
|
| 192 |
+
|
| 193 |
+
**Columnas de salida:**
|
| 194 |
+
- *Expresi贸n*: la frase temporal extra铆da.
|
| 195 |
+
- *Normalizaci贸n*: la etiqueta TIMEX3 generada.
|
| 196 |
"""
|
| 197 |
)
|
| 198 |
|
| 199 |
with gr.Row():
|
| 200 |
with gr.Column(scale=1):
|
| 201 |
+
files = gr.File(
|
| 202 |
+
file_types=['.txt', '.pdf'],
|
| 203 |
+
file_count='multiple',
|
| 204 |
+
label='Archivos (.txt, .pdf)'
|
| 205 |
+
)
|
| 206 |
+
dct_input = gr.Textbox(
|
| 207 |
+
value="2025-06-11",
|
| 208 |
+
label="Fecha de Anclaje (YYYY-MM-DD)"
|
| 209 |
+
)
|
| 210 |
run_btn = gr.Button("Procesar")
|
| 211 |
+
download_btn = gr.Button("Descargar CSV")
|
| 212 |
with gr.Column(scale=2):
|
| 213 |
+
raw_text = gr.Textbox(
|
| 214 |
+
lines=15,
|
| 215 |
+
placeholder='Pega o escribe aqu铆 tu texto... (opcional si subes archivos)',
|
| 216 |
+
label='Texto libre'
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
output_table = gr.Dataframe(
|
| 220 |
+
headers=['Expresi贸n', 'Normalizaci贸n'],
|
| 221 |
+
label="Resultados",
|
| 222 |
+
interactive=False,
|
| 223 |
+
datatype=["str", "str"],
|
| 224 |
+
type="pandas"
|
| 225 |
+
)
|
| 226 |
+
output_logs = gr.Textbox(
|
| 227 |
+
label="Logs",
|
| 228 |
+
lines=5,
|
| 229 |
+
interactive=False
|
| 230 |
+
)
|
| 231 |
+
csv_file_output = gr.File(label="Descargar resultados en CSV", visible=False)
|
| 232 |
|
| 233 |
+
run_btn.click(
|
| 234 |
+
fn=run_pipeline,
|
| 235 |
+
inputs=[files, raw_text, dct_input],
|
| 236 |
+
outputs=[output_table, output_logs]
|
| 237 |
+
)
|
| 238 |
|
| 239 |
+
def export_csv(df):
|
| 240 |
+
csv_io = io.StringIO()
|
| 241 |
+
df.to_csv(csv_io, index=False)
|
| 242 |
+
csv_io.seek(0)
|
| 243 |
+
return gr.File.update(value=csv_io, visible=True)
|
| 244 |
+
|
| 245 |
+
download_btn.click(
|
| 246 |
+
fn=export_csv,
|
| 247 |
+
inputs=[output_table],
|
| 248 |
+
outputs=csv_file_output
|
| 249 |
+
)
|
| 250 |
|
| 251 |
demo.launch()
|