{"id":15066201,"url":"https://github.com/writer/fitbert","last_synced_at":"2025-10-05T03:30:51.010Z","repository":{"id":39482598,"uuid":"193996429","full_name":"writer/fitbert","owner":"writer","description":"Use BERT to Fill in the Blanks","archived":true,"fork":false,"pushed_at":"2022-01-06T23:05:45.000Z","size":358,"stargazers_count":82,"open_issues_count":4,"forks_count":14,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-01-19T05:35:04.559Z","etag":null,"topics":["bert","fill-in-the-blank","nlp","python","pytorch"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/fitbert/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/writer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-27T00:39:59.000Z","updated_at":"2024-01-04T16:35:16.000Z","dependencies_parsed_at":"2022-07-04T08:44:38.913Z","dependency_job_id":null,"html_url":"https://github.com/writer/fitbert","commit_stats":null,"previous_names":["qordobacode/fitbert","writer/fitbert","writerai/fitbert"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writer%2Ffitbert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writer%2Ffitbert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writer%2Ffitbert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writer%2Ffitbert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/writer","download_url":"https://codeload.github.com/writer/fitbert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235356202,"owners_count":18976819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bert","fill-in-the-blank","nlp","python","pytorch"],"created_at":"2024-09-25T01:03:38.520Z","updated_at":"2025-10-05T03:30:45.562Z","avatar_url":"https://github.com/writer.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# FitBERT\n\n![buff bert](img/fitbert.png)\n\nFitBert ((F)ill (i)n (t)he blanks, (BERT)) is a library for using [BERT](https://arxiv.org/abs/1810.04805) to fill in the blank(s) in a section of text from a list of options. Here is the envisioned usecase for FitBert:\n\n1. A service (statistical model or something simpler) suggests replacements/corrections for a segment of text\n2. That service is specialized to a domain, and isn't good at the big picture, e.g. grammar\n3. That service passes the segment of text, with the words to be replaced identified, and the list of suggestions\n4. FitBert _crushes_ all but the best suggestion :muscle:\n\n[Blog post walkthrough](https://medium.com/@samhavens/introducing-fitbert-4b047af860fd)\n\n## Installation\n\n## License\n\nThis software is distributed under the Apache 2.0 license, except for the WordNet lemma data used for delemmatization, which is distributed with its original license, which is located in `./fitbert/data/LICENSE`.\n\n## From PyPi\n\n`pip install fitbert`\n\n## Usage\n\n[A Jupyter notebook with a short introduction is available here.](https://colab.research.google.com/drive/1WrYzy9l_arpnTlhCCKViiilPe4WKZJjq)\n\nFitBert will automatically use GPU if `torch.cuda.is_available()`. Or when you instantiate it, you can pass `FitBert(model_name=\"distilbert-base-uncased\", disable_gpu=True)`. Fastest batches are using distilbert on CPU with batch size one, maximum throughput is with GPU and larger batches.\n\n### Usage as a library / in a server\n\n```python\nfrom fitbert import FitBert\n\n\n# currently supported models: bert-large-uncased and distilbert-base-uncased\n# this takes a while and loads a whole big BERT into memory\nfb = FitBert()\n\nmasked_string = \"Why Bert, you're looking ***mask*** today!\"\noptions = ['buff', 'handsome', 'strong']\n\nranked_options = fb.rank(masked_string, options=options)\n# \u003e\u003e\u003e ['handsome', 'strong', 'buff']\n# or\nfilled_in = fb.fitb(masked_string, options=options)\n# \u003e\u003e\u003e \"Why Bert, you're looking handsome today!\"\n```\n\nWe commonly find ourselves knowing what verb to suggest, but not what conjugation:\n\n```python\nfrom fitbert import FitBert\n\n\nfb = FitBert()\n\nmasked_string = \"Why Bert, you're ***mask*** handsome today!\"\noptions = ['looks']\n\nfilled_in = fb.fitb(masked_string, options=options)\n# \u003e\u003e\u003e \"Why Bert, you're looking handsome today!\"\n\n# under the hood, we notice there is only one suggestion and act as if\n# fitb was called with delemmatize=True:\nfilled_in = fb.fitb(masked_string, options=options, delemmatize=True)\n```\n\nIf you are already using `pytorch_pretrained_bert.BertForMaskedLM`, or `transformers.BertForMaskedLM` and have an instance of BertForMaskedLM already instantiated, you can pass pass it in to reuse it:\n\n```python\nBLM = pytorch_pretrained_bert.BertForMaskedLM.from_pretrained(model_name)\n# or\nBLM = transfomers.BertForMaskedLM.from_pretrained(model_name)\nfb = FitBert(model=BLM)\n```\n\nYou can also have FitBert mask the string for you\n\n```python\nfrom fitbert import FitBert\n\n\nfb = FitBert()\n\nunmasked_string = \"Why Bert, you're looks handsome today!\"\nspan_to_mask = (17, 22)\nmasked_string, masked = fb.mask(unmasked_string, span_to_mask)\n# \u003e\u003e\u003e \"Why Bert, you're ***mask*** handsome today!\", 'looks'\n\n# you can set options = [masked] or use any List[str]\noptions = [masked]\n\nfilled_in = fb.fitb(masked_string, options=options)\n# \u003e\u003e\u003e \"Why Bert, you're looking handsome today!\"\n```\n\nand there is a convenience method for doing this:\n\n```python\nunmasked_string = \"Why Bert, you're looks handsome today!\"\nspan_to_mask = (17, 22)\n\nfilled_in = fb.mask_fitb(unmasked_string, span_to_mask)\n# \u003e\u003e\u003e \"Why Bert, you're looking handsome today!\"\n```\n\n### Client\n\nIf you are sending strings to a FitBert server, you need to either mask the string yourself, or identify the span you want masked:\n\n```python\nfrom fitbert import FitBert\n\ns = \"This might be justified as a means of signalling the connection between drunken driving and fatal accidents.\"\n\nbetter_string, span_to_change = MyRuleBasedNLPModel.remove_overly_fancy_language(s)\n\nassert better_string == \"This might be justified to signalling the connection between drunken driving and fatal accidents.\", \"Notice 'as a means of' became 'to', but we didn't re-conjuagte signalling, or fix the spelling mistake\"\n\nassert span_to_change == (27, 37), \"This span is the start and stop of the characters for the substring 'signalling'.\"\n\nmasked_string, replaced_substring = FitBert.mask(better_string, span_to_change)\n\nassert masked_string == \"This might be justified to ***mask*** the connection between drunken driving and fatal accidents.\"\n\nassert replaced_substring == \"signalling\"\n\nFitBertServer.fitb(masked_string, options=[replaced_substring])\n```\n\nThe benefit to doing this over masking yourself is that if the internally used masking token changes, you don't have to know about that. Also, you don't need to make an instance of FitBert, so you don't have to incur the cost of downloading a pretrained Bert model.\n\nHowever, you could also write your `CallFitBertServer` function to take an unmasked string and a span, something like:\n\n```python\nFitBertServer.mask_fitb(better_string, span_to_change)\n```\n\nAnd then not need to install `FitBert` in your client at all.\n\n## Development\n\nRun tests with `python -m pytest` or `python -m pytest -m \"not slow\"` to skip the 20 seconds of loading pretrained bert.\n\n### Acknowledgement\n\nThanks to [NodoBird](https://instagram.com/nodobird?igshid=lqt5h1uicxsy) for letting us use the awesome portrait of Bert depicted above.\n\n## Citing\n\nIf you use FitBERT in your research, please cite with the following BibText\n\n```bibtext\n@misc{havens2019fitbert,\n    title  = {Use BERT to Fill in the Blanks},\n    author = {Sam Havens and Aneta Stal},\n    url    = {https://github.com/Qordobacode/fitbert},\n    year   = {2019}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwriter%2Ffitbert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwriter%2Ffitbert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwriter%2Ffitbert/lists"}