Skip to content

Commit

Permalink
chore: pre commit (#29)
Browse files Browse the repository at this point in the history
* fix: ruff formatting

* fix: install pre-commit hook properly

* chore: add gh action for linting / testing
  • Loading branch information
frederik-encord authored Dec 16, 2024
1 parent 528b83b commit 96a3dbf
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 45 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Pull Request workflow

on:
pull_request:
workflow_dispatch:

env:
PYTHON: 3.11.6

concurrency:
group: active-private-${{ github.ref }}-pr
cancel-in-progress: true

jobs:
# This will cache the virtual env for the following jobs
setup-venv:
runs-on: ubuntu-latest
name: Set up a cached VENV
steps:
- uses: actions/checkout@v3

- name: Setup root poetry environment
uses: ./.github/actions/setup-root-poetry-environment

pre-commit:
needs: [setup-venv]
name: Linting and type checking
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup root poetry environment
uses: ./.github/actions/setup-root-poetry-environment

- name: Run linting, type checking
uses: pre-commit/[email protected]
with:
extra_args: "--all-files --hook-stage=push"
timeout-minutes: 60
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ repos:
types_or: [python, pyi]
language: system
pass_filenames: false

default_stages: [pre-push]
default_install_hook_types: [pre-push]
14 changes: 6 additions & 8 deletions docs/code_examples/fastapi/frame_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import numpy as np
from anthropic import Anthropic
from encord.objects.ontology_labels_impl import LabelRowV2
from fastapi import Depends, FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
from numpy.typing import NDArray
from typing_extensions import Annotated

from encord_agents.core.data_model import Frame
from encord_agents.core.ontology import OntologyDataModel
from encord_agents.core.utils import get_user_client
Expand All @@ -11,11 +16,6 @@
dep_label_row,
dep_single_frame,
)
from numpy.typing import NDArray
from typing_extensions import Annotated

from fastapi import Depends, FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware

# Initialize FastAPI app
app = FastAPI()
Expand Down Expand Up @@ -67,9 +67,7 @@ async def classify_frame(
try:
classifications = data_model(message.content[0].text)
for clf in classifications:
clf.set_for_frames(
frame_data.frame, confidence=0.5, manual_annotation=False
)
clf.set_for_frames(frame_data.frame, confidence=0.5, manual_annotation=False)
lr.add_classification_instance(clf)
except Exception:
import traceback
Expand Down
12 changes: 5 additions & 7 deletions docs/code_examples/fastapi/object_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from anthropic import Anthropic
from encord.objects.ontology_labels_impl import LabelRowV2
from fastapi import Depends, FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
from typing_extensions import Annotated

from encord_agents.core.data_model import InstanceCrop
from encord_agents.core.ontology import OntologyDataModel
from encord_agents.core.utils import get_user_client
Expand All @@ -10,10 +14,6 @@
dep_label_row,
dep_object_crops,
)
from typing_extensions import Annotated

from fastapi import Depends, FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware

