{"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":"!git lfs install","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:58:29.230213Z","iopub.execute_input":"2023-06-02T01:58:29.230766Z","iopub.status.idle":"2023-06-02T01:58:30.317781Z","shell.execute_reply.started":"2023-06-02T01:58:29.230732Z","shell.execute_reply":"2023-06-02T01:58:30.316395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://huggingface.co/google/matcha-plotqa-v1","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:58:30.320335Z","iopub.execute_input":"2023-06-02T01:58:30.321134Z","iopub.status.idle":"2023-06-02T01:58:43.572469Z","shell.execute_reply.started":"2023-06-02T01:58:30.321087Z","shell.execute_reply":"2023-06-02T01:58:43.570794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor, AutoProcessor","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:58:43.574739Z","iopub.execute_input":"2023-06-02T01:58:43.575158Z","iopub.status.idle":"2023-06-02T01:58:58.198849Z","shell.execute_reply.started":"2023-06-02T01:58:43.5751Z","shell.execute_reply":"2023-06-02T01:58:58.197978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = Pix2StructForConditionalGeneration.from_pretrained(\"/kaggle/working/matcha-plotqa-v1\", return_dict=True).to(0)\nprocessor = AutoProcessor.from_pretrained(\"/kaggle/working/matcha-plotqa-v1\", is_vqa=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:58:58.201553Z","iopub.execute_input":"2023-06-02T01:58:58.201903Z","iopub.status.idle":"2023-06-02T01:59:06.803583Z","shell.execute_reply.started":"2023-06-02T01:58:58.20187Z","shell.execute_reply":"2023-06-02T01:59:06.802496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel2 = Pix2StructForConditionalGeneration.from_pretrained(\"/kaggle/input/matcha-base/matcha-base/\", return_dict=True).to(0)\nprocessor2 = AutoProcessor.from_pretrained(\"/kaggle/input/matcha-base/matcha-base/\", is_vqa=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:06.804857Z","iopub.execute_input":"2023-06-02T01:59:06.80522Z","iopub.status.idle":"2023-06-02T01:59:20.717684Z","shell.execute_reply.started":"2023-06-02T01:59:06.805187Z","shell.execute_reply":"2023-06-02T01:59:20.716664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython import display\nimport cv2\nfrom PIL import Image\nfrom glob import glob, glob1","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:20.719242Z","iopub.execute_input":"2023-06-02T01:59:20.719851Z","iopub.status.idle":"2023-06-02T01:59:20.886442Z","shell.execute_reply.started":"2023-06-02T01:59:20.719815Z","shell.execute_reply":"2023-06-02T01:59:20.88553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = f'/kaggle/input/benetech-making-graphs-accessible/train/images/3d1010bd41f8.jpg'\nimage = cv2.imread(filepath)\ndisplay.Image(filepath)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:20.887912Z","iopub.execute_input":"2023-06-02T01:59:20.888617Z","iopub.status.idle":"2023-06-02T01:59:20.924775Z","shell.execute_reply.started":"2023-06-02T01:59:20.888577Z","shell.execute_reply":"2023-06-02T01:59:20.923966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = processor(images=image, return_tensors=\"pt\").to(0)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:20.926109Z","iopub.execute_input":"2023-06-02T01:59:20.926418Z","iopub.status.idle":"2023-06-02T01:59:21.005195Z","shell.execute_reply.started":"2023-06-02T01:59:20.926388Z","shell.execute_reply":"2023-06-02T01:59:21.004248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.generate(**inputs, max_new_tokens=512)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:21.006826Z","iopub.execute_input":"2023-06-02T01:59:21.007205Z","iopub.status.idle":"2023-06-02T01:59:24.063679Z","shell.execute_reply.started":"2023-06-02T01:59:21.007171Z","shell.execute_reply":"2023-06-02T01:59:24.062801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(processor.decode(predictions[0], skip_special_tokens=True))","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:24.067552Z","iopub.execute_input":"2023-06-02T01:59:24.067845Z","iopub.status.idle":"2023-06-02T01:59:24.074423Z","shell.execute_reply.started":"2023-06-02T01:59:24.06782Z","shell.execute_reply":"2023-06-02T01:59:24.073424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs2 = processor2(images=image, return_tensors=\"pt\").to(0)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:24.075877Z","iopub.execute_input":"2023-06-02T01:59:24.077032Z","iopub.status.idle":"2023-06-02T01:59:24.110097Z","shell.execute_reply.started":"2023-06-02T01:59:24.076936Z","shell.execute_reply":"2023-06-02T01:59:24.109154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions2 = model2.generate(**inputs2, max_new_tokens=512)\npredictions2","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:24.111541Z","iopub.execute_input":"2023-06-02T01:59:24.111888Z","iopub.status.idle":"2023-06-02T01:59:42.716843Z","shell.execute_reply.started":"2023-06-02T01:59:24.111854Z","shell.execute_reply":"2023-06-02T01:59:42.715866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(processor2.decode(predictions2[0], skip_special_tokens=True))","metadata":{"execution":{"iopub.status.busy":"2023-06-02T01:59:42.718295Z","iopub.execute_input":"2023-06-02T01:59:42.718632Z","iopub.status.idle":"2023-06-02T01:59:42.725653Z","shell.execute_reply.started":"2023-06-02T01:59:42.718599Z","shell.execute_reply":"2023-06-02T01:59:42.723994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions2.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-02T02:00:53.071102Z","iopub.execute_input":"2023-06-02T02:00:53.07148Z","iopub.status.idle":"2023-06-02T02:00:53.077785Z","shell.execute_reply.started":"2023-06-02T02:00:53.071448Z","shell.execute_reply":"2023-06-02T02:00:53.076882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions2.shape","metadata":{},"execution_count":null,"outputs":[]}]}