File size: 7,146 Bytes
6ae1fb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1f175efa",
   "metadata": {},
   "source": [
    "# Latin Interpunctuator\n",
    "\n",
    "This notebook demonstrates how to use the mt5 model `mschonhardt/mt5-latin-punctuator-large`.\n",
    "It applies interpunctuation and text formatting standards to Latin text.\n",
    "\n",
    "## Setup Environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "044ae4ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Torch version: 2.10.0+cu128\n",
      "Device: cuda\n",
      "Environment ready.\n"
     ]
    }
   ],
   "source": [
    "# Import necessary libraries\n",
    "import torch\n",
    "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",
    "\n",
    "# Model should be used with GPU (cuda) if available for faster inference\n",
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "\n",
    "print(f\"Torch version: {torch.__version__}\")\n",
    "print(f\"Device: {device}\")\n",
    "\n",
    "print(\"Environment ready.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4de2def2",
   "metadata": {},
   "source": [
    "## Load the Model from Hugging Face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "aa5810a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading model: mschonhardt/mt5-latin-punctuator-large ...\n",
      "Model loaded successfully!\n"
     ]
    }
   ],
   "source": [
    "# Load the model and tokenizer from Huggingface\n",
    "model_name = \"mschonhardt/mt5-latin-punctuator-large\" \n",
    "print(f\"Loading model: {model_name} ...\")\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)\n",
    "model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)\n",
    "print(\"Model loaded successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dd05d72",
   "metadata": {},
   "source": [
    "### Prediction Logic\n",
    "Model was trained on prefix \"punctuate: \". `Num_beams` needs to be adjusted when running into hallucinations or repetitions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "e858df99",
   "metadata": {},
   "outputs": [],
   "source": [
    "def punctuate(text: str) -> str:\n",
    "    # Best practice: Add prefix 'punctuate: 'and lowercase as per training script\n",
    "    input_text = \"punctuate: \" + text.lower()\n",
    "    \n",
    "    inputs = tokenizer(\n",
    "        input_text,\n",
    "        return_tensors=\"pt\",\n",
    "        truncation=True,\n",
    "        max_length=1024,\n",
    "    ).to(device)\n",
    "\n",
    "    with torch.no_grad():\n",
    "        output_ids = model.generate(\n",
    "            **inputs,\n",
    "            max_length=1024,\n",
    "            # Adjust numbeams if hallucination occurs, but 4 is a good starting point for better punctuation\n",
    "            num_beams=4,\n",
    "            early_stopping=True,\n",
    "        )\n",
    "    return tokenizer.decode(output_ids[0], skip_special_tokens=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "52fd09e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "Si quis Patrem et Filium et Spiritum Sanctum non confitetur tres personas unius substantiae et virtutis ac potestatis, \n",
    "sicut catholica et apostolica ecclesia docet, sed unam tantum ac solitariam dicit esse personam, \n",
    "ita ut ipse sit Pater qui Filius, ipse etiam sit Paraclitus Spiritus, sicut Sabellius et Priscillianus dixerunt, anathema sit.\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e582b0e4",
   "metadata": {},
   "source": [
    "Model was trained on lower case input to prevent overfitting on capital letters and force learning of linguistic pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "6573900a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " si quis patrem et filium et spiritum sanctum non confitetur tres personas unius\n",
      "substantiae et virtutis ac potestatis  sicut catholica et apostolica ecclesia\n",
      "docet sed unam tantum ac solitariam dicit esse personam  ita ut ipse sit pater\n",
      "qui filius ipse etiam sit paraclitus spiritus sicut sabellius et priscillianus\n",
      "dixerunt anathema sit\n"
     ]
    }
   ],
   "source": [
    "text_without_punctuation = text.replace(\".\",\"\").replace(\",\",\"\").replace(\";\",\"\").replace(\":\",\"\").replace(\"?\",\"\").replace(\"!\",\"\").replace(\"-\",\"\").replace(\"(\",\"\").replace(\")\",\"\").replace(\"[\",\"\").replace(\"]\",\"\").replace(\"{\",\"\").replace(\"}\",\"\").replace(\"\\\"\",\"\")\n",
    "text_without_punctuation = text_without_punctuation.lower()\n",
    "import textwrap\n",
    "print(textwrap.fill(text_without_punctuation, width=80))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "843757c0",
   "metadata": {},
   "source": [
    "### Run Inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "86c7521d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Model will predict punctuation for the input text as well as appropriate use of capital letters\n",
    "# Note: The model will reflect conventions of material it has seen, which might differ from your expectations.\n",
    "text_with_punctuation = punctuate(text_without_punctuation)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ef7e70",
   "metadata": {},
   "source": [
    "As the model does apply conventions it has learned from training data, the models decision might differ from your own conventions and expectations. It has not been designed to prepare a 'perfect' text, but to provide structure to unstrucutred text enabling downstream tasks,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "1c908d35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Si quis Patrem et Filium et Spiritum sanctum non confitetur tres personas unius\n",
      "substantiae et virtutis ac potestatis, sicut catholica et apostolica Ecclesia\n",
      "docet, sed unam tantum ac solitariam dicit esse personam, ita ut ipse sit Pater\n",
      "qui Filius, ipse etiam sit Paraclitus Spiritus, sicut Sabellius et Priscillianus\n",
      "dixerunt, anathema sit.\n"
     ]
    }
   ],
   "source": [
    "print(textwrap.fill(text_with_punctuation, width=80))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv-jupyter",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}