merlemcdurrrmott commited on
Commit
bc57e00
·
verified ·
1 Parent(s): c07c190

Upload app/assets/services/tagging.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app/assets/services/tagging.py +101 -0
app/assets/services/tagging.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Sequence
2
+
3
+ from app.assets.database.queries import (
4
+ AddTagsResult,
5
+ RemoveTagsResult,
6
+ add_tags_to_reference,
7
+ get_reference_with_owner_check,
8
+ list_tags_with_usage,
9
+ remove_tags_from_reference,
10
+ )
11
+ from app.assets.database.queries.tags import list_tag_counts_for_filtered_assets
12
+ from app.assets.services.schemas import TagUsage
13
+ from app.database.db import create_session
14
+
15
+
16
+ def apply_tags(
17
+ reference_id: str,
18
+ tags: list[str],
19
+ origin: str = "manual",
20
+ owner_id: str = "",
21
+ ) -> AddTagsResult:
22
+ with create_session() as session:
23
+ ref_row = get_reference_with_owner_check(session, reference_id, owner_id)
24
+
25
+ result = add_tags_to_reference(
26
+ session,
27
+ reference_id=reference_id,
28
+ tags=tags,
29
+ origin=origin,
30
+ create_if_missing=True,
31
+ reference_row=ref_row,
32
+ )
33
+ session.commit()
34
+
35
+ return result
36
+
37
+
38
+ def remove_tags(
39
+ reference_id: str,
40
+ tags: list[str],
41
+ owner_id: str = "",
42
+ ) -> RemoveTagsResult:
43
+ with create_session() as session:
44
+ get_reference_with_owner_check(session, reference_id, owner_id)
45
+
46
+ result = remove_tags_from_reference(
47
+ session,
48
+ reference_id=reference_id,
49
+ tags=tags,
50
+ )
51
+ session.commit()
52
+
53
+ return result
54
+
55
+
56
+ def list_tags(
57
+ prefix: str | None = None,
58
+ limit: int = 100,
59
+ offset: int = 0,
60
+ order: str = "count_desc",
61
+ include_zero: bool = True,
62
+ owner_id: str = "",
63
+ ) -> tuple[list[TagUsage], int]:
64
+ limit = max(1, min(1000, limit))
65
+ offset = max(0, offset)
66
+
67
+ with create_session() as session:
68
+ rows, total = list_tags_with_usage(
69
+ session,
70
+ prefix=prefix,
71
+ limit=limit,
72
+ offset=offset,
73
+ include_zero=include_zero,
74
+ order=order,
75
+ owner_id=owner_id,
76
+ )
77
+
78
+ return [TagUsage(name, count) for name, count in rows], total
79
+
80
+
81
+ def list_tag_histogram(
82
+ owner_id: str = "",
83
+ include_tags: Sequence[str] | None = None,
84
+ exclude_tags: Sequence[str] | None = None,
85
+ name_contains: str | None = None,
86
+ metadata_filter: dict | None = None,
87
+ limit: int = 100,
88
+ # Appended last so pre-existing positional callers keep binding correctly.
89
+ any_tags: Sequence[str] | None = None,
90
+ ) -> dict[str, int]:
91
+ with create_session() as session:
92
+ return list_tag_counts_for_filtered_assets(
93
+ session,
94
+ owner_id=owner_id,
95
+ include_tags=include_tags,
96
+ exclude_tags=exclude_tags,
97
+ any_tags=any_tags,
98
+ name_contains=name_contains,
99
+ metadata_filter=metadata_filter,
100
+ limit=limit,
101
+ )