Spaces:
Sleeping
Sleeping
Commit ·
b619b60
1
Parent(s): d3e7057
R3.2.8.6-R1.3C-RU2 Russian Contamination Adjudication and Target Preview State (#31)
Browse files- R3.2.8.6-R1.3C-RU2 Russian Contamination Adjudication and Target Preview State (6cc768837af94a89ccf9d1602c3d1d0016259026)
Files changed (3)
7 unmodified lines891011121319 unmodified lines33343536373839259 unmodified lines29930030130230330430530630730817 unmodified lines326327328329330331388 unmodified lines7207217227237247257267 unmodified linesfrom __future__ import annotationsimport hashlibimport jsonimport loggingimport math19 unmodified linesMIN_FINAL_REFERENCES = 50LITERATURE_EVIDENCE_TARGET = 75CITATION_PLAN_TARGET = 70TRANSLATION_CONTRACT_VERSION = 'hierarchical-multilingual-v1'def _clean_trailing_references(text, keywords=("reference", "references", "reference list", "参考文献")):259 unmodified linespartial = self._assemble_partial_translation(translation_sections, adaptive_structure, include_front=include_front)self._publish_target_preview(partial, f'translating:{unit_name}', force=True, complete=False)updated_sections = dict(translation_sections or {})try:result = agent.translate_frozen_section(source, target_lang, glossary=translation_glossary, section_name=unit_name, max_attempts=2)except PipelineCancelledError:raiseexcept Exception as error:17 unmodified lines'Translation deferred until final gate | unit=%s code=%s error=%s',unit_name, error_code, type(error).__name__,)return updated_sections, translation_glossaryupdated_glossary = dict(result.get('glossary') or translation_glossary or {})updated_sections[unit_key] = {388 unmodified linesif working_abstract and not state.get("english_finalized"):section_order_for_front = self._unique_top_level_sections(adaptive_structure)if section_order_for_front:working_front_source = self._extract_front_matter(full_draft_en, section_order_for_front[0][1])translation_sections, translation_glossary = self._translate_frozen_unit(agent, unit_key='__front_matter__', unit_name='Working Front Matter',source_text=working_front_source, target_lang=target_lang,7 unmodified lines89101112131419 unmodified lines34353637383940259 unmodified lines30030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933017 unmodified lines348349350351352353354355356388 unmodified lines7457467477487497507517527537547557567577 unmodified linesfrom __future__ import annotationsimport hashlibimport inspectimport jsonimport loggingimport math19 unmodified linesMIN_FINAL_REFERENCES = 50LITERATURE_EVIDENCE_TARGET = 75CITATION_PLAN_TARGET = 70TRANSLATION_CONTRACT_VERSION = 'hierarchical-multilingual-v2'def _clean_trailing_references(text, keywords=("reference", "references", "reference list", "参考文献")):259 unmodified linespartial = self._assemble_partial_translation(translation_sections, adaptive_structure, include_front=include_front)self._publish_target_preview(partial, f'translating:{unit_name}', force=True, complete=False)updated_sections = dict(translation_sections or {})def publish_translation_stage(stage):value = str(stage or '').strip().casefold()if value in {'qa', 'repairing'}:preview_stage = f'translation_qa:{unit_name}'else:preview_stage = f'translating:{unit_name}'self._publish_target_preview(partial, preview_stage, force=True, complete=False)try:translate_method = agent.translate_frozen_sectioncall_kwargs = {'glossary': translation_glossary, 'section_name': unit_name, 'max_attempts': 2,}try:parameters = inspect.signature(translate_method).parameters.values()supports_stage_callback = any(item.name == 'stage_callback' or item.kind == inspect.Parameter.VAR_KEYWORDfor item in parameters)except (TypeError, ValueError):supports_stage_callback = Falseif supports_stage_callback:call_kwargs['stage_callback'] = publish_translation_stageresult = translate_method(source, target_lang, **call_kwargs)except PipelineCancelledError:raiseexcept Exception as error:17 unmodified lines'Translation deferred until final gate | unit=%s code=%s error=%s',unit_name, error_code, type(error).__name__,)self._publish_target_preview(partial, f'translation_repair_pending:{unit_name}', force=True, complete=False)return updated_sections, translation_glossaryupdated_glossary = dict(result.get('glossary') or translation_glossary or {})updated_sections[unit_key] = {388 unmodified linesif working_abstract and not state.get("english_finalized"):section_order_for_front = self._unique_top_level_sections(adaptive_structure)if section_order_for_front:first_section_title = section_order_for_front[0][1]working_front_source = self._extract_front_matter(full_draft_en, first_section_title)if not working_front_source and not completed_sections:# Fresh tasks contain only Title/Abstract/Keywords here; the# first body heading does not exist yet. Preserve legacy# resume behavior when completed body sections are present.working_front_source = str(full_draft_en or '').strip()translation_sections, translation_glossary = self._translate_frozen_unit(agent, unit_key='__front_matter__', unit_name='Working Front Matter',source_text=working_front_source, target_lang=target_lang,
60 unmodified lines616263646566673391 unmodified lines345934603461346234633464837 unmodified lines430243034304430543064307430810 unmodified lines431943204321432243234324432543264327432843294330433143324333351 unmodified lines468546864687468846894690469124 unmodified lines471647174718471947204721472247233 unmodified lines4727472847294730473147329 unmodified lines4742474347444745474647474748474910 unmodified lines47604761476247634764476560 unmodified linesclass SSciPipelineAgent:TRANSLATION_CONTRACT_VERSION = 'hierarchical-multilingual-v1'def __init__(self, ds_key, s2_key=""):self.ds_key = str(ds_key or "").strip()3391 unmodified linescore = 'Use formal academic Simplified Chinese with explicit concepts, natural Chinese syntax, clear hierarchy, and logically connected claims. Avoid carrying English nominalization and clause order into Chinese mechanically.'return core + ' ' + profile['term_policy'] + ' ' + profile['contamination_policy']@classmethoddef _compile_translation_ir(cls, source):"""Compile a private, language-neutral translation intermediate representation."""837 unmodified linessystem_prompt = f"""You are a bilingual academic translation QA classifier for {target_lang}.Review EACH claim independently. Do not rewrite and do not provide free-form explanations. Check semantic equivalence and language quality: omissions/additions, negation, uncertainty, association-versus-causation strength, terminology, grammar, language contamination, citation attachment, and acronym attachment.LANGUAGE ADAPTER: {profile['adapter_id']}; morphology={profile['morphology']}. {profile['term_policy']} {profile['contamination_policy']}For Russian, do not flag a terminology error merely because a translated term is inflected for grammatical case/number/gender. For Kazakh, do not flag a terminology error merely because productive suffixes attach to the correct term stem. For Kazakh language identity, absence of Kazakh-specific letters in one short claim is not sufficient evidence of Russian contamination.Return valid JSON only in this exact form: {{"items":[{{"id":"SP-P000001-C000001","ok":true,"codes":[]}}]}}.If ok=false, codes MUST contain only values from allowed_issue_codes. Never return numbered prose, examples, quotes, or corrective wording. Return every supplied id exactly once."""response = self.call_deepseek(10 unmodified linesf'Translation claim QA id mismatch: expected={expected_row_ids}, got={sorted(by_id)}.')row_lookup = {row['id']: row for row in rows}for row_id in expected_row_ids:returned_row = by_id[row_id]if returned_row.get('ok') is True:continueraw_codes = [str(code or '').strip().upper() for code in (returned_row.get('codes') or [])]codes = [code for code in raw_codes if code in allowed_codes]if not codes:codes = ['SEMANTIC_DRIFT']meta = row_lookup[row_id]issues.setdefault(meta['block_id'], {})[meta['claim_id']] = list(dict.fromkeys(codes))[:5]return issues351 unmodified lines)return {}def translate_frozen_section(self, section_text, target_lang, glossary=None, section_name='', max_attempts=2):"""Translate one frozen English unit and publish only after all QA passes."""value = str(section_text or '').strip()if not value:24 unmodified lines)attempts = max(1, int(max_attempts or 1))last_error = Nonefor attempt in range(1, attempts + 1):self._check_cancelled()translations = {}try:for batch in batches:3 unmodified linesfor batch in batchesfor block_id, source in batch}quality_issues = self._audit_translation_blocks_with_retry(source_blocks, translations, target_lang, frozen_glossary,section_name=section_name or 'frozen section', max_attempts=2,9 unmodified lineslogging.warning('Translation claim-local QA recovery: section=%s round=%s failed=%s',section_name or 'section', local_round,[f'SP-P{block_id}-{claim_id}' for block_id, claim_id, _codes in failed_pairs],)touched_blocks = set()for block_id, claim_id, codes in failed_pairs:source_claims = {10 unmodified lines)translations[block_id] = self._compose_translation_claim_segments(claim_order, segments)touched_blocks.add(block_id)quality_issues = self._audit_translation_blocks_with_retry({block_id: source_blocks[block_id] for block_id in sorted(touched_blocks)},{block_id: translations[block_id] for block_id in sorted(touched_blocks)},60 unmodified lines616263646566673391 unmodified lines345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581837 unmodified lines441944204421442244234424442510 unmodified lines443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477351 unmodified lines482948304831483248334834483524 unmodified lines4860486148624863486448654866486748684869487048714872487348744875487648774878487948803 unmodified lines48844885488648874888488948909 unmodified lines49004901490249034904490549064907490849094910491110 unmodified lines492249234924492549264927492860 unmodified linesclass SSciPipelineAgent:TRANSLATION_CONTRACT_VERSION = 'hierarchical-multilingual-v2'def __init__(self, ds_key, s2_key=""):self.ds_key = str(ds_key or "").strip()3391 unmodified linescore = 'Use formal academic Simplified Chinese with explicit concepts, natural Chinese syntax, clear hierarchy, and logically connected claims. Avoid carrying English nominalization and clause order into Chinese mechanically.'return core + ' ' + profile['term_policy'] + ' ' + profile['contamination_policy']@classmethoddef _russian_contamination_evidence(cls, source, target, glossary=None):"""Deterministically confirm only sustained, unprotected Latin prose in Russian.LLM QA may propose LANGUAGE_CONTAMINATION, but a fatal/repair signal isaccepted only when this evidence check finds a multi-word Latin clauseafter removing immutable research objects and explicit glossary terms.This prevents AI/LLM/ChatGPT/SEM/model names from becoming lexicalblacklists while preserving fail-closed behavior for real English prose."""source_value = str(source or '')text = cls._strip_translation_claim_markers(str(target or ''))# A verbatim quoted span is protected evidence only when the same quote# exists in the locked English source. Invented/untranslated English# quotes are not silently exempted. Research-object protection also# removes acronyms, model/tool names, citations, URLs, DOI strings,# statistics and numeric anchors.quote_pattern = re.compile(r'“[^”\n]{1,500}”|"[^"\n]{1,500}"')text = quote_pattern.sub(lambda match: ' ' if match.group(0) in source_value else match.group(0), text)text = cls._translation_protected_pattern().sub(' ', text)text = re.sub(r'\[\[SP-(?:P\d{6}|C\d{6}|T\d{6})\]\]', ' ', text)# A glossary target may legitimately remain Latin (for example a# standardized construct/tool name). Remove only exact glossary targets;# do not whitelist arbitrary English source prose.glossary_terms = []english_function_words = {'a','an','and','as','at','by','for','from','in','of','on','or','the','to','with',}for value in dict(glossary or {}).values():term = str(value or '').strip()words = re.findall(r'[A-Za-z][A-Za-z0-9+.#-]*', term)title_like = bool(words) and len(words) <= 5 and all(word.casefold() in english_function_words or word[:1].isupper() for word in words)technical_shape = bool(words) and any(any(ch.isdigit() for ch in word)or word.isupper()or (len(word) > 2 and any(ch.isupper() for ch in word[1:]))for word in words)if term and re.search(r'[A-Za-z]', term) and (title_like or technical_shape):glossary_terms.append(term)for term in sorted(set(glossary_terms), key=len, reverse=True):text = re.sub(re.escape(term), ' ', text, flags=re.I)# Mixed-case technical names such as OpenAI/TensorFlow are not covered by# the all-caps acronym rule. Allow them only when they are present in the# locked English source; ordinary source prose is never whitelisted.source_tokens = set(re.findall(r'\b[A-Za-z][A-Za-z0-9+.#-]*\b', str(source or '')))technical = {token for token in source_tokensif any(ch.isdigit() for ch in token)or (len(token) > 2 and any(ch.isupper() for ch in token[1:]))}for token in sorted(technical, key=len, reverse=True):text = re.sub(rf'(?<![A-Za-z0-9_]){re.escape(token)}(?![A-Za-z0-9_])', ' ', text)latin = list(re.finditer(r"[A-Za-z][A-Za-z'’-]*", text))allowed_gap = re.compile(r"^[\s,;:()\[\]{}.!?\"'“”‘’—–/\\-]*$")function_words = {'a','an','and','are','as','at','be','been','being','by','for','from','had','has','have','in','into','is','it','of','on','or','that','the','these','this','those','to','was','were','which','with','within','without','would','could','should','than','then','their','its',}max_run = 0max_run_has_function = Falserun = []previous_end = Nonenormalized_source = re.sub(r'\s+', ' ', source_value).casefold()def evaluate(items):nonlocal max_run, max_run_has_functionif not items:return Falseoriginal_words = [item.group(0) for item in items]words = [word.casefold() for word in original_words]has_function = any(word in function_words for word in words)if len(words) > max_run:max_run = len(words)max_run_has_function = has_function# Preserve a bounded source-matched proper name/title such as# "University of California at Berkeley". Ordinary copied English# prose is not title-like and therefore remains detectable.title_like = len(words) <= 6 and all(word in function_words or original[:1].isupper()for word, original in zip(words, original_words))candidate = ' '.join(original_words).casefold()if title_like and candidate and candidate in normalized_source:return False# Four-word clause with an English function word is strong evidence;# seven Latin words is strong even without one. Short titles/terms# and isolated Latin research objects remain legal.return (len(words) >= 4 and has_function) or len(words) >= 7confirmed = Falsefor match in latin:if previous_end is None:run = [match]else:gap = text[previous_end:match.start()]if allowed_gap.fullmatch(gap or ''):run.append(match)else:confirmed = confirmed or evaluate(run)run = [match]previous_end = match.end()confirmed = confirmed or evaluate(run)return {'confirmed': bool(confirmed),'max_latin_run_words': int(max_run),'max_run_has_function_word': bool(max_run_has_function),'glossary_latin_terms': int(len(set(glossary_terms))),'source_technical_tokens': int(len(technical)),}@classmethoddef _compile_translation_ir(cls, source):"""Compile a private, language-neutral translation intermediate representation."""837 unmodified linessystem_prompt = f"""You are a bilingual academic translation QA classifier for {target_lang}.Review EACH claim independently. Do not rewrite and do not provide free-form explanations. Check semantic equivalence and language quality: omissions/additions, negation, uncertainty, association-versus-causation strength, terminology, grammar, language contamination, citation attachment, and acronym attachment.LANGUAGE ADAPTER: {profile['adapter_id']}; morphology={profile['morphology']}. {profile['term_policy']} {profile['contamination_policy']}For Russian, do not flag a terminology error merely because a translated term is inflected for grammatical case/number/gender. For Russian LANGUAGE_CONTAMINATION, require sustained unprotected non-Russian prose such as a multi-word English clause or sentence. Never use LANGUAGE_CONTAMINATION merely for protected acronyms, AI/LLM/SEM, model or tool names, variable names, citations, glossary terms, source-matched proper names/titles, or isolated Latin terminology. For Kazakh, do not flag a terminology error merely because productive suffixes attach to the correct term stem. For Kazakh language identity, absence of Kazakh-specific letters in one short claim is not sufficient evidence of Russian contamination.Return valid JSON only in this exact form: {{"items":[{{"id":"SP-P000001-C000001","ok":true,"codes":[]}}]}}.If ok=false, codes MUST contain only values from allowed_issue_codes. Never return numbered prose, examples, quotes, or corrective wording. Return every supplied id exactly once."""response = self.call_deepseek(10 unmodified linesf'Translation claim QA id mismatch: expected={expected_row_ids}, got={sorted(by_id)}.')row_lookup = {row['id']: row for row in rows}language_code = profile.get('language_code')for row_id in expected_row_ids:returned_row = by_id[row_id]meta = row_lookup[row_id]raw_codes = [str(code or '').strip().upper() for code in (returned_row.get('codes') or [])]codes = [code for code in raw_codes if code in allowed_codes]model_contamination = 'LANGUAGE_CONTAMINATION' in codescontamination = Noneif language_code == 'ru':contamination = self._russian_contamination_evidence(meta['english'], meta['target'], relevant_glossary)if contamination.get('confirmed'):if 'LANGUAGE_CONTAMINATION' not in codes:codes.append('LANGUAGE_CONTAMINATION')elif model_contamination:codes = [code for code in codes if code != 'LANGUAGE_CONTAMINATION']self._record_translation_assurance('RU_LANGUAGE_CONTAMINATION_UNCONFIRMED', target_lang=target_lang, block_id=row_id,severity='diagnostic', decision='PASS_WITH_WARNING', detail=contamination,)# A deterministic confirmed Russian contamination signal can# override an LLM false negative. Conversely, a sole Russian# contamination code without deterministic evidence is suppressed.if returned_row.get('ok') is True and not (contamination and contamination.get('confirmed')):continueif not codes:only_suppressed_contamination = (language_code == 'ru' and model_contaminationand not (contamination or {}).get('confirmed')and bool(raw_codes) and all(code == 'LANGUAGE_CONTAMINATION' for code in raw_codes))if only_suppressed_contamination:continuecodes = ['SEMANTIC_DRIFT']issues.setdefault(meta['block_id'], {})[meta['claim_id']] = list(dict.fromkeys(codes))[:5]return issues351 unmodified lines)return {}def translate_frozen_section(self, section_text, target_lang, glossary=None, section_name='', max_attempts=2, stage_callback=None):"""Translate one frozen English unit and publish only after all QA passes."""value = str(section_text or '').strip()if not value:24 unmodified lines)attempts = max(1, int(max_attempts or 1))last_error = Nonedef emit_stage(stage):if not callable(stage_callback):returntry:stage_callback(str(stage or ''))except Exception as error:logging.warning('Translation stage callback ignored: section=%s stage=%s error=%s',section_name or 'section', stage, type(error).__name__,)for attempt in range(1, attempts + 1):self._check_cancelled()emit_stage('translating')translations = {}try:for batch in batches:3 unmodified linesfor batch in batchesfor block_id, source in batch}emit_stage('qa')quality_issues = self._audit_translation_blocks_with_retry(source_blocks, translations, target_lang, frozen_glossary,section_name=section_name or 'frozen section', max_attempts=2,9 unmodified lineslogging.warning('Translation claim-local QA recovery: section=%s round=%s failed=%s',section_name or 'section', local_round,[{'id': f'SP-P{block_id}-{claim_id}', 'codes': list(codes)}for block_id, claim_id, codes in failed_pairs],)emit_stage('repairing')touched_blocks = set()for block_id, claim_id, codes in failed_pairs:source_claims = {10 unmodified lines)translations[block_id] = self._compose_translation_claim_segments(claim_order, segments)touched_blocks.add(block_id)emit_stage('qa')quality_issues = self._audit_translation_blocks_with_retry({block_id: source_blocks[block_id] for block_id in sorted(touched_blocks)},{block_id: translations[block_id] for block_id in sorted(touched_blocks)},
334 unmodified lines3353363373383393403413423433441 unmodified line346347348349350351352353354355356357154 unmodified lines512513514515516517518334 unmodified linestarget.textContent = value;}function updateTargetPreparation(parentDocument, payload, labels) {const target = parentDocument.getElementById("sp-live-translation-preview");const channel = renderChannels.target;if (!target || channel.authoritative || target.querySelector("[data-sp-live-stream='1']")) return;const map = labels && typeof labels === "object" ? labels : {};const stage = String(payload && payload.target_stage || "");let value = String(map.waiting || "");1 unmodified linevalue = formatTemplate(map.translating || value, {name:stage.slice("translating:".length)});} else if (stage.startsWith("translation_qa:")) {value = formatTemplate(map.qa || value, {name:stage.slice("translation_qa:".length)});} else if (stage.startsWith("translation_ready:")) {value = formatTemplate(map.ready || value, {name:stage.slice("translation_ready:".length)});} else if (stage === "translation_complete") {value = String(map.complete || value);}if (!value) return;target.dataset.spPlaceholder = "1";target.textContent = value;}154 unmodified linesupdateOnePreview(parentDocument, parentWindow, "sp-live-translation-preview", "sp-live-translation-shell", targetText, renderChannels.target);lastTargetPreviewText = targetText;}if (!targetText) updateTargetPreparation(parentDocument, payload, watch && watch.targetStatusLabels);}async function fetchPreview(watch) {334 unmodified lines3353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663671 unmodified line369370371372373374375376377378379380381382383384385386387388389390391154 unmodified lines546547548549550551552334 unmodified linestarget.textContent = value;}function ensureTargetStatusNode(parentDocument) {const shell = parentDocument.getElementById("sp-live-translation-shell");const target = parentDocument.getElementById("sp-live-translation-preview");if (!shell || !target) return null;let status = parentDocument.getElementById("sp-live-translation-status");if (!status) {status = parentDocument.createElement("div");status.id = "sp-live-translation-status";status.style.whiteSpace = "pre-wrap";status.style.fontSize = "0.88em";status.style.opacity = "0.72";status.style.marginBottom = "8px";status.style.paddingBottom = "4px";status.style.position = "sticky";status.style.top = "0";status.style.zIndex = "1";status.style.background = "#ffffff";status.style.display = "none";shell.insertBefore(status, target);}return status;}function updateTargetPreparation(parentDocument, payload, labels) {const target = parentDocument.getElementById("sp-live-translation-preview");const channel = renderChannels.target;if (!target) return;const map = labels && typeof labels === "object" ? labels : {};const stage = String(payload && payload.target_stage || "");let value = String(map.waiting || "");1 unmodified linevalue = formatTemplate(map.translating || value, {name:stage.slice("translating:".length)});} else if (stage.startsWith("translation_qa:")) {value = formatTemplate(map.qa || value, {name:stage.slice("translation_qa:".length)});} else if (stage.startsWith("translation_repair_pending:")) {value = formatTemplate(map.qa || value, {name:stage.slice("translation_repair_pending:".length)});} else if (stage.startsWith("translation_ready:")) {value = formatTemplate(map.ready || value, {name:stage.slice("translation_ready:".length)});} else if (stage === "translation_complete") {value = String(map.complete || value);}if (!value) return;const hasTargetText = Boolean(String(payload && payload.target_text || "").trim()) || Boolean(channel.authoritative);if (hasTargetText) {const status = ensureTargetStatusNode(parentDocument);if (!status) return;status.textContent = value;status.style.display = "block";return;}if (target.querySelector("[data-sp-live-stream='1']")) return;target.dataset.spPlaceholder = "1";target.textContent = value;}154 unmodified linesupdateOnePreview(parentDocument, parentWindow, "sp-live-translation-preview", "sp-live-translation-shell", targetText, renderChannels.target);lastTargetPreviewText = targetText;}updateTargetPreparation(parentDocument, payload, watch && watch.targetStatusLabels);}async function fetchPreview(watch) {
core/generation_runner.py+36-5
core/pipeline_agent.py+170-7
smartpaper_restore_bridge/frontend/index.html+36-2