Demo for the tiny
, base
and large
variant of ALBERT models trained to understand Bahasa Malaysia. Type a sentence in BM, and ask ALBERT to guess a word masked with '[MASK]'. Inputs are case sensitive.
Models are made available by Malaysia AI on HuggingFace:
Code for more BM NLP demos hosted on github.
Gradio supports directly creating an interface for models hosted on HuggingFace. In fact, this is how this application is put together!
import gradio as gr
iface = gr.Interface.load(
# replace w/ the intended model variant
name="malay-huggingface/albert-base-bahasa-cased", src="huggingface"
)
iface.launch()
Alternatively, these models are also accessible directly through HuggingFace's Inference API, allowing easy integration w/ your own applications. Note that if the API wasn't called recently, HuggingFace will need about half a minute to load the models for a cold start.
import requests
# Sign up w/ HuggingFace to get a API token
# ref: https://api-inference.huggingface.co/docs/python/html/quicktour.html
API_TOKEN = ...
# Replace URL string w/ the intended model variant
API_URL = "https://api-inference.huggingface.co/models/malay-huggingface/albert-base-bahasa-cased"
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# Text input to model
prompt = "Cuaca terlalu panas untuk bermain [MASK] di padang bersama kawan-kawan."
# Post API request
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
output = response.json()
# Output format is a List[Dict]:
# [
# {
# "sequence": "Cuaca terlalu panas untuk bermain permainan di padang bersama kawan - kawan.",
# "score": 0.24029025435447693,
# "token": 5699,
# "token_str": "permainan"
# },
# ...
# ]
If your use case requires local inferencing, usage via HuggingFace is quite straightforward:
# Code adapted from model card at
# https://huggingface.co/malay-huggingface/albert-tiny-bahasa-cased
from transformers import pipeline
# Model-specific and task-specific imports
# In this example we are importing the LM model class
# for BERT to perform mask filling
from transformers import BertTokenizer, BertForMaskedLM
# Inference pipeline setup
# Will download models when run for the first time
# On Linux, models are stored in ~/.cache/huggingface/transformers
model_name = "malay-huggingface/albert-base-bahasa-cased"
model = BertForMaskedLM.from_pretrained(model_name)
tokenizer = BertTokenizer.from_pretrained(
model_name, do_lower_case = False,
)
fill_mask = pipeline('fill-mask', model=model, tokenizer=tokenizer)
# Inferencing
prompt = "Cuaca terlalu panas untuk bermain [MASK] di padang bersama kawan-kawan."
output = fill_mask(prompt)
# Output format is a List[Dict]:
# [
# {
# "sequence": "Cuaca terlalu panas untuk bermain permainan di padang bersama kawan - kawan.",
# "score": 0.24029025435447693,
# "token": 5699,
# "token_str": "p e r m a i n a n"
# },
# ...
# ]
Refer to transformers
docs and the hosted model repos linked above for more info.