Just finished all courses in this series and playing around to “vectorize” this lesson with Pandas. The code below will work in the DeepLearning environment. I have also implemented it in Google Collaboratory, including calls to OpenAI using my own account, which I can post if there is interest.
- Imports
# Imports
from helper_functions import get_llm_response, print_llm_response, display_table
import pandas as pd
import os
# from IPython.display import Markdown
# import csv
- Load itinerary.csv into Pandas
df = pd.read_csv('itinerary.csv')
df.head()
- Create Prompts for all Cities
df['Prompt'] = "I will visit " + df['City'] + ", " + df['Country'] + ", from " + df['Arrival'].astype(str) + " to " + df['Departure'].astype(str) + ". Please create a detailed daily itinerary."
df.head()
- Get Responses for all Cities
df['Response'] = df['Prompt'].apply(get_llm_response)
df.head()
- Save Dataframe to detailed_itineraries.csv
df.to_csv('detailed_itineraries.csv', index=False)
!cat detailed_itineraries.csv
- Save detailed itineraries for each city in separate txt files
# Create the 'Output' directory if it doesn't exist
if not os.path.exists('Output'):
os.makedirs('Output')
# Iterate over the DataFrame
for index, row in df.iterrows():
city = row['City']
response = row['Response']
# Create the file path
file_path = os.path.join('Output', f'{city}.txt')
# Write response to the file
with open(file_path, 'w') as f:
f.write(response)
!ls -l Output