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