Building a generative AI-based GeoGuesser

building-a-generative-ai-based-geoguesser

Learn how to use generative AI to create a GeoGuesser game app

Photo by Juliana Kozoski on Unsplash

For those who are not familiar with Geoguessr, it is a simple and fun game in which you are placed on a random world location on Google Maps and have to guess the location during a time countdown, the goal is to get your guess as close as possible to the real location. After playing this game with some friends I started to think about how I could use the game concept to build something that allows me to practice with generative AI, that was how this project “GenAI GeoGuesser” was born. On my version of the game you will have to guess the country name based on hints generated by AI models, to help with the understanding here are a few screenshots showcasing the game’s workflow.

How the game works

Starting game menu

First, the user selects the desired hint modalities, you can choose any number of options between “Audio”, “Text” and ”Image”, you also must select the number of hints that will be generated for each modality. For the example above you would get 1 hint for each one of the 3 types.

Sample text hint

The text hint will have a textual description of the country.

Sample image hint

The image hint will be images that resemble the country.

Sample audio hint

Finally, the audio hint should be an audio/sound related to the country (In my experience the audio hints do not work as well as the other two).

All the models used to generate the hints above have parameters to fine-tune the generation process, you could generate longer text or audio hints, and even change the models. The repository has intuitive parameters to play with.

Once you finish evaluating all the hints and are ready to guess, type the guess in the “Country guess” field.

Wrong guess message

If the guess is wrong you will get the correct country name and the distance between your guess and the correct place.

Correct guess message

If the guess is correct you will receive a congratulations message.

Now that you are familiar with the game’s workflow let’s understand what is happening under the hood at each step.

Building GenAI GeoGuesser

The game starts with the country selection, here I wanted to mimic the original Geoguessr behavior where probabilistically you would be dropped into larger countries (more chance of being placed there), for this reason, just randomly selecting a county would not be enough, small countries would have the same chance of large ones, thankfully I found the countryinfo lib which provided a list of countries and some metadata like country area, below you can see how the code looks like.

Selecting the country

from countryinfo import CountryInfo

country_list = list(CountryInfo().all().keys())

# build a dict with country:area pairs
country_df = {
country: CountryInfo(country).area() for country in country_list
}
country_df = pd.DataFrame(country_df.items(), columns=["country", "area"])

# pick a random country where the probability is the country's area
country = country_df.sample(n=1, weights="area")["country"].iloc[0]

Text hints

For the text hint generation step, I’ve chosen a Gemma model, the version with 2 billion parameters is able to generate high-quality text while still running fast enough not to disrupt the user experience, the Gemma models are a family of lightweight, state-of-the-art open models built from the same research and technology used to create the Gemini models.

from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig

tokenizer = AutoTokenizer.from_pretrained("google/gemma-1.1-2b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-1.1-2b-it")

prompt = f"Describe the country {country} without mentioning its name"
input_ids = tokenizer(prompt, return_tensors="pt")
text_hint = model.generate(**input_ids)

# extract the text from the output and clean up
text_hint = (
tokenizer.decode(text_hint, skip_special_tokens=True)
.replace(prompt, "")
)

You can also run the text hint generation using Gemini models via Vertex to get faster and better-quality outputs (check the configs file).

from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.5-pro-preview-0409")

prompt = f"Describe the country {country} without mentioning its name"
responses = model.generate_content(prompt)

# extract the text from the output
text_hint = responses.candidates[0].content.parts[0].text

Image hints

For the image generation part, I’ve chosen the SDXL-Turbo model, this is the version of the popular Stable Diffusion model that can generate high-quality images with as little as a single inference step.

from diffusers import AutoPipelineForText2Image

model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")

prompt = f"An image related to the country {country}"
img_hints = model(prompt=prompt).images

Audio hints

To generate the audio hints we will be using the AudioLDM2 model. From my experiments with different audio generation models, this one had a good trade-off between the speed and quality of the outputs for this specific use case.

from diffusers import AudioLDM2Pipeline

model = AudioLDM2Pipeline.from_pretrained("cvssp/audioldm2-music")

prompt = f"A sound that resembles the country of {country}"
audio_hints = model(prompt).audios

With this, we conclude the hint-generation process, as you can see the HuggingFace libraries make our work quite easy here, the main complexity of this app was related to the actual workflow of the Streamlit app, this part is a bit out of context of this article because it is more technical and specific to that framework, but if you are curious to understand it you can visit the Git repository of this project.

Keep learning

If you want to look into other fun use cases of generative AI applied to games you might enjoy reading my other project Gemini Hangman.

GitHub – dimitreOliveira/Gemini-Hangman: Generative AI take on the classic Hangman game

To look into another project using multiple modalities of generative AI, check out my previous article on generating music clips with AI.

How to generate music clips with AI


Building a generative AI-based GeoGuesser was originally published in Google Developer Experts on Medium, where people are continuing the conversation by highlighting and responding to this story.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
product-design-and-development:-the-ultimate-2024-guide

Product Design and Development: The Ultimate 2024 Guide

Next Post
wasm-+-flutter-web — things-you-need-to-know!

WASM + Flutter Web — Things you need to know!

Related Posts