How to Interact with Google Colab using an API?

Viewed 2085

Is it possible to interact with the Google Colab environment using an API? I want to write and execute python code on a GPU based on some user input via an API & return the response back to the user. Can I do this using Colab? or will I need to set up my own Jupyter Notebook to do this.

1 Answers

There is one way using python and api: create profile here: https://huggingface.co/settings/profile You may have to replace API_URL with your own. I believe you can create an API_URL using postman though im not 100% certain.

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