File size: 3,182 Bytes
6dbf09f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sentiment Analysis Model Demo\n",
"This notebook demonstrates how to use the sentiment analysis models to predict sentiment for new text."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import joblib\n",
"import numpy as np\n",
"import pandas as pd\n",
"from sentence_transformers import SentenceTransformer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load the models\n",
"model1 = joblib.load('model1.joblib')\n",
"model2 = joblib.load('model2.joblib')\n",
"\n",
"# Load the embedder\n",
"embedder = SentenceTransformer('BAAI/bge-large-en-v1.5')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def predict_sentiment(text):\n",
" # Generate embedding\n",
" embedding = embedder.encode([text])\n",
" \n",
" # Make predictions\n",
" pred1 = model1.predict(embedding)[0]\n",
" pred2 = model2.predict(embedding)[0]\n",
" \n",
" # Average and round\n",
" final_prediction = np.round((pred1 + pred2) / 2).astype(int)\n",
" \n",
" return final_prediction, pred1, pred2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Try with a sample text\n",
"sample_text = \"I absolutely loved this movie! The actors were amazing and the plot was fantastic.\"\n",
"final_score, score1, score2 = predict_sentiment(sample_text)\n",
"\n",
"print(f\"Text: {sample_text}\")\n",
"print(f\"Final sentiment score: {final_score}\")\n",
"print(f\"Model 1 score: {score1}\")\n",
"print(f\"Model 2 score: {score2}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Try with multiple texts\n",
"texts = [\n",
" \"This product is terrible. Complete waste of money.\",\n",
" \"The service was okay, nothing special.\",\n",
" \"Absolutely fantastic experience! Would highly recommend.\",\n",
" \"Not what I expected, but it wasn't bad either.\"\n",
"]\n",
"\n",
"results = []\n",
"for text in texts:\n",
" final_score, score1, score2 = predict_sentiment(text)\n",
" results.append({\n",
" 'Text': text,\n",
" 'Final Score': final_score,\n",
" 'Expert 1 Score': score1,\n",
" 'Expert 2 Score': score2\n",
" })\n",
"\n",
"pd.DataFrame(results)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
} |