Screenshot of GPT-3 powered trip scheduler application created with Streamlit

Create a GPT-3 Powered Trip Scheduler with Streamlit

Creating a GPT-3 Powered Trip Scheduling Service with Streamlit

In this tutorial, we will create a simple GPT-3 powered Streamlit service for trip scheduling. This app will utilize the capabilities of GPT-3 to generate tailored trip schedules based on user input, while Streamlit will help us build an easy-to-use interface.

Prerequisites

Before we begin, ensure that you have the following prerequisites:

  • Python installed on your machine.
  • Access to OpenAI's GPT-3 model via an API key.
  • Knowledge of basic Python programming.
  • Streamlit installed for web app development.

Installing Necessary Libraries

To get started, you need to install the required libraries using pip. Run the following command:

pip install openai streamlit python-dotenv

Setting Up the Environment

Next, create a .env file in your project directory and include your OpenAI API key like this:

OPENAI_API_KEY=your_openai_api_key

Creating the main.py File

Now, let’s create the main.py file. This file will import all necessary libraries and load our API key from the .env file. You can use the following code structure:

import os
from dotenv import load_dotenv
import openai
import streamlit as st

load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')

Building the Streamlit App

Let’s move on to building our Streamlit app. Start by creating a function that generates the trip schedule prompt based on user input. Here's a simple implementation:

def generate_trip_schedule(location, days, interests):
    prompt = f"Create a trip schedule for {days} days in {location} focusing on {interests}.
    Use bullet points to outline daily activities."
    response = openai.Completion.create(
        engine='text-davinci-003',
        prompt=prompt,
        max_tokens=500
    )
    return response['choices'][0]['text']

Creating the User Interface

With the prompt generation function in place, we can now set up the user interface using Streamlit.

# Create input form
st.title('Trip Scheduler')
location = st.text_input('Enter Trip Location:')
days = st.number_input('Number of Days:', min_value=1, max_value=30)
interests = st.text_input('What are your interests? (e.g., adventure, culture)')

if st.button('Generate Schedule'):
    schedule = generate_trip_schedule(location, days, interests)
    st.write('### Your Trip Schedule:')
    st.write(schedule)

Running the Streamlit App

Finally, you can run your Streamlit app by navigating to your project's directory in the terminal and executing:

streamlit run main.py

Result

Once you run the app, open your browser to the provided local URL. Input your desired trip details, and click the "Generate Schedule" button. You should see the generated trip schedule displayed on the page!

Conclusion

This tutorial has guided you through the process of creating a simple GPT-3 powered trip scheduling application using Streamlit. You can further enhance this app by adding more features like saving schedules, offering tips, or integrating maps. Feel free to experiment and enrich your app further!

JSON Implementation

If you want to structure the generated schedule in a JSON format for better data handling, you could modify the return section of the function:

def generate_trip_schedule(location, days, interests):
    prompt = f"Create a trip schedule for {days} days in {location} focusing on {interests}.
    Use bullet points to outline daily activities."
    response = openai.Completion.create(
        engine='text-davinci-003',
        prompt=prompt,
        max_tokens=500
    )
    schedule_list = response['choices'][0]['text'].split('\n')
    schedule_json = {'trip_schedule': schedule_list}
    return schedule_json

This structure allows efficient storage and retrieval of your schedule data.

Back to blog

Leave a comment