Commit ·
68c9ed6
1
Parent(s): fee1bf4
Update the requirements, fix the notebook and improve the readme
Browse filesFiles changed (13)
- .gitignore+3-3
- README.md+15-26
- SentimentClassification.ipynb+172-119
- app.py+4-3
- compile.py+11-29
- deployment/samples_for_compilation.csvAdded
- deployment/sentiment_fhe_model/client.zipAdded+3
- deployment/sentiment_fhe_model/server.zipAdded+3
- deployment/sentiment_fhe_model/versions.jsonAdded+1
- deployment/serialized_modelAdded
- requirements.txt+2-2
- sentiment_fhe_model/samples_for_compilation.csvDeleted
- server.py+1-1
123456tmp_encrypted_prediction.npytmp_encrypted_quantized_encoding.npytmp_evaluation_key.npy.venv.fhe_keys*.pyc123456tmp/.venv.fhe_keys*.pyclocal_datasets/.vscode/
12 unmodified lines131415161718192021222310 unmodified lines3435363738394041424344454647484950515253545556575859606162636465666768697071727374757612 unmodified lines# Sentiment Analysis With FHE## Running the application on your machineIn this directory, ie `sentiment-analysis-with-transformer`, you can do the following steps.### Do once- First, create a virtual env and activate it:10 unmodified linespip3 install -r requirements.txt --ignore-installed```- If not on Linux, or if you want to compile the FHE algorithms by yourself:```bashpython3 compile.py```Check it finish well (with a "Done!").### Do each time you relaunch the application- Then, in a terminal Tab 1:```bashsource .venv/bin/activateuvicorn server:app```Tab 1 will be for the Server side.- And, in another terminal Tab 2:```bashsource .venv/bin/activatepython3 app.py```Tab 2 will be for the Client side.## Interacting with the applicationOpen the given URL link (search for a line like `Running on local URL: http://127.0.0.1:8888/` in your Terminal 2).## Training a new modelThe notebook SentimentClassification.ipynb provides a way to train a new model.Before running the notebook, you need to download the data.```bashbash download_data.sh12 unmodified lines1314151617181910 unmodified lines30313233343536373839404142434445464748495051525354555657585960616263646512 unmodified lines# Sentiment Analysis With FHE## Set up the app locally- First, create a virtual env and activate it:10 unmodified linespip3 install -r requirements.txt --ignore-installed```- (optional) Compile the FHE algorithm:```bashpython3 compile.py```Check it finish well (with a "Done!"). Please note that the actual model initialization and trainingcan be found in the [SentimentClassification notebook](SentimentClassification.ipynb) (see below).### Launch the app locally- In a terminal:```bashsource .venv/bin/activatepython3 app.py```## Interact with the applicationOpen the given URL link (search for a line like `Running on local URL: http://127.0.0.1:8888/` in theterminal).## Train a new modelThe notebook [SentimentClassification notebook](SentimentClassification.ipynb) provides a way totrain a new model. Be aware that the data needs to be downloaded beforehand using the[download_data.sh](download_data.sh) file (which requires Kaggle CLI).Alternatively, the dataset can be downloaded manually athttps://www.kaggle.com/datasets/crowdflower/twitter-airline-sentiment```bashbash download_data.sh
20 unmodified lines212223242526272829303132333435363 unmodified lines4041424344454629 unmodified lines7677787980818222 unmodified lines10510610710810911011111 unmodified lines1231241251261271281295 unmodified lines13513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818910 unmodified lines20020120220320420520620720820921021121221321421521621 unmodified lines2382392402412422432441 unmodified line2462472482492502512522532542552562572582592602612622631 unmodified line26526626726826927027127227327427527627727827928028128228328428528628728828929012 unmodified lines30330430530630730830931031131231331431531631731831932032132232332429 unmodified lines35435535635735835936024 unmodified lines38538638738838939039139239339439539639739822 unmodified lines4214224234244254264271 unmodified line4294304314324334344354364374 unmodified lines44244344444544644744839 unmodified lines48848949049149249349449549649749849950050150239 unmodified lines5425435445455465475485495505515525535545555565575585595605615625635645655665675685696 unmodified lines57657757857958058158258358458558658758858959010 unmodified lines60160260360460560660760860961061161261361461561661730 unmodified lines6486496506516526536541 unmodified line65665765865966066166266366466566666766866967067167267315 unmodified lines6896906916926936946951 unmodified line69769869970070170270370470570670770870971071171237 unmodified lines75075175275375475575675775875976076176276376476576676776876977077177277377477577677713 unmodified lines7917927937947957967971 unmodified line7998008018028038048058068078088098108118128134 unmodified lines81881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085133 unmodified lines8858868878888898908918928938948958968978988999009019029039049059069079082 unmodified lines91191291391491591691791891992092192292392492592692792892993093193293393456 unmodified lines99199299399499599699720 unmodified lines},{"cell_type": "code","execution_count": 3,"metadata": {},"outputs": [],"source": ["# Import the required packages\n","import os\n","import time\n","\n","import numpy\n","import onnx\n","import pandas as pd\n","from sklearn.metrics import average_precision_score\n","from sklearn.model_selection import GridSearchCV, train_test_split\n",3 unmodified lines},{"cell_type": "code","execution_count": 4,"metadata": {},"outputs": [{29 unmodified lines},{"cell_type": "code","execution_count": 5,"metadata": {},"outputs": [],"source": [22 unmodified lines},{"cell_type": "code","execution_count": 19,"metadata": {},"outputs": [],"source": [11 unmodified lines},{"cell_type": "code","execution_count": 20,"metadata": {},"outputs": [],"source": [5 unmodified lines" \"n_bits\": [2, 3],\n"," \"max_depth\": [1],\n"," \"n_estimators\": [10, 30, 50],\n"," \"n_jobs\": [-1],\n","}"]},{"cell_type": "code","execution_count": 21,"metadata": {},"outputs": [{"data": {"text/html": ["<style>#sk-container-id-3 {color: black;background-color: white;}#sk-container-id-3 pre{padding: 0;}#sk-container-id-3 div.sk-toggleable {background-color: white;}#sk-container-id-3 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-3 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-3 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-3 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-3 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-3 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-3 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-3 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-3 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-3 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-3 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-3 div.sk-item {position: relative;z-index: 1;}#sk-container-id-3 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-3 div.sk-item::before, #sk-container-id-3 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-3 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-3 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-3 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-3 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-3 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-3 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-3 div.sk-label-container {text-align: center;}#sk-container-id-3 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-3 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50], 'n_jobs': [-1]},\n"," scoring='accuracy')</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-7\" type=\"checkbox\" ><label for=\"sk-estimator-id-7\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GridSearchCV</label><div class=\"sk-toggleable__content\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50], 'n_jobs': [-1]},\n"," scoring='accuracy')</pre></div></div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-8\" type=\"checkbox\" ><label for=\"sk-estimator-id-8\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">estimator: XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier()</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-9\" type=\"checkbox\" ><label for=\"sk-estimator-id-9\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier()</pre></div></div></div></div></div></div></div></div></div></div>"],"text/plain": ["GridSearchCV(cv=3, estimator=XGBClassifier(), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50], 'n_jobs': [-1]},\n"," scoring='accuracy')"]},"execution_count": 21,"metadata": {},"output_type": "execute_result"}],"source": ["# Run the gridsearch\n","grid_search = GridSearchCV(model, parameters, cv=3, n_jobs=1, scoring=\"accuracy\")\n","grid_search.fit(X_train, y_train)"]},{"cell_type": "code","execution_count": 22,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Best score: 0.6842744383727991\n","Best parameters: {'max_depth': 1, 'n_bits': 3, 'n_estimators': 50, 'n_jobs': -1}\n"]}],10 unmodified lines},{"cell_type": "code","execution_count": 24,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Accuracy: 0.6810\n","Average precision score for positive class: 0.5615\n","Average precision score for negative class: 0.8349\n","Average precision score for neutral class: 0.3820\n"]}],21 unmodified lines},{"cell_type": "code","execution_count": 48,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["5 most positive tweets (class 2):\n","@united sent a DM just now. Thanks I am incredibly happy the fast response I got via Twitter than via customer care. Thank you\n","@JetBlue Great Thank you, lets hope so! Could you please notify me if flight 2302 leaves JFK? Thank you again\n","@AmericanAir Great, thanks. Followed.\n","@SouthwestAir I continue to be amazed by the amazing customer service. Thank you SWA!\n","@JetBlue Awesome thanks! Thanks for the quick response. You guys ROCK! :)\n","----------------------------------------------------------------------------------------------------\n","5 most negative tweets (class 0):\n","@USAirways been on hold 2 hours for a Cancelled Flighted flight. I understand the delay. I don't understand you auto-reFlight Booking Problems me on TUESDAY. HELP!\n","@SouthwestAir 2 hours on hold for customer service never us SW again\n","@SouthwestAir placed on hold for total of two hours today after flight was Cancelled Flightled. Online option not available. What to do?\n","@southwestair I've been on hold for 2 hours to reschedule my Cancelled Flightled flight for the morning. What gives? I need help NOW\n","@USAirways Customer service is dead. Last wk, flts delayed/Cancelled Flighted. Bags lost 4 days. Last nt, flt delayed/Cancelled Flighted. No meal voucher?\n"]}],1 unmodified line"# Let's see what are the top predictions based on the probabilities in y_pred_test\n","print(\"5 most positive tweets (class 2):\")\n","for i in range(5):\n"," print(text_X_test.iloc[y_proba_test_tfidf[:, 2].argsort()[-1 - i]])\n","\n","print(\"-\" * 100)\n","\n","print(\"5 most negative tweets (class 0):\")\n","for i in range(5):\n"," print(text_X_test.iloc[y_proba_test_tfidf[:, 0].argsort()[-1 - i]])"]},{"cell_type": "code","execution_count": 56,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Compilation time: 11.5009 seconds\n","FHE inference time: 48.6880 seconds\n"]}],12 unmodified lines"\n","# Now let's predict with FHE over a single tweet and print the time it takes\n","start = time.perf_counter()\n","decrypted_proba = best_model.predict_proba(X_tested_tweet, execute_in_fhe=True)\n","end = time.perf_counter()\n","print(f\"FHE inference time: {end - start:.4f} seconds\")"]},{"cell_type": "code","execution_count": 57,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Probabilities from the FHE inference: [[0.50224707 0.25647676 0.24127617]]\n","Probabilities from the clear model: [[0.50224707 0.25647676 0.24127617]]\n"]}],29 unmodified lines},{"cell_type": "code","execution_count": 8,"metadata": {},"outputs": [{24 unmodified lines},{"cell_type": "code","execution_count": 11,"metadata": {},"outputs": [{"name": "stderr","output_type": "stream","text": ["100%|██████████| 30/30 [00:33<00:00, 1.10s/it]\n"]}],22 unmodified lines},{"cell_type": "code","execution_count": 12,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["Predictions for the first 3 tweets:\n"," [[-2.3807464 -0.61802083 2.9900746 ]\n"," [ 2.0166504 0.4938078 -2.8006463 ]\n"," [ 2.3892698 0.1344364 -2.6873822 ]]\n"]}],4 unmodified lines},{"cell_type": "code","execution_count": 13,"metadata": {},"outputs": [{39 unmodified lines},{"cell_type": "code","execution_count": 14,"metadata": {},"outputs": [{"name": "stderr","output_type": "stream","text": ["100%|██████████| 13176/13176 [07:20<00:00, 29.91it/s]\n","100%|██████████| 1464/1464 [00:47<00:00, 30.75it/s]\n"]}],39 unmodified lines},{"cell_type": "code","execution_count": 15,"metadata": {},"outputs": [{"data": {"text/html": ["<style>#sk-container-id-2 {color: black;background-color: white;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50], 'n_jobs': [-1]},\n"," scoring='accuracy')</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" ><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GridSearchCV</label><div class=\"sk-toggleable__content\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50], 'n_jobs': [-1]},\n"," scoring='accuracy')</pre></div></div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-5\" type=\"checkbox\" ><label for=\"sk-estimator-id-5\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">estimator: XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier()</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-6\" type=\"checkbox\" ><label for=\"sk-estimator-id-6\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier()</pre></div></div></div></div></div></div></div></div></div></div>"],"text/plain": ["GridSearchCV(cv=3, estimator=XGBClassifier(), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50], 'n_jobs': [-1]},\n"," scoring='accuracy')"]},"execution_count": 15,"metadata": {},"output_type": "execute_result"}6 unmodified lines},{"cell_type": "code","execution_count": 16,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Best score: 0.8378111718275654\n","Best parameters: {'max_depth': 1, 'n_bits': 3, 'n_estimators': 50, 'n_jobs': -1}\n"]}],10 unmodified lines},{"cell_type": "code","execution_count": 17,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Accuracy: 0.8504\n","Average precision score for positive class: 0.8917\n","Average precision score for negative class: 0.9597\n","Average precision score for neutral class: 0.7341\n"]}],30 unmodified lines},{"cell_type": "code","execution_count": 19,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["5 most positive tweets (class 2):\n","@SouthwestAir love them! Always get the best deals!\n","@AmericanAir THANK YOU FOR ALL THE HELP! :P You guys are the best. #americanairlines #americanair\n","@SouthwestAir - Great flight from Phoenix to Dallas tonight!Great service and ON TIME! Makes @timieyancey very happy! http://t.co/TkVCMhbPim\n","@AmericanAir AA2416 on time and awesome flight. Great job American!\n","@SouthwestAir AMAZING c/s today by SW thank you SO very much. This is the reason we fly you #southwest\n","----------------------------------------------------------------------------------------------------\n","5 most negative tweets (class 0):\n","@AmericanAir This entire process took sooooo long that no decent seats are left. #customerservice\n","@USAirways Not only did u lose the flight plan! Now ur flight crew is FAA timed out! Thx for havin us sit on the tarmac for an hr! #Pathetic\n","@United site errored out at last step of changing award. Now can't even pull up reservation. 60 minute wait time. Thanks @United!\n","@united OKC ticket agent Roger McLarren(sp?) LESS than helpful with our Intl group travel problems Can't find a supervisor for help.\n","@AmericanAir the dinner and called me \"hon\". Not the service I would expect from 1st class. #disappointed\n"]}],15 unmodified lines},{"cell_type": "code","execution_count": 20,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["5 most positive (predicted) tweets that are actually negative (ground truth class 0):\n","@USAirways as far as being delayed goes… Looks like tailwinds are going to make up for it. Good news!\n","@united thanks for the link, now finally arrived in Brussels, 9 h after schedule...\n","@USAirways your saving grace was our flight attendant Dallas who was amazing. wish he would transfer to Delta where I would see him again\n","@AmericanAir that luggage you forgot...#mia.....he just won an oscar😄💝💝💝\n","@united thanks for having changed me. Managed to arrive with only 8 hours of delay and exhausted\n","----------------------------------------------------------------------------------------------------\n","5 most negative (predicted) tweets that are actually positive (ground truth class 2):\n","@united thanks for updating me about the 1+ hour delay the exact second I got to ATL. 🙅🙅🙅\n","@JetBlue you don't remember our date Monday night back to NYC? #heartbroken\n","@SouthwestAir save mile to visit family in 2015 and this will impact how many times I can see my mother. I planned and you change the rules\n","@SouthwestAir hot stewardess flipped me off\n","@SouthwestAir - We left iPad in a seat pocket. Filed lost item report. Received it exactly 1 week Late Flightr. Is that a record? #unbelievable\n"]37 unmodified lines},{"cell_type": "code","execution_count": 26,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Compilation time: 12.6855 seconds\n"]},{"name": "stderr","output_type": "stream","text": ["100%|██████████| 1/1 [00:00<00:00, 36.43it/s]\n"]},{"name": "stdout","output_type": "stream","text": ["FHE inference time: 53.0192 seconds\n"]}],13 unmodified lines"\n","# Now let's predict with FHE over a single tweet and print the time it takes\n","start = time.perf_counter()\n","decrypted_proba = best_model.predict_proba(X_tested_tweet, execute_in_fhe=True)\n","end = time.perf_counter()\n","fhe_exec_time = end - start\n","print(f\"FHE inference time: {fhe_exec_time:.4f} seconds\")"1 unmodified line},{"cell_type": "code","execution_count": 40,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Probabilities from the FHE inference: [[0.08434131 0.05571389 0.8599448 ]]\n","Probabilities from the clear model: [[0.08434131 0.05571389 0.8599448 ]]\n"]}],4 unmodified lines},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["# Let's export the final model such that we can reuse it in a client/server environment\n","\n","# Export the model to ONNX\n","onnx.save(best_model._onnx_model_, \"server_model.onnx\") # pylint: disable=protected-access\n","\n","# Export some data to be used for compilation\n","X_train_numpy = X_train_transformer[:100]\n","\n","# Merge the two arrays in a pandas dataframe\n","X_test_numpy_df = pd.DataFrame(X_train_numpy)\n","\n","# to csv\n","X_test_numpy_df.to_csv(\"samples_for_compilation.csv\")\n","\n","# Let's save the model to be pushed to a server later\n","from concrete.ml.deployment import FHEModelDev\n","\n","fhe_api = FHEModelDev(\"sentiment_fhe_model\", best_model)\n","fhe_api.save()"]},{"cell_type": "code","execution_count": 26,"metadata": {},"outputs": [{33 unmodified lines" <tbody>\n"," <tr>\n"," <th>TF-IDF + XGBoost</th>\n"," <td>0.681011</td>\n"," <td>0.561521</td>\n"," <td>0.834914</td>\n"," <td>0.382002</td>\n"," </tr>\n"," <tr>\n"," <th>Transformer Only</th>\n"," <td>0.805328</td>\n"," <td>0.854827</td>\n"," <td>0.954804</td>\n"," <td>0.680110</td>\n"," </tr>\n"," <tr>\n"," <th>Transformer + XGBoost</th>\n"," <td>0.850410</td>\n"," <td>0.891691</td>\n"," <td>0.959747</td>\n"," <td>0.734144</td>\n"," </tr>\n"," </tbody>\n","</table>\n",2 unmodified lines"text/plain": [" Accuracy Average Precision (positive) \\\n","Model \n","TF-IDF + XGBoost 0.681011 0.561521 \n","Transformer Only 0.805328 0.854827 \n","Transformer + XGBoost 0.850410 0.891691 \n","\n"," Average Precision (negative) \\\n","Model \n","TF-IDF + XGBoost 0.834914 \n","Transformer Only 0.954804 \n","Transformer + XGBoost 0.959747 \n","\n"," Average Precision (neutral) \n","Model \n","TF-IDF + XGBoost 0.382002 \n","Transformer Only 0.680110 \n","Transformer + XGBoost 0.734144 "]},"execution_count": 26,"metadata": {},"output_type": "execute_result"}56 unmodified lines"name": "python3"},"language_info": {"name": "python","version": "3.10.11"}},20 unmodified lines212223242526272829303132333435363 unmodified lines4041424344454629 unmodified lines7677787980818222 unmodified lines10510610710810911011111 unmodified lines1231241251261271281295 unmodified lines13513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818910 unmodified lines20020120220320420520620720820921021121221321421521621 unmodified lines2382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722731 unmodified line2752762772782792802812822832842852862872882892902912921 unmodified line29429529629729829930030130230330430530630730830931031131231331431531631731831912 unmodified lines33233333433533633733833934034134234334434534634734834935035135235329 unmodified lines38338438538638738838924 unmodified lines41441541641741841942042142242342442542642742842943043143222 unmodified lines4554564574584594604611 unmodified line4634644654664674684694704714 unmodified lines47647747847948048148239 unmodified lines52252352452552652752852953053153253353453553639 unmodified lines5765775785795805815825835845855865875885895905915925935945955965975985996006016026036 unmodified lines61061161261361461561661761861962062162262362410 unmodified lines63563663763863964064164264364464564664764864965065130 unmodified lines6826836846856866876881 unmodified line69069169269369469569669769869970070170270370470570670715 unmodified lines7237247257267277287291 unmodified line73173273373473573673773873974074174274374474574637 unmodified lines78478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781813 unmodified lines8328338348358368378381 unmodified line8408418428438448458468478488498508518528538544 unmodified lines85986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589633 unmodified lines9309319329339349359369379389399409419429439449459469479489499509519529532 unmodified lines95695795895996096196296396496596696796896997097197297397497597697797897956 unmodified lines10361037103810391040104110421043104410451046104710481049105020 unmodified lines},{"cell_type": "code","execution_count": 31,"metadata": {},"outputs": [],"source": ["# Import the required packages\n","import os\n","import time\n","from pathlib import Path\n","\n","import numpy\n","import pandas as pd\n","from sklearn.metrics import average_precision_score\n","from sklearn.model_selection import GridSearchCV, train_test_split\n",3 unmodified lines},{"cell_type": "code","execution_count": 2,"metadata": {},"outputs": [{29 unmodified lines},{"cell_type": "code","execution_count": 3,"metadata": {},"outputs": [],"source": [22 unmodified lines},{"cell_type": "code","execution_count": 4,"metadata": {},"outputs": [],"source": [11 unmodified lines},{"cell_type": "code","execution_count": 5,"metadata": {},"outputs": [],"source": [5 unmodified lines" \"n_bits\": [2, 3],\n"," \"max_depth\": [1],\n"," \"n_estimators\": [10, 30, 50],\n"," # \"n_jobs\": [-1],\n","}"]},{"cell_type": "code","execution_count": 6,"metadata": {},"outputs": [{"data": {"text/html": ["<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(n_jobs=1),\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50]},\n"," scoring='accuracy')</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GridSearchCV</label><div class=\"sk-toggleable__content\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(n_jobs=1),\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50]},\n"," scoring='accuracy')</pre></div></div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">estimator: XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier(n_jobs=1)</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier(n_jobs=1)</pre></div></div></div></div></div></div></div></div></div></div>"],"text/plain": ["GridSearchCV(cv=3, estimator=XGBClassifier(n_jobs=1),\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50]},\n"," scoring='accuracy')"]},"execution_count": 6,"metadata": {},"output_type": "execute_result"}],"source": ["# Run the gridsearch\n","grid_search = GridSearchCV(model, parameters, cv=3, scoring=\"accuracy\")\n","grid_search.fit(X_train, y_train)"]},{"cell_type": "code","execution_count": 7,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Best score: 0.705980570734669\n","Best parameters: {'max_depth': 1, 'n_bits': 3, 'n_estimators': 50}\n"]}],10 unmodified lines},{"cell_type": "code","execution_count": 8,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Accuracy: 0.7117\n","Average precision score for positive class: 0.6404\n","Average precision score for negative class: 0.8719\n","Average precision score for neutral class: 0.4349\n"]}],21 unmodified lines},{"cell_type": "code","execution_count": 9,"metadata": {},"outputs": [{"data": {"text/plain": ["array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n"," 2, 2, 2, 2, 2, 2])"]},"execution_count": 9,"metadata": {},"output_type": "execute_result"}],"source": ["y_pred_test_tfidf[y_pred_test_tfidf == 2]"]},{"cell_type": "code","execution_count": 10,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["5 most positive tweets (class 2):\n","@JetBlue do bags still fly free or have you started charging? thanks!\n","@SouthwestAir Is there a way to receive a refund on a trip that was Cancelled Flight online instead of calling? Your phone lines are super busy.\n","@JetBlue bag is supposedly here in Boston\n","@AmericanAir Cancelled Flights my flight, doesn't send an email, text or call. Now I'm stranded in Louisville.\n","@SouthwestAir I need to Cancelled Flight one leg of a flight, but can't seem to do this online. Been on hold on the phone for 10 minutes. Any help?\n","----------------------------------------------------------------------------------------------------\n","5 most negative tweets (class 0):\n","@AmericanAir - keeping AA up in the Air! My crew chief cousin Alex Espinosa in DFW! http://t.co/0HXLNvZknP\n","@JetBlue Called JB 3 times!Everytime, Auto Vmsg:\"your wait time should not be longer than 9 mins\" waited longer than 18 mins and no answer!\n","@SouthwestAir can you outline the policies for both scenarios?\n","@united is not a company that values it's customer & after reading tweets to them I'm not the only one who feels that way #lostmybusiness\n","@JetBlue how about free wifi on flt 1254 out of PBI to make up for 2.5 hr delay? Treat us right.\n"]}],1 unmodified line"# Let's see what are the top predictions based on the probabilities in y_pred_test\n","print(\"5 most positive tweets (class 2):\")\n","for i in range(5):\n"," print(text_X_test.iloc[y_pred_test_tfidf[y_pred_test_tfidf==2].argsort()[-1 - i]])\n","\n","print(\"-\" * 100)\n","\n","print(\"5 most negative tweets (class 0):\")\n","for i in range(5):\n"," print(text_X_test.iloc[y_pred_test_tfidf[y_pred_test_tfidf==0].argsort()[-1 - i]])"]},{"cell_type": "code","execution_count": 11,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Compilation time: 5.4779 seconds\n","FHE inference time: 1.1039 seconds\n"]}],12 unmodified lines"\n","# Now let's predict with FHE over a single tweet and print the time it takes\n","start = time.perf_counter()\n","decrypted_proba = best_model.predict_proba(X_tested_tweet, fhe=\"execute\")\n","end = time.perf_counter()\n","print(f\"FHE inference time: {end - start:.4f} seconds\")"]},{"cell_type": "code","execution_count": 12,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Probabilities from the FHE inference: [[0.30244059 0.17506451 0.5224949 ]]\n","Probabilities from the clear model: [[0.30244059 0.17506451 0.5224949 ]]\n"]}],29 unmodified lines},{"cell_type": "code","execution_count": 13,"metadata": {},"outputs": [{24 unmodified lines},{"cell_type": "code","execution_count": 14,"metadata": {},"outputs": [{"name": "stderr","output_type": "stream","text": ["huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n","To disable this warning, you can either:\n","\t- Avoid using `tokenizers` before the fork if possible\n","\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"," 0%| | 0/30 [00:00<?, ?it/s]We strongly recommend passing in an `attention_mask` since your input_ids may be padded. See https://huggingface.co/docs/transformers/troubleshooting#incorrect-output-when-padding-tokens-arent-masked.\n","100%|██████████| 30/30 [00:20<00:00, 1.46it/s]\n"]}],22 unmodified lines},{"cell_type": "code","execution_count": 15,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["Predictions for the first 3 tweets:\n"," [[-2.3807454 -0.61802197 2.9900734 ]\n"," [ 2.0166504 0.49380752 -2.8006463 ]\n"," [ 2.3892734 0.13443531 -2.6873832 ]]\n"]}],4 unmodified lines},{"cell_type": "code","execution_count": 16,"metadata": {},"outputs": [{39 unmodified lines},{"cell_type": "code","execution_count": 17,"metadata": {},"outputs": [{"name": "stderr","output_type": "stream","text": ["100%|██████████| 13176/13176 [08:10<00:00, 26.88it/s]\n","100%|██████████| 1464/1464 [00:54<00:00, 26.90it/s]\n"]}],39 unmodified lines},{"cell_type": "code","execution_count": 18,"metadata": {},"outputs": [{"data": {"text/html": ["<style>#sk-container-id-2 {color: black;background-color: white;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(n_jobs=1), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50]},\n"," scoring='accuracy')</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" ><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GridSearchCV</label><div class=\"sk-toggleable__content\"><pre>GridSearchCV(cv=3, estimator=XGBClassifier(n_jobs=1), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50]},\n"," scoring='accuracy')</pre></div></div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-5\" type=\"checkbox\" ><label for=\"sk-estimator-id-5\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">estimator: XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier(n_jobs=1)</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-6\" type=\"checkbox\" ><label for=\"sk-estimator-id-6\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">XGBClassifier</label><div class=\"sk-toggleable__content\"><pre>XGBClassifier(n_jobs=1)</pre></div></div></div></div></div></div></div></div></div></div>"],"text/plain": ["GridSearchCV(cv=3, estimator=XGBClassifier(n_jobs=1), n_jobs=1,\n"," param_grid={'max_depth': [1], 'n_bits': [2, 3],\n"," 'n_estimators': [10, 30, 50]},\n"," scoring='accuracy')"]},"execution_count": 18,"metadata": {},"output_type": "execute_result"}6 unmodified lines},{"cell_type": "code","execution_count": 19,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Best score: 0.8381147540983607\n","Best parameters: {'max_depth': 1, 'n_bits': 3, 'n_estimators': 50}\n"]}],10 unmodified lines},{"cell_type": "code","execution_count": 20,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Accuracy: 0.8463\n","Average precision score for positive class: 0.8959\n","Average precision score for negative class: 0.9647\n","Average precision score for neutral class: 0.7449\n"]}],30 unmodified lines},{"cell_type": "code","execution_count": 21,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["5 most positive tweets (class 2):\n","@united I think this is the best first class I have ever gotten!! Denver to LAX and it's wonderful!!!\n","@AmericanAir Flight 236 was great. Fantastic cabin crew. A+ landing. #thankyou #JFK http://t.co/dRW08djHAI\n","@SouthwestAir Jason (108639) at Gate #3 in SAN made my afternoon!!! #southwestairlines #stellarservice #thanks!\n","@SouthwestAir love them! Always get the best deals!\n","@AmericanAir simply amazing. Smiles for miles.Thank u for my upgrade tomorrow for ORD.We are spending a lot of time together next few weeks!\n","----------------------------------------------------------------------------------------------------\n","5 most negative tweets (class 0):\n","@united first you lost all my bags, now you Cancelled Flight my flight home. 30 min wait to talk to somebody #poorservice #notgoodenough\n","@USAirways Not only did u lose the flight plan! Now ur flight crew is FAA timed out! Thx for havin us sit on the tarmac for an hr! #Pathetic\n","@AmericanAir Phone just disconnects if you stay on the line. Need to checkout of hotel in 2 hrs & have no place to go. Can't keep calling.\n","@VirginAmerica I have lots of flights to book and your site it not working!!!! I've been on the phone waiting for over 10 minutes..........\n","@united 3 hour delay plus a jetway that won't move. This biz traveler is never flying u again!\n"]}],15 unmodified lines},{"cell_type": "code","execution_count": 22,"metadata": {},"outputs": [{1 unmodified line"output_type": "stream","text": ["5 most positive (predicted) tweets that are actually negative (ground truth class 0):\n","@united thanks for the link, now finally arrived in Brussels, 9 h after schedule...\n","@USAirways as far as being delayed goes… Looks like tailwinds are going to make up for it. Good news!\n","@united thanks for having changed me. Managed to arrive with only 8 hours of delay and exhausted\n","@USAirways your saving grace was our flight attendant Dallas who was amazing. wish he would transfer to Delta where I would see him again\n","@AmericanAir that luggage you forgot...#mia.....he just won an oscar😄💝💝💝\n","----------------------------------------------------------------------------------------------------\n","5 most negative (predicted) tweets that are actually positive (ground truth class 2):\n","@united thanks for updating me about the 1+ hour delay the exact second I got to ATL. 🙅🙅🙅\n","@SouthwestAir save mile to visit family in 2015 and this will impact how many times I can see my mother. I planned and you change the rules\n","@JetBlue you don't remember our date Monday night back to NYC? #heartbroken\n","@SouthwestAir hot stewardess flipped me off\n","@SouthwestAir - We left iPad in a seat pocket. Filed lost item report. Received it exactly 1 week Late Flightr. Is that a record? #unbelievable\n"]37 unmodified lines},{"cell_type": "code","execution_count": 23,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Compilation time: 5.9232 seconds\n"]},{"name": "stderr","output_type": "stream","text": ["100%|██████████| 1/1 [00:00<00:00, 17.83it/s]"]},{"name": "stdout","output_type": "stream","text": ["FHE inference time: 0.8374 seconds\n"]},{"name": "stderr","output_type": "stream","text": ["\n"]}],13 unmodified lines"\n","# Now let's predict with FHE over a single tweet and print the time it takes\n","start = time.perf_counter()\n","decrypted_proba = best_model.predict_proba(X_tested_tweet, fhe=\"execute\")\n","end = time.perf_counter()\n","fhe_exec_time = end - start\n","print(f\"FHE inference time: {fhe_exec_time:.4f} seconds\")"1 unmodified line},{"cell_type": "code","execution_count": 24,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": ["Probabilities from the FHE inference: [[0.05162184 0.04558276 0.90279541]]\n","Probabilities from the clear model: [[0.05162184 0.04558276 0.90279541]]\n"]}],4 unmodified lines},{"cell_type": "code","execution_count": 40,"metadata": {},"outputs": [],"source": ["DEPLOYMENT_DIR = Path(\"deployment\")\n","DEPLOYMENT_DIR.mkdir(exist_ok=True)\n","\n","# Let's export the final model such that we can reuse it in a client/server environment\n","\n","# Serialize the model (for development only)\n","with (DEPLOYMENT_DIR / \"serialized_model\").open(\"w\") as file:\n"," best_model.dump(file)\n","\n","# Export some data to be used for compilation \n","X_train_numpy = X_train_transformer[:100]\n","\n","# Merge the two arrays in a pandas dataframe\n","X_test_numpy_df = pd.DataFrame(X_train_numpy)\n","\n","# to csv\n","X_test_numpy_df.to_csv(DEPLOYMENT_DIR / \"samples_for_compilation.csv\")\n","\n","# Let's save the model to be pushed to a server later\n","from concrete.ml.deployment import FHEModelDev\n","\n","fhe_api = FHEModelDev(DEPLOYMENT_DIR / \"sentiment_fhe_model\", best_model)\n","fhe_api.save(via_mlir=True)"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [{33 unmodified lines" <tbody>\n"," <tr>\n"," <th>TF-IDF + XGBoost</th>\n"," <td>0.711749</td>\n"," <td>0.640422</td>\n"," <td>0.871891</td>\n"," <td>0.43486</td>\n"," </tr>\n"," <tr>\n"," <th>Transformer Only</th>\n"," <td>0.805328</td>\n"," <td>0.854827</td>\n"," <td>0.954804</td>\n"," <td>0.68011</td>\n"," </tr>\n"," <tr>\n"," <th>Transformer + XGBoost</th>\n"," <td>0.846311</td>\n"," <td>0.895930</td>\n"," <td>0.964674</td>\n"," <td>0.74489</td>\n"," </tr>\n"," </tbody>\n","</table>\n",2 unmodified lines"text/plain": [" Accuracy Average Precision (positive) \\\n","Model \n","TF-IDF + XGBoost 0.711749 0.640422 \n","Transformer Only 0.805328 0.854827 \n","Transformer + XGBoost 0.846311 0.895930 \n","\n"," Average Precision (negative) \\\n","Model \n","TF-IDF + XGBoost 0.871891 \n","Transformer Only 0.954804 \n","Transformer + XGBoost 0.964674 \n","\n"," Average Precision (neutral) \n","Model \n","TF-IDF + XGBoost 0.43486 \n","Transformer Only 0.68011 \n","Transformer + XGBoost 0.74489 "]},"execution_count": 33,"metadata": {},"output_type": "execute_result"}56 unmodified lines"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.10.11"}},
25 unmodified lines26272829303128 unmodified lines6061626364656612 unmodified lines7980818283848557 unmodified lines14314414514614714814925 unmodified lines# (encrypted data is too large to display in the browser)ENCRYPTED_DATA_BROWSER_LIMIT = 500N_USER_KEY_STORED = 20print("Loading the transformer model...")28 unmodified lines# Let's create a user_iduser_id = numpy.random.randint(0, 2**32)fhe_api = FHEModelClient("sentiment_fhe_model/deployment", f".fhe_keys/{user_id}")fhe_api.load()12 unmodified linesif not user_id:raise gr.Error("You need to generate FHE keys first.")fhe_api = FHEModelClient("sentiment_fhe_model/deployment", f".fhe_keys/{user_id}")fhe_api.load()encodings = transformer_vectorizer.transform([text])quantized_encodings = fhe_api.model.quantize_input(encodings).astype(numpy.uint8)57 unmodified lines# Read encrypted_prediction from the fileencrypted_prediction = numpy.load(encoded_data_path).tobytes()fhe_api = FHEModelClient("sentiment_fhe_model/deployment", f".fhe_keys/{user_id}")fhe_api.load()# We need to retrieve the private key that matches the client specs (see issue #18)25 unmodified lines2627282930313228 unmodified lines6162636465666712 unmodified lines8081828384858657 unmodified lines14414514614714814915025 unmodified lines# (encrypted data is too large to display in the browser)ENCRYPTED_DATA_BROWSER_LIMIT = 500N_USER_KEY_STORED = 20FHE_MODEL_PATH = "deployment/sentiment_fhe_model"print("Loading the transformer model...")28 unmodified lines# Let's create a user_iduser_id = numpy.random.randint(0, 2**32)fhe_api = FHEModelClient(FHE_MODEL_PATH, f".fhe_keys/{user_id}")fhe_api.load()12 unmodified linesif not user_id:raise gr.Error("You need to generate FHE keys first.")fhe_api = FHEModelClient(FHE_MODEL_PATH, f".fhe_keys/{user_id}")fhe_api.load()encodings = transformer_vectorizer.transform([text])quantized_encodings = fhe_api.model.quantize_input(encodings).astype(numpy.uint8)57 unmodified lines# Read encrypted_prediction from the fileencrypted_prediction = numpy.load(encoded_data_path).tobytes()fhe_api = FHEModelClient(FHE_MODEL_PATH, f".fhe_keys/{user_id}")fhe_api.load()# We need to retrieve the private key that matches the client specs (see issue #18)
12345672 unmodified lines101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import onnximport pandas as pdfrom concrete.ml.deployment import FHEModelDev, FHEModelClientfrom concrete.ml.onnx.convert import get_equivalent_numpy_forwardimport jsonimport osimport shutil2 unmodified linesscript_dir = Path(__file__).parentprint("Compiling the model...")# Load the onnx modelmodel_onnx = onnx.load(Path.joinpath(script_dir, "sentiment_fhe_model/server_model.onnx"))# Load the data from the csv file to be used for compilationdata = pd.read_csv(Path.joinpath(script_dir, "sentiment_fhe_model/samples_for_compilation.csv"), index_col=0).values# Convert the onnx model to a numpy model_tensor_tree_predict = get_equivalent_numpy_forward(model_onnx)model = FHEModelClient(Path.joinpath(script_dir, "sentiment_fhe_model/deployment"), ".fhe_keys").model# Assign the numpy model and compile the modelmodel._tensor_tree_predict = _tensor_tree_predict# Compile the modelmodel.compile(data)# Load the serialized_processing.json filewith open(Path.joinpath(script_dir, "sentiment_fhe_model/deployment/serialized_processing.json"), "r") as f:serialized_processing = json.load(f)# Delete the deployment folder if it existif Path.joinpath(script_dir, "sentiment_fhe_model/deployment").exists():shutil.rmtree(Path.joinpath(script_dir, "sentiment_fhe_model/deployment"))fhe_api = FHEModelDev(model=model, path_dir=Path.joinpath(script_dir, "sentiment_fhe_model/deployment"))fhe_api.save(via_mlir=True)# Write the serialized_processing.json file to the deployment folderwith open(Path.joinpath(script_dir, "sentiment_fhe_model/deployment/serialized_processing.json"), "w") as f:json.dump(serialized_processing, f)print("Done!")123456782 unmodified lines1112131415161718192021222324252627282930313233343536373839import onnximport pandas as pdfrom concrete.ml.deployment import FHEModelDev, FHEModelClientfrom concrete.ml.common.serialization.loaders import loadfrom concrete.ml.onnx.convert import get_equivalent_numpy_forward_from_onnx_treeimport jsonimport osimport shutil2 unmodified linesscript_dir = Path(__file__).parentDEPLOYMENT_DIR = script_dir / "deployment"print("Compiling the model...")with (DEPLOYMENT_DIR / "serialized_model").open("r") as file:model = load(file)# Load the data from the csv file to be used for compilationdata = pd.read_csv(DEPLOYMENT_DIR / "samples_for_compilation.csv", index_col=0).values# Compile the modelmodel.compile(data)dev_model_path = DEPLOYMENT_DIR / "sentiment_fhe_model"# Delete the deployment folder if it existif dev_model_path.is_dir():shutil.rmtree(dev_model_path)fhe_api = FHEModelDev(model=model, path_dir=dev_model_path)fhe_api.save(via_mlir=True)print("Done!")
123version https://git-lfs.github.com/spec/v1oid sha256:972f0c7d83f12e3a43e8f923fc422cdb443b9f64bb6f74c1abf912836ba27e60size 3887326123version https://git-lfs.github.com/spec/v1oid sha256:216d2a78d7ec47ec2a478d5f32ed34cee8a9c45700325e5d8de4e087b7ed8dfcsize 30041{"concrete-python": "2.5", "concrete-ml": "1.4.0", "python": "3.10.11"}12345concrete-ml==1.1.0gradio==3.40.1pandas==1.4.3transformers==4.32.0jupyter==1.0.012345concrete-ml==1.4.0gradio==3.40.1pandas==1.4.3transformers==4.36.0jupyter==1.0.0
8 unmodified lines91011121314158 unmodified linescurrent_dir = Path(__file__).parent# Load the modelfhe_model = FHEModelServer(Path.joinpath(current_dir, "sentiment_fhe_model/deployment"))class PredictRequest(BaseModel):evaluation_key: str8 unmodified lines91011121314158 unmodified linescurrent_dir = Path(__file__).parent# Load the modelfhe_model = FHEModelServer("deployment/sentiment_fhe_model")class PredictRequest(BaseModel):evaluation_key: str
.gitignore+3-3
README.md+15-26
SentimentClassification.ipynb+172-119
app.py+4-3
compile.py+11-29
deployment/samples_for_compilation.csvAdded
deployment/sentiment_fhe_model/client.zipAdded+3
deployment/sentiment_fhe_model/server.zipAdded+3
deployment/sentiment_fhe_model/versions.jsonAdded+1
deployment/serialized_modelAdded
requirements.txt+2-2
sentiment_fhe_model/samples_for_compilation.csvDeleted
server.py+1-1