Creating a random charity picker with Python and Bing AI
Is there anything you want to create? Start asking an AI and you may be surprised at what you could achieve with it.
Today I learned I can use generative AI tools to make simple Python scripts and the possibilities now seem endless.
I was asking Bing AI for charity ideas, and it mentioned GlobalGiving where they crowdfund for over 6,000 active charitable projects. I thought “Well, what if I could have a way to pick a few at random out of those thousands?”. Of course, I tried asking Bing AI and Bard since they are connected to the internet. The results were lackluster. Bing AI gave me fake results with everything linked back to the main webpage at GlobalGiving. Bard tried a little harder but kept giving me the same set of projects that were no longer active or expired.
At this point I decided to use ChatGPT and ask it how I could make a simple program to randomly choose projects to donate to. To my surprise, it gave me step-by-step instructions on how to make a Python script and connect it via API.
When I asked for clarification on how to install and use Python it easily gave those instructions too.
Once I had Python installed and API access at GlobalGiving by following ChatGPT’s instructions, I tried the script, and it gave errors unfortunately. I then spent a couple of hours wrestling between Bard, Bing AI, and ChatGPT to find a solution for those errors to no avail.
A breakthrough occurred when I started feeding in snippets of the API documentation into Bing AI’s “creative mode” and asked it to create a script randomly selecting from the active projects.
I want to emphasize here that it was key to figure out which parts of the documentation to feed in. I was able to determine that I need to retrieve all the active projects as outlined in the API doc. I then had to share an XML example from a different page in documentation. This is where my human judgment came in and it made all the difference.
After sharing those snippets and reiterating my goal Bing AI created working code with explanations! To my best understanding, it downloads an XML data set with the API connection, parses it for the desired elements, and then randomly picks and prints info for four projects from the list of those XML elements. It might be hard to tell from my screenshot below, but the output is exactly the task and details I wanted.
Here’s the code I used for the random project selector as created by Bing AI
import requests
import xml.etree.ElementTree as ET
import random
# Define the API key and the download URL
api_key = "YOUR_API_KEY"
download_url = "https://api.globalgiving.org/api/public/projectservice/all/projects/active/download.xml"
# Request the download URL for the XML file containing all active projects
response = requests.get(download_url, params={"api_key": api_key})
# Parse the XML response and extract the URL element
root = ET.fromstring(response.text)
url = root.find("url").text
url = url.replace("&", "&")
# Download the XML file from the URL
projects = requests.get(url)
# Parse the XML file and extract the project elements
projects_root = ET.fromstring(projects.text)
projects_list = projects_root.findall("project")
# Pick four random projects from the list
random_projects = random.sample(projects_list, 4)
# Print out the information of the four random projects
for project in random_projects:
print(f"Project Title: {project.find('title').text}")
print(f"Project Summary: {project.find('summary').text}")
print(f"Project Country: {project.find('country').text}")
print(f"Project Link: {project.find('projectLink').text}")
print()
I used Bing AI in the same afternoon to take a recipe collection file I had and create a separate file for each recipe based on a consistent separator I had in the file. Again, the code worked and finished the job in seconds.
# Open the input file
with open("recipes.txt", "r") as input_file:
# Read the whole file as a string
input_data = input_file.read()
# Split the string by the separator
recipes = input_data.split("=====================================================================================")
# Loop through each recipe
for recipe in recipes:
# Skip empty recipes
if recipe.strip():
# Split the recipe by newline characters
lines = recipe.split("\n")
# Get the first non-empty line as the file name
file_name = ""
for line in lines:
if line.strip():
file_name = line.strip()
break
# Write the recipe to a new file with the file name
with open(file_name + ".txt", "w") as output_file:
# Strip any leading and trailing whitespace from the recipe
recipe = recipe.strip()
output_file.write(recipe)
This makes me excited for the possibilities of using Python and AI to make small, customized scripts for my own interests. This makes me want to take an online course on Python so I can better understand the process and make better judgement calls. I have a rough idea what these scripts are doing at each line based on how the AI explained it but I’m certainly not knowledgeable enough where I could recreate these on my own from scratch. The more context knowledge I can get, the better I can collaborate with AI.
Just a year ago it would’ve taken me days to piece together code based on my limited understanding. Now I can create functional small programs during the afternoon and be on my way.
There is a lot of both utopian hype and dystopian fear surrounding AI, but I do believe it has enormous potential as a tool set for creating and improving things in ways that were previously difficult, if not impossible, before. Suddenly it makes those goals something achievable and within grasp.
Of course, the generative AIs out there may become advanced soon enough where I won’t even need to create a little program to choose four random projects from a charity website because it could handle it directly. But using AI as a Python co-pilot is still exciting to me and can open doors to whatever else I can think of to make.
Is there anything you want to create? Start asking an AI and you may be surprised at what you could achieve with it.