File size: 965 Bytes
178c5c2
5bc7c1a
 
 
 
 
 
 
 
 
 
 
e8e65ad
5bc7c1a
 
 
 
 
178c5c2
fe4bf72
 
 
 
 
178c5c2
fe4bf72
5bc7c1a
fe4bf72
 
5bc7c1a
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
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)
# Load the Lora model
model = PeftModel.from_pretrained(model, peft_model_id)


def gen_entities(text):
    text = f"<SP> text: {text}\n\n entities: "
    batch = tokenizer(text, return_tensors="pt")
    with torch.cuda.amp.autocast():
        output_tokens = model.generate(**batch, max_new_tokens=256, eos_token_id=50258)

    return tokenizer.decode(output_tokens[0], skip_special_tokens=False)


iface = gr.Interface(fn=gen_entities, inputs="text", outputs="text")
iface.launch()