Python 如何使用API与Google Colab交互?

Python 如何使用API与Google Colab交互?,python,api,jupyter-notebook,gpu,google-colaboratory,Python,Api,Jupyter Notebook,Gpu,Google Colaboratory,是否可以使用API与Google Colab环境交互?我想在GPU上基于用户通过API输入编写和执行python代码&将响应返回给用户。我能用Colab做这个吗?或者我需要设置自己的Jupyter笔记本来完成这项工作 使用python和api有一种方法:在此处创建概要文件: 您可能需要用自己的API_URL替换。我相信你可以使用postman创建一个API\uURL,尽管我不是100%确定 import json import requests API_URL = "https://a

是否可以使用API与Google Colab环境交互?我想在GPU上基于用户通过API输入编写和执行python代码&将响应返回给用户。我能用Colab做这个吗?或者我需要设置自己的Jupyter笔记本来完成这项工作

使用python和api有一种方法:在此处创建概要文件: 您可能需要用自己的API_URL替换。我相信你可以使用postman创建一个API\uURL,尽管我不是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.",
        }
    }
)

使用python和api有一种方法:在此处创建概要文件: 您可能需要用自己的API_URL替换。我相信你可以使用postman创建一个API\uURL,尽管我不是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.",
        }
    }
)

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