# Initialize FastAPI app
app = FastAPI()
Expand Down Expand Up @@ -76,9 +76,7 @@ async def classify_objects(
try:
instance = data_model(message.content[0].text)

coordinates = crop.instance.get_annotation(
frame=frame_data.frame
).coordinates
coordinates = crop.instance.get_annotation(frame=frame_data.frame).coordinates
instance.set_for_frames(
coordinates=coordinates,
frames=frame_data.frame,
Expand Down
5 changes: 2 additions & 3 deletions docs/code_examples/gcp/add_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from encord.objects.coordinates import BoundingBoxCoordinates
from encord.objects.ontology_labels_impl import LabelRowV2

from encord_agents.core.data_model import FrameData
from encord_agents.gcp import editor_agent

Expand All @@ -26,9 +27,7 @@ def add_bounding_box(frame_data: FrameData, label_row: LabelRowV2) -> None:
ins = label_row.ontology_structure.objects[0].create_instance()
ins.set_for_frames(
frames=frame_data.frame,
coordinates=BoundingBoxCoordinates(
top_left_x=0.2, top_left_y=0.2, width=0.6, height=0.6
),
coordinates=BoundingBoxCoordinates(top_left_x=0.2, top_left_y=0.2, width=0.6, height=0.6),
)
label_row.add_object_instance(ins)
label_row.save()
9 changes: 4 additions & 5 deletions docs/code_examples/gcp/frame_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from anthropic import Anthropic
from encord.objects.ontology_labels_impl import LabelRowV2
from numpy.typing import NDArray
from typing_extensions import Annotated

from encord_agents.core.ontology import OntologyDataModel
from encord_agents.core.utils import get_user_client
from encord_agents.core.video import Frame
from encord_agents.gcp import Depends, editor_agent
from encord_agents.gcp.dependencies import FrameData, dep_single_frame
from numpy.typing import NDArray
from typing_extensions import Annotated

client = get_user_client()
project = client.get_project("<your_project_hash>")
Expand Down Expand Up @@ -53,9 +54,7 @@ def agent(
try:
classifications = data_model(message.content[0].text)
for clf in classifications:
clf.set_for_frames(
frame_data.frame, confidence=0.5, manual_annotation=False
)
clf.set_for_frames(frame_data.frame, confidence=0.5, manual_annotation=False)
lr.add_classification_instance(clf)
except Exception:
import traceback
Expand Down
7 changes: 3 additions & 4 deletions docs/code_examples/gcp/object_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from anthropic import Anthropic
from encord.objects.ontology_labels_impl import LabelRowV2
from typing_extensions import Annotated

from encord_agents.core.ontology import OntologyDataModel
from encord_agents.core.utils import get_user_client
from encord_agents.gcp import Depends, editor_agent
from encord_agents.gcp.dependencies import FrameData, InstanceCrop, dep_object_crops
from typing_extensions import Annotated

# User client
client = get_user_client()
Expand Down Expand Up @@ -62,9 +63,7 @@ def agent(
try:
instance = data_model(message.content[0].text)

coordinates = crop.instance.get_annotation(
frame=frame_data.frame
).coordinates
coordinates = crop.instance.get_annotation(frame=frame_data.frame).coordinates
instance.set_for_frames(
coordinates=coordinates,
frames=frame_data.frame,
Expand Down
9 changes: 4 additions & 5 deletions docs/code_examples/tasks/prelabel_videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
from encord.objects.coordinates import BoundingBoxCoordinates
from encord.objects.ontology_labels_impl import LabelRowV2
from encord.project import Project
from numpy.typing import NDArray
from typing_extensions import Annotated

from encord_agents.core.data_model import Frame
from encord_agents.tasks import Depends, Runner
from encord_agents.tasks.dependencies import dep_video_iterator
from numpy.typing import NDArray
from typing_extensions import Annotated

runner = Runner(project_hash="<project_hash>")

Expand Down Expand Up @@ -64,9 +65,7 @@ def run_something(
outputs = model(frame.content)
for output in outputs:
ins = ontology.objects[output.label].create_instance()
ins.set_for_frames(
frames=frame.frame, coordinates=output.coords, confidence=output.conf
)
ins.set_for_frames(frames=frame.frame, coordinates=output.coords, confidence=output.conf)

lr.add_object_instance(ins)

Expand Down
1 change: 1 addition & 0 deletions docs/code_examples/tasks/prioritize_by_data_title.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from encord.objects.ontology_labels_impl import LabelRowV2

from encord_agents.tasks import Runner

runner = Runner(project_hash="<your_project_hash>")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from encord.objects.ontology_labels_impl import LabelRowV2

from encord_agents.tasks import Runner

runner = Runner(project_hash="<your_project_hash>")
Expand Down
7 changes: 3 additions & 4 deletions docs/code_examples/tasks/twin_project.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from encord.objects.ontology_labels_impl import LabelRowV2
from encord.objects.options import Option
from encord.workflow.stages.agent import AgentTask
from typing_extensions import Annotated

from encord_agents.tasks import Depends, Runner
from encord_agents.tasks.dependencies import Twin, dep_twin_label_row
from typing_extensions import Annotated

# 1. Setup the runner
runner = Runner(project_hash="<project_hash_a>")
Expand All @@ -19,9 +20,7 @@
@runner.stage(stage="<transfer_agent_stage_uuid>")
def copy_labels(
manually_annotated_lr: LabelRowV2,
twin: Annotated[
Twin, Depends(dep_twin_label_row(twin_project_hash="<project_hash_b>"))
],
twin: Annotated[Twin, Depends(dep_twin_label_row(twin_project_hash="<project_hash_b>"))],
) -> str | None:
# 4. Reading the checkboxes that have been set
instance = manually_annotated_lr.get_classification_instances()[0]
Expand Down
1 change: 1 addition & 0 deletions docs/code_examples/tasks/wrong_stage_and_pathway_names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from encord.objects.ontology_labels_impl import LabelRowV2

from encord_agents.tasks import Runner

r = Runner()
Expand Down
2 changes: 1 addition & 1 deletion encord_agents/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ def ssh_key(self) -> str:
if self.ssh_key_file is None:
raise ValueError("Both ssh key content and ssh key file is None")
self.ssh_key_content = self.ssh_key_file.read_text()
return self.ssh_key_content
return self.ssh_key_content
2 changes: 0 additions & 2 deletions encord_agents/core/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Literal

Base64Formats = Literal[".jpeg", ".jpg", ".png"]


4 changes: 2 additions & 2 deletions encord_agents/core/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def rbb_to_poly(
[(x + w) * img_width, (y + h) * img_height],
[x * img_width, (y + h) * img_height],
],
dtype=np.float32
dtype=np.float32,
)
angle = rbb.theta # [0; 360]
center = tuple(bbox_not_rotated.mean(0).tolist())
Expand Down Expand Up @@ -110,6 +110,6 @@ def crop_to_object(image: NDArray[np.uint8], coordinates: CroppableCoordinates)
return crop_to_bbox(image, box)


def b64_encode_image(img: NDArray[np.uint8], format: Base64Formats =".jpg") -> str:
def b64_encode_image(img: NDArray[np.uint8], format: Base64Formats = ".jpg") -> str:
_, encoded_image = cv2.imencode(format, img)
return base64.b64encode(encoded_image).decode("utf-8") # type: ignore
8 changes: 6 additions & 2 deletions encord_agents/fastapi/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def my_route(
return get_initialised_label_row(frame_data)


def dep_single_frame(lr: Annotated[LabelRowV2, Depends(dep_label_row)], frame_data: Annotated[FrameData, Form()]) -> NDArray[np.uint8]:
def dep_single_frame(
lr: Annotated[LabelRowV2, Depends(dep_label_row)], frame_data: Annotated[FrameData, Form()]
) -> NDArray[np.uint8]:
"""
Dependency to inject the underlying asset of the frame data.
Expand Down Expand Up @@ -214,7 +216,9 @@ def my_route(
yield iter_video(asset)


def dep_project(frame_data: Annotated[FrameData, Form()], client: Annotated[EncordUserClient, Depends(dep_client)]) -> Project:
def dep_project(
frame_data: Annotated[FrameData, Form()], client: Annotated[EncordUserClient, Depends(dep_client)]
) -> Project:
r"""
Dependency to provide an instantiated
[Project](https://docs.encord.com/sdk-documentation/sdk-references/LabelRowV2){ target="\_blank", rel="noopener noreferrer" }.
Expand Down
6 changes: 4 additions & 2 deletions encord_agents/tasks/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def _execute_tasks(
task.proceed(pathway_name=next_stage, bundle=bundle)

if pbar_update is not None:
pbar_update(1.)
pbar_update(1.0)
break

except KeyboardInterrupt:
Expand Down Expand Up @@ -474,7 +474,9 @@ def {fn_name}(...):
for lr in batch_lrs:
if lr:
lr.initialise_labels(bundle=lr_bundle)
self._execute_tasks(project, zip(batch, batch_lrs), runner_agent, num_retries, pbar_update=pbar.update)
self._execute_tasks(
project, zip(batch, batch_lrs), runner_agent, num_retries, pbar_update=pbar.update
)
except (PrintableError, AssertionError) as err:
if self.was_called_from_cli:
panel = Panel(err.args[0], width=None)
Expand Down

0 comments on commit 96a3dbf

Please sign in to comment.