Spaces:
Sleeping
Sleeping
File size: 3,423 Bytes
2682f23 59f7528 2682f23 e1512bf 0bacb15 8a77843 2682f23 a898915 2682f23 e1512bf 2682f23 e1512bf 2682f23 e1512bf 8a77843 e8a04ec e1512bf 2682f23 61e3a1b 8a77843 2682f23 8a77843 e1512bf 8a77843 2682f23 8a77843 e1512bf 8a77843 e1512bf 2682f23 8a77843 2682f23 8a77843 2682f23 e1512bf 8a77843 61e3a1b 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 75 76 77 78 |
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)
# 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 clear 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'],
["Let It Be", "The Beatles"], ["Blank Space", "Taylor Swift"]], [song, artist])
demo.launch()
|