Deploy a Language AI App Easily with Cohere and Streamlit

So you have an idea for a language AI app using Cohere. But how do you deploy it and share it with the world? You can do that with Streamlit. In this article, we’ll look at how we can quickly prototype a Startup Idea Generator web app.

Deploy a Language AI App Easily with Cohere and Streamlit
Deploy a Language AI App Easily with Cohere and Streamlit

You woke up this morning with an idea for a language AI app. This app would generate innovative startup ideas just by a user entering the nature of the industry, such as productivity, education, food, and so on. It could help aspiring founders brainstorm the next big thing they are going to build. You experimented with the Cohere API, and it worked!

Now you can’t wait to test it out and get much-needed feedback by sharing it with the world. And then you realize you’re stuck.

Web development is a multi-stage process, and the front-end development will usually come back in towards the end of the process. And while it may seem like front-end development should be one of the quickest parts of the process, it often turns out to be quite a lengthy endeavor.

Enter Streamlit. It provides a very fast way to build front-end web applications with very little hassle. In this article, we’ll see how we can quickly prototype our Startup Idea Generator idea, deploy it as a web app and share it with the world!

To give you an idea, here’s a preview of what the final app looks like.

But let’s go back to the start and see how we might approach this. We can break the plan into three key steps, as follows:

  1. Build the language AI back-end with Cohere
  2. Build the front-end with Streamlit
  3. Deploy with Streamlit Cloud

We’ll see only selected code snippets in this article, but you can find the complete code in this repository.

1 - Build the language AI back-end with Cohere

Build the startup idea generator

The first thing we need to do is to build our main feature - the startup idea generator. For this, we use Cohere’s Generate endpoint, which generates text given an input, called a prompt.

Creating prompts, or prompt engineering, does require some experimentation. This is also good news - it means there’s huge room for you to innovate and apply your own creativity in designing prompts that get the best out of the endpoint. Read this documentation if you are interested in learning more.

In our case, we create a prompt containing a few examples of an industry and its corresponding startup idea. And since we want our app to take a user input (i.e. the industry) before generating an idea, our prompt ends with the term “Industry:”.

The following is what the prompt looks like.

This program generates a startup idea given the industry.

Industry: Workplace
Startup Idea: A platform that generates slide deck contents automatically based on a given outline

--
Industry: Home Decor
Startup Idea: An app that calculates the best position of your indoor plants for your apartment

--
Industry: Healthcare
Startup Idea: A hearing aid for the elderly that automatically adjusts its levels and with a battery lasting a whole week

--
Industry: Education
Startup Idea: An online primary school that lets students mix and match their own curriculum based on their interests and goals

--
Industry:

We then build a function that leverages Cohere’s Python SDK to take in a user input and return the generated text, and the code looks as follows.

def generate_idea(industry):
    base_idea_prompt="< OUR FULL PROMPT STRING >"
    response = co.generate(
        model='xlarge',
        prompt = base_idea_prompt + " " + industry + "\nStartup Idea: ",
        max_tokens=50,
        temperature=0.5,
        k=0,
        p=0.7,
        frequency_penalty=0.1,
        presence_penalty=0,
        stop_sequences=["--"])
        response = response.generations[0].text
        response = response.replace("\n\n--","").replace("\n--","").strip()
    return response

Notice that now the prompt is a concatenation of the base prompt that we created earlier, the industry name provided by the user, and the “Startup Idea:” prefix. This ‘forces’ the model to generate an output in a similar pattern to our prompt, that is, the startup idea description.

We also passed a number of arguments to the Generate endpoint, such as max_tokens, temperature, stop_sequences, and so on. If you’d like to know what they mean, you can find their definitions in the Generate API reference.

Doing a quick test, entering “Public Transportation” returns the following startup idea: “A small electronic device that notifies users when they have to get off the train or bus”. Looking good so far.

Build the startup name generator

