AI 日报

Cohere 教程:如何创建狗的品种识别 API

  • By aihubon
  • Nov 01, 2023 - 2 min read



Cohere 教程:如何创建狗的品种识别 API

在当今世界,有许多来源可以获得有关动物的信息。然而,有时仅凭描述不足以确定动物的类型或品种。那么为什么不使用 Cohere AI 呢?

在本 Cohere 教程中,我们将尝试创建一个可用于根据简短描述确定狗品种的 API – 使用 Cohere API,然后使用 DALL·E 2 生成照片。

如果您想了解有关如何使用 Cohere AI 的更多信息,请查看我们的 Cohere 教程页面。

如果您想与来自世界各地的一群志同道合的人一起尝试本教程或我们的其他 AI 教程,请登录我们的 AIHubPro

🏹 让我们开始吧

我们需要首先为我们的项目创建一个目录。我们将调用它dog-breed-recognition,然后为它创建一个虚拟环境。

mkdir dog-breed-recognitioncd dog-breed-recognitionpython3 -m venv venv.envScriptsctivate

我们必须创建 Cohere 和 OpenAI 帐户,然后下载 API 密钥。我们将使用它们进行 API 授权。让我们创建.env文件并将它们放在那里:

COHERE_API_KEY=<COHERE API KEY>OPENAI_API_KEY=<OPENAI API KEY>

现在让我们安装所有必要的库:

pip install python-dotenvpip install --upgrade coherepip install openaipip install fastapi[all]

现在我们可以创建一个app.py文件并开始编写我们的代码。

🤖 编码!

首先,我们需要导入所有必要的库并加载环境变量:

import osimport cohereimport openaifrom dotenv import load_dotenvfrom fastapi import FastAPI # load env. variablesload_dotenv()

然后我们需要创建一个 FastAPI 应用程序并授权 Cohere 和 OpenAI 客户端:

app = FastAPI() # initialize the Cohere Client with an API Keyco = cohere.Client(os.getenv('COHERE_API_KEY'))openai.api_key = os.getenv('OPENAI_API_KEY')

现在我将为 Cohere 的 LLM 定义一个提示。它将用于生成对狗品种的预测。我会将狗的品种及其描述的一些示例传递给模型。我会为我们的描述准备空间,这将被传递给模型。

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

现在我们可以为我们的预测定义一个端点。我们将使用 Cohere 的 LLM 来生成预测。我们会将我们的提示传递给模型,然后我们将返回结果。然后结果将用于生成照片。

@app.get('/')def breed_prediction(description: str):    prompt = generate_prompt(description)    response = co.generate(        model='xlarge',        prompt=prompt,        max_tokens=5,        temperature=0.75,        stop_sequences=['--'],    )     breed = response.generations[0].text.strip()     img = openai.Image.create(        prompt=f'Image of {breed}',        n=1,        size='512x512'    )     img = img['data'][0]['url']      return {        'breed': breed,        'img': img    }

🚀 运行应用程序

现在我们可以运行我们的应用程序:

uvicorn app:app --reload

我们可以通过向端点发送请求来测试我们的应用程序。为此,我将使用 Postman。它是测试 API 的绝佳工具。我们可以在这里下载。这就是我们的请求 URL 应该是这样的:

localhost:8000/?description=<description>

我的描述看起来像:

This is a large, white, fluffy dog with a thick coat. They are very friendly and love being around people. They were originally bred in Siberia to help people with their daily chores, such as pulling sleds and herding reindeer.

🧑‍🚀 来看看结果吧!

Cohere 教程:如何创建狗的品种识别 API
预测:“西伯利亚雪橇犬”

你可以自己判断结果!令人惊奇的是,我们现在可以从文本中得到多少。我鼓励您尝试描述并在我们的 aihubpro.cn 圈子上分享您的结果!

现在就在这个发展最快的行业中提升您的知识并改变您的职业生涯!加入我们的人工智能黑客马拉松!