Spaces:
Runtime error
Runtime error
File size: 10,813 Bytes
e997328 d78781e e997328 d78781e e997328 db2d292 e997328 d78781e e997328 db2d292 e997328 db2d292 e997328 ccabaa8 d78781e 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 d78781e db2d292 e997328 d78781e db2d292 d78781e db2d292 d78781e db2d292 d78781e db2d292 d78781e db2d292 ccabaa8 db2d292 d78781e e997328 db2d292 ccabaa8 db2d292 d78781e db2d292 e997328 d78781e e997328 d78781e e997328 d78781e ccabaa8 d78781e ccabaa8 d78781e ccabaa8 d78781e ccabaa8 d78781e ccabaa8 d78781e e997328 d78781e e997328 d78781e 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "26b62e0c",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b1a6a020",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/zuppif/miniconda3/envs/activeloop/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.4.3) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n",
" warnings.warn(\n",
"-"
]
},
{
"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": "97c3370c",
"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": "8a474a1c",
"metadata": {},
"source": [
"## Using all the songs emotions in the prommpt"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c3cb2f3d",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from langchain.chains import LLMChain\n",
"from langchain.prompts import PromptTemplate\n",
"from pathlib import Path\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=0.7)\n",
"\n",
"chain = LLMChain(llm=llm, prompt=prompt)"
]
},
{
"cell_type": "markdown",
"id": "b1ca9c9c",
"metadata": {},
"source": [
"Let's create the songs string"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "00416443",
"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": 25,
"id": "e4bf60d4",
"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": 26,
"id": "32cd1a47",
"metadata": {},
"outputs": [],
"source": [
"# prompt.format(songs=songs_str, user_input=\"I am feeling great today\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "a056e5e9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[\"coco;remember me (dúo)\", \"mulan;reflection\", \"frozen;do you want to build a snowman?\"]'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = chain.run(songs=songs_str, user_input=\"I am sad\")\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "e84eeeaa",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"res = random.choice(eval(res))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "e24ed65f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"frozen;do you want to build a snowman?\n"
]
},
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"700\"\n",
" height=\"350\"\n",
" src=\"https://open.spotify.com/embed/track/2yi7HZrBOC4bMUSTcs4VK6?utm_source=generator\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" \n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f54178b9d00>"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03de1b93",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
|