Spaces:
Runtime error
Runtime error
Commit ·
e76f718
1
Parent(s): dca816b
Upd local llm infer
Browse filesFiles changed (4)
- app.py+3-1
- utils/augment.py+40-27
- utils/local_llm.py+234-29
- utils/rag.py+38-15
455 unmodified lines456457458459460461462455 unmodified linesseed=params.seed,progress_cb=lambda p, msg=None: set_state(progress=p, message=msg or STATE["message"]),translator=translator,paraphraser=paraphraser)else:# Standard SFT processing mode455 unmodified lines456457458459460461462463464455 unmodified linesseed=params.seed,progress_cb=lambda p, msg=None: set_state(progress=p, message=msg or STATE["message"]),translator=translator,paraphraser=paraphraser,is_local=IS_LOCAL,hf_token=os.getenv("HF_TOKEN"))else:# Standard SFT processing mode
251 unmodified lines2522532542552562572582594 unmodified lines2642652662672682692702712722732742752762772784 unmodified lines283284285286287288289290291292293294295296297298299300301302303304251 unmodified linesreturn Falsetry:# Use the existing consistency check but with medical focusreturn paraphraser.consistency_check(question, answer)except Exception as e:logger.warning(f"Medical accuracy validation failed: {e}")return True # Default to accepting if validation fails4 unmodified linesreturn texttry:prompt = ("Improve the medical terminology in this text while preserving all factual information:\n\n"f"{text}\n\n""Return only the improved text with better medical terminology:")enhanced = paraphraser.paraphrase(text, difficulty="hard", custom_prompt=prompt)if enhanced and not is_invalid_response(enhanced):return enhancedexcept Exception as e:logger.warning(f"Medical terminology enhancement failed: {e}")4 unmodified linesscenarios = []try:# Generate different clinical contextscontext_prompts = [f"Rewrite this medical question as if asked by a patient in an emergency room:\n\n{question}",f"Rewrite this medical question as if asked by a patient in a routine checkup:\n\n{question}",f"Rewrite this medical question as if asked by a patient with chronic conditions:\n\n{question}",f"Rewrite this medical question as if asked by a patient's family member:\n\n{question}"]for i, prompt in enumerate(context_prompts):try:scenario_question = paraphraser.paraphrase(question, difficulty="hard", custom_prompt=prompt)if scenario_question and not is_invalid_response(scenario_question):scenarios.append((scenario_question, answer, f"clinical_scenario_{i+1}"))except Exception as e:logger.warning(f"Failed to create clinical scenario {i+1}: {e}")continueexcept Exception as e:logger.warning(f"Clinical scenario creation failed: {e}")251 unmodified lines2522532542552562572582592602612624 unmodified lines2672682692702712722732742752762772782792802812822832842852862874 unmodified lines292293294295296297298299300301302303304305306307308309310311312313314315316317251 unmodified linesreturn Falsetry:# Use medical accuracy check if available (local mode), otherwise fallback to consistency checkif hasattr(paraphraser, 'medical_accuracy_check'):return paraphraser.medical_accuracy_check(question, answer)else:return paraphraser.consistency_check(question, answer)except Exception as e:logger.warning(f"Medical accuracy validation failed: {e}")return True # Default to accepting if validation fails4 unmodified linesreturn texttry:# Use dedicated method if available (local mode), otherwise use paraphrase with custom promptif hasattr(paraphraser, 'enhance_medical_terminology'):enhanced = paraphraser.enhance_medical_terminology(text)if enhanced and not is_invalid_response(enhanced):return enhancedelse:prompt = ("Improve the medical terminology in this text while preserving all factual information:\n\n"f"{text}\n\n""Return only the improved text with better medical terminology:")enhanced = paraphraser.paraphrase(text, difficulty="hard", custom_prompt=prompt)if enhanced and not is_invalid_response(enhanced):return enhancedexcept Exception as e:logger.warning(f"Medical terminology enhancement failed: {e}")4 unmodified linesscenarios = []try:# Use dedicated method if available (local mode), otherwise use paraphrase with custom promptsif hasattr(paraphraser, 'create_clinical_scenarios'):scenarios = paraphraser.create_clinical_scenarios(question, answer)else:# Fallback to original implementationcontext_prompts = [f"Rewrite this medical question as if asked by a patient in an emergency room:\n\n{question}",f"Rewrite this medical question as if asked by a patient in a routine checkup:\n\n{question}",f"Rewrite this medical question as if asked by a patient with chronic conditions:\n\n{question}",f"Rewrite this medical question as if asked by a patient's family member:\n\n{question}"]for i, prompt in enumerate(context_prompts):try:scenario_question = paraphraser.paraphrase(question, difficulty="hard", custom_prompt=prompt)if scenario_question and not is_invalid_response(scenario_question):scenarios.append((scenario_question, answer, f"clinical_scenario_{i+1}"))except Exception as e:logger.warning(f"Failed to create clinical scenario {i+1}: {e}")continueexcept Exception as e:logger.warning(f"Clinical scenario creation failed: {e}")
93 unmodified lines94959697989910010110210310410510610710810913 unmodified lines1231241251261271281291301311321331341351361371381391401411421431441451461471481491501 unmodified line1521531541551561571583 unmodified lines16216316416516616717 unmodified lines1851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182 unmodified lines22122222322422522622722822923023123223323423523623723823924024124224393 unmodified linesmax_length=2048).to(self.device)# Generatewith torch.no_grad():outputs = self.model.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,do_sample=True,pad_token_id=self.tokenizer.eos_token_id,eos_token_id=self.tokenizer.eos_token_id,repetition_penalty=1.1)# Decode output13 unmodified linesreturn Nonedef _format_prompt(self, prompt: str) -> str:"""Format prompt for MedAlpaca model"""# MedAlpaca uses a specific format for medical Q&Aif "Question:" in prompt and "Answer:" in prompt:return promptelif "Context:" in prompt and "Question:" in prompt:return promptelse:# Simple medical Q&A formatreturn f"Question: {prompt}\n\nAnswer:"def _clean_response(self, text: str) -> str:"""Clean generated response"""if not text:return text# Remove common prefixesprefixes_to_remove = ["Answer:","The answer is:","Based on the information provided:","Here's the answer:","Here is the answer:",]text = text.strip()1 unmodified lineif text.startswith(prefix):text = text[len(prefix):].strip()breakreturn textdef _snip(self, text: str, max_words: int = 12) -> str:3 unmodified lineswords = text.strip().split()return " ".join(words[:max_words]) + (" …" if len(words) > max_words else "")def unload_model(self):"""Unload model to free memory"""if self.model is not None:17 unmodified linesself.client = MedAlpacaClient(model_name, hf_token)def paraphrase(self, text: str, difficulty: str = "easy", custom_prompt: str = None) -> str:"""Paraphrase text using MedAlpaca"""if not text or len(text) < 12:return textif custom_prompt:prompt = custom_promptelse:prompt = ("Paraphrase the following medical text concisely, preserve meaning and clinical terms.\n""Do not fabricate or remove factual claims.\n""Return ONLY the rewritten text, without any introduction, commentary.\n\n"f"Original text: {text}")result = self.client.generate(prompt, max_tokens=min(600, max(128, len(text)//2)), temperature=0.1)return result if result else textdef translate(self, text: str, target_lang: str = "vi") -> Optional[str]:"""Translate text using MedAlpaca"""if not text:return textprompt = f"Translate the following medical text to {target_lang}. Keep meaning exact, preserve medical terms:\n\n{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"""if not text:return text2 unmodified linesif not translated:return None# Then translate back to Englishprompt = f"Translate the following {via_lang} text back to English, preserving the exact meaning:\n\n{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"""prompt = ("You are a strict medical QA validator. Given the USER input (question+context) ""and the MODEL ANSWER, reply with exactly 'PASS' if the answer is supported and safe, ""otherwise 'FAIL'. No extra text.\n\n"f"USER:\n{user}\n\nANSWER:\n{output}")result = self.client.generate(prompt, max_tokens=3, temperature=0.0)return isinstance(result, str) and "PASS" in result.upper()def unload(self):"""Unload the model"""self.client.unload_model()93 unmodified lines94959697989910010110210310410510610710810911011111211313 unmodified lines1271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621 unmodified line1641651661671681691701711721731741751763 unmodified lines18018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924017 unmodified lines2582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123132 unmodified lines31631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744893 unmodified linesmax_length=2048).to(self.device)# Generate with optimized parameters for MedAlpacawith torch.no_grad():outputs = self.model.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,do_sample=True if temperature > 0 else False,pad_token_id=self.tokenizer.eos_token_id,eos_token_id=self.tokenizer.eos_token_id,repetition_penalty=1.1,top_p=0.9 if temperature > 0 else 1.0,top_k=50 if temperature > 0 else 0,num_beams=1 if temperature > 0 else 4,early_stopping=True)# Decode output13 unmodified linesreturn Nonedef _format_prompt(self, prompt: str) -> str:"""Format prompt for MedAlpaca model with medical-specific formatting"""# MedAlpaca was trained on medical Q&A pairs, so we use its expected formatif "Question:" in prompt and "Answer:" in prompt:return promptelif "Context:" in prompt and "Question:" in prompt:return promptelif "You are a" in prompt or "medical" in prompt.lower():# For medical instructions, use Alpaca formatreturn f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{prompt}\n\n### Response:"else:# Default medical Q&A format for MedAlpacareturn f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\nAnswer the following medical question accurately and professionally.\n\n### Input:\n{prompt}\n\n### Response:"def _clean_response(self, text: str) -> str:"""Clean generated response with medical-specific cleaning"""if not text:return text# Remove common prefixes and Alpaca format artifactsprefixes_to_remove = ["Answer:","The answer is:","Based on the information provided:","Here's the answer:","Here is the answer:","### Response:","Response:","Below is an instruction","### Instruction:","Instruction:",]text = text.strip()1 unmodified lineif text.startswith(prefix):text = text[len(prefix):].strip()break# Remove any remaining Alpaca format artifactsif "### Response:" in text:text = text.split("### Response:")[-1].strip()if "### Input:" in text:text = text.split("### Input:")[0].strip()return textdef _snip(self, text: str, max_words: int = 12) -> str:3 unmodified lineswords = text.strip().split()return " ".join(words[:max_words]) + (" …" if len(words) > max_words else "")def generate_batch(self, prompts: list, max_tokens: int = 512, temperature: float = 0.2) -> list:"""Generate text for multiple prompts in batch for better efficiency"""if not self.is_loaded:self.load_model()if not prompts:return []try:# Format all promptsformatted_prompts = [self._format_prompt(prompt) for prompt in prompts]# Tokenize all inputsinputs = self.tokenizer(formatted_prompts,return_tensors="pt",padding=True,truncation=True,max_length=2048).to(self.device)# Generate for all promptswith torch.no_grad():outputs = self.model.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,do_sample=True if temperature > 0 else False,pad_token_id=self.tokenizer.eos_token_id,eos_token_id=self.tokenizer.eos_token_id,repetition_penalty=1.1,top_p=0.9 if temperature > 0 else 1.0,top_k=50 if temperature > 0 else 0,num_beams=1 if temperature > 0 else 4,early_stopping=True)# Decode all outputsresults = []input_length = inputs['input_ids'].shape[1]for i, output in enumerate(outputs):generated_text = self.tokenizer.decode(output[input_length:],skip_special_tokens=True).strip()cleaned_text = self._clean_response(generated_text)results.append(cleaned_text)logger.info(f"[LOCAL_LLM] Generated batch of {len(prompts)} texts")return resultsexcept Exception as e:logger.error(f"[LOCAL_LLM] Batch generation failed: {e}")return [None] * len(prompts)def unload_model(self):"""Unload model to free memory"""if self.model is not None:17 unmodified linesself.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"""if not text or len(text) < 12:return textif custom_prompt:prompt = custom_promptelse:# Medical-specific paraphrasing prompts based on difficultyif difficulty == "easy":prompt = ("You are a medical professional. Rewrite the following medical text using different words while preserving all medical facts, clinical terms, and meaning. Keep the same level of detail and accuracy.\n\n"f"Original medical text: {text}\n\n""Rewritten medical text:")else: # hard difficultyprompt = ("You are a medical expert. Rewrite the following medical text using more sophisticated medical language and different sentence structures while preserving all clinical facts, medical terminology, and diagnostic information. Maintain professional medical tone.\n\n"f"Original medical text: {text}\n\n""Enhanced medical text:")# Adjust temperature based on difficultytemperature = 0.1 if difficulty == "easy" else 0.3result = 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 = ("You are a medical translator. Translate the following English medical text to Vietnamese while preserving all medical terminology, clinical facts, and professional medical language. Use appropriate Vietnamese medical terms.\n\n"f"English medical text: {text}\n\n""Vietnamese medical translation:")else:prompt = (f"You are a medical translator. Translate the following medical text to {target_lang} while preserving all medical terminology, clinical facts, and professional medical language.\n\n"f"Original medical text: {text}\n\n"f"{target_lang} medical translation:")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 text2 unmodified linesif not translated:return None# Then translate back to English with medical focusif via_lang == "vi":prompt = ("You are a medical translator. 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.\n\n"f"Vietnamese medical text: {translated}\n\n""English medical translation:")else:prompt = (f"You are a medical translator. Translate the following {via_lang} medical text back to English while preserving all medical terminology, clinical facts, and professional medical language.\n\n"f"{via_lang} medical text: {translated}\n\n""English medical translation:")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"""prompt = ("You are a medical quality assurance expert. Evaluate if the medical answer is consistent with the question/context and medically accurate. Consider:\n""1. Medical accuracy and clinical appropriateness\n""2. Consistency with the question asked\n""3. Safety and professional medical standards\n""4. Completeness of the medical information\n\n""Reply with exactly 'PASS' if the answer is medically sound and consistent, otherwise 'FAIL'.\n\n"f"Question/Context: {user}\n\n"f"Medical Answer: {output}\n\n""Evaluation:")result = self.client.generate(prompt, max_tokens=5, temperature=0.0)return isinstance(result, str) and "PASS" in result.upper()def medical_accuracy_check(self, question: str, answer: str) -> bool:"""Check medical accuracy of Q&A pairs using MedAlpaca"""if not question or not answer:return Falseprompt = ("You are a medical accuracy validator. Evaluate if the medical answer is accurate and appropriate for the question. Consider:\n""1. Medical facts and clinical knowledge\n""2. Appropriate medical terminology\n""3. Clinical reasoning and logic\n""4. Safety considerations\n\n""Reply with exactly 'ACCURATE' if the answer is medically correct, otherwise 'INACCURATE'.\n\n"f"Medical Question: {question}\n\n"f"Medical Answer: {answer}\n\n""Medical Accuracy Assessment:")result = self.client.generate(prompt, max_tokens=5, temperature=0.0)return isinstance(result, str) and "ACCURATE" in result.upper()def enhance_medical_terminology(self, text: str) -> str:"""Enhance medical terminology in text using MedAlpaca"""if not text or len(text) < 20:return textprompt = ("You are a medical terminology expert. Improve the medical terminology in the following text while preserving all factual information and clinical accuracy. Use more precise medical terms where appropriate.\n\n"f"Original text: {text}\n\n""Enhanced medical text:")result = self.client.generate(prompt, max_tokens=min(800, len(text)+100), temperature=0.1)return result if result else textdef create_clinical_scenarios(self, question: str, answer: str) -> list:"""Create different clinical scenarios from Q&A pairs using MedAlpaca"""scenarios = []# Different clinical context promptscontext_prompts = [("Rewrite this medical question as if asked by a patient in an emergency room setting:","emergency_room"),("Rewrite this medical question as if asked by a patient during a routine checkup:","routine_checkup"),("Rewrite this medical question as if asked by a patient with chronic conditions:","chronic_care"),("Rewrite this medical question as if asked by a patient's family member:","family_inquiry")]for prompt_template, scenario_type in context_prompts:try:prompt = f"{prompt_template}\n\nOriginal question: {question}\n\nRewritten question:"scenario_question = self.client.generate(prompt, max_tokens=min(400, len(question)+50), temperature=0.2)if scenario_question and not self._is_invalid_response(scenario_question):scenarios.append((scenario_question, answer, scenario_type))except Exception as e:logger.warning(f"Failed to create clinical scenario {scenario_type}: {e}")continuereturn scenariosdef _is_invalid_response(self, text: str) -> bool:"""Check if response is invalid (similar to augment.py)"""if not text or not isinstance(text, str):return Truetext_lower = text.lower().strip()invalid_patterns = ["fail", "invalid", "i couldn't", "i can't", "i cannot", "unable to","sorry", "error", "not available", "no answer", "insufficient","don't know", "do not know", "not sure", "cannot determine","unable to provide", "not possible", "not applicable", "n/a"]if len(text_lower) < 3:return Truefor pattern in invalid_patterns:if pattern in text_lower:return Truereturn Falsedef unload(self):"""Unload the model"""self.client.unload_model()
6 unmodified lines78910111228 unmodified lines414243444546474849505112 unmodified lines646566676869707172737413 unmodified lines8889909192939495969798231 unmodified lines3303313323333343353365 unmodified lines3423433443453463473486 unmodified linesfrom utils.schema import sft_row, rag_rowfrom utils.cloud_llm import NvidiaClient, KeyRotatorfrom vi.processing import should_translate, translate_rag_rowfrom utils import augment as A28 unmodified linesclass RAGProcessor:"""Processes medical datasets into RAG-specific QCA (Question, Context, Answer) format"""def __init__(self, nvidia_model: str):self.nvidia_client = NvidiaClient(KeyRotator("NVIDIA_API"), nvidia_model)def clean_conversational_content(self, text: str) -> str:"""Remove conversational elements and non-medical information using NVIDIA model; keep concise for embeddings."""if not text or len(text.strip()) < 10:return text12 unmodified linesCleaned medical content:"""try:cleaned = self.nvidia_client.generate(prompt,temperature=0.1,max_tokens=min(1000, len(text) + 200))return cleaned.strip() if cleaned else textexcept Exception as e:logger.warning(f"[RAG] Error cleaning text: {e}")13 unmodified linesGenerate a concise medical context:"""try:context = self.nvidia_client.generate(prompt,temperature=0.2,max_tokens=200)# Trim to a single short paragraphreturn (context or "").strip().split("\n")[0][:600]except Exception as e:231 unmodified linesseed: int,progress_cb: Optional[Callable[[float, str], None]],translator=None,paraphraser=None) -> Tuple[int, Dict]:"""Main entry point for RAG processing"""random.seed(seed)5 unmodified lineslogger.info(f"[RAG] Begin RAG processing dataset={dataset_key} sample_limit={sample_limit}")# Initialize RAG processorrag_processor = RAGProcessor(nvidia_model)dedupe_seen = set()key = dataset_key.lower()6 unmodified lines7891011121328 unmodified lines424344454647484950515253545556575812 unmodified lines71727374757677787980818283848586878813 unmodified lines102103104105106107108109110111112113114115116117118119231 unmodified lines3513523533543553563573583595 unmodified lines3653663673683693703716 unmodified linesfrom utils.schema import sft_row, rag_rowfrom utils.cloud_llm import NvidiaClient, KeyRotatorfrom utils.local_llm import MedAlpacaClientfrom vi.processing import should_translate, translate_rag_rowfrom utils import augment as A28 unmodified linesclass RAGProcessor:"""Processes medical datasets into RAG-specific QCA (Question, Context, Answer) format"""def __init__(self, nvidia_model: str, is_local: bool = False, hf_token: str = None):self.is_local = is_localif is_local:self.medalpaca_client = MedAlpacaClient(hf_token=hf_token)self.nvidia_client = Noneelse:self.nvidia_client = NvidiaClient(KeyRotator("NVIDIA_API"), nvidia_model)self.medalpaca_client = Nonedef clean_conversational_content(self, text: str) -> str:"""Remove conversational elements and non-medical information using MedAlpaca or NVIDIA model; keep concise for embeddings."""if not text or len(text.strip()) < 10:return text12 unmodified linesCleaned medical content:"""try:if self.is_local and self.medalpaca_client:cleaned = self.medalpaca_client.generate(prompt,temperature=0.1,max_tokens=min(1000, len(text) + 200))else:cleaned = self.nvidia_client.generate(prompt,temperature=0.1,max_tokens=min(1000, len(text) + 200))return cleaned.strip() if cleaned else textexcept Exception as e:logger.warning(f"[RAG] Error cleaning text: {e}")13 unmodified linesGenerate a concise medical context:"""try:if self.is_local and self.medalpaca_client:context = self.medalpaca_client.generate(prompt,temperature=0.2,max_tokens=200)else:context = self.nvidia_client.generate(prompt,temperature=0.2,max_tokens=200)# Trim to a single short paragraphreturn (context or "").strip().split("\n")[0][:600]except Exception as e:231 unmodified linesseed: int,progress_cb: Optional[Callable[[float, str], None]],translator=None,paraphraser=None,is_local: bool = False,hf_token: str = None) -> Tuple[int, Dict]:"""Main entry point for RAG processing"""random.seed(seed)5 unmodified lineslogger.info(f"[RAG] Begin RAG processing dataset={dataset_key} sample_limit={sample_limit}")# Initialize RAG processorrag_processor = RAGProcessor(nvidia_model, is_local=is_local, hf_token=hf_token)dedupe_seen = set()key = dataset_key.lower()
app.py+3-1
utils/augment.py+40-27
utils/local_llm.py+234-29
utils/rag.py+38-15