如何使用 API 与 Google Colab 交互?

lis*_*aci 5 python api gpu jupyter-notebook google-colaboratory

是否可以使用 API 与 Google Colab 环境进行交互?我想根据一些用户通过 API 输入的输入在 GPU 上编写和执行 python 代码并将响应返回给用户。我可以使用 Colab 来完成此操作吗?或者我需要设置自己的 Jupyter Notebook 来执行此操作。

小智 -2

有一种使用 python 和 api 的方法:在此处创建配置文件:https: //huggingface.co/settings/profile 您可能需要将 API_URL 替换为您自己的。我相信您可以使用邮递员创建 API_URL,尽管我不能 100% 确定。

import json
import requests
API_URL = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
headers = {"Authorization": f"Bearer {API_TOKEN}"}

def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))
data = query(
    {
        "inputs": {
            "question": "What is the purpose of Spark",
            "context": "The purpose of Spark is to help scientists find and better understand the potential causes of autism.",
        }
    }
)
Run Code Online (Sandbox Code Playgroud)