Spaces:
Configuration error
Download docs/source/plugins/dynamic_text_spans.md from bisalsaha/lebel-studio: direct link, hf CLI and curl.
- Browser
- Download file 4.56 kB
-
https://huggingface.co/spaces/bisalsaha/lebel-studio/resolve/main/docs/source/plugins/dynamic_text_spans.md
- Command line
-
hf download hf://spaces/bisalsaha/lebel-studio/docs/source/plugins/dynamic_text_spans.md
-
curl -L -o dynamic_text_spans.md https://huggingface.co/spaces/bisalsaha/lebel-studio/resolve/main/docs/source/plugins/dynamic_text_spans.md
title: Dynamic Text Spans
short: Dynamic Text Spans
type: plugins
category: Automation
cat: automation
order: 45
meta_title: Dynamic Text Spans
meta_description: Annotate edited text
tier: enterprise
!!! note For information about modifying this plugin or creating your own custom plugins, see Customize and Build Your Own Plugins.
For general plugin information, see [Plugins for projects](/guide/plugins) and [Plugin FAQ](faq).
About
This plugin “binds” a TextArea to a Text field, keeping them in sync as you edit. You can then apply labels to the text in the Text field, allowing you annotate text that is unique from the text you imported.
How this plugin works:
It defines two tag names:
TextAreaistranscriptionTextisextraction_text
It captures the current annotation ID so that it can stop running when you switch regions.
In
init(), it polls every 300 ms until it finds the whose name starts with "transcription" (Label Studio appends random suffixes). </p> </li> <li><p>Once found, it marks it <code>_already_loaded</code> and starts <code>tick()</code>, which runs every 300 ms: </p> <ul> <li>If the DOM node got replaced (e.g. you re-loaded or changed annotation), it re-queries for it. </li> <li>If you’ve moved to a different annotation ID, it stops. </li> <li>It grabs the live DOM value (<code>$ta.value</code>) or falls back to the annotation’s last saved result. </li> <li>It compares that string to the current <code>_value</code> of your <code>extraction_text</code> control. </li> <li>If they differ, it calls <code>text.setRemoteValue(val)</code>. This uses the Label Studio frontend API to update the other field’s value.</li> </ul> </li> </ol> <p>The net effect is a real-time mirror: whatever you type or paste into the "transcription" <code>TextArea</code> automatically appears in the "extraction_text" <code>Text</code> field. And, importantly, the text you annotate in the <code>Text</code> field can be exported with your results. </p> <h2 class="relative group flex items-baseline"> <a id="plugin" class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#plugin" > <span class="header-link"><!--[--><svg class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!--]--></span> </a> <span> Plugin </span> </h2> <pre><code class="language-javascript"><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">TEXTAREA_NAME</span> = <span class="hljs-string">"transcription"</span>; <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">TEXT_NAME</span> = <span class="hljs-string">"extraction_text"</span>; <span class="hljs-keyword">const</span> current_annotation = <span class="hljs-variable constant_">LSI</span>.<span class="hljs-property">annotation</span>.<span class="hljs-property">id</span>; (<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// textarea results have names like "tag_name:h4$h", so we use ^=</span> <span class="hljs-keyword">let</span> $ta = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">`textarea[name^="<span class="hljs-subst">${TEXTAREA_NAME}</span>"]`</span>); <span class="hljs-keyword">if</span> (!$ta) <span class="hljs-keyword">return</span> <span class="hljs-built_in">setTimeout</span>(init, <span class="hljs-number">300</span>); <span class="hljs-keyword">if</span> ($ta.<span class="hljs-property">_already_loaded</span>) <span class="hljs-keyword">return</span>; $ta.<span class="hljs-property">_already_loaded</span> = <span class="hljs-literal">true</span>; (<span class="hljs-keyword">function</span> <span class="hljs-title function_">tick</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// it can be another textarea with submitted result</span> <span class="hljs-keyword">if</span> (!$ta?.<span class="hljs-property">isConnected</span>) { $ta = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">`textarea[name^="<span class="hljs-subst">${TEXTAREA_NAME}</span>"]`</span>); } <span class="hljs-comment">// if we lost textarea/moved out of current annotation — break the cycle</span> <span class="hljs-keyword">if</span> (!$ta?.<span class="hljs-property">isConnected</span> || <span class="hljs-variable constant_">LSI</span>.<span class="hljs-property">annotation</span>?.<span class="hljs-property">id</span> !== current_annotation) <span class="hljs-keyword">return</span>; <span class="hljs-built_in">setTimeout</span>(tick, <span class="hljs-number">300</span>); <span class="hljs-keyword">const</span> textarea = <span class="hljs-variable constant_">LSI</span>.<span class="hljs-property">annotation</span>.<span class="hljs-property">names</span>.<span class="hljs-title function_">get</span>(<span class="hljs-variable constant_">TEXTAREA_NAME</span>); <span class="hljs-keyword">const</span> text = <span class="hljs-variable constant_">LSI</span>.<span class="hljs-property">annotation</span>.<span class="hljs-property">names</span>.<span class="hljs-title function_">get</span>(<span class="hljs-variable constant_">TEXT_NAME</span>); <span class="hljs-keyword">const</span> val = <span class="hljs-title class_">String</span>($ta.<span class="hljs-property">value</span> ?? textarea.<span class="hljs-property">result</span>?.<span class="hljs-property">value</span>.<span class="hljs-property">text</span> ?? <span class="hljs-string">""</span>); <span class="hljs-keyword">if</span> (text.<span class="hljs-property">_value</span> !== val) { text.<span class="hljs-title function_">setRemoteValue</span>(val); } })(); })(); </code></pre> <p><strong>Related LSI instance methods:</strong></p> <ul> <li><a href="/spaces/bisalsaha/lebel-studio/tree/main/docs/source/plugins/custom#LSI-annotation" rel="nofollow">LSI.annotation</a></li> </ul> <h2 class="relative group flex items-baseline"> <a id="labeling-interface" class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#labeling-interface" > <span class="header-link"><!--[--><svg class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!--]--></span> </a> <span> Labeling interface </span> </h2> <pre><code class="language-xml"><span class="hljs-tag"><<span class="hljs-name">View</span>></span> <span class="hljs-tag"><<span class="hljs-name">Header</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"1. Transcribe the audio below:"</span>/></span> <span class="hljs-tag"><<span class="hljs-name">Audio</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"audio"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$audio"</span> <span class="hljs-attr">zoom</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">hotkey</span>=<span class="hljs-string">"ctrl+enter"</span>/></span> <span class="hljs-tag"><<span class="hljs-name">TextArea</span></span> <span class="hljs-tag"> <span class="hljs-attr">name</span>=<span class="hljs-string">"transcription"</span></span> <span class="hljs-tag"> <span class="hljs-attr">toName</span>=<span class="hljs-string">"audio"</span></span> <span class="hljs-tag"> <span class="hljs-attr">rows</span>=<span class="hljs-string">"4"</span></span> <span class="hljs-tag"> <span class="hljs-attr">editable</span>=<span class="hljs-string">"true"</span></span> <span class="hljs-tag"> <span class="hljs-attr">maxSubmissions</span>=<span class="hljs-string">"1"</span></span> <span class="hljs-tag"> <span class="hljs-attr">showSubmitButton</span>=<span class="hljs-string">"false"</span></span> <span class="hljs-tag"> <span class="hljs-attr">value</span>=<span class="hljs-string">"$text"</span></span> <span class="hljs-tag"> /></span> <span class="hljs-tag"><<span class="hljs-name">Header</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"2. Extract spans from the transcript:"</span>/></span> <span class="hljs-comment"><!-- force HyperText into the DOM via non-breaking space --></span> <span class="hljs-tag"><<span class="hljs-name">View</span> <span class="hljs-attr">idAttr</span>=<span class="hljs-string">"extractionBlock"</span>></span> <span class="hljs-tag"><<span class="hljs-name">Text</span></span> <span class="hljs-tag"> <span class="hljs-attr">name</span>=<span class="hljs-string">"extraction_text"</span></span> <span class="hljs-tag"> <span class="hljs-attr">idAttr</span>=<span class="hljs-string">"extractionText"</span></span> <span class="hljs-tag"> <span class="hljs-attr">value</span>=<span class="hljs-string">"$transcription"</span></span> <span class="hljs-tag"> <span class="hljs-attr">inline</span>=<span class="hljs-string">"false"</span></span> <span class="hljs-tag"> /></span> <span class="hljs-tag"></<span class="hljs-name">View</span>></span> <span class="hljs-tag"><<span class="hljs-name">Labels</span></span> <span class="hljs-tag"> <span class="hljs-attr">name</span>=<span class="hljs-string">"span_labels"</span></span> <span class="hljs-tag"> <span class="hljs-attr">toName</span>=<span class="hljs-string">"extraction_text"</span></span> <span class="hljs-tag"> <span class="hljs-attr">choice</span>=<span class="hljs-string">"multiple"</span></span> <span class="hljs-tag"> <span class="hljs-attr">showInline</span>=<span class="hljs-string">"true"</span></span> <span class="hljs-tag"> ></span> <span class="hljs-tag"><<span class="hljs-name">Label</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"EntityType1"</span> <span class="hljs-attr">background</span>=<span class="hljs-string">"#8cc6ff"</span>/></span> <span class="hljs-tag"><<span class="hljs-name">Label</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"EntityType2"</span> <span class="hljs-attr">background</span>=<span class="hljs-string">"#ffa39e"</span>/></span> <span class="hljs-tag"></<span class="hljs-name">Labels</span>></span> <span class="hljs-tag"></<span class="hljs-name">View</span>></span> </code></pre> <p><strong>Related tags:</strong></p> <ul> <li><a href="/tags/view.html" rel="nofollow">View</a></li> <li><a href="/tags/header.html" rel="nofollow">Header</a></li> <li><a href="/tags/textarea.html" rel="nofollow">TextArea</a></li> <li><a href="/tags/audio.html" rel="nofollow">Audio</a></li> <li><a href="/tags/text.html" rel="nofollow">Text</a></li> <li><a href="/tags/labels.html" rel="nofollow">Labels</a></li> </ul> <h2 class="relative group flex items-baseline"> <a id="sample-data" class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#sample-data" > <span class="header-link"><!--[--><svg class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!--]--></span> </a> <span> Sample data </span> </h2> <pre><code class="language-json"><span class="hljs-punctuation">{</span> <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"audio"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"https://data.heartex.net/librispeech/dev-clean/3536/8226/3536-8226-0024.flac.wav"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"transcription"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">""</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"text"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"this is a test"</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span> </code></pre>