File size: 1,125 Bytes
6fd136c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
This script takes all the songs we have and use the lyric to create a list of 8 emotions we then use to replace the lyric itself.
This is needed to properly match user's emotions to the songs.
"""

from dotenv import load_dotenv

load_dotenv()

import json
from collections import defaultdict
from pathlib import Path

from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate

prompt = PromptTemplate(
    input_variables=["song"],
    template=Path("prompts/summary_with_emotions.prompt").read_text(),
)

llm = ChatOpenAI(temperature=0.7)

chain = LLMChain(llm=llm, prompt=prompt)

with open("data/lyrics_with_spotify_url.json", "r") as f:
    data = json.load(f)

new_data = defaultdict(list)

for movie, songs in data.items():
    for song in songs:
        print(f"{song['name']}")
        emotions = chain.run(song=song["text"])
        new_data[movie].append(
            {"name": song["name"], "text": emotions, "embed_url": song["embed_url"]}
        )


with open("data/emotions_with_spotify_url.json", "w") as f:
    json.dump(new_data, f)