Spaces:
Sleeping
Sleeping
Commit ·
53e751d
1
Parent(s): e3a165a
Upd dynamic translation NLPs
Browse filesFiles changed (5)
- Dockerfile+7-3
- utils/local_llm.py+237-18
- ~/.cache/huggingface/.locks/models--medalpaca--medalpaca-13b/091214b5ef67577540895961dcef678657b46f45.lockAdded
- ~/.cache/huggingface/models--medalpaca--medalpaca-13b/blobs/091214b5ef67577540895961dcef678657b46f45Added+10
- ~/.cache/huggingface/models--medalpaca--medalpaca-13b/refs/mainAdded+1
27 unmodified lines282930313233345 unmodified lines4041424344454647484927 unmodified lines# Copy the applicationCOPY --chown=user . .# Download Vietnamese translation modelRUN python vi/download.py# Hugging Face cache setup5 unmodified linesRUN mkdir -p $HOME/app/logs $HOME/app/cache $HOME/app/cache/hf $HOME/app/cache/outputs $HOME/app/data && \chown -R user:user $HOME/app# Download MedAlpaca model if in local modeRUN if [ "$IS_LOCAL" = "true" ]; then \echo "Downloading MedAlpaca-13b model for local mode..."; \python -c "from huggingface_hub import snapshot_download; import os; snapshot_download('medalpaca/medalpaca-13b', token=os.getenv('HF_TOKEN'), cache_dir='$HOME/.cache/huggingface')"; \fiUSER user27 unmodified lines282930313233345 unmodified lines404142434445464748495051525327 unmodified lines# Copy the applicationCOPY --chown=user . .# Download Vietnamese translation model (always needed for fallback)RUN python vi/download.py# Hugging Face cache setup5 unmodified linesRUN mkdir -p $HOME/app/logs $HOME/app/cache $HOME/app/cache/hf $HOME/app/cache/outputs $HOME/app/data && \chown -R user:user $HOME/app# Download models based on modeRUN if [ "$IS_LOCAL" = "true" ]; then \echo "Downloading models for local mode..."; \echo "Downloading MedAlpaca-13b model..."; \python -c "from huggingface_hub import snapshot_download; import os; snapshot_download('medalpaca/medalpaca-13b', token=os.getenv('HF_TOKEN'), cache_dir='$HOME/.cache/huggingface')"; \echo "Vietnamese translation model already downloaded above"; \else \echo "Cloud mode: Only Vietnamese translation model downloaded"; \fiUSER user
290 unmodified lines29129229329429529629729829930020 unmodified lines321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370104 unmodified lines475476477478479480290 unmodified lineslogger.info("[LOCAL_LLM] Model unloaded and memory freed")class LocalParaphraser:"""Local paraphraser using MedAlpaca model"""def __init__(self, model_name: str = "medalpaca/medalpaca-13b", hf_token: str = None):self.client = MedAlpacaClient(model_name, hf_token)def paraphrase(self, text: str, difficulty: str = "easy", custom_prompt: str = None) -> str:"""Paraphrase text using MedAlpaca with medical-specific optimization"""20 unmodified linesresult = self.client.generate(prompt, max_tokens=min(600, max(128, len(text)//2)), temperature=temperature)return result if result else textdef translate(self, text: str, target_lang: str = "vi") -> Optional[str]:"""Translate text using MedAlpaca with medical terminology preservation"""if not text:return text# Medical-specific translation promptif target_lang == "vi":prompt = ("Translate the following English medical text to Vietnamese while preserving all medical terminology, clinical facts, and professional medical language. Use appropriate Vietnamese medical terms. Return only the translation without any introduction or commentary.\n\n"f"{text}")else:prompt = (f"Translate the following medical text to {target_lang} while preserving all medical terminology, clinical facts, and professional medical language. Return only the translation without any introduction or commentary.\n\n"f"{text}")result = self.client.generate(prompt, max_tokens=min(800, len(text)+100), temperature=0.0)return result.strip() if result else Nonedef backtranslate(self, text: str, via_lang: str = "vi") -> Optional[str]:"""Backtranslate text using MedAlpaca with medical accuracy"""if not text:return text# First translate to target languagetranslated = self.translate(text, target_lang=via_lang)if not translated:return None# Then translate back to English with medical focusif via_lang == "vi":prompt = ("Translate the following Vietnamese medical text back to English while preserving all medical terminology, clinical facts, and professional medical language. Ensure the translation is medically accurate. Return only the translation without any introduction or commentary.\n\n"f"{translated}")else:prompt = (f"Translate the following {via_lang} medical text back to English while preserving all medical terminology, clinical facts, and professional medical language. Return only the translation without any introduction or commentary.\n\n"f"{translated}")result = self.client.generate(prompt, max_tokens=min(900, len(text)+150), temperature=0.0)return result.strip() if result else Nonedef consistency_check(self, user: str, output: str) -> bool:"""Check consistency using MedAlpaca with medical validation focus"""104 unmodified linesreturn Falsedef unload(self):"""Unload the model"""self.client.unload_model()290 unmodified lines29129229329429529629729829930030130230330430530630730830931031131231331431520 unmodified lines336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484104 unmodified lines589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699290 unmodified lineslogger.info("[LOCAL_LLM] Model unloaded and memory freed")class LocalParaphraser:"""Local paraphraser using MedAlpaca model with Vietnamese fallback translation"""def __init__(self, model_name: str = "medalpaca/medalpaca-13b", hf_token: str = None):self.client = MedAlpacaClient(model_name, hf_token)self.vietnamese_translator = Noneself._init_vietnamese_translator()def _init_vietnamese_translator(self):"""Initialize Vietnamese translator for fallback translation"""try:from vi.translator import VietnameseTranslatorself.vietnamese_translator = VietnameseTranslator()logger.info("[LOCAL_LLM] Vietnamese translator initialized for fallback")except ImportError as e:logger.warning(f"[LOCAL_LLM] Vietnamese translator not available: {e}")self.vietnamese_translator = Noneexcept Exception as e:logger.warning(f"[LOCAL_LLM] Failed to initialize Vietnamese translator: {e}")self.vietnamese_translator = Nonedef paraphrase(self, text: str, difficulty: str = "easy", custom_prompt: str = None) -> str:"""Paraphrase text using MedAlpaca with medical-specific optimization"""20 unmodified linesresult = self.client.generate(prompt, max_tokens=min(600, max(128, len(text)//2)), temperature=temperature)return result if result else textdef translate(self, text: str, target_lang: str = "vi", max_retries: int = 2) -> Optional[str]:"""Translate text using MedAlpaca with Vietnamese fallback mechanism"""if not text:return text# Only implement fallback for Vietnamese translationif target_lang != "vi":return self._translate_other_language(text, target_lang)# Try MedAlpaca translation with retriesfor attempt in range(max_retries + 1):try:# Medical-specific Vietnamese translation promptprompt = ("Translate the following English medical text to Vietnamese while preserving all medical terminology, clinical facts, and professional medical language. Use appropriate Vietnamese medical terms. Return only the translation without any introduction or commentary.\n\n"f"{text}")result = self.client.generate(prompt, max_tokens=min(800, len(text)+100), temperature=0.0)if result and result.strip():# Validate the translationif self._is_valid_vietnamese_translation(text, result.strip()):logger.info(f"[LOCAL_LLM] Vietnamese translation successful (attempt {attempt + 1})")return result.strip()else:logger.warning(f"[LOCAL_LLM] Invalid Vietnamese translation (attempt {attempt + 1}): {result[:100]}...")else:logger.warning(f"[LOCAL_LLM] Empty Vietnamese translation (attempt {attempt + 1})")except Exception as e:logger.warning(f"[LOCAL_LLM] Vietnamese translation attempt {attempt + 1} failed: {e}")# Fallback: Use translation model to translate English answerlogger.info("[LOCAL_LLM] MedAlpaca Vietnamese translation failed, using fallback translation model")return self._fallback_vietnamese_translation(text)def _translate_other_language(self, text: str, target_lang: str) -> Optional[str]:"""Translate to languages other than Vietnamese using MedAlpaca"""prompt = (f"Translate the following medical text to {target_lang} while preserving all medical terminology, clinical facts, and professional medical language. Return only the translation without any introduction or commentary.\n\n"f"{text}")result = self.client.generate(prompt, max_tokens=min(800, len(text)+100), temperature=0.0)return result.strip() if result else Nonedef _is_valid_vietnamese_translation(self, original: str, translation: str) -> bool:"""Check if the Vietnamese translation is valid"""if not translation or not translation.strip():return False# Check if translation is too similar to original (likely failed)if translation.strip().lower() == original.strip().lower():return False# Check if translation contains English words (likely failed)english_words = ['the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were', 'be', 'been', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'may', 'might', 'can', 'must']translation_lower = translation.lower()english_word_count = sum(1 for word in english_words if word in translation_lower)# If more than 30% of common English words are present, likely failedif english_word_count > len(translation.split()) * 0.3:return False# Check minimum length (should be reasonable)if len(translation.strip()) < len(original.strip()) * 0.3:return Falsereturn Truedef _fallback_vietnamese_translation(self, text: str) -> Optional[str]:"""Use translation model as fallback for Vietnamese translation"""if not self.vietnamese_translator:logger.warning("[LOCAL_LLM] Vietnamese translator not available for fallback")return Nonetry:result = self.vietnamese_translator.translate_text(text)if result and result.strip() and result.strip() != text.strip():logger.info("[LOCAL_LLM] Fallback Vietnamese translation successful")return result.strip()else:logger.warning("[LOCAL_LLM] Fallback Vietnamese translation failed or returned identical text")return Noneexcept Exception as e:logger.error(f"[LOCAL_LLM] Fallback Vietnamese translation error: {e}")return Nonedef backtranslate(self, text: str, via_lang: str = "vi") -> Optional[str]:"""Backtranslate text using MedAlpaca with Vietnamese fallback mechanism"""if not text:return text# First translate to target language (this will use fallback if needed)translated = self.translate(text, target_lang=via_lang)if not translated:return None# Then translate back to English with medical focusif via_lang == "vi":# Try MedAlpaca for back-translation firstprompt = ("Translate the following Vietnamese medical text back to English while preserving all medical terminology, clinical facts, and professional medical language. Ensure the translation is medically accurate. Return only the translation without any introduction or commentary.\n\n"f"{translated}")result = self.client.generate(prompt, max_tokens=min(900, len(text)+150), temperature=0.0)if result and result.strip():return result.strip()# Fallback: Use translation model for back-translationlogger.info("[LOCAL_LLM] MedAlpaca back-translation failed, using fallback translation model")return self._fallback_english_translation(translated)else:prompt = (f"Translate the following {via_lang} medical text back to English while preserving all medical terminology, clinical facts, and professional medical language. Return only the translation without any introduction or commentary.\n\n"f"{translated}")result = self.client.generate(prompt, max_tokens=min(900, len(text)+150), temperature=0.0)return result.strip() if result else Nonedef _fallback_english_translation(self, vietnamese_text: str) -> Optional[str]:"""Use translation model as fallback for English back-translation"""if not self.vietnamese_translator:logger.warning("[LOCAL_LLM] Vietnamese translator not available for back-translation fallback")return Nonetry:# Use the translator's back-translation capability# Note: This would need to be implemented in the VietnameseTranslator class# For now, we'll use a simple approachresult = self.vietnamese_translator.translate_text(vietnamese_text)if result and result.strip() and result.strip() != vietnamese_text.strip():logger.info("[LOCAL_LLM] Fallback English back-translation successful")return result.strip()else:logger.warning("[LOCAL_LLM] Fallback English back-translation failed or returned identical text")return Noneexcept Exception as e:logger.error(f"[LOCAL_LLM] Fallback English back-translation error: {e}")return Nonedef consistency_check(self, user: str, output: str) -> bool:"""Check consistency using MedAlpaca with medical validation focus"""104 unmodified linesreturn Falsedef create_vietnamese_training_data(self, question: str, answer: str, max_retries: int = 2) -> list:"""Create Vietnamese training data with fallback mechanism.This method tries to get Vietnamese translations from MedAlpaca first.If MedAlpaca fails (max 2 retries), it allows MedAlpaca to answer in Englishand uses translation models to create Vietnamese versions.Args:question: English questionanswer: English answermax_retries: Maximum retries for MedAlpaca Vietnamese translationReturns:List of training data tuples: [(question_vi, answer_vi), ...]"""training_data = []# Try to get Vietnamese translation from MedAlpacaquestion_vi = self.translate(question, target_lang="vi", max_retries=max_retries)answer_vi = self.translate(answer, target_lang="vi", max_retries=max_retries)if question_vi and answer_vi:# MedAlpaca successfully translated bothtraining_data.append((question_vi, answer_vi))logger.info("[LOCAL_LLM] Created Vietnamese training data using MedAlpaca translation")else:# MedAlpaca failed, use fallback mechanismlogger.info("[LOCAL_LLM] MedAlpaca Vietnamese translation failed, using fallback mechanism")# Allow MedAlpaca to answer in English (this should always work)english_answer = self.client.generate(f"Answer the following medical question: {question}",max_tokens=min(800, len(answer)+100),temperature=0.1)if english_answer and english_answer.strip():# Use translation models to create Vietnamese versionsif self.vietnamese_translator:try:# Translate question using fallbackquestion_vi_fallback = self._fallback_vietnamese_translation(question)# Translate answer using fallbackanswer_vi_fallback = self._fallback_vietnamese_translation(english_answer.strip())if question_vi_fallback and answer_vi_fallback:training_data.append((question_vi_fallback, answer_vi_fallback))logger.info("[LOCAL_LLM] Created Vietnamese training data using fallback translation")else:logger.warning("[LOCAL_LLM] Fallback translation failed, no Vietnamese training data created")except Exception as e:logger.error(f"[LOCAL_LLM] Fallback translation error: {e}")else:logger.warning("[LOCAL_LLM] Vietnamese translator not available for fallback")else:logger.warning("[LOCAL_LLM] MedAlpaca failed to generate English answer for fallback")return training_datadef create_vietnamese_augmented_data(self, question: str, answer: str) -> list:"""Create multiple Vietnamese training data variations using different approaches.This method creates:1. Direct Vietnamese translation (if successful)2. English answer + Vietnamese translation fallback3. Paraphrased English + Vietnamese translationArgs:question: English questionanswer: English answerReturns:List of training data tuples: [(question_vi, answer_vi), ...]"""training_data = []# 1. Try direct Vietnamese translationdirect_data = self.create_vietnamese_training_data(question, answer)training_data.extend(direct_data)# 2. Create paraphrased English version and translatetry:paraphrased_answer = self.paraphrase(answer, difficulty="easy")if paraphrased_answer and paraphrased_answer != answer:paraphrased_data = self.create_vietnamese_training_data(question, paraphrased_answer)training_data.extend(paraphrased_data)logger.info("[LOCAL_LLM] Created Vietnamese training data from paraphrased English")except Exception as e:logger.warning(f"[LOCAL_LLM] Failed to create paraphrased Vietnamese data: {e}")# 3. Create back-translated versiontry:backtranslated_answer = self.backtranslate(answer, via_lang="vi")if backtranslated_answer and backtranslated_answer != answer:backtranslated_data = self.create_vietnamese_training_data(question, backtranslated_answer)training_data.extend(backtranslated_data)logger.info("[LOCAL_LLM] Created Vietnamese training data from back-translated English")except Exception as e:logger.warning(f"[LOCAL_LLM] Failed to create back-translated Vietnamese data: {e}")logger.info(f"[LOCAL_LLM] Created {len(training_data)} Vietnamese training data variations")return training_datadef unload(self):"""Unload the model"""self.client.unload_model()
12345678910{ "bos_token": "</s>", "eos_token": "</s>", "model_max_length": 512, "padding_side": "right", "special_tokens_map_file": "/sc-projects/sc-proj-cc06-medbert/hfcache/hub/models--decapoda-research--llama-13b-hf/snapshots/438770a656712a5072229b62256521845d4de5ce/special_tokens_map.json", "tokenizer_class": "LlamaTokenizer", "unk_token": "</s>", "pad_token": "[PAD]"}13e5d80d26b0373167ab2daed45b7f9ef35f998e5Dockerfile+7-3
utils/local_llm.py+237-18
~/.cache/huggingface/.locks/models--medalpaca--medalpaca-13b/091214b5ef67577540895961dcef678657b46f45.lockAdded
~/.cache/huggingface/models--medalpaca--medalpaca-13b/blobs/091214b5ef67577540895961dcef678657b46f45Added+10
~/.cache/huggingface/models--medalpaca--medalpaca-13b/refs/mainAdded+1