{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"gpuType":"T4","authorship_tag":"ABX9TyMTReh0EoDSDru3JR0TKQID"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"6bCby4UHZJzE","executionInfo":{"status":"ok","timestamp":1715176156744,"user_tz":240,"elapsed":2647,"user":{"displayName":"Binbin Chen","userId":"10839306029887912307"}},"outputId":"22521c69-b709-497f-e60d-2d5b60ce957e"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"]}]},{"cell_type":"code","source":["ls"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"X3qPPBZwHs_v","executionInfo":{"status":"ok","timestamp":1715176178163,"user_tz":240,"elapsed":9,"user":{"displayName":"Binbin Chen","userId":"10839306029887912307"}},"outputId":"23a75c2e-37ea-411d-f5fe-867359d84f0a"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["basic_prediction.ipynb \u001b[0m\u001b[01;34mdata\u001b[0m/ DSL_statement_generation.ipynb ra_tasks.md \u001b[01;34mscripts\u001b[0m/\n"]}]},{"cell_type":"code","source":["from transformers import (\n"," AutoTokenizer,\n"," AutoModelForSequenceClassification\n",")\n","\n","import numpy as np\n","import pandas as pd\n","import torch\n","from sklearn.feature_extraction.text import TfidfVectorizer\n","import torch.nn as nn\n","import torch.optim as optim\n","from torch.utils.data import DataLoader, TensorDataset\n","from sklearn.model_selection import train_test_split\n","from sklearn.preprocessing import StandardScaler\n","from transformers import BertTokenizer, BertModel\n","from torch.utils.data import DataLoader, Dataset\n","from sklearn.metrics import f1_score, accuracy_score\n"],"metadata":{"id":"UKzsApTNV1Qx"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### Surrogate Q"],"metadata":{"id":"hBqyPKvjW9cv"}},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"D-JnQZOQY6Xh","executionInfo":{"status":"ok","timestamp":1715176154101,"user_tz":240,"elapsed":16149,"user":{"displayName":"Binbin Chen","userId":"10839306029887912307"}},"outputId":"0152e36e-afda-4f29-c31e-0f6625e3418b"},"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: openai in /usr/local/lib/python3.10/dist-packages (1.26.0)\n","Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)\n","Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)\n","Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai) (0.27.0)\n","Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai) (2.7.1)\n","Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.1)\n","Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.4)\n","Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai) (4.11.0)\n","Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (3.7)\n","Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (1.2.1)\n","Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2024.2.2)\n","Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.5)\n","Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n","Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (0.6.0)\n","Requirement already satisfied: pydantic-core==2.18.2 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (2.18.2)\n"]}],"source":["!pip install openai"]},{"cell_type":"code","source":["import os\n","import numpy as np\n","import pandas as pd\n","import time\n","from openai import OpenAI\n","from getpass import getpass\n","import base64\n","import requests\n","import random\n","\n","openai_api_key = 'YOURKEY'\n","os.environ[\"OPENAI_API_KEY\"] = openai_api_key\n","\n","client = OpenAI()\n","image_directory = './phone_screenshots'\n","\n","def encode_image(image_path):\n"," with open(image_path, \"rb\") as image_file:\n"," return base64.b64encode(image_file.read()).decode('utf-8')\n","\n","headers = {\n"," \"Content-Type\": \"application/json\",\n"," \"Authorization\": f\"Bearer {os.getenv('OPENAI_API_KEY')}\"\n","}"],"metadata":{"id":"h-HzyqH6ZHnp"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["data = pd.read_csv('data/Statement corpus.csv')"],"metadata":{"id":"5hO-ijFAZL8r"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["data"],"metadata":{"id":"xsrkBjSCZwyi","executionInfo":{"status":"ok","timestamp":1715176192114,"user_tz":240,"elapsed":22,"user":{"displayName":"Binbin Chen","userId":"10839306029887912307"}},"outputId":"75120a9e-068c-45a1-d14d-b0a789a6de5b","colab":{"base_uri":"https://localhost:8080/","height":545}},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" statement behavior everyday \\\n","0 1 plus 1 is 2 0 1 \n","1 5 is alot bigger than 1 0 0 \n","2 a balanced diet and regular exercise is needed... 1 1 \n","3 a ball is round 0 1 \n","4 a baton twirler doesn't want a broken finger 0 1 \n","... ... ... ... \n","4402 young people should not believe that they cann... 0 0 \n","4403 your credibility is the currency of the realm 1 0 \n","4404 your religion is not the only one true religion 1 1 \n","4405 zero plus any number equals that number 1 0 \n","4406 zest is the secret of all beauty there is no b... 0 0 \n","\n"," figure_of_speech judgment opinion reasoning \\\n","0 0 0 0 1 \n","1 0 0 0 0 \n","2 0 1 0 1 \n","3 0 0 0 0 \n","4 0 1 1 0 \n","... ... ... ... ... \n","4402 0 0 0 0 \n","4403 1 0 0 1 \n","4404 0 1 1 1 \n","4405 1 0 1 0 \n","4406 1 1 0 0 \n","\n"," category elicitation \n","0 Mathematics and logic category response \n","1 Mathematics and logic category response \n","2 Health and fitness category response \n","3 Natural and physical sciences Concept Net \n","4 Human activities Concept Net \n","... ... ... \n","4402 History and events news media \n","4403 People and self aphorism \n","4404 Religion and belief systems category response \n","4405 General reference category response \n","4406 General reference aphorism \n","\n","[4407 rows x 9 columns]"],"text/html":["\n","
\n"," | statement | \n","behavior | \n","everyday | \n","figure_of_speech | \n","judgment | \n","opinion | \n","reasoning | \n","category | \n","elicitation | \n","
---|---|---|---|---|---|---|---|---|---|
0 | \n","1 plus 1 is 2 | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","Mathematics and logic | \n","category response | \n","
1 | \n","5 is alot bigger than 1 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","Mathematics and logic | \n","category response | \n","
2 | \n","a balanced diet and regular exercise is needed... | \n","1 | \n","1 | \n","0 | \n","1 | \n","0 | \n","1 | \n","Health and fitness | \n","category response | \n","
3 | \n","a ball is round | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","0 | \n","Natural and physical sciences | \n","Concept Net | \n","
4 | \n","a baton twirler doesn't want a broken finger | \n","0 | \n","1 | \n","0 | \n","1 | \n","1 | \n","0 | \n","Human activities | \n","Concept Net | \n","
... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","
4402 | \n","young people should not believe that they cann... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","History and events | \n","news media | \n","
4403 | \n","your credibility is the currency of the realm | \n","1 | \n","0 | \n","1 | \n","0 | \n","0 | \n","1 | \n","People and self | \n","aphorism | \n","
4404 | \n","your religion is not the only one true religion | \n","1 | \n","1 | \n","0 | \n","1 | \n","1 | \n","1 | \n","Religion and belief systems | \n","category response | \n","
4405 | \n","zero plus any number equals that number | \n","1 | \n","0 | \n","1 | \n","0 | \n","1 | \n","0 | \n","General reference | \n","category response | \n","
4406 | \n","zest is the secret of all beauty there is no b... | \n","0 | \n","0 | \n","1 | \n","1 | \n","0 | \n","0 | \n","General reference | \n","aphorism | \n","
4407 rows × 9 columns
\n","\n"," | statement | \n","Behavior | \n","Everyday | \n","Figure of Speech | \n","Judgment | \n","Opinion | \n","Reasoning | \n","Judgement | \n","
---|---|---|---|---|---|---|---|---|
0 | \n","remaining hydrated is important since your bod... | \n","0 | \n","1 | \n","0 | \n","1.0 | \n","0 | \n","1 | \n","NaN | \n","
1 | \n","remember democracy never lasts long it soon wa... | \n","0 | \n","0 | \n","1 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
2 | \n","remember that life is neither pain nor pleasur... | \n","1 | \n","0 | \n","0 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
3 | \n","remember that there is nothing stable in human... | \n","0 | \n","0 | \n","1 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
4 | \n","remember this is your body your residence for ... | \n","1 | \n","1 | \n","1 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","
1402 | \n","young people should not believe that they cann... | \n","0 | \n","1 | \n","0 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
1403 | \n","your credibility is the currency of the realm | \n","1 | \n","0 | \n","1 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
1404 | \n","your religion is not the only one true religion | \n","1 | \n","0 | \n","0 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
1405 | \n","zero plus any number equals that number | \n","0 | \n","0 | \n","0 | \n","0.0 | \n","0 | \n","0 | \n","NaN | \n","
1406 | \n","zest is the secret of all beauty there is no b... | \n","1 | \n","0 | \n","1 | \n","1.0 | \n","1 | \n","0 | \n","NaN | \n","
1407 rows × 8 columns
\n","\n"," | statement | \n","Behavior | \n","Everyday | \n","Figure of Speech | \n","Judgment | \n","Opinion | \n","Reasoning | \n","
---|---|---|---|---|---|---|---|
0 | \n","1 plus 1 is 2 | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
1 | \n","5 is alot bigger than 1 | \n","0 | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","
2 | \n","a balanced diet and regular exercise is needed... | \n","0 | \n","1 | \n","0 | \n","1 | \n","0 | \n","0 | \n","
3 | \n","a ball is round | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
4 | \n","a baton twirler doesn't want a broken finger | \n","1 | \n","1 | \n","0 | \n","0 | \n","1 | \n","1 | \n","
\n"," | statement | \n","Behavior | \n","Everyday | \n","Figure of Speech | \n","Judgment | \n","Opinion | \n","Reasoning | \n","
---|---|---|---|---|---|---|---|
4336 | \n","you think more clearly after a good night's sleep | \n","0 | \n","1 | \n","0 | \n","0 | \n","1 | \n","0 | \n","
3505 | \n","the last thing you do when you take a bath is ... | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
3134 | \n","something that might happen while getting a go... | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
1448 | \n","if john pulls mary away mary will assume she i... | \n","1 | \n","1 | \n","0 | \n","1 | \n","1 | \n","0 | \n","
1749 | \n","if max accepts the job then sam feels proud | \n","1 | \n","1 | \n","0 | \n","1 | \n","1 | \n","0 | \n","
... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","
3444 | \n","the higher you go the cooler it becomes | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
466 | \n","during economic crisis local businesses should... | \n","1 | \n","0 | \n","0 | \n","1 | \n","1 | \n","0 | \n","
3092 | \n","something that might happen as a consequence o... | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
3772 | \n","waiting for luck is similar to waiting for death | \n","0 | \n","0 | \n","1 | \n","1 | \n","1 | \n","0 | \n","
860 | \n","if alex calls sam's mother they would have a c... | \n","0 | \n","1 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
3525 rows × 7 columns
\n","