🤖

Part 4: Automatic For The People - Let’s Automate All This

Now that we know we can use ChatGPT to create meaningful content for SEO let’s see how we go about doing this. Here’s the requirements:
  • I have richest neighborhood maps for 50 top us cities.
  • These are stored in a GCP bucket on public URL’s.
  • The maps are in html format created with Folium library in Python.
  • ChatGPT accepts png or jpg file formats
 
Here’s what I will need to do for each map.
  1. Download the map and convert it to png format
  1. Call ChatGPT API with the map and the prompt
I’ll go into details of each with the code that can be used.

Converting Folium maps into PNG

We have the relevant folium maps stored in GCP bucket in html format. The only way to convert these html maps to PNG format that ChatGPT can use is to take a screenshot of it using Selenium package. We’ll do this in a google Colab notebook. Here are the steps we’ll take.
  1. Install packages related to Selenium, chrome and required drivers.
    1. %%capture !pip install selenium webdriver-manager pillow !apt-get update !apt-get install chromium-bsu chromium-driver from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager
  1. Write a webdriver function for telling how to read the html from URL given
    1. def web_driver(window_size_width=1920, window_size_height=1200): options = webdriver.ChromeOptions() options.add_argument("--verbose") options.add_argument('--no-sandbox') options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument(f"--window-size={window_size_width}, {window_size_height}") options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(options=options) return driver
  1. Write a function saving the screenshot
    1. def save_screenshot_of_url(url, path): driver = web_driver(800, 600) driver.get(url) screenshot_path = path # print(screenshot_path) driver.save_screenshot(screenshot_path)
  1. Call the functions with the correct URL
    1. city = 'asutin' url_for_austin_map = 'https://storage.googleapis.com/decision-science-lab-bucket/client_map_assets/free-location-analyzer/census_reports/seo_map_miles_20_n_cbg_35_Austin_TX_01_17_2024_19_45.html' screenshot_path = f'{city}.png' save_screenshot_of_url(url_for_austin_map, screenshot_path)
 
You can see the Google Colab code here
I took some of the code aboce from this video. https://www.youtube.com/watch?v=mOJiWrjFVKY

Call ChatGPT API with the map image and the prompt

In this step we are going to write the code for calling ChatGPT API with our map and prompt to get an analysis of the map.
Here’s the steps we’ll take
  1. Install openai pip package and import the necesarry libraries
    1. !pip install OpenAI import requests import openai import base64
  1. A function to encode the PNG image in the right format
    1. def encode_image(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8')
  1. A function to call ChatGPT API with the image and the prompt
    1. def get_image_analysis(image_path, prompt): base64_image = encode_image(image_path) headers = { "Content-Type": "application/json", "Authorization": f"Bearer {secrets['openai_api_key']}" } payload = { "model": "gpt-4-vision-preview", "messages": [ { "role": "user", "content": [ { "type": "text", "text": prompt }, { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } } ] } ], "max_tokens": 1000 } response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) return response.json()
  1. Call the API with the right image to get the results
    1. chat_gpt_result = get_image_analysis('austin.png', get_prompt_web('austin')) chat_gpt_result
  1. We get the following result
    1. {'id': 'chatcmpl-8lh8Jv6XUqsN7wgsirZMtsRAkT3yz', 'object': 'chat.completion', 'created': 1706377911, 'model': 'gpt-4-1106-vision-preview', 'usage': {'prompt_tokens': 1037, 'completion_tokens': 425, 'total_tokens': 1462}, 'choices': [{'message': {'role': 'assistant', 'content': "Welcome to Austin, a city known for its vibrant culture, dynamic economy, and diverse neighborhoods. As you explore the map, you'll discover a visual guide to understanding the city's demographic profile analysis through shaded areas indicating various income levels. The heart icon situated in the city center represents the bustling downtown, a hub of activities and opportunities.\n\nThe shaded pink areas you see on the map highlight neighborhoods with the highest incomes in Austin. These areas are ranked numerically, with lower numbers denoting the highest income levels. As per neighborhood stats, a fascinating pattern emerges: clusters of affluent neighborhoods are primarily located to the northwest and west of the city center. Areas such as those labeled '5', '11', and '19' are examples of these high-income zones, suggesting a trend toward these directions for those seeking the best neighborhoods.\n\nUpon closer inspection, neighborhood insights gained from the map reveal that wealth is not uniformly distributed across Austin. Instead, it demonstrates a tendency for higher-income areas to group together, forming a belt of affluence that wraps around the north-western to the western fringe of the city. This clustering effect may be due to various factors, including the appeal of certain school districts, the proximity to upscale amenities, or the natural beauty of the region, which often includes rolling hills and lakefront vistas.\n\nLocation analysis further shows that as you move further from the heart icon – Austin's city center – toward the outskirts, particularly to the southwest and northwest, the higher the income ranking becomes. In stark contrast, areas immediately surrounding the city center vary significantly in income levels, reflecting the dynamic and diverse fabric of central Austin.\n\nThis content, aimed at non-technical users, is provided to assist in analyzing the map and data more efficiently and has been generated with the help of ChatGPT, an AI. The information, which draws from public census office publications, is presented in a user-friendly manner to offer valuable insights into Austin's neighborhoods without the complexity of technical jargon. Whether you are curious about your community or considering relocation, this map serves as an essential tool for a simplified examination of Austin's socioeconomic landscape."}, 'finish_reason': 'stop', 'index': 0}]}
 
You can see the colab notebook for calling the ChatGPT Vision API here.