SDK for interacting with the HOPPR API
Project description
HOPPR Python SDK
The HOPPR Python SDK allows you to interact with the HOPPR API for managing studies, adding images, and performing model inferences.
Installation
To install the HOPPR SDK, you can use pip:
pip install -U hopprai
This will install the HOPPR SDK along with its dependencies.
Initialization
To start using the HOPPR SDK, you need to initialize it with your API key:
Note: Do not place your API key directly in your code. Instead, use environment variables or other secure methods to manage your API key. This helps prevent accidental exposure of sensitive information.
import os
from hopprai import HOPPR
api_key = os.getenv("HOPPR_API_KEY")
hoppr = HOPPR(api_key)
Make sure to set the HOPPR_API_KEY environment variable with your actual API key.
Error Handling
To handle errors more effectively, the HOPPR SDK includes a custom HOPPRError type that provides the status code and an error message, allowing users to respond accordingly to specific error situations.
Here's an example of how to handle errors:
from hopprai import HOPPR
api_key = os.getenv("HOPPR_API_KEY")
hoppr = HOPPR(api_key)
try:
study = hoppr.create_study("example_study_reference")
except Exception as e:
print(f"Error occurred: Status code: {e.status_code}, Message: {e.message}")
Examples
Create a Study
To create a new study, use the create_study method:
reference = "example_study_reference"
study = hoppr.create_study(reference)
print(f"Created study with id: {study.id}")
Retrieve a Study
To retrieve an existing study by its id:
study_id = "a3f1d5c2-41d4-4c9a-8b8f-8e5f9a4e8f9a"
study = hoppr.get_study(study_id)
if study:
print(f"Study id: {study.id}, Reference: {study.reference}")
else:
print("Study not found.")
Delete a Study
To delete a study by its id:
study_id = "a3f1d5c2-41d4-4c9a-8b8f-8e5f9a4e8f9a"
if hoppr.delete_study(study_id):
print("Study deleted successfully.")
else:
print("Failed to delete study.")
Add an Image to a Study
To add an image to a study:
study_id = "a3f1d5c2-41d4-4c9a-8b8f-8e5f9a4e8f9a"
reference = "1.2.840.113619.2.312.4120.793348.11111.1586342345.233"
image_data = open("path/to/your/image.dcm", "rb").read()
image = hoppr.add_study_image(study_id, reference, image_data)
print(f"Added image with id: {image.id}")
Delete an Image from a Study
To delete an image from a study:
study_id = "a3f1d5c2-41d4-4c9a-8b8f-8e5f9a4e8f9a"
image_id = "1.2.840.113619.2.312.4120.793348.11111.1586342345.233"
if hoppr.delete_study_image(study_id, image_id):
print("Image deleted successfully.")
else:
print("Failed to delete image.")
Prompt a Model for Inference
To prompt a model for inference and wait for the response:
study_id = "a3f1d5c2-41d4-4c9a-8b8f-8e5f9a4e8f9a"
model = "model"
prompt = "What findings are present in this study?"
organization = "your_org" # optional
response = hoppr.prompt_model(study_id=study_id, model=model, prompt=prompt, organization=organization)
if response:
print(f"Model response: {response.response}")
else:
print("Model inference timed out or failed.")
Prompt a Model for Inference Asynchronously
To prompt a model for inference asynchronously:
study_id = "a3f1d5c2-41d4-4c9a-8b8f-8e5f9a4e8f9a"
model = "model"
prompt = "What findings are present in this study?"
organization = "your_org" # optional
inference_id = hoppr.prompt_model_async(study_id=study_id, model=model, prompt=prompt, organization=organization)
print(f"Inference queued with id: {inference_id}")
To retrieve the response later:
timeout = 180
response = hoppr.retrieve_prompt_response(study_id, inference_id, timeout)
if response:
print(f"Model response: {response.response}")
else:
print("Model inference timed out or failed.")
Datasets and Training Jobs
List Datasets
To retrieve all datasets:
datasets = hoppr.get_datasets()
for ds in datasets:
print(f"Dataset id: {ds['id']}, Name: {ds['name']}")
Retrieve a Dataset
To retrieve a dataset:
dataset_id = "your_dataset_id"
dataset = hoppr.get_dataset(dataset_id)
print(f"Dataset: {dataset}")
Create a Dataset
To create a new dataset (expects a .jsonl file):
dataset_name = "my_dataset"
description = "A test dataset."
file_path = "path/to/dataset.jsonl"
dataset = hoppr.create_dataset(dataset_name, description, file_path)
print(f"Created dataset with id: {dataset['id']}")
Create a Training Job
To create a new training job:
training_job = {
"dataset_id": "your_dataset_id",
"model": "hoppr_model:1.0.0",
# Add other required fields as needed
}
job = hoppr.create_training_job(training_job)
print(f"Created training job with id: {job['id']}")
Retrieve a Training Job
To get the status or details of a training job:
training_job_id = "your_training_job_id"
job = hoppr.get_training_job(training_job_id)
print(f"Training job status: {job['status']}")
Cancel a Training Job
To cancel a running training job:
training_job_id = "your_training_job_id"
result = hoppr.cancel_training_job(training_job_id)
print(f"Cancel result: {result}")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hopprai-0.0.5.tar.gz.
File metadata
- Download URL: hopprai-0.0.5.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3d371d466bfda1ede7b9eda9b7442ce0462536c7bc5bde687f897a9db2d52ce
|
|
| MD5 |
76bc1fc10456e1525f8a23b0ab1cd809
|
|
| BLAKE2b-256 |
308bbeefaa2ae9efbc83908f34fedb9e4e5e484234167e3e4661a3a2b813bdb4
|
File details
Details for the file hopprai-0.0.5-py3-none-any.whl.
File metadata
- Download URL: hopprai-0.0.5-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7674ba4fc833320b1bf78dc95530f441b02ef489a25b3f34fd21f7d03bb1aee4
|
|
| MD5 |
03d3f448509dc5df05064c24308a371a
|
|
| BLAKE2b-256 |
7ab8c21b8618544f6178a6a7c2d871d3aef7895447f812035c6ebc0bb12dc1c6
|