{
"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",
" \n",
" "
],
"text/plain": [
""
]
},
"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
}