Spaces:
Sleeping
Sleeping
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() | |