Created
March 4, 2024 16:50
-
-
Save bojanbabic/f128a05f6728c1de892f5df13dfc4522 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import numpy as np | |
import pandas as pd | |
import json | |
def create_tf_serving_json(data): | |
return {'inputs': {name: data[name].tolist() for name in data.keys()} if isinstance(data, dict) else data.tolist()} | |
def score_model(dataset): | |
url = 'https://dbc-717ce2a2-8606.cloud.databricks.com/serving-endpoints/ir_ip_l1_classifier/invocations' | |
headers = {'Authorization': f'Bearer {os.environ.get("DATABRICKS_TOKEN")}', | |
'Content-Type': 'application/json'} | |
ds_dict = {'dataframe_split': dataset.to_dict(orient='split')} if isinstance(dataset, pd.DataFrame) else create_tf_serving_json(dataset) | |
data_json = json.dumps(ds_dict, allow_nan=True) | |
response = requests.request(method='POST', headers=headers, url=url, data=data_json) | |
if response.status_code != 200: | |
raise Exception(f'Request failed with status {response.status_code}, {response.text}') | |
return response.json() | |
df = pd.DataFrame.from_dict({'text': ["What is the latest news on Biden?", "Latest result on RNA vaccine"]}) | |
print(score_model(df)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment