File size: 19,363 Bytes
e997328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db2d292
e997328
 
 
 
 
 
 
 
 
db2d292
e997328
 
 
 
 
 
db2d292
e997328
 
 
 
 
 
 
db2d292
e997328
 
 
 
 
 
db2d292
e997328
db2d292
 
e997328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccabaa8
 
 
 
 
 
 
 
e997328
 
db2d292
e997328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db2d292
e997328
 
 
 
 
 
db2d292
e997328
 
db2d292
e997328
 
 
 
 
db2d292
e997328
 
 
 
 
db2d292
e997328
 
 
 
 
 
db2d292
 
 
 
 
 
 
 
e997328
 
 
 
 
 
 
db2d292
e997328
 
 
 
 
 
 
db2d292
e997328
 
db2d292
e997328
 
 
 
 
db2d292
 
 
 
 
e997328
 
 
 
 
db2d292
 
ccabaa8
db2d292
 
 
 
 
e997328
 
ccabaa8
 
db2d292
 
 
 
 
 
 
 
 
 
ccabaa8
db2d292
 
 
 
 
 
ccabaa8
db2d292
 
 
 
 
 
 
ccabaa8
 
db2d292
 
 
 
ccabaa8
 
 
db2d292
 
 
 
ccabaa8
 
e997328
 
 
db2d292
 
 
 
 
 
ccabaa8
 
 
db2d292
 
 
 
ccabaa8
 
db2d292
e997328
 
 
 
ccabaa8
e997328
 
ccabaa8
e997328
 
 
 
 
ccabaa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e997328
 
 
 
 
ccabaa8
e997328
 
