【问题标题】:How to Interact with Google Colab using an API?如何使用 API 与 Google Colab 交互?
【发布时间】:2020-10-05 12:16:05
【问题描述】:

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

【问题讨论】:

  • 不,没有用于以编程方式访问 Colab 的 API。
  • @BobSmith,您对如何使用 API 根据用户输入在 GPU 上运行 python 代码并将响应返回给用户有什么建议吗?

标签: python api jupyter-notebook gpu google-colaboratory


【解决方案1】:

使用 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.",
        }
    }
)

【讨论】:

  • 警告:根据 HuggingFace API 计划使用。免费套餐每月允许 30000 个字符。
  • 这甚至没有回答问题。复制面食?
猜你喜欢
  • 2012-05-12
  • 2020-10-08
  • 2021-01-18
  • 2015-09-22
  • 2017-07-24
  • 2011-03-21
  • 1970-01-01
  • 2020-01-26
  • 2019-03-22
相关资源
最近更新 更多