02-20-2023 10:24 PM
Hello Community,
I have a FastAPI endpoint on a cluster with addess 0.0.0.0:8084/predict. And I want to send a request to this endpoint from a React App which is locally hosted on my computer. I have a Personal access token for the workspace but dont know how to send the request using PAT. If anyone knows, please mention how can I hit
http://0.0.0.0:8084/predict
endpoint in a cluster so that I can do realtime prediction with low latency. Mentioning a URL template to hit would be a great help.
PS. My current requirement does not allow me to use Model serving feature of Databricks.
03-31-2023 08:32 AM
@Aakash Bhandari :
To send a request from a React App to a FastAPI endpoint on a Databricks cluster using a Personal Access Token (PAT), you can use the requests module in Python to make HTTP requests.
Here's an example of how to use requests to send a POST request to your FastAPI endpoint:
import requests
url = "http://0.0.0.0:8084/predict"
headers = {"Authorization": "Bearer <your PAT here>"}
data = {"input": "your input data here"}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
# do something with the result
else:
print("Request failed with status code:", response.status_code)
Replace <your PAT here> with your actual Personal Access Token. You can generate a PAT in your Databricks workspace by going to User Settings > Access Tokens and creating a new token with the appropriate permissions.
In this example, the data parameter is a dictionary containing the input data that you want to send to your endpoint. You can modify this dictionary to include any additional parameters that your endpoint requires.
Also, note that the headers parameter includes an Authorization header with the value "Bearer <your PAT here>". This header is required for authenticating with your Databricks cluster using the PAT.
Make sure to modify the url parameter to match the actual address of your FastAPI endpoint. If your Databricks cluster is behind a firewall or network security group, you may need to configure your firewall rules to allow traffic from your local computer to the cluster's IP address and port number.
Hope all these suggestions help you to come to the solution.
03-31-2023 08:32 AM
@Aakash Bhandari :
To send a request from a React App to a FastAPI endpoint on a Databricks cluster using a Personal Access Token (PAT), you can use the requests module in Python to make HTTP requests.
Here's an example of how to use requests to send a POST request to your FastAPI endpoint:
import requests
url = "http://0.0.0.0:8084/predict"
headers = {"Authorization": "Bearer <your PAT here>"}
data = {"input": "your input data here"}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
# do something with the result
else:
print("Request failed with status code:", response.status_code)
Replace <your PAT here> with your actual Personal Access Token. You can generate a PAT in your Databricks workspace by going to User Settings > Access Tokens and creating a new token with the appropriate permissions.
In this example, the data parameter is a dictionary containing the input data that you want to send to your endpoint. You can modify this dictionary to include any additional parameters that your endpoint requires.
Also, note that the headers parameter includes an Authorization header with the value "Bearer <your PAT here>". This header is required for authenticating with your Databricks cluster using the PAT.
Make sure to modify the url parameter to match the actual address of your FastAPI endpoint. If your Databricks cluster is behind a firewall or network security group, you may need to configure your firewall rules to allow traffic from your local computer to the cluster's IP address and port number.
Hope all these suggestions help you to come to the solution.
04-10-2023 03:20 AM
Thank you @Suteja Kanuri . Previously, I was using context create and command run APIs before for the work around.
04-10-2023 05:48 AM
@Aakash Bhandari : I am glad my resolution helped you! Cheers~~Happy progressing.
04-16-2023 11:07 PM
Hello @Suteja Kanuri
Following up on this, How would the request module know the URL request is for databricks. If we try to hit the url
http://0.0.0.0:8080
It will look into the computer's localhost to get the service running in 8080 port. Is there a way to address this?
04-17-2023 05:55 AM
@Aakash Bhandari :
You are correct that if you try to hit http://0.0.0.0:8080 from your React App, it will look for the service running on the local machine's localhost and not on the Databricks cluster.
To address this, you need to specify the URL of the cluster's FastAPI endpoint in the request. This URL will typically be of the form https://<databricks-instance-name>.cloud.databricks.com:443/<path-to-your-endpoint>.
Here's an example code snippet that shows how to make a POST request to a FastAPI endpoint hosted on a Databricks cluster:
import requests
import json
# Replace <databricks-instance-name> with the name of your Databricks instance
url = "https://<databricks-instance-name>.cloud.databricks.com:443/predict"
# Replace <token> with your Databricks Personal Access Token
headers = {"Authorization": f"Bearer <token>"}
# Example payload for the POST request
payload = {"data": {"age": 35, "income": 45000}}
# Send the request to the FastAPI endpoint
response = requests.post(url, headers=headers, data=json.dumps(payload))
# Print the response
print(response.json())
Note that you need to replace <databricks-instance-name> with the name of your Databricks instance and <token> with your Databricks Personal Access Token. You also need to replace the example payload with your own JSON payload for the POST request.
04-19-2023 01:25 AM
Hey @Suteja Kanuri ,
I implemented your code in my usecase but it is throwing 404 error as reponse. Below is the code I am using and dont know why it is not working.
header = {"Authorization": "Bearer " + os.getenv("PAT")}
context_response = requests.get(
"https://<dtaabricks-instance>.gcp.databricks.com:443/api_health",
headers=header
)
context_data = context_response.json()
print(context_data)
I am running my fastAPI app in a notebook in one of the cluster. The app is running on port 8081. Could you please help me out where am I getting wrong here?
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group