Conversation
- Add gemma_translate() function with google/translategemma-4b-it as default - Implement language validation for 55 supported languages - Call get_max_new_tokens() as required by issue - Add HuggingFace authentication error handling with clear messages - Update translate() router to support model='gemma' - Add comprehensive HF authentication documentation to DEVELOPERNOTES.md Closes #43
laurejt
left a comment
There was a problem hiding this comment.
This is looking pretty good, just needs a few changes before it's ready to go.
- Revise the developer notes making sure that our documentation is consistent with HuggingFace's.
- Remove
gemma_langs.pysince we don't need it for the current use case. The model will throw an error if an unsupported language is requested. - Update
gemma_translatemethod to (1) remove language validation, (2) update the automodel, (3) update model input handling
src/muse/translation/gemma_langs.py
Outdated
There was a problem hiding this comment.
We don't need this since TranslateGemma accepts ISO-639-1 language codes. We need these indices for NLLB and HY-MT 1.5 because we need a mapping from ISO-639-1 language codes to whatever form the model is expecting for that language.
src/muse/translation/translate.py
Outdated
| # Validate input languages | ||
| if src_lang not in gemma_lang_idx: | ||
| raise ValueError(f"Source language '{src_lang}' is not supported") | ||
| if tgt_lang not in gemma_lang_idx: | ||
| raise ValueError(f"Target language '{tgt_lang}' is not supported") |
There was a problem hiding this comment.
Remove this validation. According to the model card, an error will be thrown if an unsupported language code is provided.
docs/DEVELOPERNOTES.md
Outdated
|
|
||
| 1. Visit [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) | ||
| 2. Click "New token" and select "Read" access type | ||
| 3. Copy the token and use it with `huggingface-cli login` |
There was a problem hiding this comment.
Noting again this was an invalid command at least for me. I needed to use hf auth login.
src/muse/translation/translate.py
Outdated
| LOADED_MODEL["tokenizer"] = AutoTokenizer.from_pretrained(model_name) | ||
| LOADED_MODEL["model"] = AutoModelForCausalLM.from_pretrained(model_name) |
There was a problem hiding this comment.
This model is actually an ImageTextToText model, so we need to load a different type of model. In this case we can still use the AutoTokenizer (which loads the Gemma3 Tokenizer) because its input is compatible with the TranslateGemma models.
| LOADED_MODEL["tokenizer"] = AutoTokenizer.from_pretrained(model_name) | |
| LOADED_MODEL["model"] = AutoModelForCausalLM.from_pretrained(model_name) | |
| LOADED_MODEL["tokenizer"] = AutoTokenizer.from_pretrained(model_name) | |
| LOADED_MODEL["model"] = AutoModelForImageTextToText.from_pretrained(model_name) |
src/muse/translation/translate.py
Outdated
| tokenized_chat = tokenizer.apply_chat_template( | ||
| messages, | ||
| tokenize=True, | ||
| add_generation_prompt=True, | ||
| return_tensors="pt", | ||
| ) | ||
| input_len = tokenized_chat[0].size()[0] | ||
| if verbose: | ||
| print(f"Input length: {input_len} tokens") | ||
|
|
||
| # Generate translation | ||
| if verbose: | ||
| start = timer() | ||
| outputs = model.generate( | ||
| tokenized_chat.to(model.device), |
There was a problem hiding this comment.
Model raises some warnings about input attention masks and padding, so follow the example from the model card instead.
| tokenized_chat = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=True, | |
| add_generation_prompt=True, | |
| return_tensors="pt", | |
| ) | |
| input_len = tokenized_chat[0].size()[0] | |
| if verbose: | |
| print(f"Input length: {input_len} tokens") | |
| # Generate translation | |
| if verbose: | |
| start = timer() | |
| outputs = model.generate( | |
| tokenized_chat.to(model.device), | |
| tokenized_chat = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=True, | |
| add_generation_prompt=True, | |
| return_dict=True, | |
| return_tensors="pt", | |
| ).to(model.device) | |
| input_len = len(tokenized_chat["input_ids"][0]) | |
| if verbose: | |
| print(f"Input length: {input_len} tokens") | |
| # Generate translation | |
| if verbose: | |
| start = timer() | |
| outputs = model.generate( | |
| **tokenized_chat, |
src/muse/translation/translate.py
Outdated
| AutoTokenizer, | ||
| ) | ||
|
|
||
| from muse.translation.gemma_langs import lang_index as gemma_lang_idx |
Co-authored-by: Laure Thompson <602628+laurejt@users.noreply.github.com>
laurejt
left a comment
There was a problem hiding this comment.
This is ready to go after the latest, code-breaking revisions to translate_hymt are removed.
src/muse/translation/translate.py
Outdated
| input_len = len(tokenized_chat["input_ids"][0]) | ||
| if verbose: | ||
| print(f"Input length: {input_len} tokens") | ||
|
|
||
| # Generate translation | ||
| if verbose: | ||
| start = timer() | ||
| outputs = model.generate( | ||
| tokenized_chat.to(model.device), | ||
| **tokenized_chat, |
There was a problem hiding this comment.
Undo these changes because they cause the code to break. These changes are not compatible with HYMT-1.5 models.
| input_len = len(tokenized_chat["input_ids"][0]) | |
| if verbose: | |
| print(f"Input length: {input_len} tokens") | |
| # Generate translation | |
| if verbose: | |
| start = timer() | |
| outputs = model.generate( | |
| tokenized_chat.to(model.device), | |
| **tokenized_chat, | |
| input_len = tokenized_chat[0].size()[0] | |
| if verbose: | |
| print(f"Input length: {input_len} tokens") | |
| # Generate translation | |
| if verbose: | |
| start = timer() | |
| outputs = model.generate( | |
| tokenized_chat.to(model.device), |
There was a problem hiding this comment.
Although HYMT-1.5 and TranslateGemma both use the apply_chat_template tokenizer method, the model's themselves have different input requirements. Additionally, the tokenized_chat variables produced in hymt_translate and gemma_translate are not the same type and so L106 cannot be the same.
…eton-CDH/muse into feature/add-translategemma
Associated Issue(s): resolves #43
Changes in this PR
gemma_translate()function withgoogle/translategemma-4b-itas default modelget_max_new_tokens()as requiredtranslate()router to supportmodel="gemma"DEVELOPERNOTES.mdNotes
Reviewer Checklist
get_max_new_tokens()is called correctlyPYTHONPATH=src .venv/bin/python -c "from muse.translation.translate import translate; print(translate('gemma', 'es', 'en', 'Hola mundo'))")