FangSen9000 commited on
Commit ·
a16e5a2
1
Parent(s): 8af7be8
Add semantic text-to-gloss option
Browse files- index.js +117 -6
- pipeline01_text2gloss.py +136 -1
- viser_backend.py +11 -4
index.js
CHANGED
|
@@ -95,6 +95,30 @@ function ensureStyles() {
|
|
| 95 |
font-size: 16px;
|
| 96 |
color: #0f172a;
|
| 97 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
.stable-video-frame {
|
| 99 |
position: relative;
|
| 100 |
width: 100%;
|
|
@@ -190,6 +214,30 @@ function ensureStyles() {
|
|
| 190 |
.stable-control-actions button {
|
| 191 |
flex: 1;
|
| 192 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
.stable-status-chip {
|
| 194 |
font-size: 13px;
|
| 195 |
padding: 6px 12px;
|
|
@@ -628,6 +676,7 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 628 |
<div class="stable-control-actions">
|
| 629 |
<button id="stable-prewarm" class="btn secondary">Prewarm</button>
|
| 630 |
<button id="stable-translate" class="btn secondary">Text → Available Gloss</button>
|
|
|
|
| 631 |
</div>
|
| 632 |
<span class="stable-status-chip" id="stable-status">Idle</span>
|
| 633 |
</div>
|
|
@@ -635,6 +684,10 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 635 |
<label>Prompt / Text</label>
|
| 636 |
<textarea id="stable-prompt-text" placeholder="Describe the action or context; the system will translate it to gloss."></textarea>
|
| 637 |
<div class="stable-hint">Input text auto-translates and fills the uppercase gloss field below (still editable).</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
<div class="form-group" style="margin-top:8px;font-size:13px;color:#475569;">
|
| 639 |
<div>Prompt samples (picked from the latest prompt2gloss eval logs with strongest gloss matches):</div>
|
| 640 |
<div>1. Can you provide instruction on the sign language for “You’re going to hit it just a little more behind.”?</div>
|
|
@@ -752,7 +805,11 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 752 |
<div class="stable-text-playground stable-signer-panel">
|
| 753 |
<div class="stable-text-playground-title">
|
| 754 |
<h3>Selection Translation Playground</h3>
|
| 755 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
</div>
|
| 757 |
<div id="stable-selection-template" class="stable-reading-area stable-dialogue-area" tabindex="0" contenteditable="true">
|
| 758 |
<div class="stable-dialogue-turn question">
|
|
@@ -820,6 +877,10 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 820 |
this.refineLogsPre = container.querySelector('#stable-refine-logs');
|
| 821 |
this.prewarmBtn = container.querySelector('#stable-prewarm');
|
| 822 |
this.translateBtn = container.querySelector('#stable-translate');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 823 |
this.selectionTemplate = container.querySelector('#stable-selection-template');
|
| 824 |
this.selectionPopover = container.querySelector('#stable-selection-popover');
|
| 825 |
this.selectionTranslateBtn = container.querySelector('#stable-selection-translate');
|
|
@@ -873,6 +934,9 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 873 |
attachEvents() {
|
| 874 |
this.prewarmBtn.addEventListener('click', () => this.handlePrewarm());
|
| 875 |
this.translateBtn.addEventListener('click', () => this.handleTranslate());
|
|
|
|
|
|
|
|
|
|
| 876 |
this.generateBtn.addEventListener('click', () => this.handleGenerate());
|
| 877 |
this.videoRefreshBtn.addEventListener('click', () => this.refreshRecentVideos());
|
| 878 |
this.videoSelect.addEventListener('change', () => this.handleRecentSelection());
|
|
@@ -885,6 +949,9 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 885 |
this.selectionTemplate.addEventListener('mouseup', () => this.handleTemplateSelection());
|
| 886 |
this.selectionTemplate.addEventListener('keyup', () => this.handleTemplateSelection());
|
| 887 |
}
|
|
|
|
|
|
|
|
|
|
| 888 |
if (this.selectionTranslateBtn) {
|
| 889 |
this.selectionTranslateBtn.addEventListener('click', () => this.handleSelectedTextGenerate(true));
|
| 890 |
}
|
|
@@ -929,7 +996,8 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 929 |
refImagePath: this.refImageInput.value,
|
| 930 |
normalizePose: this.normalizeCheckbox.checked,
|
| 931 |
hideTorsoLines: this.hideTorsoCheckbox.checked,
|
| 932 |
-
translationMode: 'text2gloss'
|
|
|
|
| 933 |
};
|
| 934 |
}
|
| 935 |
|
|
@@ -1040,11 +1108,18 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 1040 |
this.setTranslateLoading(true);
|
| 1041 |
}
|
| 1042 |
try {
|
| 1043 |
-
const
|
|
|
|
| 1044 |
this.glossInput.value = gloss;
|
| 1045 |
if (!isAuto) {
|
| 1046 |
this.updateStatus('Gloss translation completed');
|
| 1047 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1048 |
}
|
| 1049 |
return gloss;
|
| 1050 |
} finally {
|
|
@@ -1059,12 +1134,15 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 1059 |
throw new Error('Prompt/Text is empty');
|
| 1060 |
}
|
| 1061 |
const payloadPrompt = originalPrompt || promptText;
|
|
|
|
| 1062 |
const response = await window.controlWaveWebSocket.sendRequest(this.id, {
|
| 1063 |
action: 'text_to_gloss',
|
| 1064 |
data: {
|
| 1065 |
text: promptText,
|
| 1066 |
prompt: payloadPrompt,
|
| 1067 |
-
maxNewTokens
|
|
|
|
|
|
|
| 1068 |
}
|
| 1069 |
});
|
| 1070 |
if (!response || response.status !== 'success' || !response.gloss) {
|
|
@@ -1073,7 +1151,37 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 1073 |
if (response.dropped && response.dropped.length > 0) {
|
| 1074 |
console.info('[StableSigner] dropped non-renderable text tokens', response.dropped);
|
| 1075 |
}
|
| 1076 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1077 |
}
|
| 1078 |
|
| 1079 |
handleTemplateSelection() {
|
|
@@ -1154,6 +1262,9 @@ export default class StableSignerPlugin extends ControlWavePlugin {
|
|
| 1154 |
throw new Error('WebSocket not ready');
|
| 1155 |
}
|
| 1156 |
const payload = this.collectPayload();
|
|
|
|
|
|
|
|
|
|
| 1157 |
payload.maxNewTokens = Math.min(toInt(this.maxNewTokensInput.value, 64), 18);
|
| 1158 |
payload.maxCandidates = Math.min(toInt(this.maxCandidatesInput.value, 10), 3);
|
| 1159 |
const response = await window.controlWaveWebSocket.sendRequest(this.id, {
|
|
|
|
| 95 |
font-size: 16px;
|
| 96 |
color: #0f172a;
|
| 97 |
}
|
| 98 |
+
.stable-playground-actions {
|
| 99 |
+
display: flex;
|
| 100 |
+
align-items: center;
|
| 101 |
+
justify-content: flex-end;
|
| 102 |
+
gap: 10px;
|
| 103 |
+
flex-wrap: wrap;
|
| 104 |
+
}
|
| 105 |
+
.stable-playground-actions label {
|
| 106 |
+
display: inline-flex;
|
| 107 |
+
align-items: center;
|
| 108 |
+
gap: 6px;
|
| 109 |
+
margin: 0;
|
| 110 |
+
font-size: 13px;
|
| 111 |
+
font-weight: 600;
|
| 112 |
+
color: #334155;
|
| 113 |
+
}
|
| 114 |
+
.stable-playground-actions input {
|
| 115 |
+
width: auto;
|
| 116 |
+
margin: 0;
|
| 117 |
+
}
|
| 118 |
+
.stable-playground-actions button {
|
| 119 |
+
padding: 6px 10px;
|
| 120 |
+
white-space: nowrap;
|
| 121 |
+
}
|
| 122 |
.stable-video-frame {
|
| 123 |
position: relative;
|
| 124 |
width: 100%;
|
|
|
|
| 214 |
.stable-control-actions button {
|
| 215 |
flex: 1;
|
| 216 |
}
|
| 217 |
+
.stable-translation-options {
|
| 218 |
+
display: flex;
|
| 219 |
+
align-items: center;
|
| 220 |
+
justify-content: space-between;
|
| 221 |
+
gap: 10px;
|
| 222 |
+
margin-top: 8px;
|
| 223 |
+
font-size: 13px;
|
| 224 |
+
color: #334155;
|
| 225 |
+
}
|
| 226 |
+
.stable-translation-options label {
|
| 227 |
+
display: inline-flex;
|
| 228 |
+
align-items: center;
|
| 229 |
+
gap: 6px;
|
| 230 |
+
margin: 0;
|
| 231 |
+
font-weight: 600;
|
| 232 |
+
}
|
| 233 |
+
.stable-translation-options input {
|
| 234 |
+
width: auto;
|
| 235 |
+
margin: 0;
|
| 236 |
+
}
|
| 237 |
+
.stable-translation-options button {
|
| 238 |
+
white-space: nowrap;
|
| 239 |
+
padding: 6px 10px;
|
| 240 |
+
}
|
| 241 |
.stable-status-chip {
|
| 242 |
font-size: 13px;
|
| 243 |
padding: 6px 12px;
|
|
|
|
| 676 |
<div class="stable-control-actions">
|
| 677 |
<button id="stable-prewarm" class="btn secondary">Prewarm</button>
|
| 678 |
<button id="stable-translate" class="btn secondary">Text → Available Gloss</button>
|
| 679 |
+
<button id="stable-semantic-demo" class="btn secondary">Semantic Demo</button>
|
| 680 |
</div>
|
| 681 |
<span class="stable-status-chip" id="stable-status">Idle</span>
|
| 682 |
</div>
|
|
|
|
| 684 |
<label>Prompt / Text</label>
|
| 685 |
<textarea id="stable-prompt-text" placeholder="Describe the action or context; the system will translate it to gloss."></textarea>
|
| 686 |
<div class="stable-hint">Input text auto-translates and fills the uppercase gloss field below (still editable).</div>
|
| 687 |
+
<div class="stable-translation-options">
|
| 688 |
+
<label><input type="checkbox" id="stable-semantic-gloss"> Semantic constrained gloss</label>
|
| 689 |
+
<span>Uses conservative synonym fallback; output remains limited to available poses.</span>
|
| 690 |
+
</div>
|
| 691 |
<div class="form-group" style="margin-top:8px;font-size:13px;color:#475569;">
|
| 692 |
<div>Prompt samples (picked from the latest prompt2gloss eval logs with strongest gloss matches):</div>
|
| 693 |
<div>1. Can you provide instruction on the sign language for “You’re going to hit it just a little more behind.”?</div>
|
|
|
|
| 805 |
<div class="stable-text-playground stable-signer-panel">
|
| 806 |
<div class="stable-text-playground-title">
|
| 807 |
<h3>Selection Translation Playground</h3>
|
| 808 |
+
<div class="stable-playground-actions">
|
| 809 |
+
<label><input type="checkbox" id="stable-selection-semantic-gloss" checked> Semantic gloss</label>
|
| 810 |
+
<button id="stable-selection-semantic-demo" type="button" class="btn secondary">Semantic Demo</button>
|
| 811 |
+
<span class="stable-status-chip">Select text to translate</span>
|
| 812 |
+
</div>
|
| 813 |
</div>
|
| 814 |
<div id="stable-selection-template" class="stable-reading-area stable-dialogue-area" tabindex="0" contenteditable="true">
|
| 815 |
<div class="stable-dialogue-turn question">
|
|
|
|
| 877 |
this.refineLogsPre = container.querySelector('#stable-refine-logs');
|
| 878 |
this.prewarmBtn = container.querySelector('#stable-prewarm');
|
| 879 |
this.translateBtn = container.querySelector('#stable-translate');
|
| 880 |
+
this.semanticDemoBtn = container.querySelector('#stable-semantic-demo');
|
| 881 |
+
this.semanticGlossCheckbox = container.querySelector('#stable-semantic-gloss');
|
| 882 |
+
this.selectionSemanticCheckbox = container.querySelector('#stable-selection-semantic-gloss');
|
| 883 |
+
this.selectionSemanticDemoBtn = container.querySelector('#stable-selection-semantic-demo');
|
| 884 |
this.selectionTemplate = container.querySelector('#stable-selection-template');
|
| 885 |
this.selectionPopover = container.querySelector('#stable-selection-popover');
|
| 886 |
this.selectionTranslateBtn = container.querySelector('#stable-selection-translate');
|
|
|
|
| 934 |
attachEvents() {
|
| 935 |
this.prewarmBtn.addEventListener('click', () => this.handlePrewarm());
|
| 936 |
this.translateBtn.addEventListener('click', () => this.handleTranslate());
|
| 937 |
+
if (this.semanticDemoBtn) {
|
| 938 |
+
this.semanticDemoBtn.addEventListener('click', () => this.handleSemanticDemo());
|
| 939 |
+
}
|
| 940 |
this.generateBtn.addEventListener('click', () => this.handleGenerate());
|
| 941 |
this.videoRefreshBtn.addEventListener('click', () => this.refreshRecentVideos());
|
| 942 |
this.videoSelect.addEventListener('change', () => this.handleRecentSelection());
|
|
|
|
| 949 |
this.selectionTemplate.addEventListener('mouseup', () => this.handleTemplateSelection());
|
| 950 |
this.selectionTemplate.addEventListener('keyup', () => this.handleTemplateSelection());
|
| 951 |
}
|
| 952 |
+
if (this.selectionSemanticDemoBtn) {
|
| 953 |
+
this.selectionSemanticDemoBtn.addEventListener('click', () => this.handleSelectionSemanticDemo());
|
| 954 |
+
}
|
| 955 |
if (this.selectionTranslateBtn) {
|
| 956 |
this.selectionTranslateBtn.addEventListener('click', () => this.handleSelectedTextGenerate(true));
|
| 957 |
}
|
|
|
|
| 996 |
refImagePath: this.refImageInput.value,
|
| 997 |
normalizePose: this.normalizeCheckbox.checked,
|
| 998 |
hideTorsoLines: this.hideTorsoCheckbox.checked,
|
| 999 |
+
translationMode: this.semanticGlossCheckbox && this.semanticGlossCheckbox.checked ? 'semantic' : 'text2gloss',
|
| 1000 |
+
useSemantic: !!(this.semanticGlossCheckbox && this.semanticGlossCheckbox.checked)
|
| 1001 |
};
|
| 1002 |
}
|
| 1003 |
|
|
|
|
| 1108 |
this.setTranslateLoading(true);
|
| 1109 |
}
|
| 1110 |
try {
|
| 1111 |
+
const result = await this.translatePromptToGloss(normalizedPrompt, maxTokens, promptTextRaw);
|
| 1112 |
+
const gloss = typeof result === 'string' ? result : result.gloss;
|
| 1113 |
this.glossInput.value = gloss;
|
| 1114 |
if (!isAuto) {
|
| 1115 |
this.updateStatus('Gloss translation completed');
|
| 1116 |
+
const semantic = result.semantic && result.semantic.length > 0
|
| 1117 |
+
? `\nSemantic fallback: ${result.semantic.map(([src, dst]) => `${src}→${dst}`).join(', ')}`
|
| 1118 |
+
: '';
|
| 1119 |
+
const dropped = result.dropped && result.dropped.length > 0
|
| 1120 |
+
? `\nDropped: ${result.dropped.join(', ')}`
|
| 1121 |
+
: '';
|
| 1122 |
+
this.logsPre.textContent = `Translation result: ${gloss}\nMode: ${result.mode || 'rule'}${semantic}${dropped}`;
|
| 1123 |
}
|
| 1124 |
return gloss;
|
| 1125 |
} finally {
|
|
|
|
| 1134 |
throw new Error('Prompt/Text is empty');
|
| 1135 |
}
|
| 1136 |
const payloadPrompt = originalPrompt || promptText;
|
| 1137 |
+
const useSemantic = !!(this.semanticGlossCheckbox && this.semanticGlossCheckbox.checked);
|
| 1138 |
const response = await window.controlWaveWebSocket.sendRequest(this.id, {
|
| 1139 |
action: 'text_to_gloss',
|
| 1140 |
data: {
|
| 1141 |
text: promptText,
|
| 1142 |
prompt: payloadPrompt,
|
| 1143 |
+
maxNewTokens,
|
| 1144 |
+
mode: useSemantic ? 'semantic' : 'rule',
|
| 1145 |
+
useSemantic
|
| 1146 |
}
|
| 1147 |
});
|
| 1148 |
if (!response || response.status !== 'success' || !response.gloss) {
|
|
|
|
| 1151 |
if (response.dropped && response.dropped.length > 0) {
|
| 1152 |
console.info('[StableSigner] dropped non-renderable text tokens', response.dropped);
|
| 1153 |
}
|
| 1154 |
+
return response;
|
| 1155 |
+
}
|
| 1156 |
+
|
| 1157 |
+
handleSemanticDemo() {
|
| 1158 |
+
if (this.semanticGlossCheckbox) {
|
| 1159 |
+
this.semanticGlossCheckbox.checked = true;
|
| 1160 |
+
}
|
| 1161 |
+
this.promptTextInput.value = 'Could you aid me? I bought a mobile and go home later.';
|
| 1162 |
+
this.handleTranslate(false).catch((error) => {
|
| 1163 |
+
this.updateStatus('Semantic demo failed');
|
| 1164 |
+
this.logsPre.textContent = (error && error.message) ? error.message : 'Semantic demo failed';
|
| 1165 |
+
});
|
| 1166 |
+
}
|
| 1167 |
+
|
| 1168 |
+
handleSelectionSemanticDemo() {
|
| 1169 |
+
if (this.selectionSemanticCheckbox) {
|
| 1170 |
+
this.selectionSemanticCheckbox.checked = true;
|
| 1171 |
+
}
|
| 1172 |
+
const demoText = 'Could you aid me? I bought a mobile and go home later.';
|
| 1173 |
+
if (this.selectionTemplate) {
|
| 1174 |
+
this.selectionTemplate.innerHTML = `<div class="stable-dialogue-turn question"><span class="stable-dialogue-speaker">User</span><p>${demoText}</p></div>`;
|
| 1175 |
+
}
|
| 1176 |
+
this.selectedTemplateText = demoText;
|
| 1177 |
+
this.lastGeneratedSelection = '';
|
| 1178 |
+
if (this.selectionTextEl) {
|
| 1179 |
+
this.selectionTextEl.textContent = demoText;
|
| 1180 |
+
}
|
| 1181 |
+
if (this.selectionPopover) {
|
| 1182 |
+
this.selectionPopover.style.display = 'block';
|
| 1183 |
+
}
|
| 1184 |
+
this.handleSelectedTextGenerate(true);
|
| 1185 |
}
|
| 1186 |
|
| 1187 |
handleTemplateSelection() {
|
|
|
|
| 1262 |
throw new Error('WebSocket not ready');
|
| 1263 |
}
|
| 1264 |
const payload = this.collectPayload();
|
| 1265 |
+
const selectionUseSemantic = !!(this.selectionSemanticCheckbox && this.selectionSemanticCheckbox.checked);
|
| 1266 |
+
payload.translationMode = selectionUseSemantic ? 'semantic' : 'text2gloss';
|
| 1267 |
+
payload.useSemantic = selectionUseSemantic;
|
| 1268 |
payload.maxNewTokens = Math.min(toInt(this.maxNewTokensInput.value, 64), 18);
|
| 1269 |
payload.maxCandidates = Math.min(toInt(this.maxCandidatesInput.value, 10), 3);
|
| 1270 |
const response = await window.controlWaveWebSocket.sendRequest(this.id, {
|
pipeline01_text2gloss.py
CHANGED
|
@@ -108,12 +108,100 @@ PHRASE_MAP = {
|
|
| 108 |
}
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
@dataclass
|
| 112 |
class TextToGlossResult:
|
| 113 |
gloss: str
|
| 114 |
tokens: List[str]
|
| 115 |
matched: List[Tuple[str, str]]
|
| 116 |
dropped: List[str]
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
def load_available_glosses(path: Path | str = DEFAULT_POSE_DICT) -> Set[str]:
|
|
@@ -168,6 +256,41 @@ def _best_vocab_match(token: str, vocab: Set[str]) -> Optional[str]:
|
|
| 168 |
return None
|
| 169 |
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
def _apply_phrase_map(tokens: Sequence[str], vocab: Set[str]) -> Tuple[List[str], List[Tuple[str, str]]]:
|
| 172 |
output: List[str] = []
|
| 173 |
matched: List[Tuple[str, str]] = []
|
|
@@ -195,11 +318,16 @@ def translate_text_to_gloss(
|
|
| 195 |
text: str,
|
| 196 |
vocab: Optional[Iterable[str]] = None,
|
| 197 |
max_tokens: int = 64,
|
|
|
|
| 198 |
) -> TextToGlossResult:
|
| 199 |
"""Translate English text into a renderable uppercase gloss sequence."""
|
| 200 |
vocab_set = {item.upper() for item in vocab} if vocab is not None else load_available_glosses()
|
| 201 |
raw_tokens = tokenize_english(text)
|
| 202 |
phrase_tokens, phrase_matches = _apply_phrase_map(raw_tokens, vocab_set)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
gloss_tokens: List[str] = []
|
| 205 |
matched: List[Tuple[str, str]] = list(phrase_matches)
|
|
@@ -212,6 +340,10 @@ def translate_text_to_gloss(
|
|
| 212 |
dropped.append(token)
|
| 213 |
continue
|
| 214 |
gloss = _best_vocab_match(token, vocab_set)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
if gloss:
|
| 216 |
gloss_tokens.append(gloss)
|
| 217 |
if token != gloss:
|
|
@@ -224,6 +356,7 @@ def translate_text_to_gloss(
|
|
| 224 |
tokens=gloss_tokens,
|
| 225 |
matched=matched,
|
| 226 |
dropped=dropped,
|
|
|
|
| 227 |
)
|
| 228 |
|
| 229 |
|
|
@@ -232,11 +365,12 @@ def main() -> None:
|
|
| 232 |
parser.add_argument("text", help="English text to translate.")
|
| 233 |
parser.add_argument("--pose-dict", default=str(DEFAULT_POSE_DICT), help="WLASL-style pose dictionary JSON.")
|
| 234 |
parser.add_argument("--max-tokens", type=int, default=64)
|
|
|
|
| 235 |
parser.add_argument("--json", action="store_true", help="Print structured JSON instead of only gloss.")
|
| 236 |
args = parser.parse_args()
|
| 237 |
|
| 238 |
vocab = load_available_glosses(args.pose_dict)
|
| 239 |
-
result = translate_text_to_gloss(args.text, vocab=vocab, max_tokens=args.max_tokens)
|
| 240 |
if args.json:
|
| 241 |
print(
|
| 242 |
json.dumps(
|
|
@@ -245,6 +379,7 @@ def main() -> None:
|
|
| 245 |
"tokens": result.tokens,
|
| 246 |
"matched": result.matched,
|
| 247 |
"dropped": result.dropped,
|
|
|
|
| 248 |
},
|
| 249 |
ensure_ascii=True,
|
| 250 |
indent=2,
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
|
| 111 |
+
SEMANTIC_PHRASE_MAP = {
|
| 112 |
+
"HELP ME": ["HELP", "I"],
|
| 113 |
+
"NEED HELP": ["NEED", "HELP"],
|
| 114 |
+
"I NEED HELP": ["I", "NEED", "HELP"],
|
| 115 |
+
"PLEASE HELP": ["PLEASE", "HELP"],
|
| 116 |
+
"GO TO SCHOOL": ["GO", "SCHOOL"],
|
| 117 |
+
"GO HOME": ["GO", "HOME"],
|
| 118 |
+
"COME HERE": ["COME", "HERE"],
|
| 119 |
+
"SEE YOU": ["SEE", "YOU"],
|
| 120 |
+
"NICE TO MEET YOU": ["NICE", "MEET", "YOU"],
|
| 121 |
+
"HOW ARE YOU": ["HOW", "YOU"],
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
SEMANTIC_TOKEN_MAP = {
|
| 126 |
+
"ASSIST": "HELP",
|
| 127 |
+
"AID": "HELP",
|
| 128 |
+
"SUPPORT": "HELP",
|
| 129 |
+
"SUPPORTING": "HELP",
|
| 130 |
+
"REQUIRE": "NEED",
|
| 131 |
+
"REQUIRES": "NEED",
|
| 132 |
+
"REQUIRED": "NEED",
|
| 133 |
+
"MUST": "NEED",
|
| 134 |
+
"SHOULD": "NEED",
|
| 135 |
+
"PURCHASE": "BUY",
|
| 136 |
+
"PURCHASED": "BUY",
|
| 137 |
+
"PURCHASING": "BUY",
|
| 138 |
+
"GET": "BUY",
|
| 139 |
+
"BOUGHT": "BUY",
|
| 140 |
+
"OBTAIN": "BUY",
|
| 141 |
+
"SPEAK": "TALK",
|
| 142 |
+
"SPEAKING": "TALK",
|
| 143 |
+
"TELL": "SAY",
|
| 144 |
+
"TOLD": "SAY",
|
| 145 |
+
"VIEW": "SEE",
|
| 146 |
+
"WATCH": "SEE",
|
| 147 |
+
"OBSERVE": "SEE",
|
| 148 |
+
"UNDERSTOOD": "UNDERSTAND",
|
| 149 |
+
"UNDERSTANDING": "UNDERSTAND",
|
| 150 |
+
"CHILDREN": "CHILD",
|
| 151 |
+
"KIDS": "CHILD",
|
| 152 |
+
"KID": "CHILD",
|
| 153 |
+
"ADULTS": "ADULT",
|
| 154 |
+
"PEOPLE": "PERSON",
|
| 155 |
+
"HUMAN": "PERSON",
|
| 156 |
+
"PERSONS": "PERSON",
|
| 157 |
+
"PHYSICIAN": "DOCTOR",
|
| 158 |
+
"MEDICAL": "DOCTOR",
|
| 159 |
+
"HOSPITAL": "DOCTOR",
|
| 160 |
+
"INSTRUCTOR": "TEACHER",
|
| 161 |
+
"EDUCATOR": "TEACHER",
|
| 162 |
+
"PUPIL": "STUDENT",
|
| 163 |
+
"LEARNER": "STUDENT",
|
| 164 |
+
"AUTOMOBILE": "CAR",
|
| 165 |
+
"VEHICLE": "CAR",
|
| 166 |
+
"CELL": "PHONE",
|
| 167 |
+
"MOBILE": "PHONE",
|
| 168 |
+
"TELEPHONE": "PHONE",
|
| 169 |
+
"RESIDENCE": "HOME",
|
| 170 |
+
"HOUSE": "HOME",
|
| 171 |
+
"WEATHER": "WEATHER",
|
| 172 |
+
"RAINING": "RAIN",
|
| 173 |
+
"SUNNY": "SUN",
|
| 174 |
+
"COLD": "COLD",
|
| 175 |
+
"HOT": "HOT",
|
| 176 |
+
"BEAUTIFUL": "NICE",
|
| 177 |
+
"GREAT": "GOOD",
|
| 178 |
+
"EXCELLENT": "GOOD",
|
| 179 |
+
"FINE": "GOOD",
|
| 180 |
+
"HAPPY": "HAPPY",
|
| 181 |
+
"SAD": "SAD",
|
| 182 |
+
"AFRAID": "FEAR",
|
| 183 |
+
"SCARED": "FEAR",
|
| 184 |
+
"SOON": "LATER",
|
| 185 |
+
"AFTER": "LATER",
|
| 186 |
+
"CURRENTLY": "NOW",
|
| 187 |
+
"IMMEDIATELY": "NOW",
|
| 188 |
+
"YESTERDAY": "YESTERDAY",
|
| 189 |
+
"TOMORROW": "TOMORROW",
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
AMBIGUOUS_SEMANTIC_BLOCKLIST = {
|
| 194 |
+
"CAN", "MAY", "MIGHT", "LEFT", "RIGHT", "LIGHT", "WELL", "KIND", "MEAN", "PART", "SECOND"
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
|
| 198 |
@dataclass
|
| 199 |
class TextToGlossResult:
|
| 200 |
gloss: str
|
| 201 |
tokens: List[str]
|
| 202 |
matched: List[Tuple[str, str]]
|
| 203 |
dropped: List[str]
|
| 204 |
+
semantic: List[Tuple[str, str]]
|
| 205 |
|
| 206 |
|
| 207 |
def load_available_glosses(path: Path | str = DEFAULT_POSE_DICT) -> Set[str]:
|
|
|
|
| 256 |
return None
|
| 257 |
|
| 258 |
|
| 259 |
+
def _best_semantic_match(token: str, vocab: Set[str]) -> Optional[str]:
|
| 260 |
+
if token in AMBIGUOUS_SEMANTIC_BLOCKLIST:
|
| 261 |
+
return None
|
| 262 |
+
candidates = _simple_lemma(token)
|
| 263 |
+
for candidate in candidates:
|
| 264 |
+
mapped = SEMANTIC_TOKEN_MAP.get(candidate)
|
| 265 |
+
if mapped and mapped in vocab:
|
| 266 |
+
return mapped
|
| 267 |
+
return None
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def _apply_semantic_phrase_map(tokens: Sequence[str], vocab: Set[str]) -> Tuple[List[str], List[Tuple[str, str]]]:
|
| 271 |
+
output: List[str] = []
|
| 272 |
+
matched: List[Tuple[str, str]] = []
|
| 273 |
+
i = 0
|
| 274 |
+
phrases = sorted(SEMANTIC_PHRASE_MAP.items(), key=lambda item: len(item[0].split()), reverse=True)
|
| 275 |
+
while i < len(tokens):
|
| 276 |
+
consumed = False
|
| 277 |
+
for phrase, gloss_tokens in phrases:
|
| 278 |
+
phrase_tokens = phrase.split()
|
| 279 |
+
if list(tokens[i : i + len(phrase_tokens)]) != phrase_tokens:
|
| 280 |
+
continue
|
| 281 |
+
available = [gloss for gloss in gloss_tokens if gloss in vocab]
|
| 282 |
+
if available:
|
| 283 |
+
output.extend(available)
|
| 284 |
+
matched.append((phrase, " ".join(available)))
|
| 285 |
+
i += len(phrase_tokens)
|
| 286 |
+
consumed = True
|
| 287 |
+
break
|
| 288 |
+
if not consumed:
|
| 289 |
+
output.append(tokens[i])
|
| 290 |
+
i += 1
|
| 291 |
+
return output, matched
|
| 292 |
+
|
| 293 |
+
|
| 294 |
def _apply_phrase_map(tokens: Sequence[str], vocab: Set[str]) -> Tuple[List[str], List[Tuple[str, str]]]:
|
| 295 |
output: List[str] = []
|
| 296 |
matched: List[Tuple[str, str]] = []
|
|
|
|
| 318 |
text: str,
|
| 319 |
vocab: Optional[Iterable[str]] = None,
|
| 320 |
max_tokens: int = 64,
|
| 321 |
+
use_semantic: bool = False,
|
| 322 |
) -> TextToGlossResult:
|
| 323 |
"""Translate English text into a renderable uppercase gloss sequence."""
|
| 324 |
vocab_set = {item.upper() for item in vocab} if vocab is not None else load_available_glosses()
|
| 325 |
raw_tokens = tokenize_english(text)
|
| 326 |
phrase_tokens, phrase_matches = _apply_phrase_map(raw_tokens, vocab_set)
|
| 327 |
+
semantic_matches: List[Tuple[str, str]] = []
|
| 328 |
+
if use_semantic:
|
| 329 |
+
phrase_tokens, semantic_phrase_matches = _apply_semantic_phrase_map(phrase_tokens, vocab_set)
|
| 330 |
+
semantic_matches.extend(semantic_phrase_matches)
|
| 331 |
|
| 332 |
gloss_tokens: List[str] = []
|
| 333 |
matched: List[Tuple[str, str]] = list(phrase_matches)
|
|
|
|
| 340 |
dropped.append(token)
|
| 341 |
continue
|
| 342 |
gloss = _best_vocab_match(token, vocab_set)
|
| 343 |
+
if not gloss and use_semantic:
|
| 344 |
+
gloss = _best_semantic_match(token, vocab_set)
|
| 345 |
+
if gloss:
|
| 346 |
+
semantic_matches.append((token, gloss))
|
| 347 |
if gloss:
|
| 348 |
gloss_tokens.append(gloss)
|
| 349 |
if token != gloss:
|
|
|
|
| 356 |
tokens=gloss_tokens,
|
| 357 |
matched=matched,
|
| 358 |
dropped=dropped,
|
| 359 |
+
semantic=semantic_matches,
|
| 360 |
)
|
| 361 |
|
| 362 |
|
|
|
|
| 365 |
parser.add_argument("text", help="English text to translate.")
|
| 366 |
parser.add_argument("--pose-dict", default=str(DEFAULT_POSE_DICT), help="WLASL-style pose dictionary JSON.")
|
| 367 |
parser.add_argument("--max-tokens", type=int, default=64)
|
| 368 |
+
parser.add_argument("--semantic", action="store_true", help="Enable conservative semantic synonym fallback.")
|
| 369 |
parser.add_argument("--json", action="store_true", help="Print structured JSON instead of only gloss.")
|
| 370 |
args = parser.parse_args()
|
| 371 |
|
| 372 |
vocab = load_available_glosses(args.pose_dict)
|
| 373 |
+
result = translate_text_to_gloss(args.text, vocab=vocab, max_tokens=args.max_tokens, use_semantic=args.semantic)
|
| 374 |
if args.json:
|
| 375 |
print(
|
| 376 |
json.dumps(
|
|
|
|
| 379 |
"tokens": result.tokens,
|
| 380 |
"matched": result.matched,
|
| 381 |
"dropped": result.dropped,
|
| 382 |
+
"semantic": result.semantic,
|
| 383 |
},
|
| 384 |
ensure_ascii=True,
|
| 385 |
indent=2,
|
viser_backend.py
CHANGED
|
@@ -213,17 +213,20 @@ class StableSignerBackend(PluginBackendBase):
|
|
| 213 |
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0].strip().upper()
|
| 214 |
return decoded
|
| 215 |
|
| 216 |
-
def _generate_constrained_text_gloss(self, text: str, max_tokens: int) -> Dict[str, Any]:
|
| 217 |
result = translate_text_to_gloss(
|
| 218 |
text,
|
| 219 |
vocab=self._get_available_glosses(),
|
| 220 |
max_tokens=max_tokens,
|
|
|
|
| 221 |
)
|
| 222 |
return {
|
| 223 |
"gloss": result.gloss,
|
| 224 |
"tokens": result.tokens,
|
| 225 |
"matched": result.matched,
|
| 226 |
"dropped": result.dropped,
|
|
|
|
|
|
|
| 227 |
}
|
| 228 |
|
| 229 |
def _build_output_path(self, gloss_tokens: List[str], draw_style: str) -> Path:
|
|
@@ -345,8 +348,10 @@ class StableSignerBackend(PluginBackendBase):
|
|
| 345 |
if not text:
|
| 346 |
return {"status": "error", "message": "Text is empty."}
|
| 347 |
max_tokens = _safe_int(data.get("maxNewTokens"), 64)
|
|
|
|
|
|
|
| 348 |
try:
|
| 349 |
-
result = self._generate_constrained_text_gloss(text, max_tokens)
|
| 350 |
if not result["gloss"]:
|
| 351 |
return {
|
| 352 |
"status": "error",
|
|
@@ -508,10 +513,11 @@ class StableSignerBackend(PluginBackendBase):
|
|
| 508 |
self._generation_lock.release()
|
| 509 |
return {"status": "error", "message": "Please enter Prompt/Text or a gloss sequence."}
|
| 510 |
translation_mode = (data.get("translationMode") or "text2gloss").strip().lower()
|
|
|
|
| 511 |
if translation_mode == "prompt2gloss":
|
| 512 |
gloss_input = self._generate_gloss(text_input, prompt_input, max_new_tokens)
|
| 513 |
else:
|
| 514 |
-
gloss_input = self._generate_constrained_text_gloss(text_input, max_new_tokens)["gloss"]
|
| 515 |
|
| 516 |
gloss_tokens = [token for token in gloss_input.upper().split() if token]
|
| 517 |
if not gloss_tokens:
|
|
@@ -603,10 +609,11 @@ class StableSignerBackend(PluginBackendBase):
|
|
| 603 |
if not text_input:
|
| 604 |
return {"status": "error", "message": "Please enter Prompt/Text or a gloss sequence."}
|
| 605 |
translation_mode = (data.get("translationMode") or "text2gloss").strip().lower()
|
|
|
|
| 606 |
if translation_mode == "prompt2gloss":
|
| 607 |
gloss_input = self._generate_gloss(text_input, prompt_input, max_new_tokens)
|
| 608 |
else:
|
| 609 |
-
gloss_result = self._generate_constrained_text_gloss(text_input, max_new_tokens)
|
| 610 |
gloss_input = gloss_result["gloss"]
|
| 611 |
|
| 612 |
gloss_tokens = [token for token in gloss_input.upper().split() if token]
|
|
|
|
| 213 |
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0].strip().upper()
|
| 214 |
return decoded
|
| 215 |
|
| 216 |
+
def _generate_constrained_text_gloss(self, text: str, max_tokens: int, use_semantic: bool = False) -> Dict[str, Any]:
|
| 217 |
result = translate_text_to_gloss(
|
| 218 |
text,
|
| 219 |
vocab=self._get_available_glosses(),
|
| 220 |
max_tokens=max_tokens,
|
| 221 |
+
use_semantic=use_semantic,
|
| 222 |
)
|
| 223 |
return {
|
| 224 |
"gloss": result.gloss,
|
| 225 |
"tokens": result.tokens,
|
| 226 |
"matched": result.matched,
|
| 227 |
"dropped": result.dropped,
|
| 228 |
+
"semantic": result.semantic,
|
| 229 |
+
"mode": "semantic" if use_semantic else "rule",
|
| 230 |
}
|
| 231 |
|
| 232 |
def _build_output_path(self, gloss_tokens: List[str], draw_style: str) -> Path:
|
|
|
|
| 348 |
if not text:
|
| 349 |
return {"status": "error", "message": "Text is empty."}
|
| 350 |
max_tokens = _safe_int(data.get("maxNewTokens"), 64)
|
| 351 |
+
mode = str(data.get("mode") or data.get("translationMode") or "rule").lower()
|
| 352 |
+
use_semantic = bool(data.get("useSemantic")) or mode in {"semantic", "semantic_text2gloss", "semantic-text2gloss"}
|
| 353 |
try:
|
| 354 |
+
result = self._generate_constrained_text_gloss(text, max_tokens, use_semantic=use_semantic)
|
| 355 |
if not result["gloss"]:
|
| 356 |
return {
|
| 357 |
"status": "error",
|
|
|
|
| 513 |
self._generation_lock.release()
|
| 514 |
return {"status": "error", "message": "Please enter Prompt/Text or a gloss sequence."}
|
| 515 |
translation_mode = (data.get("translationMode") or "text2gloss").strip().lower()
|
| 516 |
+
use_semantic = bool(data.get("useSemantic")) or translation_mode in {"semantic", "semantic_text2gloss", "semantic-text2gloss"}
|
| 517 |
if translation_mode == "prompt2gloss":
|
| 518 |
gloss_input = self._generate_gloss(text_input, prompt_input, max_new_tokens)
|
| 519 |
else:
|
| 520 |
+
gloss_input = self._generate_constrained_text_gloss(text_input, max_new_tokens, use_semantic=use_semantic)["gloss"]
|
| 521 |
|
| 522 |
gloss_tokens = [token for token in gloss_input.upper().split() if token]
|
| 523 |
if not gloss_tokens:
|
|
|
|
| 609 |
if not text_input:
|
| 610 |
return {"status": "error", "message": "Please enter Prompt/Text or a gloss sequence."}
|
| 611 |
translation_mode = (data.get("translationMode") or "text2gloss").strip().lower()
|
| 612 |
+
use_semantic = bool(data.get("useSemantic")) or translation_mode in {"semantic", "semantic_text2gloss", "semantic-text2gloss"}
|
| 613 |
if translation_mode == "prompt2gloss":
|
| 614 |
gloss_input = self._generate_gloss(text_input, prompt_input, max_new_tokens)
|
| 615 |
else:
|
| 616 |
+
gloss_result = self._generate_constrained_text_gloss(text_input, max_new_tokens, use_semantic=use_semantic)
|
| 617 |
gloss_input = gloss_result["gloss"]
|
| 618 |
|
| 619 |
gloss_tokens = [token for token in gloss_input.upper().split() if token]
|