Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
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 |
+
|
| 7 |
+
def song_meaning(song, artist):
|
| 8 |
+
artist_input = artist.title()
|
| 9 |
+
song_input = song.title()
|
| 10 |
+
query_input = f"{song_input} by {artist_input}"
|
| 11 |
+
|
| 12 |
+
retriever = WikipediaRetriever()
|
| 13 |
+
docs = retriever.get_relevant_documents(query=query_input)
|
| 14 |
+
|
| 15 |
+
template_song_meaning = """
|
| 16 |
+
{artist} has released a song called {song}.
|
| 17 |
+
|
| 18 |
+
{content}
|
| 19 |
+
|
| 20 |
+
based on the the content above what does the song {song} by {artist} tell us about? give me a long explanations
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
prompt_template_song_meaning = PromptTemplate(input_variables=["artist", "song", "content"], template=template_song_meaning)
|
| 25 |
+
|
| 26 |
+
llm = ChatOpenAI(openai_api_key=os.environ['OPENAI_API_KEY'], model_name="gpt-3.5-turbo", temperature=0)
|
| 27 |
+
chain = LLMChain(llm=llm, prompt=prompt_template_song_meaning)
|
| 28 |
+
results = chain.run(artist=artist_input, song=song_input, content=docs[0].page_content)
|
| 29 |
+
|
| 30 |
+
return results
|
| 31 |
+
|
| 32 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 33 |
+
song = gr.Textbox(label="Song")
|
| 34 |
+
artist = gr.Textbox(label="Artist")
|
| 35 |
+
output = gr.Textbox(label="Meaning")
|
| 36 |
+
gr.Interface(fn=song_meaning, inputs=[song, artist], outputs=output)
|
| 37 |
+
example = gr.Examples([['Maroon', 'Taylor Swift'], ['Devil In Her Heart', 'The Beatles'],
|
| 38 |
+
['Time Machine', 'Jay Chou'], ['Last Farewell', 'BIGBANG']], [song, artist])
|
| 39 |
+
|
| 40 |
+
demo.launch()
|