Примеры запросов
Создание проекта
from crowd_sdk.tagme import TagmeClient, types
async def create_project() -> types.ProjectResponse:
client = TagmeClient.from_config()
# список организаций доступных пользователю
available_organization_ids = await client.get_organizations_ids()
return await client.create_project(
types.CreateProjectRequest(
organization_id=available_organization_ids[0],
name='Новый проект',
description='Описание нового проекта',
)
)
Создание и запуск задачи
import asyncio
from pathlib import Path
from crowd_sdk.tagme import TagmeClient, types
async def create_and_start_task(test_data_path: Path):
"""
Метод создаёт и запускает задачу для разметки
"""
client = TagmeClient.from_config()
available_organization_ids = await client.get_organizations_ids()
organization_id = available_organization_ids[0]
pool = await client.create_pool(
types.CreatePoolRequest(
name='test_name',
markers=['test_marker@sber.ru'],
organization_id=organization_id,
)
)
project = await client.create_project(
types.CreateProjectRequest(
name='project_name',
organization_id=organization_id,
description='desc',
)
)
method = await client.create_method(
types.CreateMethodRequest(
name='test method',
organization_id=organization_id,
forms=types.MethodForms(
css='test css',
html='test html',
js='test js',
data='test data',
),
marker_brief='test task instruction',
)
)
workflow = await client.get_default_project_workflow(project.uid)
test_task_name = 'test_task'
await client.create_task(
types.CreateTaskRequest(
name=test_task_name,
organization_id=organization_id,
workflow_id=workflow.uid,
method_id=method.uid,
description='description',
overlap=1,
)
)
tasks = await client.get_tasks(types.GetTasksRequest(workflow_id=workflow.uid))
for _task in tasks:
if _task.name == test_task_name:
new_task = _task
break
else: # pylint: disable=useless-else-on-loop
raise ValueError('Task wasn\'t created')
await client.add_users_to_project(types.AddUsersToProjectRequest(project_id=project.uid, pool_id=pool.uid))
await client.upload_folder_to_task(
bucket_id=new_task.storage_id,
task_id=new_task.uid,
folder=test_data_path,
)
await client.start_task(new_task.uid)
for _ in range(5):
await asyncio.sleep(5)
new_task = await client.get_task(new_task.uid)
if new_task.state is types.TaskState.RUNNING:
break
else: # pylint: disable=W0120
raise ValueError(f'Incorrent task state {new_task.state}, expected {types.TaskState.RUNNING}')
Создание и запуск задачи с собственными классами
import asyncio
import json
from pathlib import Path
from typing import Optional, Tuple
from crowd_sdk.tagme import ProjectConfig, TagmeClientAdvanced
async def create_test_pool(
tagme_client: TagmeClientAdvanced, pool_name: str, organization_id: Optional[str] = None
) -> str:
pool = await tagme_client.client.create_pool(name=pool_name, markers=[], organization_id=organization_id)
current_user = await tagme_client.client.get_self_person()
await tagme_client.client.add_markers(emails=[current_user.email], pools=[pool.uid])
return pool.uid
async def prerequisites(organization_id: str) -> Tuple[TagmeClientAdvanced, str]:
client = TagmeClientAdvanced()
config = ProjectConfig.load(Path(__file__).parent / 'example_data' / 'project' / 'project.yaml')
new_pool = await create_test_pool(client, 'Test pool', organization_id)
config.pools = [new_pool]
# Create project using text segmentation template
project_id = await client.create_project_by_config(config, organization_id=organization_id)
# Create task, upload data and add pool
task = await client.create_and_start_task(config, project_id, start_task=False)
assert task.uid
return client, task.uid
async def main(organization_id: str) -> None:
# prerequisites for this example: ready for markup task
client, task_id = await prerequisites(organization_id)
# Update entities only for the created task
entities_file = Path(__file__).parent / 'example_data' / 'new_entities.json'
with open(entities_file) as f:
new_entities = json.load(f)
await client.set_task_payload(task_id, {'entities': new_entities})
# Run task
await client.start_task(task_id)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main('064d1d79-8c71-4068-b4e5-39850c90eaa1'))
Отключение контрольного задания
Если после запуска задачи вы заметили, что допустили ошибку в контрольном задании, вы можете отключить это задание. Для этого:
-
Остановите разметку задачи.
-
Отключите задание, чтобы оно больше не показывалось разметчикам.
-
Запустите разметку задачи снова.
import asyncio
from crowd_sdk.tagme import TagmeClient
async def main(task_id: str, file_uid: str) -> None:
tagme_client = TagmeClient()
task = await tagme_client.get_task(task_id=task_id)
file = await tagme_client.get_file(task_id, file_uid)
flag = False
assert file.metadata[0] and len(file.metadata) == 1 # making sure that this file is a control task
if task.state.value == 'RUNNING':
await tagme_client.stop_task(task.uid) # stops the task if it was active
flag = True
assert file.enabled is True # the file is issued to markers
await tagme_client.disable_control_task(task.uid, file_uid) # disable this file
file = await tagme_client.get_file(task_id, file_uid) # update file info
assert file.enabled is False # the file is not issued to markers
if flag:
await tagme_client.start_task(task.uid) # starts a stopped task if it was active
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(
main(
task_id='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
file_uid='yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy',
)
)
Скачивание результатов
import json
from pathlib import Path
from crowd_sdk.tagme import TagmeClientAdvanced
client = TagmeClientAdvanced()
path = Path(__file__).parent / 'test_data' / 'task_results.json' # куда сохранить результаты'
task_id = 'b9a1c93c-1a0e-44d1-8b14-bfb0abd3104d'
task_results = await client.get_task_results(task_id)
with open(path, 'w') as f:
json.dump([result.to_dict() for result in task_results], f, ensure_ascii=False, indent=2)
Выгрузка статистики
from typing import List
from crowd_sdk.tagme import TagmeClientAdvanced, types
tagme_client = TagmeClientAdvanced('~/.crowd.cfg')
task_id = '2ba17b92-257c-6ff6-8e16-73a11382ab09'
project_id = '2ba17b92-257c-6ff6-8e16-73a11382ab09'
task: types.TaskData = await tagme_client.get_task(task_id=task_id)
task_statistics, marker_statistics = await tagme_client.export_project_stats(
project_id=project_id,
from_date=(2022, 4, 1),
to_date=(2022, 4, 18),
)
task_result: List[types.TaskResult] = await tagme_client.get_task_results(task_id=task_id)