-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (39 loc) · 1.21 KB
/
Copy pathmain.py
File metadata and controls
47 lines (39 loc) · 1.21 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import BertTokenizer
from typing import List, Dict
app = FastAPI()
class PromptRequest(BaseModel):
prompt: str
class TokenAnalysis(BaseModel):
tokens: List[str]
token_ids: List[int]
attention_mask: List[int]
token_type_ids: List[int]
special_tokens: Dict[str, int]
@app.post("/tokenize/")
async def tokenize(prompt: PromptRequest):
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
tokens = tokenizer.tokenize(prompt.prompt)
encoding = tokenizer.encode_plus(
prompt.prompt,
add_special_tokens=True,
padding='max_length',
max_length=512,
truncation=True,
return_attention_mask=True,
return_token_type_ids=True
)
special_tokens = {
'CLS': tokenizer.cls_token_id,
'SEP': tokenizer.sep_token_id,
'PAD': tokenizer.pad_token_id,
'UNK': tokenizer.unk_token_id
}
return TokenAnalysis(
tokens=tokens,
token_ids=encoding['input_ids'],
attention_mask=encoding['attention_mask'],
token_type_ids=encoding['token_type_ids'],
special_tokens=special_tokens
)