Files
Roel van Dijk 93b020aef4 Update documentation to 0.9.16 (#10)
Co-authored-by: Roel van Dijk <rdvdijk@users.noreply.github.com>
2026-03-06 09:59:38 +01:00

245 lines
16 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../../">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Using Transformers for on-demand execution &mdash; Hansken Extraction Plugins for plugin developers 0.9.16
documentation</title>
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=d75fae25" />
<link rel="stylesheet" type="text/css" href="../../_static/css/theme.css?v=e59714d7" />
<link rel="stylesheet" type="text/css" href="../../_static/wider_pages.css?v=32ad70ab" />
<script src="../../_static/jquery.js?v=5d32c60e"></script>
<script src="../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="../../_static/documentation_options.js?v=433a2a34"></script>
<script src="../../_static/doctools.js?v=9a2dae69"></script>
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="next" title="Advanced use of the Test Framework in Python" href="testing.html" />
<link rel="prev" title="Python code snippets" href="snippets.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../../index.html" class="icon icon-home">
Hansken Extraction Plugins for plugin developers
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../introduction.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../concepts.html">General concepts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../spec.html">Extraction Plugin specifications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../java.html">Java</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../python.html">Python</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="api_changelog.html">Python API Changelog</a></li>
<li class="toctree-l2"><a class="reference internal" href="prerequisites.html">Prerequisites</a></li>
<li class="toctree-l2"><a class="reference internal" href="getting_started.html">Getting started</a></li>
<li class="toctree-l2"><a class="reference internal" href="packaging.html">Packaging</a></li>
<li class="toctree-l2"><a class="reference internal" href="snippets.html">Python code snippets</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Using Transformers for on-demand execution</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#what-are-transformers">What are Transformers?</a></li>
<li class="toctree-l3"><a class="reference internal" href="#how-do-transformers-work-in-hansken">How do Transformers work in Hansken?</a></li>
<li class="toctree-l3"><a class="reference internal" href="#developing-transformers">Developing Transformers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#limitations-on-transformers">Limitations on Transformers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#error-handling-in-transformers">Error handling in Transformers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#testing-transformers">Testing transformers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#calling-transformer-with-tools-transformers">Calling transformer with /tools/transformers</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="testing.html">Advanced use of the Test Framework in Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="hanskenpy.html">Run plugins with Hansken.py</a></li>
<li class="toctree-l2"><a class="reference internal" href="debugging.html">How to debug an Extraction Plugin</a></li>
<li class="toctree-l2"><a class="reference internal" href="../python.html#api-documentation">API Documentation</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../examples.html">Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../contact.html">Contact</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../changes.html">Changelog</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../index.html">Hansken Extraction Plugins for plugin developers</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="../python.html">Python</a></li>
<li class="breadcrumb-item active">Using Transformers for on-demand execution</li>
<li class="wy-breadcrumbs-aside">
<a href="../../_sources/dev/python/transformers.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="using-transformers-for-on-demand-execution">
<h1>Using Transformers for on-demand execution<a class="headerlink" href="#using-transformers-for-on-demand-execution" title="Link to this heading"></a></h1>
<section id="what-are-transformers">
<h2>What are Transformers?<a class="headerlink" href="#what-are-transformers" title="Link to this heading"></a></h2>
<p>Transformers are methods inside a plugin that can be called remotely at any moment.
This allows for live plugin execution independent of extraction.
Examples on how transformers could be used:</p>
<ul class="simple">
<li><p>For searching images using text (i.e. a purple car).</p></li>
<li><p>For translating text in traces so that an investigator can read the text in their preferred language in a UI.</p></li>
<li><p>For converting speech to text.</p></li>
</ul>
</section>
<section id="how-do-transformers-work-in-hansken">
<h2>How do Transformers work in Hansken?<a class="headerlink" href="#how-do-transformers-work-in-hansken" title="Link to this heading"></a></h2>
<p>Transformers can be implemented in extraction plugins. Using the Hansken REST API, calls can be made to a
specific plugins transformer by specifying the transformer one wishes to call as well as its arguments.
Hansken can automatically discover transformers before plugins are actually started.
Once it has received a request for invoking a transformer it can choose to start the plugins Docker container if it
is not already started and send the request once it is up and running.</p>
</section>
<section id="developing-transformers">
<h2>Developing Transformers<a class="headerlink" href="#developing-transformers" title="Link to this heading"></a></h2>
<p>This section assumes you use the same setup as is used in the <a class="reference external" href="https://github.com/NetherlandsForensicInstitute/hansken-extraction-plugin-template-python/">Extraction Plugin Examples</a>.</p>
<p>A transformer can be easily defined by using the transformer decorator. By creating a method inside your plugin and
decorating it with the transformer decorator it will automatically be made available to be remotely called.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">hansken_extraction_plugin.api.extraction_plugin</span><span class="w"> </span><span class="kn">import</span> <span class="n">ExtractionPlugin</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">hansken_extraction_plugin.decorators.transformer</span><span class="w"> </span><span class="kn">import</span> <span class="n">transformer</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Plugin</span><span class="p">(</span><span class="n">ExtractionPlugin</span><span class="p">):</span>
<span class="k">def</span><span class="w"> </span><span class="nf">plugin_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">def</span><span class="w"> </span><span class="nf">process</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trace</span><span class="p">,</span> <span class="n">data_context</span><span class="p">):</span>
<span class="o">...</span>
<span class="nd">@transformer</span>
<span class="k">def</span><span class="w"> </span><span class="nf">translate_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">language</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="c1"># To implement: Translate the text here.</span>
<span class="k">return</span> <span class="s2">&quot;Translated text&quot;</span>
</pre></div>
</div>
</section>
<section id="limitations-on-transformers">
<h2>Limitations on Transformers<a class="headerlink" href="#limitations-on-transformers" title="Link to this heading"></a></h2>
<p>However, there are some limitations on which methods can be turned into a decorator method:</p>
<ul class="simple">
<li><p>Transformers may only be defined on methods of a class that derives (indirectly) from BaseExtractionPlugin.</p>
<ul>
<li><p>Note: ExtractionPlugin derives from BaseExtractionPlugin and is therefore allowed.</p></li>
</ul>
</li>
<li><p>Transformer may not be static methods.</p></li>
<li><p>All parameters and the return type must be annotated with type hints (except the <code class="docutils literal notranslate"><span class="pre">self</span></code> parameter).</p></li>
<li><p>Parameters may not be positional-only or contain variable parameters like <code class="docutils literal notranslate"><span class="pre">*args</span></code> or <code class="docutils literal notranslate"><span class="pre">**kwarg</span></code>.</p></li>
<li><p>Parameters and return types may only be of the following (returning None is not supported):</p>
<ul>
<li><p>bool</p></li>
<li><p>int</p></li>
<li><p>float</p></li>
<li><p>str</p></li>
<li><p>bytes</p></li>
<li><p>bytearray</p></li>
<li><p>datetime.datetime (The datetime will be converted to unix time and then converted to datetime with the zone “UTC”. So the zone id is always(“UTC”))</p></li>
<li><p>hansken.util.GeographicLocation</p></li>
<li><p>hansken.util.Vector</p></li>
<li><p>typing.Sequence</p></li>
<li><p>typing.Mapping</p></li>
</ul>
</li>
</ul>
<p>Upon starting a plugin every method decorated with the transformer decorator will be automatically validated to see if they adhere to these requirements. An exception will be thrown when a method does not adhere to all of these requirements.</p>
</section>
<section id="error-handling-in-transformers">
<h2>Error handling in Transformers<a class="headerlink" href="#error-handling-in-transformers" title="Link to this heading"></a></h2>
<p>If at some point a transformer wishes to signal to the caller of the transformer that something has gone wrong
it can simply throw a suitable exception. Any exceptions being thrown will automatically be propagated to the client
calling the transformer by wrapping the exception in a gRPC exception. The stack trace will be provided as well for
debugging purposes.</p>
</section>
<section id="testing-transformers">
<h2>Testing transformers<a class="headerlink" href="#testing-transformers" title="Link to this heading"></a></h2>
<p>For more information on testing transformers, see the Testing Transformers section on the <a class="reference internal" href="testing.html"><span class="doc">page on testing plugins</span></a>.</p>
</section>
<section id="calling-transformer-with-tools-transformers">
<h2>Calling transformer with /tools/transformers<a class="headerlink" href="#calling-transformer-with-tools-transformers" title="Link to this heading"></a></h2>
<p>This REST Call calls a specified transformer method on a given tool/plugin with supplied arguments.
This endpoint allows a transformer to be executed on demand, outside an extraction. The transformer call can be used to transform user input to an output. A tool/plugin can have multiple transformer methods.
To use /tools/transformers REST CALL you need to know the following (see extractions tool or use the rest call/tools to get the information below):</p>
<ul class="simple">
<li><p>The name of the plugin/tool</p></li>
<li><p>The transformer name</p></li>
<li><p>The names of the parameters (arguments)</p></li>
</ul>
<p>Example JSON request body for the transformer above:</p>
<div class="highlight-JSON notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="w"> </span><span class="nt">&quot;arguments&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nt">&quot;text&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;value1&quot;</span><span class="p">,</span>
<span class="w"> </span><span class="nt">&quot;language&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;value2&quot;</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nt">&quot;tool&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;ExampleTool&quot;</span><span class="p">,</span>
<span class="w"> </span><span class="nt">&quot;transformer&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;translate_text&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="snippets.html" class="btn btn-neutral float-left" title="Python code snippets" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="testing.html" class="btn btn-neutral float-right" title="Advanced use of the Test Framework in Python" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2020-2026 Netherlands Forensic Institute.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>