{"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":"code","source":"from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer","metadata":{"execution":{"iopub.status.busy":"2022-06-09T23:03:18.313412Z","iopub.execute_input":"2022-06-09T23:03:18.313878Z","iopub.status.idle":"2022-06-09T23:03:18.319274Z","shell.execute_reply.started":"2022-06-09T23:03:18.313839Z","shell.execute_reply":"2022-06-09T23:03:18.318311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = AutoModelForSequenceClassification.from_pretrained(\"../input/goodreads-books-reviews-rating-prediction/goodreads-bert-model/\")","metadata":{"execution":{"iopub.status.busy":"2022-06-09T22:37:16.667433Z","iopub.execute_input":"2022-06-09T22:37:16.668275Z","iopub.status.idle":"2022-06-09T22:37:22.695975Z","shell.execute_reply.started":"2022-06-09T22:37:16.668229Z","shell.execute_reply":"2022-06-09T22:37:22.695011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoTokenizer\ntokenizer = AutoTokenizer.from_pretrained(\"bert-base-cased\")","metadata":{"execution":{"iopub.status.busy":"2022-06-09T22:37:22.697977Z","iopub.execute_input":"2022-06-09T22:37:22.698482Z","iopub.status.idle":"2022-06-09T22:37:38.121484Z","shell.execute_reply.started":"2022-06-09T22:37:22.698439Z","shell.execute_reply":"2022-06-09T22:37:38.120526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import pipeline\n\n# device=0 uses GPU\ngenerator = pipeline(task=\"text-classification\", model=model, tokenizer=tokenizer, device=0)","metadata":{"execution":{"iopub.status.busy":"2022-06-09T22:43:28.117784Z","iopub.execute_input":"2022-06-09T22:43:28.118448Z","iopub.status.idle":"2022-06-09T22:43:32.507626Z","shell.execute_reply.started":"2022-06-09T22:43:28.118414Z","shell.execute_reply":"2022-06-09T22:43:32.506728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-06-09T22:37:40.939783Z","iopub.execute_input":"2022-06-09T22:37:40.940188Z","iopub.status.idle":"2022-06-09T22:37:40.949139Z","shell.execute_reply.started":"2022-06-09T22:37:40.940147Z","shell.execute_reply":"2022-06-09T22:37:40.944307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import Dataset\nfrom transformers.pipelines.pt_utils import KeyDataset\n\ndf_test = pd.read_parquet(\"../input/goodreads-books-reviews-parquet-files/goodreads_test.parquet\")\nds_test = Dataset.from_pandas(df_test, preserve_index=False, split=\"test\")\n\nds_test_reviewText = KeyDataset(ds_test, \"review_text\")","metadata":{"execution":{"iopub.status.busy":"2022-06-09T23:03:29.788450Z","iopub.execute_input":"2022-06-09T23:03:29.788896Z","iopub.status.idle":"2022-06-09T23:03:33.207858Z","shell.execute_reply.started":"2022-06-09T23:03:29.788860Z","shell.execute_reply":"2022-06-09T23:03:33.206975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inferred_ratings_gen = generator(ds_test_reviewText, padding='max_length', truncation=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-09T22:56:06.971593Z","iopub.execute_input":"2022-06-09T22:56:06.974735Z","iopub.status.idle":"2022-06-09T22:56:06.982521Z","shell.execute_reply.started":"2022-06-09T22:56:06.974689Z","shell.execute_reply":"2022-06-09T22:56:06.981234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\n\ninferred_ratings = [rating[\"label\"] for rating in tqdm(inferred_ratings_gen)]","metadata":{"execution":{"iopub.status.busy":"2022-06-09T22:56:06.989719Z","iopub.execute_input":"2022-06-09T22:56:06.992557Z","iopub.status.idle":"2022-06-09T22:56:09.635483Z","shell.execute_reply.started":"2022-06-09T22:56:06.992482Z","shell.execute_reply":"2022-06-09T22:56:09.634222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def label_to_int(label):\n    if label == \"LABEL_0\":\n        return 0\n    elif label == \"LABEL_1\":\n        return 1\n    elif label == \"LABEL_2\":\n        return 2\n    elif label == \"LABEL_3\":\n        return 3\n    elif label == \"LABEL_4\":\n        return 4\n    else:\n        return 5\n\ninferred_ratings_int = map(label_to_int, inferred_ratings)","metadata":{"execution":{"iopub.status.busy":"2022-06-09T23:02:18.412499Z","iopub.execute_input":"2022-06-09T23:02:18.413379Z","iopub.status.idle":"2022-06-09T23:02:18.419584Z","shell.execute_reply.started":"2022-06-09T23:02:18.413339Z","shell.execute_reply":"2022-06-09T23:02:18.418653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({\"review_id\": ds_test[\"review_id\"], \"rating\": inferred_ratings_int})","metadata":{"execution":{"iopub.status.busy":"2022-06-09T23:02:22.808312Z","iopub.execute_input":"2022-06-09T23:02:22.808709Z","iopub.status.idle":"2022-06-09T23:02:22.818485Z","shell.execute_reply.started":"2022-06-09T23:02:22.808656Z","shell.execute_reply":"2022-06-09T23:02:22.817562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-09T23:02:25.152043Z","iopub.execute_input":"2022-06-09T23:02:25.152437Z","iopub.status.idle":"2022-06-09T23:02:25.158988Z","shell.execute_reply.started":"2022-06-09T23:02:25.152404Z","shell.execute_reply":"2022-06-09T23:02:25.158094Z"},"trusted":true},"execution_count":null,"outputs":[]}]}