Spaces:
Sleeping
Sleeping
modify the application file
Browse files- requirements.txt +3 -1
- song-insight-app.py +37 -11
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
gradio==4.21.0
|
2 |
langchain==0.1.12
|
3 |
wikipedia==1.4.0
|
4 |
-
openai==1.14.1
|
|
|
|
|
|
1 |
gradio==4.21.0
|
2 |
langchain==0.1.12
|
3 |
wikipedia==1.4.0
|
4 |
+
openai==1.14.1
|
5 |
+
transformers==4.38.2
|
6 |
+
torch==2.2.1
|
song-insight-app.py
CHANGED
@@ -3,17 +3,26 @@ from langchain import PromptTemplate
|
|
3 |
from langchain.chat_models import ChatOpenAI
|
4 |
from langchain.chains import LLMChain
|
5 |
from langchain_community.retrievers import WikipediaRetriever
|
|
|
6 |
import os
|
7 |
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
query_input = f"{song_input} by {artist_input}"
|
13 |
|
|
|
14 |
retriever = WikipediaRetriever()
|
15 |
docs = retriever.get_relevant_documents(query=query_input)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
template_song_meaning = """
|
18 |
{artist} has released a song called {song}.
|
19 |
|
@@ -22,22 +31,39 @@ def song_meaning(song, artist):
|
|
22 |
based on the the content above what does the song {song} by {artist} tell us about? give me a long explanations
|
23 |
|
24 |
"""
|
25 |
-
|
26 |
prompt_template_song_meaning = PromptTemplate(input_variables=["artist", "song", "content"],
|
27 |
template=template_song_meaning)
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
return
|
34 |
|
35 |
|
36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
37 |
song = gr.Textbox(label="Song")
|
38 |
artist = gr.Textbox(label="Artist")
|
39 |
-
|
40 |
-
gr.
|
|
|
41 |
example = gr.Examples([['Maroon', 'Taylor Swift'], ['Devil In Her Heart', 'The Beatles'],
|
42 |
['Time Machine', 'Jay Chou'], ['Last Farewell', 'BIGBANG']], [song, artist])
|
43 |
|
|
|
3 |
from langchain.chat_models import ChatOpenAI
|
4 |
from langchain.chains import LLMChain
|
5 |
from langchain_community.retrievers import WikipediaRetriever
|
6 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
7 |
import os
|
8 |
|
9 |
|
10 |
+
def song_insight(song, artist):
|
11 |
+
# input
|
12 |
+
query_input = f"{song.title()} by {artist.title()}"
|
|
|
13 |
|
14 |
+
# get info about the song from wikipedia using wikipedia retriever
|
15 |
retriever = WikipediaRetriever()
|
16 |
docs = retriever.get_relevant_documents(query=query_input)
|
17 |
|
18 |
+
# LLM model
|
19 |
+
llm = ChatOpenAI(openai_api_key=os.environ['OPENAI_API_KEY'], model_name="gpt-3.5-turbo", temperature=0)
|
20 |
+
|
21 |
+
# Emotion Classifier Model
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained("yangswei/emotion_text_classification")
|
23 |
+
emotion_model = AutoModelForSequenceClassification.from_pretrained("yangswei/emotion_text_classification")
|
24 |
+
|
25 |
+
# get the song meaning
|
26 |
template_song_meaning = """
|
27 |
{artist} has released a song called {song}.
|
28 |
|
|
|
31 |
based on the the content above what does the song {song} by {artist} tell us about? give me a long explanations
|
32 |
|
33 |
"""
|
|
|
34 |
prompt_template_song_meaning = PromptTemplate(input_variables=["artist", "song", "content"],
|
35 |
template=template_song_meaning)
|
36 |
+
chain_song_meaning = LLMChain(llm=llm, prompt=prompt_template_song_meaning)
|
37 |
+
results_song_meaning = chain_song_meaning.run(artist=artist.title(), song=song.title(),
|
38 |
+
content=docs[0].page_content)
|
39 |
|
40 |
+
# get the song theme
|
41 |
+
template_song_theme = """
|
42 |
+
{artist} has released a song called {song}.
|
43 |
+
|
44 |
+
{content}
|
45 |
+
|
46 |
+
based on the the content above what themes does the lyrics have?
|
47 |
+
|
48 |
+
"""
|
49 |
+
prompt_template_song_theme = PromptTemplate(input_variables=["artist", "song", "content"],
|
50 |
+
template=template_song_theme)
|
51 |
+
chain_song_theme = LLMChain(llm=llm, prompt=prompt_template_song_theme)
|
52 |
+
text_song_theme = chain_song_theme.run(artist=artist.title(), song=song.title(), content=docs[0].page_content)
|
53 |
+
inputs_song_theme = tokenizer(text_song_theme, return_tensors="pt")
|
54 |
+
output_song_theme_proba = emotion_model(**inputs_song_theme).logits.softmax(1)
|
55 |
+
labels = emotion_model.config.id2label
|
56 |
+
confidences = {labels[i]: output_song_theme_proba[0][i].item() for i in range(len(labels))}
|
57 |
|
58 |
+
return results_song_meaning, confidences
|
59 |
|
60 |
|
61 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
62 |
song = gr.Textbox(label="Song")
|
63 |
artist = gr.Textbox(label="Artist")
|
64 |
+
output_song_meaning = gr.Textbox(label="Meaning")
|
65 |
+
output_song_theme = gr.Label(num_top_classes=6, label="Theme")
|
66 |
+
gr.Interface(fn=song_insight, inputs=[song, artist], outputs=[output_song_meaning, output_song_theme])
|
67 |
example = gr.Examples([['Maroon', 'Taylor Swift'], ['Devil In Her Heart', 'The Beatles'],
|
68 |
['Time Machine', 'Jay Chou'], ['Last Farewell', 'BIGBANG']], [song, artist])
|
69 |
|