File size: 1,648 Bytes
2682f23
 
 
 
 
 
 
 
1040dfe
 
2682f23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1040dfe
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
import gradio as gr
from langchain import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain_community.retrievers import WikipediaRetriever
import os


OPENAI_API_KEY = os.environ['OPENAI_API_KEY']

def song_meaning(song, artist):
    artist_input = artist.title()
    song_input = song.title()
    query_input = f"{song_input} by {artist_input}"

    retriever = WikipediaRetriever()
    docs = retriever.get_relevant_documents(query=query_input)

    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)

    llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=0)
    chain = LLMChain(llm=llm, prompt=prompt_template_song_meaning)
    results = chain.run(artist=artist_input, song=song_input, content=docs[0].page_content)

    return results


with gr.Blocks(theme=gr.themes.Soft()) as demo:
    song = gr.Textbox(label="Song")
    artist = gr.Textbox(label="Artist")
    output = gr.Textbox(label="Meaning")
    gr.Interface(fn=song_meaning, inputs=[song, artist], outputs=output)
    example = gr.Examples([['Maroon', 'Taylor Swift'], ['Devil In Her Heart', 'The Beatles'],
                           ['Time Machine', 'Jay Chou'], ['Last Farewell', 'BIGBANG']], [song, artist])

demo.launch()