Being able to generate startup ideas is great, but it would make the app much more exciting if we could also suggest a startup name for each of the ideas. For this, using a similar approach as before, we create a new prompt containing a few examples of a startup idea description and its corresponding startup name. The following is what the prompt looks like.

This program generates a startup name and name given the startup idea.

Startup Idea: A platform that generates slide deck contents automatically based on a given outline
Startup Name: Deckerize

--
Startup Idea: An app that calculates the best position of your indoor plants for your apartment
Startup Name: Planteasy 

--
Startup Idea: A hearing aid for the elderly that automatically adjusts its levels and with a battery lasting a whole week
Startup Name: Hearspan

--
Startup Idea: An online primary school that lets students mix and match their own curriculum based on their interests and goals
Startup Name: Prime Age

--
Startup Idea:

We create another function that takes in a startup idea as the input and returns the generated startup name. The code looks as follows.

def generate_name(idea):
    base_name_prompt="< OUR FULL PROMPT STRING >"
    response = co.generate(
        model='xlarge',
        prompt = base_name_prompt + " " + idea + "\nStartup Name:",
        max_tokens=10,
        temperature=0.5,
        k=0,
        p=0.7,
        frequency_penalty=0,
        presence_penalty=0,
        stop_sequences=["--"])
    response = response.generations[0].text
    response = response.replace("\n\n--","").replace("\n--","").strip()
    return response

Doing a quick test, entering the public transportation startup idea we got earlier returns the following startup name - “Beepro”. Not bad.

2 - Build the front-end with Streamlit

Build the basic components

Now that we’ve got the text generation part working, let’s create the front-end with Streamlit. Streamlit is an open-source Python library that makes it easy to create and share custom web apps. Instead of hours or even days, you can get a basic front-end up and running and deploy it in a matter of minutes.

The following is our front-end code using Streamlit, that in just a few lines, gets us a basic working app.

st.title("🚀 Startup Idea Generator")

form = st.form(key="user_settings")
with form:
    industry_input = st.text_input("Industry", key = "industry_input")
    generate_button = form.form_submit_button("Generate Idea")
    if generate_button:
        startup_idea = generate_idea(industry_input)
        startup_name = generate_name(startup_idea)
        st.markdown("##### " + startup_name)
        st.write(startup_idea)

It uses a number of features from the Streamlit API, as follows:

  • A form via the st.form() control flow
  • st.title() for the header title
  • st.text_input() for the user text input
  • A submit button that comes with st.form()
  • st.markdown() and st.write() to display the generated text.

We can deploy this Streamlit app by running the following command at the terminal (in this example, our code above is stored in app.py).

$ streamlit run app.py

Running the app locally renders the following.

A basic version of the app
A basic version of the app

Add some interactivity

Now that we have a basic version of the app running, let’s see how we can enhance it. One way is giving greater control to users when generating the ideas. Let’s add a couple of options to add interactivity to the app.

The first one is straightforward. We want to let users generate more than one idea in one go. For this, we’ll use the st.slider() widget to let users input the number of generations to make. We’ll set the maximum number of generations to 10.

The second is a bit more interesting. With the Generate endpoint, we can use the temperature parameter to control the randomness of the model output. The value can range between 0 and 5. Lower values tend to generate more “predictable” output, while higher values tend to generate more “creative” output. The sweet spot is typically between 0 and 1, and for our app, we’ll just add a small buffer and set the range to be between 0.1 and 0.9. We’ll name this user setting Creativity.

For this, we’ll also use st.slider() to let the users control the temperature value. We’ll also need to modify the generate_idea() and generate_name() functions to accept the temperature argument, to be passed to the Generate API call. The following is the example with generate_idea().

