File size: 3,230 Bytes
7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae 8db9176 7f364ae | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Proofbird grammar model (Italian): quick start\n",
"\n",
"Run [`proofbird/gector-bert-it`](https://huggingface.co/proofbird/gector-bert-it) in a few lines. It is small (about 450 MB), runs on a CPU in about\n",
"30 ms a sentence, and needs no API key."
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"!pip install -q onnxruntime tokenizers huggingface_hub"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Load the model and correct a sentence\n",
"\n",
"`quickstart.py`, published with the model, downloads it, runs it, and returns the corrected text plus one suggestion per\n",
"change (character position, original, replacement)."
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"import importlib.util\n",
"from huggingface_hub import hf_hub_download\n",
"\n",
"path = hf_hub_download(\"proofbird/gector-bert-it\", \"quickstart.py\")\n",
"spec = importlib.util.spec_from_file_location(\"quickstart\", path)\n",
"quickstart = importlib.util.module_from_spec(spec)\n",
"spec.loader.exec_module(quickstart)\n",
"\n",
"model = quickstart.Gector(\"proofbird/gector-bert-it\")\n",
"corrected, suggestions = model.correct(\"La ragazza sono molto stanco.\")\n",
"print(corrected)\n",
"suggestions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. See every pass\n",
"\n",
"The model does not write text. For every word it picks one of 5 000 edits; the corrected sentence goes back in until\n",
"nothing changes, so connected errors are fixed one after the other. `verbose=True` prints what each pass sent and got\n",
"back."
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"corrected, suggestions = model.correct(\"La ragazza sono molto stanco.\", verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Your own text\n",
"\n",
"`model.passes` holds the same per-pass detail as a list of dictionaries, if you want to build on it."
]
},
{
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": [],
"source": [
"text = \"Le mie sorelle è molto simpatico.\"\n",
"print(model.correct(text)[0])\n",
"model.passes[0][\"words\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"Published by Patrick Andrew Hartnett. The [Proofbird](https://proofbird.app) writing assistant for Windows uses this\n",
"model on-device and adds the guards in `gector-guards.json` and an explanation for each change. If you build on this\n",
"model, a link back is appreciated."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
},
"colab": {
"provenance": []
}
},
"nbformat": 4,
"nbformat_minor": 5
} |