File size: 3,288 Bytes
2682f23
 
59f7528
 
2682f23
 
e1512bf
0bacb15
2682f23
 
 
e1512bf
 
 
2682f23
e1512bf
2682f23
 
 
e1512bf
0bacb15
 
e1512bf
 
 
 
 
 
2682f23
 
 
 
 
 
 
 
 
 
e1512bf
 
 
2682f23
e1512bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2682f23
e1512bf
2682f23
 
 
 
 
e1512bf
 
 
c8d523b
8c65bc9
2682f23
 
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
import gradio as gr
from langchain import PromptTemplate
# from langchain.chat_models import ChatOpenAI
from langchain_community.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain_community.retrievers import WikipediaRetriever
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from langchain_google_genai import ChatGoogleGenerativeAI
import os


def song_insight(song, artist):
    # input
    query_input = f"{song.title()} by {artist.title()}"

    # get info about the song from wikipedia using wikipedia retriever
    retriever = WikipediaRetriever()
    docs = retriever.get_relevant_documents(query=query_input)

    # LLM model
    # llm = ChatOpenAI(openai_api_key=os.environ['OPENAI_API_KEY'], model_name="gpt-3.5-turbo", temperature=0)
    llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.environ['GOOGLE_API_KEY'])

    # Emotion Classifier Model
    tokenizer = AutoTokenizer.from_pretrained("yangswei/emotion_text_classification")
    emotion_model = AutoModelForSequenceClassification.from_pretrained("yangswei/emotion_text_classification")

    # get the song meaning
    template_song_meaning = """
      {artist} has released a song called {song}.

      {content}

      based on the the content above what does the song {song} by {artist} tell us about? give me a long explanations

      """
    prompt_template_song_meaning = PromptTemplate(input_variables=["artist", "song", "content"],
                                                  template=template_song_meaning)
    chain_song_meaning = LLMChain(llm=llm, prompt=prompt_template_song_meaning)
    results_song_meaning = chain_song_meaning.run(artist=artist.title(), song=song.title(),
                                                  content=docs[0].page_content)

    # get the song theme
    template_song_theme = """
      {artist} has released a song called {song}.

      {content}

      based on the the content above what themes does the lyrics have?

      """
    prompt_template_song_theme = PromptTemplate(input_variables=["artist", "song", "content"],
                                                template=template_song_theme)
    chain_song_theme = LLMChain(llm=llm, prompt=prompt_template_song_theme)
    text_song_theme = chain_song_theme.run(artist=artist.title(), song=song.title(), content=docs[0].page_content)
    inputs_song_theme = tokenizer(text_song_theme, return_tensors="pt")
    output_song_theme_proba = emotion_model(**inputs_song_theme).logits.softmax(1)
    labels = emotion_model.config.id2label
    confidences = {labels[i]: output_song_theme_proba[0][i].item() for i in range(len(labels))}

    return results_song_meaning, confidences


with gr.Blocks(theme=gr.themes.Soft()) as demo:
    song = gr.Textbox(label="Song")
    artist = gr.Textbox(label="Artist")
    output_song_meaning = gr.Textbox(label="Meaning")
    output_song_theme = gr.Label(num_top_classes=6, label="Theme")
    gr.Interface(fn=song_insight, inputs=[song, artist], outputs=[output_song_meaning, output_song_theme])
    example = gr.Examples([['Life Goes On', 'BTS'], ['Here Comes The Sun', 'The Beatles'],
                           ['Bedtime Stories', 'Jay Chou'], ['Loser', 'BIGBANG']], [song, artist])

demo.launch()