song-insight / song-insight-app.py
yangswei's picture
Update song-insight-app.py
a898915 verified
raw
history blame
3.49 kB
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
from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory
import os
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
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=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=0)
safety_setting = {
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
}
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=GOOGLE_API_KEY, temperature=0,
safety_settings=safety_setting, top_p=0)
# 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 and
do not bold any text.
"""
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 song recom
template_song_recom = """
here are the meaning of {song} by {artist}:
{song_meaning}
can you give me a 3 songs recommendation similar to the meaning of the song above?
use this format for the output and do not bold any text:
1. recommended song 1, with a brief explanation.
2. recommended song 2, with a brief explanation.
3. recommended song 3, with a brief explanation.
"""
prompt_template_song_recom = PromptTemplate(input_variables=["artist", "song", "song_meaning"], template=template_song_recom)
chain_song_recom = LLMChain(llm=llm, prompt=prompt_template_song_recom)
results_song_recom = chain_song_recom.run(artist=artist, song=song, song_meaning=results_song_meaning)
return results_song_meaning, results_song_recom
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_recom = gr.Textbox(label="Song Recommendation")
gr.Interface(fn=song_insight, inputs=[song, artist], outputs=[output_song_meaning, output_song_recom])
example = gr.Examples([["They Don't Care About Us", 'Michael Jackson'], ["Bad Romance", 'Lady Gaga'],
["Let It Be", "The Beatles"], ["Life Goes On", 'BTS'], ["Blank Space", "Taylor Swift"]], [song, artist])
demo.launch()