def generate_idea(industry, temperature):
    base_idea_prompt="< OUR FULL PROMPT STRING >"
    response = co.generate(
    model='xlarge',
    prompt = base_idea_prompt + " " + industry + "\nStartup Idea: ",
    max_tokens=50,
    temperature=temperature,
    . . .
    . . .

Let’s also add a couple more things. First, we’ll use st.progress() to show a progress bar as the idea generation takes place. Second, we’ll add a check to the Industry user input to make it a required field.

Altogether, the completed front-end Streamlit code looks like the following, about 20 lines.

st.title("🚀 Startup Idea Generator")

form = st.form(key="user_settings")
with form:
# User input - Industry name
industry_input = st.text_input("Industry", key = "industry_input")

# Create a two-column view
col1, col2 = st.columns(2)

with col1:
    # User input - The number of ideas to generate
    num_input = st.slider("Number of ideas", value = 3, key = "num_input", min_value=1, max_value=10)

with col2:
    # User input - The 'temperature' value representing the level of creativity
    creativity_input = st.slider("Creativity", value = 0.5, key = "creativity_input", min_value=0.1, max_value=0.9)

# Submit button to start generating ideas
generate_button = form.form_submit_button("Generate Idea")

if generate_button:
    if industry_input == "":
        st.error("Industry field cannot be blank")
    else:
        my_bar = st.progress(0.05)
        st.subheader("Startup Ideas")
        for i in range(num_input):
            st.markdown("""---""")
            startup_idea = generate_idea(industry_input,creativity_input)
            startup_name = generate_name(startup_idea,creativity_input)
            st.markdown("##### " + startup_name)
            st.write(startup_idea)
            my_bar.progress((i+1)/num_input)

And with that, the final front-end now looks like the following.

The final version of the app
The final version of the app

3 - Deploy with Streamlit Cloud

Now that our app is ready, let’s deploy it to the web so everyone can start interacting with it. You can deploy the app via your own hosting option, but there’s also a quick and hassle-free way to do it — via Streamlit Cloud.

Streamlit Cloud launches apps directly from a GitHub repository. So, before you can deploy your app, do the following steps.

  1. Push your code to a GitHub repository
  2. Sign up for Streamlit Cloud
  3. Connect your Streamlit Cloud to the GitHub repository

Once you have completed the steps, on your Streamlit Cloud page, click on New app.

Creating a new app in Streamlit
Creating a new app in Streamlit

You’ll be brought to the Deploy an app page, where you can select the repository, the branch and the main file path i.e. the code we wrote earlier. You will also need to add your Cohere API key in the Advanced settings… link (more information here).

Deploying an app in Streamlit
Deploying an app in Streamlit

And once that is done, click Deploy!. Wait a few minutes for Streamlit to deploy it, and the app is live. Now literally anyone on the web can access it!

Let’s now take the app for a spin and, say, we want to generate three startup ideas in the productivity industry.

Let’s first try with a low Creativity setting, of say 0.2, which should give us ideas that are more predictable and possibly more proven. The outcome? It generates ideas revolving around the usual suspects — to-do, time management, and scheduling.

Next, we try with a high Creativity setting, of say 0.8, which should result in ideas that are more creative but are probably bordering on being too ambitious. This time, the ideas are a lot more diverse — travel planning, diagram drawing, and personal finance.

Here are the screenshots of these generated ideas.

Example outputs generated with different `Creativity' settings, 0.2 (left) and 0.8 (right)
Example outputs generated with different `Creativity' settings, 0.2 (left) and 0.8 (right)

And that concludes our startup idea generator prototype, powered by Cohere and deployed with Streamlit.

Conclusion

This example demonstrated how, using Cohere and Streamlit, you can quickly turn your language AI product idea into a basic prototype. It’s an amazing feeling to be able to deploy a web app without spending long, frustrating hours building a front-end.

We have only covered the surface of what’s possible, though. In this article, we covered the Generate endpoint for generating startup ideas. But there are many more ways how Generate can be applied, such as in this article. Or you can read this article if you’d like to dive deeper into prompt engineering.

Additionally, there are other endpoints you can experiment with, such as Classify and Embed, which you can browse in the Cohere API documentation. Hopefully, these resources can help you start discovering ideas and finding what to build next!

To get started, sign up for the Cohere platform here, free credits included!