Spaces:
Runtime error
Runtime error
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 lines28293031313233345 unmodified lines4041424343444545464748495051525327 unmodified lines# Copy the applicationCOPY --chown=user . .
# Download Vietnamese translation model# 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 MedAlpaca model if in local mode# Download models based on modeRUN if [ "$IS_LOCAL" = "true" ]; then \ echo "Downloading MedAlpaca-13b model for local mode..."; \ 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"; \ fi
USER user290 unmodified lines29129229329429429529629729829930030130230330430530630730830931031131231331431520 unmodified lines336337338324325339340341342343329330331332333334335336337338339344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428345429430431432349433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467366367468469470471472473474475476477478479480481482483484104 unmodified lines589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699290 unmodified lines logger.info("[LOCAL_LLM] Model unloaded and memory freed")
class LocalParaphraser: """Local paraphraser using MedAlpaca model""" """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 = None self._init_vietnamese_translator() def _init_vietnamese_translator(self): """Initialize Vietnamese translator for fallback translation""" try: from vi.translator import VietnameseTranslator self.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 = None except Exception as e: logger.warning(f"[LOCAL_LLM] Failed to initialize Vietnamese translator: {e}") self.vietnamese_translator = None def paraphrase(self, text: str, difficulty: str = "easy", custom_prompt: str = None) -> str: """Paraphrase text using MedAlpaca with medical-specific optimization"""20 unmodified lines result = self.client.generate(prompt, max_tokens=min(600, max(128, len(text)//2)), temperature=temperature) return result if result else text def translate(self, text: str, target_lang: str = "vi") -> Optional[str]: """Translate text using MedAlpaca with medical terminology preservation""" def 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 # Medical-specific translation prompt if 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}" ) # Only implement fallback for Vietnamese translation if target_lang != "vi": return self._translate_other_language(text, target_lang) # Try MedAlpaca translation with retries for attempt in range(max_retries + 1): try: # Medical-specific Vietnamese translation prompt 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}" ) result = self.client.generate(prompt, max_tokens=min(800, len(text)+100), temperature=0.0) if result and result.strip(): # Validate the translation if 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 answer logger.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 None def _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 failed if 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 False return True def _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 None try: 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 None except Exception as e: logger.error(f"[LOCAL_LLM] Fallback Vietnamese translation error: {e}") return None def backtranslate(self, text: str, via_lang: str = "vi") -> Optional[str]: """Backtranslate text using MedAlpaca with medical accuracy""" """Backtranslate text using MedAlpaca with Vietnamese fallback mechanism""" if not text: return text # First translate to target language # 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 focus if via_lang == "vi": # Try MedAlpaca for back-translation first 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}" ) 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-translation logger.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 None def _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 None result = self.client.generate(prompt, max_tokens=min(900, len(text)+150), temperature=0.0) return result.strip() if result else None try: # 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 approach result = 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 None except Exception as e: logger.error(f"[LOCAL_LLM] Fallback English back-translation error: {e}") return None def consistency_check(self, user: str, output: str) -> bool: """Check consistency using MedAlpaca with medical validation focus"""104 unmodified lines return False def 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 English and uses translation models to create Vietnamese versions. Args: question: English question answer: English answer max_retries: Maximum retries for MedAlpaca Vietnamese translation Returns: List of training data tuples: [(question_vi, answer_vi), ...] """ training_data = [] # Try to get Vietnamese translation from MedAlpaca question_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 both training_data.append((question_vi, answer_vi)) logger.info("[LOCAL_LLM] Created Vietnamese training data using MedAlpaca translation") else: # MedAlpaca failed, use fallback mechanism logger.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 versions if self.vietnamese_translator: try: # Translate question using fallback question_vi_fallback = self._fallback_vietnamese_translation(question) # Translate answer using fallback answer_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_data def 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 fallback 3. Paraphrased English + Vietnamese translation Args: question: English question answer: English answer Returns: List of training data tuples: [(question_vi, answer_vi), ...] """ training_data = [] # 1. Try direct Vietnamese translation direct_data = self.create_vietnamese_training_data(question, answer) training_data.extend(direct_data) # 2. Create paraphrased English version and translate try: 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 version try: 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_data def 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