And if you want to learn more on how to use Cohere AI, check out our Cohere tutorial page. And if you want to try out this tutorial or other our AI tutorials with a group of like-minded people from around the world, sign in to our AI Hackathons.
We need to start by creating a directory for our project. We will call it dog-breed-recognition and then we will create a virtual environment for it.
We have to create Cohere and OpenAI account, and then download API Keys. We will use them for APIs authorization. Let's create .env file and put them there:
Now let's install all the necessary libraries:
Now we can create an app.py file and start writing our code.
First, we need to import all the necessary libraries and load environment variables:
Then we need to create a FastAPI app and authorize Cohere and OpenAI clients:
Now I will define a prompt for Cohere's LLM. It will be used to generate a prediction of a dog's breed. I will pass some examples of dog's breeds and their descriptions to the model. I will prepare space for our description, which will be passed to the model.
def generate_prompt(description):
prompt = f'''Please predict the dog's breed in the description.
Description: This dog is a medium-sized breed of dog that was developed in the United States. The breed is often used as a working dog on farms and ranches, and is also a popular companion animal.
Breed: Australian Shepher
--
Description: This dog is a cheerful, high-spirited breed that is well suited for a variety of activities, including hunting, agility, and obedience. They are very intelligent and easily trained, and are excellent companions for active families. They are also known for their friendly dispositions and love of children.
Breed: Brittany
--
Description: They are large, muscular dogs with a short, thick coat. They have a wide head and a short, square muzzle. Their eyes are small and their ears are floppy. They have a thick neck and a strong, muscular body. Their tail is short and their legs are muscular.
Breed: American Bulldog
--
Description: This dog is a medium-sized breed of dog that is native to Poland. They are intelligent and active dogs that are known for their herding abilities. They have a thick, shaggy coat that can be either black, white, or brindle in color. They are loyal and protective dogs that make great family pets.
Breed: Polish Lowland Sheepdog
--
Description: They are large, working breed of dog known for its obedience, loyalty, and intelligence. They are strong, agile dogs with a well-proportioned build. They have a long head, erect ears, and a long, thick coat. The breed is intelligent and easily trained, making them excellent working dogs. They are used in a variety of roles, including law enforcement, search and rescue, and as assistance dogs.
Breed: German Shepherd Dog
--
Description: They are a medium-sized breed of dog, originally bred in England as gun dogs. They are bred to be lovable companions and are often considered one of the best breeds for families. They are relatively easy to train and are known for being intelligent and eager to please. Cockers have a reputation for being one of the most affectionate dog breeds, and are also known for being good with children.
Breed: Cocker Spaniel
--
Description: {description}
Breed:
'''
return prompt
Now we can define an endpoint for our prediction. We will use Cohere's LLM to generate a prediction. We will pass our prompt to the model and then we will return the result. Then the result will be used for generating a photo.
Now we can run our app:
We can test our app by sending a request to the endpoint. I will use Postman for that. It's a great tool for testing APIs. We can download it here. That's how our request URL should look like:
My description looks like:
Prediction: 'Siberian Husky'