ccabaa8
 
 
 
 
 
 
e997328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "26b62e0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "b1a6a020",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This dataset can be visualized in Jupyter Notebook by ds.visualize() or at https://app.activeloop.ai/zuppif/disney-lyrics-emotions\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "-"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hub://zuppif/disney-lyrics-emotions loaded successfully.\n",
      "\n",
      "Deep Lake Dataset in hub://zuppif/disney-lyrics-emotions already exists, loading from the storage\n",
      "Dataset(path='hub://zuppif/disney-lyrics-emotions', read_only=True, tensors=['embedding', 'ids', 'metadata', 'text'])\n",
      "\n",
      "  tensor     htype     shape      dtype  compression\n",
      "  -------   -------   -------    -------  ------- \n",
      " embedding  generic  (85, 1536)  float32   None   \n",
      "    ids      text     (85, 1)      str     None   \n",
      " metadata    json     (85, 1)      str     None   \n",
      "   text      text     (85, 1)      str     None   \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " \r",
      "\r",
      " \r"
     ]
    }
   ],
   "source": [
    "from dotenv import load_dotenv\n",
    "load_dotenv() \n",
    "from names import DATASET_ID, MODEL_ID\n",
    "from data import load_db\n",
    "import os\n",
    "from langchain.chains import RetrievalQA, ConversationalRetrievalChain\n",
    "from langchain.vectorstores import DeepLake\n",
    "from langchain.llms import OpenAI\n",
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "embeddings = OpenAIEmbeddings(model=MODEL_ID)\n",
    "dataset_path = f\"hub://{os.environ['ACTIVELOOP_ORG_ID']}/{DATASET_ID}\"\n",
    "\n",
    "db = load_db(dataset_path, embedding_function=embeddings, token=os.environ['ACTIVELOOP_TOKEN'], org_id=os.environ[\"ACTIVELOOP_ORG_ID\"], read_only=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5e75439",
   "metadata": {},
   "source": [
    "## Using similarity search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "07d8a381",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import LLMChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "from pathlib import Path\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"content\"],\n",
    "    template=Path(\"prompts/bot.prompt\").read_text(),\n",
    ")\n",
    "\n",
    "llm = ChatOpenAI(temperature=0.7)\n",
    "\n",
    "chain = LLMChain(llm=llm, prompt=prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "ebca722d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Exhaustion, Fatigue, Sleepiness, Drained.'"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emotions = chain.run(content=\"Damn I am feeling so tired\")\n",
    "emotions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "9598a36c",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(Document(page_content='Hopeful, determined, inspired, optimistic, longing, driven, passionate, adventurous.', metadata={'movie': 'Hercules', 'name': 'Go the Distance', 'embed_url': 'https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator'}), 0.8135085701942444), (Document(page_content='upset, mad, regret, sad, fine, longing, hopeful, impatient', metadata={'movie': 'Encanto', 'name': 'Waiting on a Miracle', 'embed_url': 'https://open.spotify.com/embed/track/3oRW9ZGPRbLRMneQ5lwflt?utm_source=generator'}), 0.8108540177345276), (Document(page_content='nasty, repentant, magic, sad, lonely, bored, withdrawn, busy', metadata={'movie': 'The Little Mermaid', 'name': 'Poor Unfortunate Souls', 'embed_url': 'https://open.spotify.com/embed/track/7zsw78LtXUD7JfEwH64HK2?utm_source=generator'}), 0.8080281615257263), (Document(page_content='hopeful, optimistic, dreamy, inspired, happy, content, fulfilled, grateful', metadata={'movie': 'Pinocchio', 'name': 'When You Wish Upon a Star', 'embed_url': 'https://open.spotify.com/embed/track/1WrPa4lrIddctGWAIYYfP9?utm_source=generator'}), 0.8055723309516907)]\n",
      "https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator\n",
      "page_content='Hopeful, determined, inspired, optimistic, longing, driven, passionate, adventurous.' metadata={'movie': 'Hercules', 'name': 'Go the Distance', 'embed_url': 'https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator'}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"700\"\n",
       "            height=\"350\"\n",
       "            src=\"https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x7f1890ed7430>"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matches = db.similarity_search_with_score(emotions, distance_metric=\"cos\")\n",
    "print(matches)\n",
    "doc, score = matches[0]\n",
    "print(doc.metadata[\"embed_url\"])\n",
    "print(doc)\n",
    "\n",
    "from IPython.display import IFrame\n",
    "IFrame(doc.metadata[\"embed_url\"], width=700, height=350)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "942b0809",
   "metadata": {},
   "source": [
    "## Using all the songs emotions in the prommpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "id": "50044e35",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"songs\", \"user_input\"],\n",
    "    template=Path(\"prompts/bot_with_summary.prompt\").read_text(),\n",
    ")\n",
    "\n",
    "llm = ChatOpenAI(temperature=1)\n",
    "\n",
    "chain = LLMChain(llm=llm, prompt=prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "873c7f81",
   "metadata": {},
   "source": [
    "Let's create the songs string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "id": "009caaaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"data/emotions_with_spotify_url.json\", \"r\") as f:\n",
    "    data = json.load(f)\n",
    "    \n",
    "movies_and_names_to_songs = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "id": "afb081f7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "songs_str = \"\"\n",
    "\n",
    "for movie, songs in data.items():\n",
    "    for song in songs:\n",
    "        movie_and_name = f\"{movie};{song['name']}\".lower()\n",
    "        songs_str += f\"{movie_and_name}:{song['text']}\\n\"\n",
    "        movies_and_names_to_songs[movie_and_name] = song"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "id": "963f2237",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"aladdin;friend like me:excitement, happiness, humor, friendliness, generosity, willingness, eagerness, confidence\\naladdin;arabian nights:nostalgic, adventurous, exotic, intense, romantic, mysterious, whimsical, passionate\\naladdin;a whole new world:wonder, joy, excitement, adventure, romance, freedom, happiness, awe\\naladdin;one jump ahead:Thrill, fear, desperation, humor, defiance, friendship, hunger, rebellion.\\naladdin (live-action);a whole new world:nostalgia, wonder, excitement, joy, romance, adventure, hope, inspiration\\naladdin (live-action);a whole new world (end title):joy, wonder, excitement, adventure, love, freedom, exhilaration, enchantment\\naladdin (live-action);friend like me:excitement, confidence, generosity, humor, friendliness, helpfulness, empowerment, joyfulness\\naladdin (live-action);one jump ahead:excitement, fear, desperation, humor, defiance, loneliness, camaraderie, determination\\naladdin (live-action);speechless (full):determination, empowerment, resilience, strength, defiance, courage, passion, perseverance\\naladdin: the return of jafar;arabian nights:excitement, wonder, adventure, awe, joy, fascination, thrill, curiosity\\nbeauty and the beast;belle:calm, content, curious, dreamy, happy, hopeful, romantic, whimsical\\nbeauty and the beast;be our guest:joy, excitement, hospitality, gratitude, entertainment, nostalgia, comfort, satisfaction\\nbrave;touch the sky:hopeful, adventurous, determined, inspired, free-spirited, empowered, awe-struck, exhilarated\\ncoco;remember me (dúo):nostalgia, love, longing, sadness, hope, comfort, devotion, perseverance\\ncoco;remember me (ernesto de la cruz):longing, separation, love, devotion, sadness, comfort, hope, reminiscence\\ncoco;the world es mi familia:joy, happiness, excitement, love, gratitude, belonging, unity, celebration\\ncoco;un poco loco:confusion, love, amusement, uncertainty, joy, surprise, frustration, whimsy\\ndescendants 3;dig a little deeper:Hope, Acceptance, Empowerment, Encouragement, Determination, Inspiration, Resilience, Confidence.\\nencanto;the family madrigal:joy, excitement, wonder, confusion, love, pride, nostalgia, humor\\nencanto;surface pressure:strength, confidence, anxiety, pressure, worry, fear, determination, resilience\\nencanto;two oruguitas:love, yearning, hunger, fear, anticipation, change, trust, growth\\nencanto;we don't talk about bruno:joy, fear, confusion, anxiety, frustration, curiosity, anger, humor\\nencanto;what else can i do?:unexpected, beautiful, practiced, carnivorous, new, sick of pretty, rising, changing minds\\nencanto;waiting on a miracle:upset, mad, regret, sad, fine, longing, hopeful, impatient\\nenchanted;happy working song:joy, cheerfulness, contentment, enthusiasm, satisfaction, excitement, pleasure, happiness\\nenchanted;so close:romantic, hopeful, longing, joy, fear, sadness, content, nostalgia\\nenchanted;that's how you know:love, happiness, affection, devotion, passion, adoration, appreciation, romance\\nfrozen;do you want to build a snowman?:loneliness, longing, sadness, nostalgia, hope, perseverance, friendship, playfulness\\nfrozen;for the first time in forever:excited, hopeful, nervous, happy, anxious, optimistic, romantic, determined\\nfrozen;in summer:joy, anticipation, excitement, happiness, nostalgia, humor, contentment, playfulness\\nfrozen;let it go:isolation, determination, freedom, empowerment, resilience, confidence, defiance, happiness\\nfrozen;love is an open door:joy, excitement, love, happiness, contentment, connection, optimism, anticipation\\nfrozen 2;all is found:nostalgia, safety, discovery, fear, magic, bravery, loss, hope.\\nfrozen 2;into the unknown:confusion, fear, hesitation, loneliness, longing, uncertainty, curiosity, determination\\nfrozen 2;lost in the woods:wondering, lost, confused, sad, longing, hopeful, uncertain, anxious\\nfrozen 2;show yourself:trembling, familiar, sense of belonging, longing, certainty, purpose, anticipation, empowerment\\nfrozen 2;some things never change:nostalgia, love, uncertainty, hope, contentment, joy, apprehension, gratitude\\nfrozen 2;when i am older:nostalgia, comfort, curiosity, fear, acceptance, wisdom, optimism, reassurance\\nhercules;a star is born:excitement, triumph, hope, inspiration, admiration, joy, pride, determination\\nhercules;go the distance:Hopeful, determined, inspired, optimistic, longing, driven, passionate, adventurous.\\nhercules;zero to hero:excitement, admiration, joy, pride, amazement, awe, gratitude, inspiration\\nhigh school musical;start of something new:hopeful, excited, enamored, inspired, optimistic, joyful, eager, elated\\nhigh school musical;we're all in this together:joyful, celebratory, unity, energetic, enthusiastic, uplifting, optimistic, empowering\\nhsm: the musical: the series;all i want:love, disappointment, frustration, confusion, doubt, loneliness, longing, self-reflection.\\nhsm: the musical: the series (season 2);the rose song:Wonder, doubt, love, trapped, beauty, freedom, growth, empowerment.\\nhsm: the musical: the series (season 3);you never know:hope, anticipation, excitement, mystery, adventure, positivity, curiosity, wonder\\nthe jungle book;the bare necessities:joy, contentment, relaxation, playfulness, curiosity, wonder, gratitude, simplicity\\nthe lion king;can you feel the love tonight:romantic, sweet, magical, disastrous, truthful, hesitant, hopeful, doomed\\nthe lion king;circle of life:hope, despair, love, faith, wonder, awe, nostalgia, inspiration\\nthe lion king;hakuna matata:joy, carefree, contentment, relief, humor, lightheartedness, friendship, acceptance\\nthe lion king;i just can't wait to be king:excitement, ambition, defiance, freedom, confidence, anticipation, joy, triumph\\nthe lion king (live-action);can you feel the love tonight:romantic, peaceful, harmonious, uncertain, fearful, hopeful, longing, doomed\\nthe lion king (live-action);circle of life:Joy, wonder, awe, hope, love, nostalgia, inspiration, contentment.\\nthe lion king (live-action);hakuna matata:joy, happiness, carefree, contentment, humor, playfulness, relaxation, comfort\\nthe lion king (live-action);i just can't wait to be king:Excitement, Ambition, Confidence, Defiance, Rebellion, Anticipation, Joy, Empowerment\\nthe little mermaid;kiss the girl:excitement, curiosity, desire, shyness, sadness, regret, anticipation, courage\\nthe little mermaid;part of your world:wonder, desire, longing, curiosity, joy, hope, anticipation, yearning\\nthe little mermaid;poor unfortunate souls:nasty, repentant, magic, sad, lonely, bored, withdrawn, busy\\nthe little mermaid;under the sea:happy, joyful, playful, carefree, energetic, lively, whimsical, adventurous\\nmary poppins;supercalifragilisticexpialidocious:joy, excitement, playfulness, nostalgia, humor, confidence, charm, love\\nmoana;how far i'll go:nostalgia, longing, determination, curiosity, confusion, satisfaction, pride, uncertainty\\nmoana;where you are:happiness, contentment, tradition, pride, belonging, determination, nostalgia, joy\\nmoana;you're welcome:adorable, confident, proud, playful, grateful, boastful, humorous, friendly\\nmulan;a girl worth fighting for:nostalgia, camaraderie, humor, determination, longing, admiration, optimism, romance\\nmulan;honor to us all:determination, pride, obedience, tradition, anxiety, hope, honor, cultural expectations\\nmulan;reflection:doubt, insecurity, confusion, sadness, frustration, longing, self-discovery, acceptance\\nmulan (live-action);reflection:confusion, frustration, identity, longing, self-discovery, vulnerability, authenticity, empowerment\\npinocchio;when you wish upon a star:hopeful, optimistic, dreamy, inspired, happy, content, fulfilled, grateful\\npinocchio (live-action);when you wish upon a star:hopeful, optimistic, joyful, fulfilled, inspired, grateful, content, peaceful\\npocahontas;colors of the wind:wonder, defiance, connection, appreciation, curiosity, empowerment, harmony, respect\\nthe princess and the frog;dig a little deeper:joy, acceptance, self-discovery, contentment, optimism, encouragement, determination, hopefulness\\nthe princess and the frog;down in new orleans (finale):excitement, happiness, joy, celebration, optimism, anticipation, nostalgia, contentment\\nthe princess and the frog;when we're human:excitement, joy, confidence, optimism, contentment, nostalgia, determination, celebration\\nrobin hood;love:nostalgia, love, longing, happiness, sadness, reflection, timelessness, sentimentality\\nsleeping beauty;once upon a dream:nostalgia, love, familiarity, hope, longing, enchantment, romanticism, dreaminess\\ntangled;i see the light:nostalgia, longing, clarity, happiness, wonder, realization, love, fulfillment\\ntangled;i've got a dream:malice, violence, dream, love, humor, optimism, perseverance, unity\\ntangled;mother knows best:protectiveness, fear, warning, love, concern, control, manipulation, persuasion\\ntangled;mother knows best (reprise):Skepticism, confidence, warning, trust, superiority, concern, cynicism, assurance.\\ntarzan;strangers like me:curiosity, familiarity, wonder, desire, longing, excitement, eagerness, awe\\ntarzan;you'll be in my heart:Love, comfort, protection, trust, hope, determination, perseverance, dedication\\ntoy story;you've got a friend in me:friendship, loyalty, comfort, support, love, companionship, perseverance, devotion\\nturning red;1 true love:sadness, love, obsession, longing, devotion, despair, hopelessness, affection\\nturning red;nobody like u:love, joy, friendship, loyalty, excitement, admiration, appreciation, happiness\\nturning red;u know what's up:confidence, determination, motivation, empowerment, excitement, ambition, persuasion, triumph\\n\""
      ]
     },
     "execution_count": 301,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "songs_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "id": "a53db65b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# movies_and_names_to_songs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "id": "4ea83493",
   "metadata": {},
   "outputs": [],
   "source": [
    "# prompt.format(songs=songs_str, user_input=\"I am feeling great today\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "id": "ba6e1232",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = chain.run(songs=songs_str, user_input=\"I need energy\").lower()"
   ]
  },
  {
   "cell_type": "raw",
   "id": "e909d1a4",
   "metadata": {},
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04903a71",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(res)\n",
    "doc = movies_and_names_to_songs[res]\n",
    "\n",
    "from IPython.display import IFrame\n",
    "IFrame(doc[\"embed_url\"], width=700, height=350)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}