|
10 | 10 | import openai
|
11 | 11 | from openai.types import ChatModel
|
12 | 12 | from openai.types.chat import ChatCompletionMessageParam
|
13 |
| -from viur.core import conf, current, errors, exposed, utils |
| 13 | +from viur.core import conf, current, db, errors, exposed, utils |
14 | 14 | from viur.core.decorators import access
|
15 | 15 | from viur.core.prototypes import List, Singleton, Tree
|
16 | 16 |
|
@@ -153,6 +153,21 @@ def generate_script(
|
153 | 153 | return message.model_dump_json() # TODO: parse real "code" value
|
154 | 154 |
|
155 | 155 | def get_viur_structures(self, modules_to_include: t.Iterable[str]) -> dict[str, dict]:
|
| 156 | + """ |
| 157 | + Collect and return ViUR module structures for a given list of module names. |
| 158 | +
|
| 159 | + For each named module, its structure is extracted if it is of type ``List`` or ``Tree``. |
| 160 | + ``Tree`` structures will return separate entries for ``node`` and ``leaf`` skeletons. |
| 161 | +
|
| 162 | + :param modules_to_include: List of ViUR module names to retrieve structures for. |
| 163 | + :return: A dictionary mapping module names to their respective structure definitions. |
| 164 | + For ``Tree`` modules, nested keys ``"node"`` and ``"leaf"`` are returned. |
| 165 | +
|
| 166 | + :raises ValueError: If a module exists but is not a supported type (i.e., not ``List`` or ``Tree``). |
| 167 | +
|
| 168 | + .. note:: |
| 169 | + Modules that are missing or not found are silently skipped. |
| 170 | + """ |
156 | 171 | structures_from_viur = {}
|
157 | 172 | for module_name in modules_to_include:
|
158 | 173 | module = getattr(conf.main_app.vi, module_name, None)
|
@@ -185,6 +200,28 @@ def translate(
|
185 | 200 | simplified: bool = False,
|
186 | 201 | characteristic: t.Optional[str] = None,
|
187 | 202 | ):
|
| 203 | + """ |
| 204 | + Translate a given text into a target language, optionally using a specific style. |
| 205 | +
|
| 206 | + This method sends the input text to OpenAI with instructions to |
| 207 | + translate it into the requested language, optionally applying predefined translation |
| 208 | + characteristics such as simplification. |
| 209 | +
|
| 210 | + :param text: The source text to translate. |
| 211 | + :param language: The target language code (e.g. ``"de"``, ``"en"``, ``"de-x-simple"``). |
| 212 | + :param simplified: **Deprecated** – Use ``characteristic="simplified"`` instead. |
| 213 | + When ``True``, applies a simplified language style. |
| 214 | + :param characteristic: Optional translation style (e.g. ``"simplified"``, ``"formal"``, etc.) |
| 215 | + as defined in ``CONFIG.translate_language_characteristics``. |
| 216 | + :return: Translated text as a plain string. HTML tags from the original text are preserved. |
| 217 | +
|
| 218 | + :raises BadRequest: If both ``simplified`` and ``characteristic`` are used simultaneously. |
| 219 | + :raises InternalServerError: If configuration is missing. |
| 220 | +
|
| 221 | + .. note:: |
| 222 | + - The translation style is determined by merging base rules (`*`) and the selected characteristic. |
| 223 | + - The returned translation contains only the translated text, with no explanation or additional formatting. |
| 224 | + """ |
188 | 225 | if simplified:
|
189 | 226 | if characteristic is not None:
|
190 | 227 | raise errors.BadRequest("Cannot use parameter *simplified* and *characteristic* at the same time")
|
@@ -218,11 +255,34 @@ def translate(
|
218 | 255 | @access("user-view")
|
219 | 256 | def describe_image(
|
220 | 257 | self,
|
221 |
| - filekey: str, |
| 258 | + filekey: db.Key | str, |
222 | 259 | prompt: str = "",
|
223 | 260 | context: str = "",
|
224 | 261 | language: str | None = None,
|
225 | 262 | ):
|
| 263 | + """ |
| 264 | + Generate an HTML ``alt`` attribute description for a given image using OpenAi. |
| 265 | +
|
| 266 | + This method reads an image via its filekey, resizes it to a configured pixel target, |
| 267 | + and sends it along with optional prompt and context data to an OpenAI model. |
| 268 | + The model returns a plain-text alternative description for accessibility purposes, |
| 269 | + formatted as an HTML ``alt`` text in the specified language. |
| 270 | +
|
| 271 | + :param filekey: Key identifying the image file in the ViUR file module. |
| 272 | + (Key of the :class:`file skeleton <viur.core.modules.file.FileLeafSkel>`). |
| 273 | + :param prompt: Optional user-defined hint or instruction for how the image should be interpreted. |
| 274 | + :param context: Optional additional background information to support a better description. |
| 275 | + :param language: Target language code for the generated description (e.g., ``"en"``, ``"de-x-simple"``). |
| 276 | + Falls back to the current session language if not specified. |
| 277 | + :return: A plain-text string suitable for use in an HTML ``alt`` attribute (no quotes or labels). |
| 278 | +
|
| 279 | + :raises InternalServerError: If required configuration is missing. |
| 280 | + :raises NotFound: If the referenced image file could not be loaded. |
| 281 | +
|
| 282 | + .. note:: |
| 283 | + - The image is resized and converted to JPEG before being sent to the model. |
| 284 | + - This function uses a preconfigured OpenAI model, pixel count, and JPEG quality settings. |
| 285 | + """ |
226 | 286 | if not (skel := self.getContents()):
|
227 | 287 | raise errors.InternalServerError(descr="Configuration missing")
|
228 | 288 |
|
|
0 commit comments