This script works with the Label section hardcoded:
import oci
import pandas as pd
# Create a default config using DEFAULT profile in default location
config = oci.config.from_file()
# Initialize service client with default config file
data_labeling_service_dataplane_client = oci.data_labeling_service_dataplane.DataLabelingClient(config)
# Define the Excel file location
excel_file_path = r'C:SMALLFILEEXCEL.xlsx'
# Load the Excel file into a pandas DataFrame
df = pd.read_excel(excel_file_path, header=None) # No header row
# Loop through each row in the DataFrame
for index, row in df.iterrows():
# Ensure there are two columns (record_id and labels)
if len(row) < 1:
print(f"Skipping row due to insufficient data: {row}")
continue
# Get the record_id and strip any unwanted characters (spaces, newlines)
record_id2 = row[0].strip()
label_str = row[1].strip()
# Print the cleaned record_id for debugging purposes
print(f"Using record_id: {record_id2}")
try:
# Create annotation request for each record
create_annotation_response = data_labeling_service_dataplane_client.create_annotation(
create_annotation_details=oci.data_labeling_service_dataplane.models.CreateAnnotationDetails(
record_id=record_id2, # Use the cleaned record_id without quotes
compartment_id="myid", # Replace with your compartment_id
entities=[
oci.data_labeling_service_dataplane.models.GenericEntity(
entity_type="GENERIC",
labels=[oci.data_labeling_service_dataplane.models.Label(label="macular_edema"),
oci.data_labeling_service_dataplane.models.Label(label="scar")]
)
],
freeform_tags={'example_key_2': 'example_value_2'},
defined_tags={'example_key_3': {'example_nested_key': 'example_nested_value'}}
),
opc_retry_token="a12354123",
opc_request_id="example_opc_request_id"
)
# Print the response for each annotation created
print(create_annotation_response.data)
except oci.exceptions.ServiceError as e:
# Print the full error details for debugging
print(f"ServiceError: {e.message}")
print(f"Request ID: {e.request_id}")
print(f"Status Code: {e.status}")
print(f"Error Code: {e.code}")
print(f"Using record_id: {record_id2}")
But when trying to update the Labels with the value from my Excel file like this:
type heimport oci
import pandas as pd
# Create a default config using DEFAULT profile in default location
config = oci.config.from_file()
# Initialize service client with default config file
data_labeling_service_dataplane_client = oci.data_labeling_service_dataplane.DataLabelingClient(config)
# Define the Excel file location
excel_file_path = r'C:SMALLFILEEXCEL.xlsx'
# Load the Excel file into a pandas DataFrame
df = pd.read_excel(excel_file_path, header=None) # No header row
# Loop through each row in the DataFrame
for index, row in df.iterrows():
# Ensure there are two columns (record_id and labels)
if len(row) < 1:
print(f"Skipping row due to insufficient data: {row}")
continue
# Get the record_id and strip any unwanted characters (spaces, newlines)
record_id2 = row[0].strip()
label_str = row[1].strip()
# Print the cleaned record_id for debugging purposes
print(f"Using record_id: {record_id2}")
try:
# Create annotation request for each record
create_annotation_response = data_labeling_service_dataplane_client.create_annotation(
create_annotation_details=oci.data_labeling_service_dataplane.models.CreateAnnotationDetails(
record_id=record_id2, # Use the cleaned record_id without quotes
compartment_id="compartmentid", # Replace with your compartment_id
entities=[
oci.data_labeling_service_dataplane.models.GenericEntity(
entity_type="GENERIC",
labels=label_str
)
],
freeform_tags={'example_key_2': 'example_value_2'},
defined_tags={'example_key_3': {'example_nested_key': 'example_nested_value'}}
),
opc_retry_token="a12354123",
opc_request_id="example_opc_request_id"
)
# Print the response for each annotation created
print(create_annotation_response.data)
except oci.exceptions.ServiceError as e:
# Print the full error details for debugging
print(f"ServiceError: {e.message}")
print(f"Request ID: {e.request_id}")
print(f"Status Code: {e.status}")
print(f"Error Code: {e.code}")
print(f"Using record_id: {record_id2}")
re
I get the error:
raise TypeError('Field {} with value {} was expected to be of type {} but was of type {}'.format(field_name, str(obj), declared_type, type(obj).__name__))
TypeError: Field entities[*].labels with value oci.data_labeling_service_dataplane.models.Label(label=”scar”), oci.data_labeling_service_dataplane.models.Label(label=”nevus”), oci.data_labeling_service_dataplane.models.Label(label=”increased_cup_disc”)
was expected to be of type list[Label] but was of type str
Any idea how to get that list[Label] to work with a value from the Excel file? My Excel file has that entire text in one cell that it is pulling over. I can update the Excel file if needed.
I tried creating a list without using strings at all which did not work.
Robert Giljohann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1