feincms3-api 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- feincms3_api/__init__.py +1 -0
- feincms3_api/adapters.py +303 -0
- feincms3_api/admin.py +93 -0
- feincms3_api/api.py +66 -0
- feincms3_api/apps.py +70 -0
- feincms3_api/auth.py +29 -0
- feincms3_api/cabinet.py +78 -0
- feincms3_api/codecs.py +495 -0
- feincms3_api/conf.py +31 -0
- feincms3_api/content.py +312 -0
- feincms3_api/errors.py +53 -0
- feincms3_api/forms.py +36 -0
- feincms3_api/formsets.py +135 -0
- feincms3_api/http.py +195 -0
- feincms3_api/incubator/__init__.py +6 -0
- feincms3_api/incubator/autotranslate/__init__.py +0 -0
- feincms3_api/incubator/autotranslate/apps.py +47 -0
- feincms3_api/incubator/autotranslate/backends/__init__.py +78 -0
- feincms3_api/incubator/autotranslate/backends/anthropic.py +98 -0
- feincms3_api/incubator/autotranslate/conf.py +59 -0
- feincms3_api/incubator/autotranslate/datasets.py +428 -0
- feincms3_api/incubator/autotranslate/fields.py +213 -0
- feincms3_api/incubator/autotranslate/links.py +122 -0
- feincms3_api/incubator/autotranslate/loader.py +1250 -0
- feincms3_api/incubator/autotranslate/management/__init__.py +0 -0
- feincms3_api/incubator/autotranslate/management/commands/__init__.py +0 -0
- feincms3_api/incubator/autotranslate/management/commands/f3autotranslate.py +162 -0
- feincms3_api/incubator/autotranslate/management/commands/f3autotranslateload.py +92 -0
- feincms3_api/incubator/autotranslate/review.py +104 -0
- feincms3_api/incubator/autotranslate/strings.py +313 -0
- feincms3_api/incubator/autotranslate/translate.py +260 -0
- feincms3_api/kinds.py +86 -0
- feincms3_api/management/__init__.py +0 -0
- feincms3_api/management/commands/__init__.py +0 -0
- feincms3_api/management/commands/f3api_cleanup.py +18 -0
- feincms3_api/management/commands/f3api_schema.py +32 -0
- feincms3_api/management/commands/f3api_token.py +39 -0
- feincms3_api/migrations/0001_initial.py +91 -0
- feincms3_api/migrations/0002_upload.py +61 -0
- feincms3_api/migrations/__init__.py +0 -0
- feincms3_api/models.py +189 -0
- feincms3_api/paths.py +43 -0
- feincms3_api/reading.py +120 -0
- feincms3_api/references.py +123 -0
- feincms3_api/resources.py +580 -0
- feincms3_api/schema.py +80 -0
- feincms3_api/snapshot.py +110 -0
- feincms3_api/templates/admin/feincms3_api/apitoken/created.html +19 -0
- feincms3_api/writing.py +255 -0
- feincms3_api-0.0.1.dist-info/METADATA +69 -0
- feincms3_api-0.0.1.dist-info/RECORD +53 -0
- feincms3_api-0.0.1.dist-info/WHEEL +4 -0
- feincms3_api-0.0.1.dist-info/licenses/LICENSE +29 -0
feincms3_api/http.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""The django-ninja router around the operations"""
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from django.http import HttpRequest, HttpResponse, JsonResponse
|
|
7
|
+
from ninja import File, Router, Schema, UploadedFile
|
|
8
|
+
|
|
9
|
+
from feincms3_api.errors import APIError
|
|
10
|
+
from feincms3_api.reading import _bool, expands_references
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ObjectIn(Schema):
|
|
14
|
+
fields: dict[str, Any] | None = None
|
|
15
|
+
content: dict[str, Any] | None = None
|
|
16
|
+
inlines: dict[str, Any] | None = None
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ContentIn(Schema):
|
|
20
|
+
content: dict[str, Any]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PluginIn(Schema):
|
|
24
|
+
fields: dict[str, Any]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def build_router(api):
|
|
28
|
+
router = Router(auth=api.auth, tags=["content"])
|
|
29
|
+
|
|
30
|
+
@router.get("schema/", operation_id="schema")
|
|
31
|
+
@_errors
|
|
32
|
+
def schema(request):
|
|
33
|
+
"""Resources, their region sets and the plugins allowed per region"""
|
|
34
|
+
return api.schema(request)
|
|
35
|
+
|
|
36
|
+
@router.get("schema/{type_label}/", operation_id="type_schema")
|
|
37
|
+
@_errors
|
|
38
|
+
def type_schema(request, type_label: str):
|
|
39
|
+
"""The JSON schema of one type's ``fields``"""
|
|
40
|
+
return api.type_schema(request, type_label)
|
|
41
|
+
|
|
42
|
+
@router.post("uploads/", operation_id="upload")
|
|
43
|
+
@_errors
|
|
44
|
+
def upload(request, file: File[UploadedFile]):
|
|
45
|
+
"""Stage a file (multipart, field ``file``); name it in a write as
|
|
46
|
+
``{"upload": id}``"""
|
|
47
|
+
return JsonResponse(api.upload(request, file), status=201)
|
|
48
|
+
|
|
49
|
+
for resource in api.resources:
|
|
50
|
+
_add_routes(api, router, resource)
|
|
51
|
+
return router
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _add_routes(api, router, resource):
|
|
55
|
+
name = resource.name
|
|
56
|
+
operation = name.replace("-", "_")
|
|
57
|
+
|
|
58
|
+
if resource.findable:
|
|
59
|
+
# Before {pk}/, which would take "find" for an id
|
|
60
|
+
@router.get(f"{name}/find/", operation_id=f"{operation}_find")
|
|
61
|
+
@_errors
|
|
62
|
+
def find(request: HttpRequest):
|
|
63
|
+
"""One object by its ``path``"""
|
|
64
|
+
return _json(api.find(request, name, request.GET))
|
|
65
|
+
|
|
66
|
+
@router.get(f"{name}/", operation_id=f"{operation}_list")
|
|
67
|
+
@_errors
|
|
68
|
+
def list_objects(request: HttpRequest):
|
|
69
|
+
"""List: ``limit``, ``offset``, ``q``, filters, ``content``, ``fields``"""
|
|
70
|
+
return api.list(request, name, request.GET)
|
|
71
|
+
|
|
72
|
+
@router.get(f"{name}/{{pk}}/", operation_id=f"{operation}_detail")
|
|
73
|
+
@_errors
|
|
74
|
+
def detail(request, pk: str, response: HttpResponse):
|
|
75
|
+
"""One object with its content; ``nested=true`` for a tree"""
|
|
76
|
+
wire = api.detail(
|
|
77
|
+
request,
|
|
78
|
+
name,
|
|
79
|
+
pk,
|
|
80
|
+
nested=_bool(request.GET.get("nested")),
|
|
81
|
+
expand=expands_references(request.GET),
|
|
82
|
+
)
|
|
83
|
+
response["ETag"] = wire["meta"]["etag"]
|
|
84
|
+
return wire
|
|
85
|
+
|
|
86
|
+
@router.get(
|
|
87
|
+
f"{name}/{{pk}}/plugins/{{type_label}}/{{plugin_id}}/",
|
|
88
|
+
operation_id=f"{operation}_plugin",
|
|
89
|
+
)
|
|
90
|
+
@_errors
|
|
91
|
+
def plugin(request, pk: str, type_label: str, plugin_id: str):
|
|
92
|
+
"""One plugin of an object"""
|
|
93
|
+
return api.plugin(
|
|
94
|
+
request,
|
|
95
|
+
name,
|
|
96
|
+
pk,
|
|
97
|
+
type_label,
|
|
98
|
+
plugin_id,
|
|
99
|
+
expand=expands_references(request.GET),
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
@router.post(f"{name}/", operation_id=f"{operation}_create")
|
|
103
|
+
@_errors
|
|
104
|
+
def create(request, payload: ObjectIn, dry_run: bool = False):
|
|
105
|
+
"""Create an object, with its content if given"""
|
|
106
|
+
wire = api.create(
|
|
107
|
+
request, name, payload.model_dump(exclude_unset=True), dry_run=dry_run
|
|
108
|
+
)
|
|
109
|
+
return _json(wire, 200 if dry_run else 201)
|
|
110
|
+
|
|
111
|
+
@router.patch(f"{name}/{{pk}}/", operation_id=f"{operation}_update")
|
|
112
|
+
@_errors
|
|
113
|
+
def update(request, pk: str, payload: ObjectIn, dry_run: bool = False):
|
|
114
|
+
"""Change fields, and replace the regions given in ``content``"""
|
|
115
|
+
wire = api.update(
|
|
116
|
+
request,
|
|
117
|
+
name,
|
|
118
|
+
pk,
|
|
119
|
+
payload.model_dump(exclude_unset=True),
|
|
120
|
+
if_match=request.headers.get("If-Match"),
|
|
121
|
+
dry_run=dry_run,
|
|
122
|
+
)
|
|
123
|
+
return _json(wire)
|
|
124
|
+
|
|
125
|
+
@router.put(f"{name}/{{pk}}/content/", operation_id=f"{operation}_content")
|
|
126
|
+
@_errors
|
|
127
|
+
def replace_content(request, pk: str, payload: ContentIn, dry_run: bool = False):
|
|
128
|
+
"""Replace the regions given; the others are left alone"""
|
|
129
|
+
wire = api.replace_content(
|
|
130
|
+
request,
|
|
131
|
+
name,
|
|
132
|
+
pk,
|
|
133
|
+
payload.content,
|
|
134
|
+
if_match=request.headers.get("If-Match"),
|
|
135
|
+
dry_run=dry_run,
|
|
136
|
+
)
|
|
137
|
+
return _json(wire)
|
|
138
|
+
|
|
139
|
+
@router.patch(
|
|
140
|
+
f"{name}/{{pk}}/plugins/{{type_label}}/{{plugin_id}}/",
|
|
141
|
+
operation_id=f"{operation}_plugin_update",
|
|
142
|
+
)
|
|
143
|
+
@_errors
|
|
144
|
+
def update_plugin(
|
|
145
|
+
request,
|
|
146
|
+
pk: str,
|
|
147
|
+
type_label: str,
|
|
148
|
+
plugin_id: str,
|
|
149
|
+
payload: PluginIn,
|
|
150
|
+
dry_run: bool = False,
|
|
151
|
+
):
|
|
152
|
+
"""Change one plugin's fields; needs the object's ETag"""
|
|
153
|
+
wire, etag = api.update_plugin(
|
|
154
|
+
request,
|
|
155
|
+
name,
|
|
156
|
+
pk,
|
|
157
|
+
type_label,
|
|
158
|
+
plugin_id,
|
|
159
|
+
payload.model_dump(),
|
|
160
|
+
if_match=request.headers.get("If-Match"),
|
|
161
|
+
dry_run=dry_run,
|
|
162
|
+
)
|
|
163
|
+
response = JsonResponse(wire)
|
|
164
|
+
response["ETag"] = etag
|
|
165
|
+
return response
|
|
166
|
+
|
|
167
|
+
@router.delete(f"{name}/{{pk}}/", operation_id=f"{operation}_delete")
|
|
168
|
+
@_errors
|
|
169
|
+
def delete(request, pk: str, dry_run: bool = False):
|
|
170
|
+
"""Delete an object"""
|
|
171
|
+
api.delete(
|
|
172
|
+
request,
|
|
173
|
+
name,
|
|
174
|
+
pk,
|
|
175
|
+
if_match=request.headers.get("If-Match"),
|
|
176
|
+
dry_run=dry_run,
|
|
177
|
+
)
|
|
178
|
+
return HttpResponse(status=204)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _json(wire, status=200):
|
|
182
|
+
response = JsonResponse(wire, status=status)
|
|
183
|
+
response["ETag"] = wire["meta"]["etag"]
|
|
184
|
+
return response
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _errors(fn):
|
|
188
|
+
@functools.wraps(fn)
|
|
189
|
+
def wrapper(*args, **kwargs):
|
|
190
|
+
try:
|
|
191
|
+
return fn(*args, **kwargs)
|
|
192
|
+
except APIError as exc:
|
|
193
|
+
return JsonResponse(exc.as_json(), status=exc.status)
|
|
194
|
+
|
|
195
|
+
return wrapper
|
|
File without changes
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from django.apps import AppConfig, apps
|
|
2
|
+
from django.core import checks
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Feincms3AutotranslateConfig(AppConfig):
|
|
6
|
+
name = "feincms3_api.incubator.autotranslate"
|
|
7
|
+
label = "feincms3_autotranslate"
|
|
8
|
+
verbose_name = "feincms3-autotranslate"
|
|
9
|
+
|
|
10
|
+
def ready(self):
|
|
11
|
+
checks.register(check_field_settings)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _resolve(entry):
|
|
15
|
+
"""``app_label.model_name.path`` -> (model, path), or a problem"""
|
|
16
|
+
app_label, _sep, rest = entry.partition(".")
|
|
17
|
+
model_name, _sep, path = rest.partition(".")
|
|
18
|
+
try:
|
|
19
|
+
model = apps.get_model(app_label, model_name)
|
|
20
|
+
except (LookupError, ValueError):
|
|
21
|
+
return None, f"{entry!r} names no model."
|
|
22
|
+
if not path:
|
|
23
|
+
return None, f"{entry!r} names no field."
|
|
24
|
+
return model, path
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def check_field_settings(app_configs=None, **kwargs):
|
|
28
|
+
from feincms3_api.incubator.autotranslate.conf import config # noqa: PLC0415
|
|
29
|
+
from feincms3_api.incubator.autotranslate.fields import all_paths # noqa: PLC0415
|
|
30
|
+
|
|
31
|
+
errors = []
|
|
32
|
+
for setting in ("EXCLUDE_FIELDS", "LINK_FIELDS"):
|
|
33
|
+
for entry in config()[setting]:
|
|
34
|
+
model, path = _resolve(entry)
|
|
35
|
+
if model is None:
|
|
36
|
+
problem = path
|
|
37
|
+
elif path not in all_paths(model):
|
|
38
|
+
problem = f"{entry!r}: {model._meta.label_lower} has no {path!r}."
|
|
39
|
+
else:
|
|
40
|
+
continue
|
|
41
|
+
errors.append(
|
|
42
|
+
checks.Error(
|
|
43
|
+
f'FEINCMS3_AUTOTRANSLATE["{setting}"]: {problem}',
|
|
44
|
+
id="feincms3_autotranslate.E001",
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
return errors
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Translators
|
|
3
|
+
|
|
4
|
+
A translator turns ``{key: text}`` into ``{key: translated text}`` for one
|
|
5
|
+
unit, and raises ``TranslationError`` when it can't. ``BackendError`` stops
|
|
6
|
+
the whole run (no credentials, an unknown model), since every further unit
|
|
7
|
+
would fail the same way.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
|
|
12
|
+
from django.utils.module_loading import import_string
|
|
13
|
+
|
|
14
|
+
from feincms3_api.incubator.autotranslate.conf import config
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TranslationError(Exception):
|
|
18
|
+
"""This unit failed, the run goes on"""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class BackendError(Exception):
|
|
22
|
+
"""Nothing will work, stop"""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def rules(*, source, target, glossary, instructions, html_keys):
|
|
26
|
+
"""The instructions every request carries, for people and for models"""
|
|
27
|
+
lines = [
|
|
28
|
+
f"Translate the values of the JSON object from {source} into {target}.",
|
|
29
|
+
"Answer with a JSON object with exactly the same keys, and nothing else.",
|
|
30
|
+
(
|
|
31
|
+
"Translate every value. Keep the meaning, register and length close to"
|
|
32
|
+
" the original. Keep numbers, dates, email addresses and URLs as they are."
|
|
33
|
+
),
|
|
34
|
+
]
|
|
35
|
+
if html_keys:
|
|
36
|
+
lines.append(
|
|
37
|
+
"Some values are HTML. Translate only the text between the tags."
|
|
38
|
+
" Keep every tag, every attribute (href in particular) and every"
|
|
39
|
+
" entity exactly as they are, in the same order."
|
|
40
|
+
)
|
|
41
|
+
if glossary.get("keep"):
|
|
42
|
+
lines.append(
|
|
43
|
+
"Never translate these terms: " + ", ".join(glossary["keep"]) + "."
|
|
44
|
+
)
|
|
45
|
+
if glossary.get("terms"):
|
|
46
|
+
lines.append(
|
|
47
|
+
"Use these translations: "
|
|
48
|
+
+ "; ".join(f"{a} -> {b}" for a, b in glossary["terms"].items())
|
|
49
|
+
+ "."
|
|
50
|
+
)
|
|
51
|
+
if instructions:
|
|
52
|
+
lines.append(instructions)
|
|
53
|
+
return "\n".join(lines)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def parse_answer(text):
|
|
57
|
+
"""The JSON object of an answer, tolerating a Markdown code fence"""
|
|
58
|
+
text = text.strip()
|
|
59
|
+
if text.startswith("```"):
|
|
60
|
+
text = text.split("\n", 1)[1] if "\n" in text else ""
|
|
61
|
+
text = text.rsplit("```", 1)[0]
|
|
62
|
+
try:
|
|
63
|
+
answer = json.loads(text)
|
|
64
|
+
except ValueError as exc:
|
|
65
|
+
raise TranslationError(f"The answer isn't valid JSON: {exc}") from exc
|
|
66
|
+
if not isinstance(answer, dict):
|
|
67
|
+
raise TranslationError("The answer isn't a JSON object.")
|
|
68
|
+
return answer
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def get_backend(*, model=None):
|
|
72
|
+
path = config()["BACKEND"]
|
|
73
|
+
if not path:
|
|
74
|
+
raise BackendError(
|
|
75
|
+
'FEINCMS3_AUTOTRANSLATE["BACKEND"] isn\'t set. Use --prepare and'
|
|
76
|
+
" --apply to translate without one."
|
|
77
|
+
)
|
|
78
|
+
return import_string(path)(model=model or config()["MODEL"])
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The Claude API
|
|
3
|
+
|
|
4
|
+
Needs ``feincms3-autotranslate[anthropic]``, ``ANTHROPIC_API_KEY`` (or
|
|
5
|
+
another credential the SDK finds) in the developer's environment, and a
|
|
6
|
+
model: ``FEINCMS3_AUTOTRANSLATE["MODEL"]`` or ``--model``. There is no
|
|
7
|
+
default model on purpose.
|
|
8
|
+
|
|
9
|
+
An object's strings are split into several requests by size; each carries
|
|
10
|
+
the rules, the glossary, the instructions and the object's title. An answer
|
|
11
|
+
which was cut off, isn't JSON or was refused fails the object. Rate limits,
|
|
12
|
+
overloads and server errors are retried by the SDK (``max_retries``).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
|
|
17
|
+
from feincms3_api.incubator.autotranslate.backends import (
|
|
18
|
+
BackendError,
|
|
19
|
+
TranslationError,
|
|
20
|
+
parse_answer,
|
|
21
|
+
rules,
|
|
22
|
+
)
|
|
23
|
+
from feincms3_api.incubator.autotranslate.conf import config
|
|
24
|
+
from feincms3_api.incubator.autotranslate.strings import split_strings
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Anthropic:
|
|
28
|
+
max_tokens = 64000
|
|
29
|
+
max_retries = 5
|
|
30
|
+
|
|
31
|
+
def __init__(self, *, model):
|
|
32
|
+
if not model:
|
|
33
|
+
raise BackendError(
|
|
34
|
+
'The anthropic backend needs a model: FEINCMS3_AUTOTRANSLATE["MODEL"]'
|
|
35
|
+
" or --model. There is no default."
|
|
36
|
+
)
|
|
37
|
+
try:
|
|
38
|
+
import anthropic # noqa: PLC0415
|
|
39
|
+
except ImportError as exc:
|
|
40
|
+
raise BackendError(
|
|
41
|
+
"Install feincms3-autotranslate[anthropic] to use the Claude API."
|
|
42
|
+
) from exc
|
|
43
|
+
self.anthropic = anthropic
|
|
44
|
+
self.model = model
|
|
45
|
+
self.client = anthropic.Anthropic(max_retries=self.max_retries)
|
|
46
|
+
|
|
47
|
+
def translate(
|
|
48
|
+
self, strings, *, source, target, glossary, instructions, title="", html_keys=()
|
|
49
|
+
):
|
|
50
|
+
system = rules(
|
|
51
|
+
source=source,
|
|
52
|
+
target=target,
|
|
53
|
+
glossary=glossary,
|
|
54
|
+
instructions=instructions,
|
|
55
|
+
html_keys=html_keys,
|
|
56
|
+
)
|
|
57
|
+
answer = {}
|
|
58
|
+
for part in split_strings(strings, config()["MAX_REQUEST_CHARS"]):
|
|
59
|
+
answer |= self._request(system, part, title=title, html_keys=html_keys)
|
|
60
|
+
return answer
|
|
61
|
+
|
|
62
|
+
def _request(self, system, strings, *, title, html_keys):
|
|
63
|
+
html = [key for key in html_keys if key in strings]
|
|
64
|
+
content = (
|
|
65
|
+
(f"The strings belong to: {title}\n" if title else "")
|
|
66
|
+
+ (f"HTML values: {', '.join(html)}\n" if html else "")
|
|
67
|
+
+ json.dumps(strings, ensure_ascii=False, indent=1)
|
|
68
|
+
)
|
|
69
|
+
anthropic = self.anthropic
|
|
70
|
+
try:
|
|
71
|
+
with self.client.messages.stream(
|
|
72
|
+
model=self.model,
|
|
73
|
+
max_tokens=self.max_tokens,
|
|
74
|
+
system=system,
|
|
75
|
+
messages=[{"role": "user", "content": content}],
|
|
76
|
+
) as stream:
|
|
77
|
+
message = stream.get_final_message()
|
|
78
|
+
except (
|
|
79
|
+
anthropic.AuthenticationError,
|
|
80
|
+
anthropic.PermissionDeniedError,
|
|
81
|
+
anthropic.NotFoundError,
|
|
82
|
+
) as exc:
|
|
83
|
+
raise BackendError(f"The Claude API refused: {exc}") from exc
|
|
84
|
+
except anthropic.APIStatusError as exc:
|
|
85
|
+
raise TranslationError(
|
|
86
|
+
f"The Claude API answered {exc.status_code}: {exc.message}"
|
|
87
|
+
) from exc
|
|
88
|
+
except anthropic.APIConnectionError as exc:
|
|
89
|
+
raise TranslationError(
|
|
90
|
+
f"The Claude API couldn't be reached: {exc}"
|
|
91
|
+
) from exc
|
|
92
|
+
|
|
93
|
+
if message.stop_reason == "max_tokens":
|
|
94
|
+
raise TranslationError("The answer was cut off at the output limit.")
|
|
95
|
+
if message.stop_reason == "refusal":
|
|
96
|
+
raise TranslationError("The model declined to translate this.")
|
|
97
|
+
text = "".join(block.text for block in message.content if block.type == "text")
|
|
98
|
+
return parse_answer(text)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import tomllib
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
DEFAULTS = {
|
|
7
|
+
# Dotted path of the translator class; the files backend needs none.
|
|
8
|
+
"BACKEND": None,
|
|
9
|
+
# Required by the anthropic backend, deliberately without a default.
|
|
10
|
+
"MODEL": None,
|
|
11
|
+
# A TOML file, see ``load_glossary()``.
|
|
12
|
+
"GLOSSARY": None,
|
|
13
|
+
"INSTRUCTIONS": "",
|
|
14
|
+
# Fields holding a URL beyond URLField, ``url`` and ``*_url``.
|
|
15
|
+
"LINK_FIELDS": [],
|
|
16
|
+
# Fields which are never sent, ``app_label.model_name.field[.json.path]``.
|
|
17
|
+
"EXCLUDE_FIELDS": [],
|
|
18
|
+
# Hosts whose absolute URLs are first-party links; defaults to
|
|
19
|
+
# ``ALLOWED_HOSTS``.
|
|
20
|
+
"FIRST_PARTY_HOSTS": None,
|
|
21
|
+
# A callable (or its dotted path) returning the public URL of a page.
|
|
22
|
+
"PAGE_URL": None,
|
|
23
|
+
# One request carries at most this many characters of source text, an
|
|
24
|
+
# object with more is split into several requests.
|
|
25
|
+
"MAX_REQUEST_CHARS": 20000,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def config():
|
|
30
|
+
return DEFAULTS | getattr(settings, "FEINCMS3_AUTOTRANSLATE", {})
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def first_language():
|
|
34
|
+
return settings.LANGUAGES[0][0]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def load_glossary(path, *, source, target):
|
|
38
|
+
"""
|
|
39
|
+
Read the glossary for one language pair
|
|
40
|
+
|
|
41
|
+
The file looks like this::
|
|
42
|
+
|
|
43
|
+
# Terms which are never translated, in any language.
|
|
44
|
+
keep = ["Acme", "AcmeConnect"]
|
|
45
|
+
|
|
46
|
+
[de.fr]
|
|
47
|
+
BVG = "LPP"
|
|
48
|
+
Vorsorgeausweis = "certificat de prévoyance"
|
|
49
|
+
|
|
50
|
+
The result is ``{"keep": [...], "terms": {source term: target term}}``.
|
|
51
|
+
"""
|
|
52
|
+
if not path:
|
|
53
|
+
return {"keep": [], "terms": {}}
|
|
54
|
+
with open(path, "rb") as f:
|
|
55
|
+
data = tomllib.load(f)
|
|
56
|
+
return {
|
|
57
|
+
"keep": list(data.get("keep", [])),
|
|
58
|
+
"terms": dict(data.get(source, {}).get(target, {})),
|
|
59
|
+
}
|