|
import gradio as gr |
|
import torch |
|
from peft import PeftModel, PeftConfig |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
peft_model_id = "hackathon-somos-nlp-2023/bertin-gpt-j-6b-ner-es" |
|
config = PeftConfig.from_pretrained(peft_model_id) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
config.base_model_name_or_path, |
|
return_dict=True, |
|
load_in_8bit=True, |
|
device_map="auto", |
|
revision="half", |
|
) |
|
tokenizer = AutoTokenizer.from_pretrained(peft_model_id) |
|
|
|
model = PeftModel.from_pretrained(model, peft_model_id) |
|
|
|
model.eval() |
|
|
|
|
|
def gen_entities(in_text): |
|
"""Does Named Entity Recognition in the given text.""" |
|
text = f"<SP> text: {in_text}\n\n entities:" |
|
batch = tokenizer(text, return_tensors="pt") |
|
batch["input_ids"] = batch["input_ids"].to("cuda") |
|
with torch.cuda.amp.autocast(): |
|
output_tokens = model.generate(**batch, max_new_tokens=256, eos_token_id=50258) |
|
|
|
response = tokenizer.batch_decode( |
|
output_tokens.detach().cpu().numpy(), skip_special_tokens=False |
|
)[0] |
|
|
|
return response[response.find("entities") : response.find("<EP>")] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=gen_entities, |
|
inputs="text", |
|
outputs="text", |
|
title="Podcast Named Entity Recognition", |
|
description="Introduce un texto corto para que el modelo identifique las identidades presentes en el mismo.", |
|
theme="gradio/monochrome", |
|
examples=[ |
|
[ |
|
"Yo hoy voy a hablar de mujeres en el mundo del arte, porque me ha leído un libro fantástico que se llama Historia del arte sin hombres, de Katie Hesel." |
|
] |
|
], |
|
) |
|
iface.launch() |
|
|