Instructions to use amankhandelia/panini with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use amankhandelia/panini with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="amankhandelia/panini")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("amankhandelia/panini") model = AutoModelForMaskedLM.from_pretrained("amankhandelia/panini", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download utils.py from amankhandelia/panini: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/amankhandelia/panini/resolve/main/utils.py
- Command line
-
hf download hf://amankhandelia/panini/utils.py
-
curl -L -o utils.py https://huggingface.co/amankhandelia/panini/resolve/main/utils.py
1.75 kB
| import regex as re | |
| import string | |
| def keep_devnagri(text:str): | |
| """ | |
| Remove all non Devnagri characters from the text. | |
| Code adapted from https://huggingface.co/flax-community/roberta-base-mr/blob/64d2c745f264f09c3d5b678a718746b2613887db/mr_clean_text.py | |
| @param text: str Text to be cleaned | |
| @return: Union[str, bool] | |
| """ | |
| pattern = r'[\p{Devanagari}0-9।\s\.\!]+' | |
| # regex pattern for all puntuation symbols | |
| punctuation_regex = re.compile("[" + re.escape(string.punctuation) + string.digits + "|" + "]") | |
| # keep only the text which is in devnagari script | |
| cleaned = "".join([tok.group() for tok in re.finditer(pattern, text)]) | |
| # remove any extra space between words | |
| cleaned = re.sub(r"[ ]+", " ", cleaned) | |
| # identify if the clean text only consists of punctuation | |
| is_just_punctuation = len(re.sub(punctuation_regex, "", cleaned)) == 0 | |
| return cleaned, is_just_punctuation | |
| def keep_devnagri_hf_doc(document): | |
| if isinstance(document['text'], str): | |
| batched = False | |
| elif isinstance(document['text'], list): | |
| batched = True | |
| else: | |
| raise TypeError("Document must be a dictionary or list.") | |
| def get_clean_text(text): | |
| cleaned_text, is_just_punctuation = keep_devnagri(text) | |
| # to handle the tokenizer as empty string may cause issues | |
| # also this only happens for 5 out of 10000 docs, should not | |
| # affect the results | |
| cleaned_text = cleaned_text if not is_just_punctuation else " " | |
| return cleaned_text | |
| if batched: | |
| document['text'] = [get_clean_text(text) for text in document['text']] | |
| else: | |
| document['text'] = get_clean_text(document['text']) | |
| return document |