Spaces:
Runtime error
Runtime error
Commit ·
1d46eb9
1
Parent(s): cfa5d44
Upd vietnamese transl
Browse filesFiles changed (12)
- Dockerfile+3
- README.md+12-12
- app.py+45-9
- requirements.txt+2
- trans_test.pyAdded+78
- utils/processor.py+30-18
- utils/rag.py+23-8
- vi/README.mdAdded+95
- vi/__init__.pyAdded+10
- vi/download.pyAdded+89
- vi/processing.pyAdded+95
- vi/translator.pyAdded+266
15 unmodified lines16171819202122232415 unmodified lines# Copy the applicationCOPY --chown=user . .
# Download Vietnamese translation modelRUN python vi/download.py
# Hugging Face cache setupENV HF_HOME="$HOME/.cache/huggingface"ENV SENTENCE_TRANSFORMERS_HOME="$HOME/.cache/huggingface/sentence-transformers"122345456789910111213141415161617181819202021222223242425262728282930313132---title: MedAI Processingtitle: MedVietAI Processingemoji: ⚕️colorFrom: indigocolorTo: bluecolorFrom: greencolorTo: pinksdk: dockerpinned: falselicense: apache-2.0short_description: Process and centralise medical doc for llm finetuningshort_description: Data processing with en-vi translation. Derived from 500k mi---
## Quick Access:
[HF Space](https://huggingface.co/spaces/BinKhoaLe1812/MedAI_Processing)[HF Space](https://huggingface.co/spaces/MedVietAI/processing)
[MedDialog-100k](https://huggingface.co/datasets/BinKhoaLe1812/MedDialog-EN-100k)[MedDialog-100k](https://huggingface.co/datasets/MedAI-COS30018/MedDialog-EN-100k)
[MedDialog-100k](https://huggingface.co/datasets/BinKhoaLe1812/MedDialog-EN-10k)[MedDialog-100k](https://huggingface.co/datasets/MedAI-COS30018/MedDialog-EN-10k)
[PubMedQA-Labelled](https://huggingface.co/datasets/BinKhoaLe1812/PubMedQA-L)[PubMedQA-Labelled](https://huggingface.co/datasets/MedAI-COS30018/PubMedQA-L)
[PubMedQA-Unlabelled](https://huggingface.co/datasets/BinKhoaLe1812/PubMedQA-U)[PubMedQA-Unlabelled](https://huggingface.co/datasets/MedAI-COS30018/PubMedQA-U)
[PubMedQA-Mapper](https://huggingface.co/datasets/BinKhoaLe1812/PubMedQA-MAP)[PubMedQA-Mapper](https://huggingface.co/datasets/MedAI-COS30018/PubMedQA-MAP)
## CURL Request Instruction[Request Doc](https://huggingface.co/spaces/MedAI-COS30018/MedAI_Processing/blob/main/REQUEST.md)[Request Doc](https://huggingface.co/spaces/MedVietAI/processing/blob/main/REQUEST.md)
## License[Apache-2.0 LICENSE](https://huggingface.co/spaces/MedAI-COS30018/MedAI_Processing/blob/main/LICENSE.txt)[Apache-2.0 LICENSE](https://huggingface.co/spaces/MedVietAI/processing/blob/main/LICENSE.txt)
17 unmodified lines1819202122232429 unmodified lines54555657585960616226 unmodified lines8990919293949531 unmodified lines12712812913013113213313413513613713813914027 unmodified lines16816917015815917117217316117417517617712 unmodified lines1901911921801931941951961979 unmodified lines207208209210211212196213214215216217218219203220221222223224159 unmodified lines3843853863873883893903913923933943953963973983994004014 unmodified lines40640740837940941041141241341441541641741841942042142242338942442542639242742842943043117 unmodified linesfrom utils.llm import Paraphraserfrom utils.schema import CentralisedWriterfrom utils.token import get_credentials, exchange_code, build_auth_urlfrom vi.translator import VietnameseTranslator
# ────────── Log ───────────logger = logging.getLogger("app")29 unmodified lines gemini_model_hard=os.getenv("GEMINI_MODEL_HARD", "gemini-2.5-flash"),)
# Vietnamese translatorvietnamese_translator = VietnameseTranslator()
app = FastAPI(title="Medical Dataset Augmenter", version="1.1.0")
STATE_LOCK = threading.Lock()26 unmodified lines sample_limit: Optional[int] = None # Set data sampling if needed seed: int = 42 rag_processing: bool = False # Enable RAG-specific processing vietnamese_translation: bool = False # Enable Vietnamese translation
def set_state(**kwargs): with STATE_LOCK:31 unmodified lines <div class="section"> <h2>⚡ Quick Actions</h2> <p>Click a button below to start processing a dataset with default augmentation parameters.</p> <div style="margin-bottom: 15px; padding: 10px; background: #f8f9fa; border-radius: 5px; border-left: 4px solid #2d89ef;"> <label style="display: flex; align-items: center; cursor: pointer;"> <input type="checkbox" id="vietnameseTranslation" style="margin-right: 8px; transform: scale(1.2);"> <strong>🇻🇳 Vietnamese Translation</strong> - Translate all content to Vietnamese before processing </label> </div> <button onclick="startJob('healthcaremagic')">▶ProcAugment HealthCareMagic (100k)</button><br> <button onclick="startJob('icliniq')">▶ProcAugment iCliniq (10k-derived)</button><br> <button onclick="startJob('pubmedqa_l')">▶ProcAugment PubMedQA (Labelled)</button><br>27 unmodified lines <script> async function startJob(dataset) {{ const log = document.getElementById("log"); const ragToggle = document.getElementById("ragToggle"); const isRagMode = ragToggle.checked; const vietnameseToggle = document.getElementById("vietnameseTranslation"); const isVietnameseMode = vietnameseToggle.checked; log.innerHTML = "⏳ Starting " + (isRagMode ? "RAG " : "") + "job for <b>" + dataset + "</b>..."; log.innerHTML = "⏳ Starting job for <b>" + dataset + "</b>" + (isVietnameseMode ? " with Vietnamese translation" : "") + "..."; try {{ const resp = await fetch("/process/" + dataset, {{ method: "POST",12 unmodified lines }}, sample_limit: null, // Sample down (currently disabled) seed: 42, rag_processing: isRagMode rag_processing: false, vietnamese_translation: isVietnameseMode }}) }}); const data = await resp.json();9 unmodified lines async function startRagJob(dataset) {{ const log = document.getElementById("log"); const vietnameseToggle = document.getElementById("vietnameseTranslation"); const isVietnameseMode = vietnameseToggle.checked; log.innerHTML = "⏳ Starting RAG processing for <b>" + dataset + "</b>..."; log.innerHTML = "⏳ Starting RAG processing for <b>" + dataset + "</b>" + (isVietnameseMode ? " with Vietnamese translation" : "") + "..."; try {{ const resp = await fetch("/rag/" + dataset, {{ method: "POST", headers: {{ "Content-Type": "application/json" }}, body: JSON.stringify({{ sample_limit: null, seed: 42 seed: 42, vietnamese_translation: isVietnameseMode }}) }}); const data = await resp.json();159 unmodified lines # Writer writer = CentralisedWriter(jsonl_path=jsonl_path, csv_path=csv_path) # Load translator if Vietnamese translation is requested translator = None if params.vietnamese_translation: set_state(message="Loading Vietnamese translator", progress=0.05) try: vietnamese_translator.load_model() translator = vietnamese_translator logger.info("✅ Vietnamese translator loaded successfully") except Exception as e: logger.error(f"❌ Failed to load Vietnamese translator: {e}") set_state(message=f"Warning: Vietnamese translation failed - {e}", progress=0.1) if params.rag_processing: # RAG processing mode set_state(message="RAG processing", progress=0.1)4 unmodified lines nvidia_model=os.getenv("NVIDIA_MODEL", "meta/llama-3.1-8b-instruct"), sample_limit=params.sample_limit, seed=params.seed, progress_cb=lambda p, msg=None: set_state(progress=p, message=msg or STATE["message"]) progress_cb=lambda p, msg=None: set_state(progress=p, message=msg or STATE["message"]), translator=translator ) else: # Standard SFT processing mode set_state(message="SFT processing", progress=0.1) # Add Vietnamese translation flag to augment options augment_opts = params.augment.dict() augment_opts["vietnamese_translation"] = params.vietnamese_translation count, stats = process_file_into_sft( dataset_key=dataset_key, input_path=local_path, writer=writer, paraphraser=paraphraser, augment_opts=params.augment.dict(), augment_opts=augment_opts, sample_limit=params.sample_limit, seed=params.seed, progress_cb=lambda p, msg=None: set_state(progress=p, message=msg or STATE["message"]) progress_cb=lambda p, msg=None: set_state(progress=p, message=msg or STATE["message"]), translator=translator ) logger.info(f"[JOB] Processed dataset={dataset_key} rows={count} stats={stats}") writer.close()10 unmodified lines111213141510 unmodified linesorjsonftfylangidtransformerstorch123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#!/usr/bin/env python3"""Test script for Vietnamese translation functionality"""
import osimport sysimport loggingfrom dotenv import load_dotenv
# Add the current directory to Python pathsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from vi.translator import VietnameseTranslator
# Setup logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
def test_translation(): """Test the Vietnamese translation functionality""" load_dotenv() # Initialize translator translator = VietnameseTranslator() try: # Load the model logger.info("Loading translation model...") translator.load_model() logger.info("✅ Model loaded successfully") # Test single text translation test_text = "Hello, how are you today? I hope you are feeling well." logger.info(f"Original text: {test_text}") translated = translator.translate_text(test_text) logger.info(f"Translated text: {translated}") # Test batch translation test_texts = [ "What are the symptoms of diabetes?", "How do I treat a headache?", "What is the recommended dosage for this medication?" ] logger.info("Testing batch translation...") batch_translated = translator.translate_batch(test_texts) for i, (original, translated) in enumerate(zip(test_texts, batch_translated)): logger.info(f"Batch {i+1}:") logger.info(f" Original: {original}") logger.info(f" Translated: {translated}") # Test dictionary translation test_dict = { "instruction": "Answer the medical question", "input": "What are the side effects of aspirin?", "output": "Common side effects include stomach irritation and bleeding." } logger.info("Testing dictionary translation...") dict_translated = translator.translate_dict(test_dict, ["instruction", "input", "output"]) logger.info("Dictionary translation result:") for key, value in dict_translated.items(): logger.info(f" {key}: {value}") logger.info("🎉 All translation tests completed successfully!") return True except Exception as e: logger.error(f"❌ Translation test failed: {e}") return False
if __name__ == "__main__": success = test_translation() sys.exit(0 if success else 1)6 unmodified lines7891011121327 unmodified lines41424343444546474821 unmodified lines707172717374737576757778777980818254 unmodified lines1371381391381401411421437 unmodified lines15115215315415515615715815916016116216316416516616716816915817017117217322 unmodified lines1961971981871992002012022031922042052062079 unmodified lines21721821920822022122222324 unmodified lines2482492502392512522532542552442562572582599 unmodified lines26927027126027227327427526 unmodified lines3023033042933053063073083092983103113123139 unmodified lines32332432531432632732832965 unmodified lines3953963973863983994004014024034043934054064074086 unmodified lines
from utils.schema import sft_rowfrom utils import augment as Afrom vi.processing import translate_sft_row, should_translate, log_translation_stats
# Loggerlogger = logging.getLogger("processor")27 unmodified lines augment_opts: Dict, sample_limit: Optional[int], seed: int, progress_cb: Optional[Callable[[float, str], None]] progress_cb: Optional[Callable[[float, str], None]], translator=None) -> Tuple[int, Dict]: random.seed(seed) stats = {21 unmodified lines if key in ("healthcaremagic", "icliniq"): count = _proc_med_dialog(source=key, path=input_path, writer=writer, paraphraser=paraphraser, opts=augment_opts, sample_limit=sample_limit, stats=stats, cb=progress_cb, dedupe_seen=dedupe_seen) sample_limit=sample_limit, stats=stats, cb=progress_cb, dedupe_seen=dedupe_seen, translator=translator) elif key == "pubmedqa_l": count = _proc_pubmedqa_l(input_path, writer, paraphraser, augment_opts, sample_limit, stats, progress_cb, dedupe_seen=dedupe_seen) count = _proc_pubmedqa_l(input_path, writer, paraphraser, augment_opts, sample_limit, stats, progress_cb, dedupe_seen=dedupe_seen, translator=translator) elif key == "pubmedqa_u": count = _proc_pubmedqa_u(input_path, writer, paraphraser, augment_opts, sample_limit, stats, progress_cb, dedupe_seen=dedupe_seen) count = _proc_pubmedqa_u(input_path, writer, paraphraser, augment_opts, sample_limit, stats, progress_cb, dedupe_seen=dedupe_seen, translator=translator) elif key == "pubmedqa_map": count = _proc_pubmedqa_map(input_path, writer, paraphraser, augment_opts, sample_limit, stats, progress_cb, dedupe_seen=dedupe_seen) count = _proc_pubmedqa_map(input_path, writer, paraphraser, augment_opts, sample_limit, stats, progress_cb, dedupe_seen=dedupe_seen, translator=translator) else: raise ValueError(f"Unknown dataset: {dataset_key}") logger.info(f"[PROC] End dataset={dataset_key} stats={stats}")54 unmodified lines
return instr, user, out, applied
def _commit_row(writer, source, rid, task, instr, user, out, opts, stats, aug_applied, extra_meta=None, dedupe_seen=None):def _commit_row(writer, source, rid, task, instr, user, out, opts, stats, aug_applied, extra_meta=None, dedupe_seen=None, translator=None): # Dedup entry if dedupe_seen is not None: fp = A.fingerprint(instr, user, out)7 unmodified lines meta.update(extra_meta)
row = sft_row(instr, user, out, source=source, rid=rid, task=task, meta=meta) # Apply Vietnamese translation if requested if should_translate(opts.get("vietnamese_translation", False), translator): try: row = translate_sft_row(row, translator) meta["vietnamese_translated"] = True row["meta"] = meta except Exception as e: logger.error(f"Failed to translate SFT row: {e}") writer.write(row) stats["written"] += 1 return True
# ——————————— dataset processors ———————————
def _proc_med_dialog(source, path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None):def _proc_med_dialog(source, path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None, translator=None): count = 0 written = 0 for i, obj in enumerate(_iter_json_or_jsonl(path), start=1):22 unmodified lines applied.append("consistency_flag")
# 2) If expansion is enabled, add augmented copies _commit_row(writer, source, rid, "medical_dialogue", instr, user, out, opts, stats, ["base"] + applied, dedupe_seen=dedupe_seen) _commit_row(writer, source, rid, "medical_dialogue", instr, user, out, opts, stats, ["base"] + applied, dedupe_seen=dedupe_seen, translator=translator) # Add augmented copies if expand if opts.get("expand", True): for (u_aug, o_aug, aug_tags) in _build_variants(user, out, paraphraser, opts, stats): rid_aug = f"{rid}-aug{random.randint(1000,9999)}" _commit_row(writer, source, rid_aug, "medical_dialogue", instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen) _commit_row(writer, source, rid_aug, "medical_dialogue", instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen, translator=translator) # Increment count only on success count += 19 unmodified lines logger.info(f"[PROC] {source} done count={count} written={stats['written']} dedup_skipped={stats['dedup_skipped']}") return count
def _proc_pubmedqa_l(path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None):def _proc_pubmedqa_l(path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None, translator=None): with open(path, "r", encoding="utf-8") as f: data = json.load(f) count = 024 unmodified lines
instr, user, out, applied = _apply_aug(instr, user, out, "pubmedqa_l", opts, paraphraser, stats) _commit_row(writer, "pubmedqa_l", rid, "biomedical_qa", instr, user, out, opts, stats, applied, extra_meta={"year": v.get("YEAR"), "meshes": v.get("MESHES"), "labels": v.get("LABELS")}, dedupe_seen=dedupe_seen) extra_meta={"year": v.get("YEAR"), "meshes": v.get("MESHES"), "labels": v.get("LABELS")}, dedupe_seen=dedupe_seen, translator=translator) if opts.get("expand", True): for (u_aug, o_aug, aug_tags) in _build_variants(user, out, paraphraser, opts, stats): rid_aug = f"{rid}-aug{random.randint(1000,9999)}" _commit_row(writer, "pubmedqa_l", rid_aug, "biomedical_qa", instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen) instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen, translator=translator)
# Increment count only on success count += 19 unmodified lines logger.info(f"[PROC] pubmedqa_l done count={count} written={stats['written']} dedup_skipped={stats['dedup_skipped']}") return count
def _proc_pubmedqa_u(path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None):def _proc_pubmedqa_u(path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None, translator=None): with open(path, "r", encoding="utf-8") as f: data = json.load(f) count = 026 unmodified lines out = guess.strip()
instr, user, out, applied = _apply_aug(instr, user, out, "pubmedqa_u", opts, paraphraser, stats) _commit_row(writer, "pubmedqa_u", str(k), "biomedical_qa_unlabeled", instr, user, out, opts, stats, applied, dedupe_seen=dedupe_seen) _commit_row(writer, "pubmedqa_u", str(k), "biomedical_qa_unlabeled", instr, user, out, opts, stats, applied, dedupe_seen=dedupe_seen, translator=translator) if opts.get("expand", True): for (u_aug, o_aug, aug_tags) in _build_variants(user, out, paraphraser, opts, stats): rid_aug = f"{rid}-aug{random.randint(1000,9999)}" _commit_row(writer, "pubmedqa_u", rid_aug, "biomedical_qa", instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen) instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen, translator=translator) # Increment count only on success count += 19 unmodified lines logger.info(f"[PROC] pubmedqa_u done count={count} written={stats['written']} dedup_skipped={stats['dedup_skipped']}") return count
def _proc_pubmedqa_map(path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None):def _proc_pubmedqa_map(path, writer, paraphraser, opts, sample_limit, stats, cb, dedupe_seen=None, translator=None): with open(path, "r", encoding="utf-8") as f: obj = json.load(f) 65 unmodified lines
# Process the item instr, user, out, applied = _apply_aug(instr, user, out, "pubmedqa_map", opts, paraphraser, stats) _commit_row(writer, "pubmedqa_map", rid, "biomedical_qa", instr, user, out, opts, stats, applied, dedupe_seen=dedupe_seen) _commit_row(writer, "pubmedqa_map", rid, "biomedical_qa", instr, user, out, opts, stats, applied, dedupe_seen=dedupe_seen, translator=translator) # Handle expansion if enabled if opts.get("expand", True): for (u_aug, o_aug, aug_tags) in _build_variants(user, out, paraphraser, opts, stats): rid_aug = f"{rid}-aug{random.randint(1000,9999)}" _commit_row(writer, "pubmedqa_map", rid_aug, "biomedical_qa", instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen) instr, u_aug, o_aug, opts, stats, aug_tags, dedupe_seen=dedupe_seen, translator=translator) # Increment count only on success count += 16 unmodified lines78910111213152 unmodified lines16616716816816917017117227 unmodified lines20020120220220320420520614 unmodified lines22122222322322422522622738 unmodified lines26626726826826927027127215 unmodified lines2882892902902912922932945 unmodified lines3003013023033043053063073083093103113123133143155 unmodified lines3213223233133243253263273289 unmodified lines3383393403413423433443453463473333483493503513523533393543553563576 unmodified lines
from utils.schema import sft_rowfrom utils.llm import NvidiaClient, KeyRotatorfrom vi.processing import translate_rag_row, should_translate, log_translation_stats
# Loggerlogger = logging.getLogger("rag_processor")152 unmodified lines return "" def process_medical_dialog(self, source: str, path: str, writer, sample_limit: Optional[int], stats: Dict, progress_cb: Optional[Callable], dedupe_seen: set = None) -> int: stats: Dict, progress_cb: Optional[Callable], dedupe_seen: set = None, translator=None, opts=None) -> int: """Process medical dialogue datasets into RAG format""" count = 0 written = 027 unmodified lines # Commit the RAG-formatted row if self._commit_rag_row(writer, source, rid, "rag_medical_qa", rag_instruction, rag_user, answer, stats, dedupe_seen=dedupe_seen): stats, dedupe_seen=dedupe_seen, translator=translator, opts=opts): written += 1 count += 114 unmodified lines return count def process_pubmedqa(self, source: str, path: str, writer, sample_limit: Optional[int], stats: Dict, progress_cb: Optional[Callable], dedupe_seen: set = None) -> int: stats: Dict, progress_cb: Optional[Callable], dedupe_seen: set = None, translator=None, opts=None) -> int: """Process PubMedQA datasets into RAG format""" with open(path, "r", encoding="utf-8") as f: data = json.load(f)38 unmodified lines # Commit the RAG-formatted row if self._commit_rag_row(writer, source, rid, "rag_biomedical_qa", rag_instruction, rag_user, answer, stats, dedupe_seen=dedupe_seen): stats, dedupe_seen=dedupe_seen, translator=translator, opts=opts): written += 1 count += 115 unmodified lines def _commit_rag_row(self, writer, source: str, rid: str, task: str, instruction: str, user_input: str, output: str, stats: Dict, dedupe_seen: set = None) -> bool: stats: Dict, dedupe_seen: set = None, translator=None, opts=None) -> bool: """Commit a RAG-formatted row to the writer""" # Simple deduplication based on content hash if dedupe_seen is not None:5 unmodified lines meta = {"rag_processing": True, "format": "qca"} row = sft_row(instruction, user_input, output, source=source, rid=rid, task=task, meta=meta) # Apply Vietnamese translation if requested if should_translate(opts.get("vietnamese_translation", False) if opts else False, translator): try: row = translate_rag_row(row, translator) meta["vietnamese_translated"] = True row["meta"] = meta except Exception as e: logger.error(f"Failed to translate RAG row: {e}") writer.write(row) stats["written"] = stats.get("written", 0) + 1 return True5 unmodified lines nvidia_model: str, sample_limit: Optional[int], seed: int, progress_cb: Optional[Callable[[float, str], None]] progress_cb: Optional[Callable[[float, str], None]], translator=None) -> Tuple[int, Dict]: """Main entry point for RAG processing""" random.seed(seed)9 unmodified lines dedupe_seen = set() key = dataset_key.lower() # Create opts with Vietnamese translation flag opts = {"vietnamese_translation": translator is not None} if key in ("healthcaremagic", "icliniq"): count = rag_processor.process_medical_dialog( source=key, path=input_path, writer=writer, sample_limit=sample_limit, stats=stats, progress_cb=progress_cb, dedupe_seen=dedupe_seen progress_cb=progress_cb, dedupe_seen=dedupe_seen, translator=translator, opts=opts ) elif key in ("pubmedqa_l", "pubmedqa_u", "pubmedqa_map"): count = rag_processor.process_pubmedqa( source=key, path=input_path, writer=writer, sample_limit=sample_limit, stats=stats, progress_cb=progress_cb, dedupe_seen=dedupe_seen progress_cb=progress_cb, dedupe_seen=dedupe_seen, translator=translator, opts=opts ) else: raise ValueError(f"Unknown dataset for RAG processing: {dataset_key}")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495# Vietnamese Translation Module
This module provides Vietnamese translation functionality for the MedAI Processing application using the Helsinki-NLP/opus-mt-en-vi model.
## Features
- **English to Vietnamese Translation**: Translates English text to Vietnamese using the Helsinki-NLP/opus-mt-en-vi model- **Batch Processing**: Efficiently translates multiple texts at once- **Dictionary Translation**: Translates specific fields in data dictionaries- **Integration**: Seamlessly integrates with both SFT and RAG processing workflows- **Error Handling**: Graceful fallback to original text if translation fails- **Logging**: Comprehensive logging for debugging and monitoring
## Configuration
Add the following environment variable to your `.env` file:
```bashEN_VI=Helsinki-NLP/opus-mt-en-vi```
## Usage
### Basic Translation
```pythonfrom vi.translator import VietnameseTranslator
# Initialize translatortranslator = VietnameseTranslator()
# Load the modeltranslator.load_model()
# Translate single texttranslated = translator.translate_text("Hello, how are you?")
# Translate batch of textstexts = ["Text 1", "Text 2", "Text 3"]translated_batch = translator.translate_batch(texts)```
### Dictionary Translation
```python# Translate specific fields in a dictionarydata = { "instruction": "Answer the question", "input": "What is diabetes?", "output": "Diabetes is a metabolic disorder..."}
translated_data = translator.translate_dict(data, ["instruction", "input", "output"])```
## Integration
The translation functionality is automatically integrated into the processing workflows:
1. **UI Toggle**: Users can enable Vietnamese translation via the checkbox in the web interface2. **SFT Processing**: All text fields in SFT format are translated when enabled3. **RAG Processing**: All text fields in RAG format are translated when enabled4. **Metadata**: Translated rows are marked with `vietnamese_translated: true` in metadata
## Model Information
- **Model**: Helsinki-NLP/opus-mt-en-vi- **Source Language**: English- **Target Language**: Vietnamese- **BLEU Score**: 37.2- **chrF Score**: 0.542- **License**: Apache 2.0
## Testing
Run the test script to verify translation functionality:
```bashpython test_translation.py```
## Files
- `translator.py`: Main translation class- `download.py`: Model download script for Docker- `processing_utils.py`: Utility functions for processing integration- `__init__.py`: Module initialization- `README.md`: This documentation
## Notes
- The model is automatically downloaded during Docker build- Translation is performed on the CPU by default, but can use GPU if available- The model requires the target language token `>>vie<<` for proper translation- All translation operations include comprehensive error handling and logging12345678910"""Vietnamese Translation Module
This module provides utilities for translating English text to Vietnameseusing the Helsinki-NLP/opus-mt-en-vi model from Hugging Face."""
from .translator import VietnameseTranslator
__all__ = ['VietnameseTranslator']1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889"""Model Download Script for Vietnamese Translation
This script downloads the Helsinki-NLP/opus-mt-en-vi modeland saves it to the Hugging Face cache directory."""
import osimport sysimport loggingfrom pathlib import Pathfrom transformers import MarianMTModel, MarianTokenizer
# Setup logginglogging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)
def download_model(model_name: str = "Helsinki-NLP/opus-mt-en-vi", cache_dir: str = None): """ Download the translation model and tokenizer. Args: model_name: Hugging Face model name cache_dir: Cache directory for the model. If None, uses HF_HOME env var """ if cache_dir is None: cache_dir = os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface")) logger.info(f"Downloading model: {model_name}") logger.info(f"Cache directory: {cache_dir}") try: # Ensure cache directory exists os.makedirs(cache_dir, exist_ok=True) # Download tokenizer logger.info("Downloading tokenizer...") tokenizer = MarianTokenizer.from_pretrained( model_name, cache_dir=cache_dir ) logger.info("✅ Tokenizer downloaded successfully") # Download model logger.info("Downloading model...") model = MarianMTModel.from_pretrained( model_name, cache_dir=cache_dir ) logger.info("✅ Model downloaded successfully") # Test the model logger.info("Testing model...") test_text = "Hello, how are you?" inputs = tokenizer(f">>vie<< {test_text}", return_tensors="pt") with model.eval(): outputs = model.generate(**inputs, max_length=50, num_beams=4) translated = tokenizer.decode(outputs[0], skip_special_tokens=True) logger.info(f"Test translation: '{test_text}' -> '{translated}'") logger.info("🎉 Model download and test completed successfully!") return True except Exception as e: logger.error(f"❌ Failed to download model: {e}") return False
def main(): """Main function to download the model.""" # Get model name from environment variable or use default model_name = os.getenv("EN_VI", "Helsinki-NLP/opus-mt-en-vi") logger.info("Starting model download process...") logger.info(f"Model: {model_name}") success = download_model(model_name) if success: logger.info("Model download completed successfully!") sys.exit(0) else: logger.error("Model download failed!") sys.exit(1)
if __name__ == "__main__": main()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495"""Processing utilities for Vietnamese translation integration"""
import loggingfrom typing import Dict, Any, List, Optional, Callable
logger = logging.getLogger(__name__)
def translate_sft_row(row: Dict[str, Any], translator, text_fields: List[str] = None) -> Dict[str, Any]: """ Translate specific text fields in an SFT row from English to Vietnamese. Args: row: SFT row dictionary translator: VietnameseTranslator instance text_fields: List of field names to translate. If None, uses default fields. Returns: Translated SFT row dictionary """ if not translator or not translator.is_loaded(): logger.warning("Translator not available, skipping translation") return row if text_fields is None: # Default fields to translate in SFT format text_fields = ["instruction", "input", "output"] try: translated_row = translator.translate_dict(row, text_fields) logger.debug(f"Translated SFT row with fields: {text_fields}") return translated_row except Exception as e: logger.error(f"Failed to translate SFT row: {e}") return row
def translate_rag_row(row: Dict[str, Any], translator, text_fields: List[str] = None) -> Dict[str, Any]: """ Translate specific text fields in a RAG row from English to Vietnamese. Args: row: RAG row dictionary translator: VietnameseTranslator instance text_fields: List of field names to translate. If None, uses default fields. Returns: Translated RAG row dictionary """ if not translator or not translator.is_loaded(): logger.warning("Translator not available, skipping translation") return row if text_fields is None: # Default fields to translate in RAG format text_fields = ["instruction", "input", "output"] try: translated_row = translator.translate_dict(row, text_fields) logger.debug(f"Translated RAG row with fields: {text_fields}") return translated_row except Exception as e: logger.error(f"Failed to translate RAG row: {e}") return row
def should_translate(vietnamese_translation: bool, translator) -> bool: """ Check if translation should be performed. Args: vietnamese_translation: Flag from user input translator: VietnameseTranslator instance Returns: True if translation should be performed """ if not vietnamese_translation: return False if not translator or not translator.is_loaded(): logger.warning("Vietnamese translation requested but translator not available") return False return True
def log_translation_stats(stats: Dict[str, Any], translated_count: int) -> None: """ Log translation statistics. Args: stats: Statistics dictionary to update translated_count: Number of items translated """ stats["vietnamese_translated"] = translated_count logger.info(f"Vietnamese translation completed: {translated_count} items translated")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266"""Vietnamese Translator using Helsinki-NLP/opus-mt-en-vi model"""
import osimport loggingfrom typing import List, Dict, Any, Optional, Unionfrom transformers import MarianMTModel, MarianTokenizerimport torch
logger = logging.getLogger(__name__)
class VietnameseTranslator: """ Vietnamese translator using Helsinki-NLP/opus-mt-en-vi model. This class handles translation from English to Vietnamese using the MarianMT model from Hugging Face Transformers. """ def __init__(self, model_name: Optional[str] = None, device: Optional[str] = None): """ Initialize the Vietnamese translator. Args: model_name: Hugging Face model name. Defaults to EN_VI env var or Helsinki-NLP/opus-mt-en-vi device: Device to run the model on ('cpu', 'cuda', 'auto'). Defaults to 'auto' """ self.model_name = model_name or os.getenv("EN_VI", "Helsinki-NLP/opus-mt-en-vi") self.device = self._get_device(device) self.model = None self.tokenizer = None self._is_loaded = False logger.info(f"VietnameseTranslator initialized with model: {self.model_name}") logger.info(f"Using device: {self.device}") def _get_device(self, device: Optional[str]) -> str: """Determine the best device to use for the model.""" if device: return device if torch.cuda.is_available(): return "cuda" elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(): return "mps" else: return "cpu" def load_model(self) -> None: """Load the translation model and tokenizer.""" if self._is_loaded: logger.debug("Model already loaded, skipping...") return try: logger.info(f"Loading translation model: {self.model_name}") logger.info(f"Loading on device: {self.device}") # Load tokenizer self.tokenizer = MarianTokenizer.from_pretrained( self.model_name, cache_dir=os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface")) ) # Load model self.model = MarianMTModel.from_pretrained( self.model_name, cache_dir=os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface")) ) # Move model to device self.model = self.model.to(self.device) self.model.eval() self._is_loaded = True logger.info("✅ Translation model loaded successfully") except Exception as e: logger.error(f"❌ Failed to load translation model: {e}") raise RuntimeError(f"Failed to load translation model: {e}") def translate_text(self, text: str) -> str: """ Translate a single text from English to Vietnamese. Args: text: English text to translate Returns: Translated Vietnamese text """ if not self._is_loaded: self.load_model() if not text or not text.strip(): return text try: # Prepare input with target language token # The model requires a target language token in the format >>id<< input_text = f">>vie<< {text.strip()}" # Tokenize inputs = self.tokenizer( input_text, return_tensors="pt", padding=True, truncation=True, max_length=512 ).to(self.device) # Translate with torch.no_grad(): outputs = self.model.generate( **inputs, max_length=512, num_beams=4, early_stopping=True, do_sample=False ) # Decode translated = self.tokenizer.decode(outputs[0], skip_special_tokens=True) logger.debug(f"Translated: '{text[:50]}...' -> '{translated[:50]}...'") return translated.strip() except Exception as e: logger.error(f"Translation failed for text: '{text[:100]}...' - Error: {e}") # Return original text if translation fails return text def translate_batch(self, texts: List[str], batch_size: int = 8) -> List[str]: """ Translate a batch of texts from English to Vietnamese. Args: texts: List of English texts to translate batch_size: Number of texts to process in each batch Returns: List of translated Vietnamese texts """ if not self._is_loaded: self.load_model() if not texts: return [] results = [] try: for i in range(0, len(texts), batch_size): batch = texts[i:i + batch_size] logger.debug(f"Processing batch {i//batch_size + 1}/{(len(texts) + batch_size - 1)//batch_size}") # Prepare batch with target language tokens batch_inputs = [f">>vie<< {text.strip()}" for text in batch] # Tokenize batch inputs = self.tokenizer( batch_inputs, return_tensors="pt", padding=True, truncation=True, max_length=512 ).to(self.device) # Translate batch with torch.no_grad(): outputs = self.model.generate( **inputs, max_length=512, num_beams=4, early_stopping=True, do_sample=False ) # Decode batch batch_translations = [ self.tokenizer.decode(output, skip_special_tokens=True).strip() for output in outputs ] results.extend(batch_translations) except Exception as e: logger.error(f"Batch translation failed: {e}") # Return original texts if translation fails results = texts logger.info(f"Translated {len(texts)} texts successfully") return results def translate_dict(self, data: Dict[str, Any], text_fields: List[str]) -> Dict[str, Any]: """ Translate specific text fields in a dictionary from English to Vietnamese. Args: data: Dictionary containing the data text_fields: List of field names to translate Returns: Dictionary with translated text fields """ if not self._is_loaded: self.load_model() result = data.copy() for field in text_fields: if field in data and isinstance(data[field], str) and data[field].strip(): try: result[field] = self.translate_text(data[field]) logger.debug(f"Translated field '{field}': '{data[field][:50]}...' -> '{result[field][:50]}...'") except Exception as e: logger.error(f"Failed to translate field '{field}': {e}") # Keep original text if translation fails result[field] = data[field] return result def translate_list_of_dicts(self, data_list: List[Dict[str, Any]], text_fields: List[str]) -> List[Dict[str, Any]]: """ Translate specific text fields in a list of dictionaries. Args: data_list: List of dictionaries containing the data text_fields: List of field names to translate in each dictionary Returns: List of dictionaries with translated text fields """ if not data_list: return [] logger.info(f"Translating {len(data_list)} items with fields: {text_fields}") results = [] for i, data in enumerate(data_list): try: translated_data = self.translate_dict(data, text_fields) results.append(translated_data) if (i + 1) % 100 == 0: logger.info(f"Translated {i + 1}/{len(data_list)} items") except Exception as e: logger.error(f"Failed to translate item {i}: {e}") results.append(data) # Keep original data if translation fails logger.info(f"Completed translation of {len(data_list)} items") return results def is_loaded(self) -> bool: """Check if the model is loaded.""" return self._is_loaded def get_model_info(self) -> Dict[str, str]: """Get information about the loaded model.""" return { "model_name": self.model_name, "device": self.device, "is_loaded": self._is_loaded }Dockerfile+3
README.md+12-12
app.py+45-9
requirements.txt+2
trans_test.pyAdded+78
utils/processor.py+30-18
utils/rag.py+23-8
vi/README.mdAdded+95
vi/__init__.pyAdded+10
vi/download.pyAdded+89
vi/processing.pyAdded+95
vi/translator.pyAdded+266