{ "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 }