python - 如何使用 API 与 Google Colab 交互?
问题描述
是否可以使用 API 与 Google Colab 环境进行交互?我想通过 API 根据一些用户输入在 GPU 上编写和执行 python 代码并将响应返回给用户。我可以使用 Colab 执行此操作吗?还是我需要设置自己的 Jupyter Notebook 来执行此操作。
解决方案
有一种使用 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.",
}
}
)
推荐阅读
- ios - Realm iOS:如何处理客户端重置
- python - Python boto3 和 SQS 没有收到消息
- wpf - 未找到元素。如何使用 winappdriver 测试多页 WPF 应用程序中的步骤顺序?
- python - 是否有推荐的方法来构建基金代码
- javascript - Ajax Jquery Laravel 表单视图保存按钮不起作用
- python - Python中两个列表之间的匹配值?
- javascript - 为什么在访问属性时使用“this”关键字?
- tensorflow - tensorflow版本之间的准确性不一致
- python - 正则表达式拼图:仅当它在两个 $$ 之间时才匹配一个模式,而不是无限期向后看
- spring - 尝试调试 Spring Boot Rest 应用程序时出现未经授权的错误