-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
68 lines (52 loc) · 1.75 KB
/
api.py
File metadata and controls
68 lines (52 loc) · 1.75 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import re
import torch
import torch.nn.functional as F
from fastapi import FastAPI
from transformers import AutoTokenizer, AutoModelForTokenClassification
app = FastAPI(title="POS Tagging API")
MODEL_DIR = "/content/drive/MyDrive/deep_learning/checkpoint-625"
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
model = AutoModelForTokenClassification.from_pretrained(MODEL_DIR)
model.eval()
# tokenizer berbasis regex (kata, angka desimal, tanda baca)
def tokenize_words(sentence: str):
return re.findall(r"\d+\.\d+|\w+|[^\w\s]", sentence)
def predict_pos(sentence: str):
# tokenisasi linguistik
words = tokenize_words(sentence)
inputs = tokenizer(
words,
is_split_into_words=True,
return_tensors="pt"
)
with torch.no_grad():
outputs = model(**inputs)
# softmax → probabilities
probs = F.softmax(outputs.logits, dim=-1)
predictions = torch.argmax(outputs.logits, dim=2)
id2label = model.config.id2label
word_ids = inputs.word_ids(batch_index=0)
previous_word_idx = None
results = []
for idx, word_idx in enumerate(word_ids):
if word_idx is None or word_idx == previous_word_idx:
continue
label_id = predictions[0][idx].item()
confidence = probs[0][idx][label_id].item()
results.append({
"word": words[word_idx],
"label": id2label[label_id],
"confidence": round(confidence, 4)
})
previous_word_idx = word_idx
return results
@app.get("/")
def home():
return {
"message": "POS Tagging API is running",
"usage": "/predict?sentence=input",
"docs": "/docs"
}
@app.get("/predict")
def predict(sentence: str):
return {"result": predict_pos(sentence)}