Views
No views yet
| Tag | Definition |
|---|---|
| B-account-from | Start of the source account in a transaction. |
| I-account-from | Complement of the source account in a transaction. |
| B-account-to | Start of the target account in a transaction. |
| I-account-to | Complement of the target account in a transaction. |
| B-bill_type | Start of the type of bill or service. |
| I-bill_type | Complement of the type of bill or service. |
| B-transaction-from | Start of the origin of a transaction or fraud. |
| I-transaction-from | Complement of the origin of a transaction or fraud. |
| B-transaction-to | Start of the destination or end of a transaction or fraud. |
| I-transaction-to | Complement of the destination of a transaction or fraud. |
| B-amount | Start of a specified amount of money. |
| I-amount | Complement of a specified amount of money. |
| B-timeRange | Start of a specific time range or date. |
| I-timeRange | Complement of a specific time range or date. |
1!pip install torch transformers
2
3import os
4import requests
5import torch
6from transformers import BertForTokenClassification, BertTokenizerFast
7
8# URL y archivo para los slots
9slots_url = 'https://huggingface.co/andgonzalez/bert-uncased-slot-filling/raw/main/slots.txt'
10slots_file = 'slots.txt'
11device = "cpu"
12
13# Descargar y guardar los slots si no existen
14if not os.path.exists(slots_file):
15 response = requests.get(slots_url)
16 response.raise_for_status()
17 with open(slots_file, 'w') as file:
18 file.write(response.text)
19
20# Leer los slots
21with open(slots_file, 'r') as file:
22 slot_labels = file.read().splitlines()
23
24# Cargar el tokenizador y el modelo
25tokenizer = BertTokenizerFast.from_pretrained('andgonzalez/bert-uncased-slot-filling')
26model = BertForTokenClassification.from_pretrained('andgonzalez/bert-uncased-slot-filling')
27
28# Ejemplo
29sentence = "Transfer $500 from checking to student savings"
30
31inputs = tokenizer(sentence, truncation=True, padding='max_length', max_length=20, return_tensors="pt")
32inputs = {k: v.to(device) for k, v in inputs.items()}
33
34with torch.no_grad():
35 model.eval()
36 outputs = model(**inputs)
37
38# Procesar los logits para obtener predicciones
39logits = outputs.logits
40predictions = torch.argmax(logits, dim=2).squeeze().cpu().numpy()
41words = tokenizer.convert_ids_to_tokens(inputs["input_ids"].squeeze().cpu().numpy())
42
43# Inicializar skip_next
44skip_next = False
45
46# Formatear la oracion
47formatted_sentence = []
48for i, (word, pred) in enumerate(zip(words, predictions)):
49 if word not in ['[PAD]', '[SEP]', '[CLS]']:
50 label = slot_labels[pred]
51
52 if word == "$" and i + 1 < len(words) and words[i + 1].replace("##", "").isdigit():
53 next_word = words[i + 1].replace("##", "")
54 combined_word = word + next_word
55 formatted_word = f'[{combined_word}:{label}]'
56 formatted_sentence.append(formatted_word)
57 skip_next = True
58 elif skip_next:
59 skip_next = False
60 continue
61 elif not word.startswith("##"):
62 if label != 'O':
63 formatted_word = f'[{word}:{label}]'
64 else:
65 formatted_word = word
66 formatted_sentence.append(formatted_word)
67
68formatted_sentence = ' '.join(formatted_sentence)
69print(formatted_sentence)
70