{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# In this notebook I show you how to use the model to make punctuation. If you find this useful, please upvote this notebook and data set!","metadata":{}},{"cell_type":"code","source":"!pip install --no-index --find-links=\"/kaggle/input/punctuator\" punctuators","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:24:53.024627Z","iopub.execute_input":"2023-10-09T21:24:53.025327Z","iopub.status.idle":"2023-10-09T21:25:05.461452Z","shell.execute_reply.started":"2023-10-09T21:24:53.025294Z","shell.execute_reply":"2023-10-09T21:25:05.460610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cp -r \"/kaggle/input/punctuationmodel/huggingface\" \"../../root/.cache\"","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:25:05.462882Z","iopub.execute_input":"2023-10-09T21:25:05.463146Z","iopub.status.idle":"2023-10-09T21:25:24.681302Z","shell.execute_reply.started":"2023-10-09T21:25:05.463122Z","shell.execute_reply":"2023-10-09T21:25:24.680054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from punctuators.models import PunctCapSegModelONNX\nimport pandas as pd\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:26:25.296860Z","iopub.execute_input":"2023-10-09T21:26:25.297679Z","iopub.status.idle":"2023-10-09T21:26:25.301912Z","shell.execute_reply.started":"2023-10-09T21:26:25.297649Z","shell.execute_reply":"2023-10-09T21:26:25.300803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = PunctCapSegModelONNX.from_pretrained(\"1-800-BAD-CODE/xlm-roberta_punctuation_fullstop_truecase\")","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:26:25.489327Z","iopub.execute_input":"2023-10-09T21:26:25.490040Z","iopub.status.idle":"2023-10-09T21:26:30.560551Z","shell.execute_reply.started":"2023-10-09T21:26:25.490006Z","shell.execute_reply":"2023-10-09T21:26:30.559425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/bengaliai-speech/train.csv\")\ntrain = train.sample(10000)\n### select only with punct\ntrain = train[train['sentence'].str[-1].isin([\"?\",\"!\"])]\n### remove punct from sentence\ntrain['sentence_without_punct'] = train['sentence'].str[:-1]\n### use punct model\nresults =  model.infer(texts=train['sentence_without_punct'].tolist(), apply_sbd=True)\ntrain['sentence_with_model_punct'] = results\ntrain['sentence_with_model_punct'] = train['sentence_with_model_punct'].map(lambda x: x[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:29:07.378664Z","iopub.execute_input":"2023-10-09T21:29:07.379047Z","iopub.status.idle":"2023-10-09T21:29:39.171058Z","shell.execute_reply.started":"2023-10-09T21:29:07.379010Z","shell.execute_reply":"2023-10-09T21:29:39.169988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### get the punct from the ground truth and compare with model punct\ntrain['sentence_last_punct'] = train['sentence'].str[-1]\ntrain['sentence_model_last_punct'] = train['sentence_with_model_punct'].str[-1]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:29:39.175763Z","iopub.execute_input":"2023-10-09T21:29:39.176440Z","iopub.status.idle":"2023-10-09T21:29:39.186510Z","shell.execute_reply.started":"2023-10-09T21:29:39.176404Z","shell.execute_reply":"2023-10-09T21:29:39.185265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:29:39.187891Z","iopub.execute_input":"2023-10-09T21:29:39.188264Z","iopub.status.idle":"2023-10-09T21:29:39.211825Z","shell.execute_reply.started":"2023-10-09T21:29:39.188232Z","shell.execute_reply":"2023-10-09T21:29:39.210544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['sentence_model_last_punct'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:29:39.214153Z","iopub.execute_input":"2023-10-09T21:29:39.214836Z","iopub.status.idle":"2023-10-09T21:29:39.227212Z","shell.execute_reply.started":"2023-10-09T21:29:39.214796Z","shell.execute_reply":"2023-10-09T21:29:39.226296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### High precision on ? is quite interesting and can be useful. \nprint(classification_report(train['sentence_last_punct'], train['sentence_model_last_punct']))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T21:29:39.228716Z","iopub.execute_input":"2023-10-09T21:29:39.229383Z","iopub.status.idle":"2023-10-09T21:29:39.280385Z","shell.execute_reply.started":"2023-10-09T21:29:39.229348Z","shell.execute_reply":"2023-10-09T21:29:39.279337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}