Skip to content

Commit

Permalink
Run black
Browse files Browse the repository at this point in the history
  • Loading branch information
nik committed Mar 5, 2024
1 parent 3cca140 commit 6e97126
Show file tree
Hide file tree
Showing 38 changed files with 1,222 additions and 1,060 deletions.
31 changes: 17 additions & 14 deletions deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,51 @@

from botocore.exceptions import ClientError

AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)


class Deployment:
def __init__(self):
self.b = boto3.Session(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
self.s3 = self.b.resource('s3')
self.b = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
self.s3 = self.b.resource("s3")

def upload_file(self, import_file):
name = os.path.basename(import_file)
logger.info(f'Copy {import_file} => s3://labelstud.io/sdk/{name}')
logger.info(f"Copy {import_file} => s3://labelstud.io/sdk/{name}")
try:
self.s3.meta.client.upload_file(
import_file,
'labelstud.io',
'sdk/' + name,
ExtraArgs={'ContentType': "text/html", 'ACL': "public-read"}
"labelstud.io",
"sdk/" + name,
ExtraArgs={"ContentType": "text/html", "ACL": "public-read"},
)
except ClientError as e:
logging.error(e)
return False
return True

def upload_dir(self, root_dir):
logger.info('Copy ' + root_dir)
logger.info("Copy " + root_dir)
for path in os.listdir(root_dir):
if os.path.isdir(path):
continue

if path.endswith('.html'):
if path.endswith(".html"):
self.upload_file(os.path.join(root_dir, path))

def run(self):
logger.info('\n\n\n===> Deployment is running')
self.upload_dir('html/label_studio_sdk')
logger.info('\n\n\n===> Deployment is finished')
logger.info("\n\n\n===> Deployment is running")
self.upload_dir("html/label_studio_sdk")
logger.info("\n\n\n===> Deployment is finished")


if __name__ == '__main__':
if __name__ == "__main__":
d = Deployment()
d.run()
30 changes: 15 additions & 15 deletions examples/active_learning/active_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline

LABEL_STUDIO_URL = 'http://localhost:8080'
API_KEY = '91b3b61589784ed069b138eae3d5a5fe1e909f57'
LABEL_STUDIO_URL = "http://localhost:8080"
API_KEY = "91b3b61589784ed069b138eae3d5a5fe1e909f57"


ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
ls.check_connection()


project = ls.start_project(
title='AL Project Created from SDK',
label_config='''
title="AL Project Created from SDK",
label_config="""
<View>
<Text name="text" value="$text"/>
<Choices name="sentiment" toName="text" choice="single" showInLine="true">
Expand All @@ -26,14 +26,14 @@
<Choice value="Neutral"/>
</Choices>
</View>
''',
""",
)


project.set_sampling(ProjectSampling.UNCERTAINTY)


labels_map = {'Positive': 0, 'Negative': 1, 'Neutral': 2}
labels_map = {"Positive": 0, "Negative": 1, "Neutral": 2}
inv_labels_map = {idx: label for label, idx in labels_map.items()}


Expand All @@ -60,8 +60,8 @@ def get_model_predictions(model, input_texts):
labeled_tasks = project.get_labeled_tasks()
texts, labels = [], []
for labeled_task in labeled_tasks:
texts.append(labeled_task['data']['text'])
labels.append(labeled_task['annotations'][0]['result'][0]['value']['choices'][0])
texts.append(labeled_task["data"]["text"])
labels.append(labeled_task["annotations"][0]["result"][0]["value"]["choices"][0])


model = get_model()
Expand All @@ -73,25 +73,25 @@ def get_model_predictions(model, input_texts):
unlabeled_tasks = project.get_tasks(selected_ids=batch_ids)


texts = [task['data']['text'] for task in unlabeled_tasks]
texts = [task["data"]["text"] for task in unlabeled_tasks]
pred_labels, pred_scores = get_model_predictions(model, texts)


model_version = f'model_{len(labeled_tasks)}'
model_version = f"model_{len(labeled_tasks)}"


predictions = []
for task, pred_label, pred_score in zip(unlabeled_tasks, pred_labels, pred_scores):
project.create_prediction(
task_id=task['id'],
task_id=task["id"],
# alternatively you can use a simple form here:
# result=pred_label,
result=[
{
'from_name': 'sentiment',
'to_name': 'text',
'type': 'choices',
'value': {'choices': [pred_label]},
"from_name": "sentiment",
"to_name": "text",
"type": "choices",
"value": {"choices": [pred_label]},
}
],
score=pred_score,
Expand Down
18 changes: 9 additions & 9 deletions examples/annotate_data_from_gcs/annotate_data_from_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
from google.cloud import storage as google_storage
from label_studio_sdk import Client

BUCKET_NAME = 'my-bucket' # specify your bucket name here
BUCKET_NAME = "my-bucket" # specify your bucket name here
GOOGLE_APPLICATION_CREDENTIALS = (
'my-service-account-credentials.json' # specify your GCS credentials
"my-service-account-credentials.json" # specify your GCS credentials
)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = GOOGLE_APPLICATION_CREDENTIALS
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = GOOGLE_APPLICATION_CREDENTIALS

google_client = google_storage.Client()
bucket = google_client.get_bucket(BUCKET_NAME)
tasks = []
for filename in bucket.list_blobs():
tasks.append({'image': f'gs://{BUCKET_NAME}/{filename}'})
tasks.append({"image": f"gs://{BUCKET_NAME}/{filename}"})


LABEL_STUDIO_URL = 'http://localhost:8080'
API_KEY = '91b3b61589784ed069b138eae3d5a5fe1e909f57'
LABEL_STUDIO_URL = "http://localhost:8080"
API_KEY = "91b3b61589784ed069b138eae3d5a5fe1e909f57"

ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
ls.check_connection()


project = ls.start_project(
title='Image Annotation Project from SDK',
label_config='''
title="Image Annotation Project from SDK",
label_config="""
<View>
<Image name="image" value="$image"/>
<RectangleLabels name="objects" toName="image">
<Choice value="Airplane"/>
<Choice value="Car"/>
</RectangleLabels>
</View>
''',
""",
)


Expand Down
16 changes: 8 additions & 8 deletions examples/export_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import time
from label_studio_sdk import Client

LABEL_STUDIO_URL = os.getenv('LABEL_STUDIO_URL', default='http://localhost:8080')
API_KEY = os.getenv('LABEL_STUDIO_API_KEY')
PROJECT_ID = int(os.getenv('LABEL_STUDIO_PROJECT_ID'))
LABEL_STUDIO_URL = os.getenv("LABEL_STUDIO_URL", default="http://localhost:8080")
API_KEY = os.getenv("LABEL_STUDIO_API_KEY")
PROJECT_ID = int(os.getenv("LABEL_STUDIO_PROJECT_ID"))

# connect to Label Studio
ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
Expand All @@ -28,21 +28,21 @@

# get the first tab
views = project.get_views()
task_filter_options = {'view': views[0]['id']} if views else {}
task_filter_options = {"view": views[0]["id"]} if views else {}

# create new export snapshot
export_result = project.export_snapshot_create(
title='Export SDK Snapshot', task_filter_options=task_filter_options
title="Export SDK Snapshot", task_filter_options=task_filter_options
)
assert 'id' in export_result
export_id = export_result['id']
assert "id" in export_result
export_id = export_result["id"]

# wait until snapshot is ready
while project.export_snapshot_status(export_id).is_in_progress():
time.sleep(1.0)

# download snapshot file
status, file_name = project.export_snapshot_download(export_id, export_type='JSON')
status, file_name = project.export_snapshot_download(export_id, export_type="JSON")
assert status == 200
assert file_name is not None
print(f"Status of the export is {status}.\nFile name is {file_name}")
10 changes: 5 additions & 5 deletions examples/import_preannotations/import_brush_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import label_studio_converter.brush as brush
from label_studio_sdk import Client

LABEL_STUDIO_URL = 'http://localhost:8080'
LABEL_STUDIO_API_KEY = '<your-token>'
LABEL = 'Mitochondria'
LABEL_STUDIO_URL = "http://localhost:8080"
LABEL_STUDIO_API_KEY = "<your-token>"
LABEL = "Mitochondria"

ls = Client(url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
ls.check_connection()
Expand All @@ -26,7 +26,7 @@
)

ids = project.import_tasks(
[{'image': f'http://example.com/data_{i:04}.png'} for i in range(64)]
[{"image": f"http://example.com/data_{i:04}.png"} for i in range(64)]
)

mask = (np.random.random([512, 512]) * 255).astype(np.uint8) # just a random 2D mask
Expand All @@ -43,7 +43,7 @@
"from_name": "brush_labels_tag",
"to_name": "image",
"type": "brushlabels",
'value': {"format": "rle", "rle": rle, "brushlabels": [LABEL]},
"value": {"format": "rle", "rle": rle, "brushlabels": [LABEL]},
}
],
)
Loading

0 comments on commit 6e97126

Please sign in to comment.