![](https://crypto4nerd.com/wp-content/uploads/2023/07/1f_ZTYOoIjgjT5L_FtwBiEg-1024x577.png)
🧑💻Let’s start with the code </>
Firstly, About the app —
We will create a Rap Song Generator. This will be our very own LLM-Powered web app.
Based on a given topic, it will generate an appropriate song title & then it will also generate verses for that title. Here is a demo of the app:
Exciting? Let’s start building!🧑🏻💻💪🏻
🙋🏻 The import statements & initial setup —
There are 3 main packages we need for this project. Install them & any other packages if needed.
After that, we will import the API KEY & set it as an environment variable. Also, since there are multiple GPT models we can pick for our app, I have defined the one I want in a variable. You can change it as per your needs.
#pip3 install streamlit
#pip3 install langchain
#pip3 install openaiimport os
import streamlit as st
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain, SequentialChain
from langchain.chat_models import ChatOpenAI
#import API key from config file
from config import OPEN_API
# environment variable & the model to use
model_id = 'gpt-3.5-turbo'
os.environ["OPENAI_API_KEY"] = OPEN_API
✒️ UI Elements (streamlit) –
Streamlit has a very user-friendly API that allows us to easily create a frontend for our ML/Data Science apps.
In the below code, we first created a title for our app & just below it we have an input box for the user where he/she will enter a topic for the song.
After that we have 2 more headings, one for what the title is going to be & the other for the verses. Each of these headings has its own output boxes, where the outputs will be attached once they are generated.
# main title & the input box
st.title("🎙️ Rap Song Generator 🎙️🔥")
prompt = st.text_input("topic for the song?") # second heading & the output area for song title
st.markdown("#### Song Title")
title_box = st.empty()
# third heading & the output area for verses
st.markdown("#### Verses")
verse_box = st.empty()
When input is provided, it will be stored in the ‘prompt’ variable. This variable will later be used to generate output.
💥 Prompt Templates, Chains & LLM (langchain, OpenAI) –
Now we need to create templates for both title generation & verse- generation.
- In the first prompt, the input is the ‘topic’ that the user entered from the UI. This ‘topic’ will be used to format the template string. This template will be used to output the ‘title’ for the song.
- The second prompt will use the ‘title’ generated above as it’s input & will use that to format the second template string, which will generate verses based on this input ‘title’. The output of this template(chain) will be the ‘verses’.
Once we have 2 templates, we will create 2 chains for each of them. The first chain will be the title chain & it will make use of the title template.
Similarly, the second chain will be the verses chain & it will make use of the verse template.
The output we will get is going to be in a dictionary format, so for each chain, we can specify what to use as the key. This can be done by setting “output_key = something” on both chains.
# prompt template for generating title
title_template = PromptTemplate(
input_variables = ["topic"],
template = "generate a rap song title on the topic: {topic}"
)# prompt template for generating verses
verse_template = PromptTemplate(
input_variables = ["title"],
template = "generate 2 rhyming verses for a rap song titled : {title}"
)
# building chains
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True, output_key="title")
verse_chain = LLMChain(llm=llm, prompt=verse_template, verbose=True, output_key="verse")
# combining chains
sequential_chain = SequentialChain(
chains=[title_chain, verse_chain],
input_variables=["topic"],
output_variables=["title", "verse"],
verbose=True,
)
At the end, we combine both chains & they will run sequentially when we start execution. The input variables in this combined chain will be [“topic”] & the output variables will be [“title”, “verse”] as defined when combining the chains.
📺 Outputting to the screen —
Once we have input from the user, we will run the combined chain that we just created. We will pass the prompt as the “topic” since that is the name we defined for our “input_variables” parameter.
The response will be a dictionary & we can extract the required text from that using keys that we also defined previously in the ‘output_variables’ & ‘output_keys’ variables.
# run chains if prompt is provided
if prompt:
response = sequential_chain({
"topic" : prompt
})title = response["title"]
body = response["verse"]
# display each output in it's own output box
title_box.markdown(title)
verse_box.markdown(body)
To run the app, use the command —
streamlit run filename.py