prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Add a function to filter labels
  When I rechecked the labels of the annotated data, I had no way of filtering out the labels I wanted to see. For example, when I am doing a check of dichotomous annotations, I would like to filter the data set to find out which labels are positive and which are negative, so that I can save time on the check. However, due to the lack of this function, I have to filter one by one from dataset, which wastes a lot of time and manpower.
  
  Thanks for every contributor!
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `backend/examples/filters.py`
  Content:
  ```
  1 from django.db.models import Count, Q
  2 from django_filters.rest_framework import BooleanFilter, FilterSet
  3 
  4 from .models import Example
  5 
  6 
  7 class ExampleFilter(FilterSet):
  8     confirmed = BooleanFilter(field_name="states", method="filter_by_state")
  9 
  10     def filter_by_state(self, queryset, field_name, is_confirmed: bool):
  11         queryset = queryset.annotate(
  12             num_confirm=Count(
  13                 expression=field_name,
  14                 filter=Q(**{f"{field_name}__confirmed_by": self.request.user})
  15                 | Q(project__collaborative_annotation=True),
  16             )
  17         )
  18         if is_confirmed:
  19             queryset = queryset.filter(num_confirm__gte=1)
  20         else:
  21             queryset = queryset.filter(num_confirm__lte=0)
  22         return queryset
  23 
  24     class Meta:
  25         model = Example
  26         fields = ("project", "text", "created_at", "updated_at")
  27
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/backend/examples/filters.py b/backend/examples/filters.py
  --- a/backend/examples/filters.py
  +++ b/backend/examples/filters.py
  @@ -1,11 +1,12 @@
  -from django.db.models import Count, Q
  -from django_filters.rest_framework import BooleanFilter, FilterSet
  +from django.db.models import Count, Q, QuerySet
  +from django_filters.rest_framework import BooleanFilter, CharFilter, FilterSet
   
   from .models import Example
   
   
   class ExampleFilter(FilterSet):
       confirmed = BooleanFilter(field_name="states", method="filter_by_state")
  +    label = CharFilter(method="filter_by_label")
   
       def filter_by_state(self, queryset, field_name, is_confirmed: bool):
           queryset = queryset.annotate(
  @@ -21,6 +22,35 @@
               queryset = queryset.filter(num_confirm__lte=0)
           return queryset
   
  +    def filter_by_label(self, queryset: QuerySet, field_name: str, label: str) -> QuerySet:
  +        """Filter examples by a given label name.
  +
  +        This performs filtering on all of the following labels at once:
  +        - categories
  +        - spans
  +        - relations
  +        - bboxes
  +        - segmentations
  +
  +        Todo: Consider project type to make filtering more efficient.
  +
  +        Args:
  +            queryset (QuerySet): QuerySet to filter.
  +            field_name (str): This equals to `label`.
  +            label (str): The label name to filter.
  +
  +        Returns:
  +            QuerySet: Filtered examples.
  +        """
  +        queryset = queryset.filter(
  +            Q(categories__label__text=label)
  +            | Q(spans__label__text=label)
  +            | Q(relations__type__text=label)
  +            | Q(bboxes__label__text=label)
  +            | Q(segmentations__label__text=label)
  +        )
  +        return queryset
  +
       class Meta:
           model = Example
  -        fields = ("project", "text", "created_at", "updated_at")
  +        fields = ("project", "text", "created_at", "updated_at", "label")
  
verification_info: {"golden_diff": "diff --git a/backend/examples/filters.py b/backend/examples/filters.py\n--- a/backend/examples/filters.py\n+++ b/backend/examples/filters.py\n@@ -1,11 +1,12 @@\n-from django.db.models import Count, Q\n-from django_filters.rest_framework import BooleanFilter, FilterSet\n+from django.db.models import Count, Q, QuerySet\n+from django_filters.rest_framework import BooleanFilter, CharFilter, FilterSet\n \n from .models import Example\n \n \n class ExampleFilter(FilterSet):\n     confirmed = BooleanFilter(field_name=\"states\", method=\"filter_by_state\")\n+    label = CharFilter(method=\"filter_by_label\")\n \n     def filter_by_state(self, queryset, field_name, is_confirmed: bool):\n         queryset = queryset.annotate(\n@@ -21,6 +22,35 @@\n             queryset = queryset.filter(num_confirm__lte=0)\n         return queryset\n \n+    def filter_by_label(self, queryset: QuerySet, field_name: str, label: str) -> QuerySet:\n+        \"\"\"Filter examples by a given label name.\n+\n+        This performs filtering on all of the following labels at once:\n+        - categories\n+        - spans\n+        - relations\n+        - bboxes\n+        - segmentations\n+\n+        Todo: Consider project type to make filtering more efficient.\n+\n+        Args:\n+            queryset (QuerySet): QuerySet to filter.\n+            field_name (str): This equals to `label`.\n+            label (str): The label name to filter.\n+\n+        Returns:\n+            QuerySet: Filtered examples.\n+        \"\"\"\n+        queryset = queryset.filter(\n+            Q(categories__label__text=label)\n+            | Q(spans__label__text=label)\n+            | Q(relations__type__text=label)\n+            | Q(bboxes__label__text=label)\n+            | Q(segmentations__label__text=label)\n+        )\n+        return queryset\n+\n     class Meta:\n         model = Example\n-        fields = (\"project\", \"text\", \"created_at\", \"updated_at\")\n+        fields = (\"project\", \"text\", \"created_at\", \"updated_at\", \"label\")\n", "issue": "Add a function to filter labels\nWhen I rechecked the labels of the annotated data, I had no way of filtering out the labels I wanted to see. For example, when I am doing a check of dichotomous annotations, I would like to filter the data set to find out which labels are positive and which are negative, so that I can save time on the check. However, due to the lack of this function, I have to filter one by one from dataset, which wastes a lot of time and manpower.\r\n\r\nThanks for every contributor!\n", "before_files": [{"content": "from django.db.models import Count, Q\nfrom django_filters.rest_framework import BooleanFilter, FilterSet\n\nfrom .models import Example\n\n\nclass ExampleFilter(FilterSet):\n    confirmed = BooleanFilter(field_name=\"states\", method=\"filter_by_state\")\n\n    def filter_by_state(self, queryset, field_name, is_confirmed: bool):\n        queryset = queryset.annotate(\n            num_confirm=Count(\n                expression=field_name,\n                filter=Q(**{f\"{field_name}__confirmed_by\": self.request.user})\n                | Q(project__collaborative_annotation=True),\n            )\n        )\n        if is_confirmed:\n            queryset = queryset.filter(num_confirm__gte=1)\n        else:\n            queryset = queryset.filter(num_confirm__lte=0)\n        return queryset\n\n    class Meta:\n        model = Example\n        fields = (\"project\", \"text\", \"created_at\", \"updated_at\")\n", "path": "backend/examples/filters.py"}], "after_files": [{"content": "from django.db.models import Count, Q, QuerySet\nfrom django_filters.rest_framework import BooleanFilter, CharFilter, FilterSet\n\nfrom .models import Example\n\n\nclass ExampleFilter(FilterSet):\n    confirmed = BooleanFilter(field_name=\"states\", method=\"filter_by_state\")\n    label = CharFilter(method=\"filter_by_label\")\n\n    def filter_by_state(self, queryset, field_name, is_confirmed: bool):\n        queryset = queryset.annotate(\n            num_confirm=Count(\n                expression=field_name,\n                filter=Q(**{f\"{field_name}__confirmed_by\": self.request.user})\n                | Q(project__collaborative_annotation=True),\n            )\n        )\n        if is_confirmed:\n            queryset = queryset.filter(num_confirm__gte=1)\n        else:\n            queryset = queryset.filter(num_confirm__lte=0)\n        return queryset\n\n    def filter_by_label(self, queryset: QuerySet, field_name: str, label: str) -> QuerySet:\n        \"\"\"Filter examples by a given label name.\n\n        This performs filtering on all of the following labels at once:\n        - categories\n        - spans\n        - relations\n        - bboxes\n        - segmentations\n\n        Todo: Consider project type to make filtering more efficient.\n\n        Args:\n            queryset (QuerySet): QuerySet to filter.\n            field_name (str): This equals to `label`.\n            label (str): The label name to filter.\n\n        Returns:\n            QuerySet: Filtered examples.\n        \"\"\"\n        queryset = queryset.filter(\n            Q(categories__label__text=label)\n            | Q(spans__label__text=label)\n            | Q(relations__type__text=label)\n            | Q(bboxes__label__text=label)\n            | Q(segmentations__label__text=label)\n        )\n        return queryset\n\n    class Meta:\n        model = Example\n        fields = (\"project\", \"text\", \"created_at\", \"updated_at\", \"label\")\n", "path": "backend/examples/filters.py"}]}