Spaces:
Runtime error
Runtime error
Commit ·
88e7ced
1
Parent(s): e0b2bb1
Rm convo
Browse filesFiles changed (5)
- utils/augment.py+8-9
- utils/cloud_llm.py+23-42
- utils/local_llm.py+22-41
- utils/processor.py+16-24
- utils/rag.py+4-16
223 unmodified lines22422522622722722822923023123123223323439 unmodified lines27427527627727827927727827928028115 unmodified lines297298299301302303304300301302303304305306223 unmodified lines retry_text = paraphraser.paraphrase(text, difficulty="easy") elif attempt == 1: # Second try: More aggressive paraphrasing with medical focus medical_prompt = f"Rewrite this medical response to be more professional and accurate:\n\n{text}" medical_prompt = f"Rewrite this medical response to be more professional and accurate. Return only the rewritten response without any introduction or commentary:\n\n{text}" retry_text = paraphraser.paraphrase(text, difficulty="hard", custom_prompt=medical_prompt) else: # Third try: Direct medical content generation medical_prompt = f"Provide a professional medical response to this question:\n\n{text}" medical_prompt = f"Provide a professional medical response to this question. Return only the medical response without any introduction or commentary:\n\n{text}" retry_text = paraphraser.paraphrase(text, difficulty="hard", custom_prompt=medical_prompt) if retry_text and not is_invalid_response(retry_text):39 unmodified lines return enhanced else: 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:" "Improve the medical terminology in this text while preserving all factual information. Return only the improved text with better medical terminology without any introduction or commentary:\n\n" f"{text}" ) enhanced = paraphraser.paraphrase(text, difficulty="hard", custom_prompt=prompt)15 unmodified lines else: # Fallback to original implementation context_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}" f"Rewrite this medical question as if asked by a patient in an emergency room. Return only the rewritten question without any introduction or commentary:\n\n{question}", f"Rewrite this medical question as if asked by a patient in a routine checkup. Return only the rewritten question without any introduction or commentary:\n\n{question}", f"Rewrite this medical question as if asked by a patient with chronic conditions. Return only the rewritten question without any introduction or commentary:\n\n{question}", f"Rewrite this medical question as if asked by a patient's family member. Return only the rewritten question without any introduction or commentary:\n\n{question}" ] for i, prompt in enumerate(context_prompts):152 unmodified lines15315415515615715815615715815916016216316416116216316416519 unmodified lines1851861871901911921881891901911921961971981931941951961978 unmodified lines2062072082132142152092102112122132192202212142152162172185 unmodified lines2242252262332342352362372382272282402412292302312326 unmodified lines2392402412542552562572582592422432612622442452462477 unmodified lines2552562572762772782582592602612628 unmodified lines271272273293274275276277297278279280281301282283284285305286287288289290291292312293294295296152 unmodified lines # Optimized medical paraphrasing prompts based on difficulty if 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:" "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. Return only the rewritten text without any introduction or commentary.\n\n" f"{text}" ) else: # hard difficulty prompt = ( "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:" "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. Return only the rewritten text without any introduction or commentary.\n\n" f"{text}" ) # Optimize temperature and token limits based on difficulty19 unmodified lines # Optimized medical translation prompts if 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:" "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"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:" 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}" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=min(800, len(text)+100))8 unmodified lines # Optimized backtranslation prompt with medical focus if 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: {mid}\n\n" "English medical translation:" "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"{mid}" ) 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: {mid}\n\n" "English medical translation:" 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"{mid}" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=min(900, len(text)+150))5 unmodified lines def consistency_check(self, user: str, output: str) -> bool: """Return True if 'output' appears supported by 'user' (context/question). Optimized medical validation.""" 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" "Evaluate if the medical answer is consistent with the question/context and medically accurate. Consider medical accuracy, clinical appropriateness, consistency with the question, safety standards, and completeness of medical information. 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:" f"Medical Answer: {output}" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=5) if not out:6 unmodified lines return False prompt = ( "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" "Evaluate if the medical answer is accurate and appropriate for the question. Consider medical facts, clinical knowledge, appropriate medical terminology, clinical reasoning, logic, and safety considerations. 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:" f"Medical Answer: {answer}" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=5)7 unmodified lines return text prompt = ( "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:" "Improve the medical terminology in the following text while preserving all factual information and clinical accuracy. Use more precise medical terms where appropriate. Return only the improved text without any introduction or commentary.\n\n" f"{text}" ) out = self.nv.generate(prompt, temperature=0.1, max_tokens=min(800, len(text)+100))8 unmodified lines # Different clinical context prompts context_prompts = [ ( "Rewrite this medical question as if asked by a patient in an emergency room setting:", "Rewrite this medical question as if asked by a patient in an emergency room setting. Return only the rewritten question without any introduction or commentary:\n\n{question}", "emergency_room" ), ( "Rewrite this medical question as if asked by a patient during a routine checkup:", "Rewrite this medical question as if asked by a patient during a routine checkup. Return only the rewritten question without any introduction or commentary:\n\n{question}", "routine_checkup" ), ( "Rewrite this medical question as if asked by a patient with chronic conditions:", "Rewrite this medical question as if asked by a patient with chronic conditions. Return only the rewritten question without any introduction or commentary:\n\n{question}", "chronic_care" ), ( "Rewrite this medical question as if asked by a patient's family member:", "Rewrite this medical question as if asked by a patient's family member. Return only the rewritten question without any introduction or commentary:\n\n{question}", "family_inquiry" ) ] for prompt_template, scenario_type in context_prompts: try: prompt = f"{prompt_template}\n\nOriginal question: {question}\n\nRewritten question:" prompt = prompt_template.format(question=question) scenario_question = self.paraphrase(question, difficulty="hard", custom_prompt=prompt) if scenario_question and not self._is_invalid_response(scenario_question):267 unmodified lines2682692702712722732712722732742752772782792762772782792809 unmodified lines29029129229529629729329429529629730130230329829930030130212 unmodified lines3153163173223233243183193203213223283293303233243253263272 unmodified lines3303313323393403413423433443333343463473353363373385 unmodified lines3443453463593603613623633643473483663673493503513525 unmodified lines3583593603793803813613623633643656 unmodified lines372373374394375376377378398379380381382402383384385386406387388389390267 unmodified lines # Medical-specific paraphrasing prompts based on difficulty if 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:" "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. Return only the rewritten text without any introduction or commentary.\n\n" f"{text}" ) else: # hard difficulty prompt = ( "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:" "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. Return only the rewritten text without any introduction or commentary.\n\n" f"{text}" ) # Adjust temperature based on difficulty9 unmodified lines # Medical-specific translation prompt if 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:" "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"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:" 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)12 unmodified lines # Then translate back to English with medical focus if 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:" "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"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:" 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)2 unmodified lines def 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" "Evaluate if the medical answer is consistent with the question/context and medically accurate. Consider medical accuracy, clinical appropriateness, consistency with the question, safety standards, and completeness of medical information. 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:" f"Medical Answer: {output}" ) result = self.client.generate(prompt, max_tokens=5, temperature=0.0)5 unmodified lines return False prompt = ( "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" "Evaluate if the medical answer is accurate and appropriate for the question. Consider medical facts, clinical knowledge, appropriate medical terminology, clinical reasoning, logic, and safety considerations. 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:" f"Medical Answer: {answer}" ) result = self.client.generate(prompt, max_tokens=5, temperature=0.0)5 unmodified lines return text prompt = ( "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:" "Improve the medical terminology in the following text while preserving all factual information and clinical accuracy. Use more precise medical terms where appropriate. Return only the improved text without any introduction or commentary.\n\n" f"{text}" ) result = self.client.generate(prompt, max_tokens=min(800, len(text)+100), temperature=0.1)6 unmodified lines # Different clinical context prompts context_prompts = [ ( "You are a medical professional. Rewrite this medical question as if asked by a patient in an emergency room setting:\n\nOriginal question: {question}\n\nEmergency room question:", "Rewrite this medical question as if asked by a patient in an emergency room setting. Return only the rewritten question without any introduction or commentary:\n\n{question}", "emergency_room" ), ( "You are a medical professional. Rewrite this medical question as if asked by a patient during a routine checkup:\n\nOriginal question: {question}\n\nRoutine checkup question:", "Rewrite this medical question as if asked by a patient during a routine checkup. Return only the rewritten question without any introduction or commentary:\n\n{question}", "routine_checkup" ), ( "You are a medical professional. Rewrite this medical question as if asked by a patient with chronic conditions:\n\nOriginal question: {question}\n\nChronic care question:", "Rewrite this medical question as if asked by a patient with chronic conditions. Return only the rewritten question without any introduction or commentary:\n\n{question}", "chronic_care" ), ( "You are a medical professional. Rewrite this medical question as if asked by a patient's family member:\n\nOriginal question: {question}\n\nFamily inquiry question:", "Rewrite this medical question as if asked by a patient's family member. Return only the rewritten question without any introduction or commentary:\n\n{question}", "family_inquiry" ) ]211 unmodified lines2122132142152162172152162172182202212222192202212222252262272232242252262302312322272282292302312 unmodified lines234235236241242243237238239240246247248241242243244251252253245246247248256257258249250251252253211 unmodified lines """Generate style-specific prompts for answer enhancement with medical focus""" prompts = { "concise": ( "You are a medical professional. Rewrite this medical answer to be more concise while preserving all key medical information, clinical facts, and diagnostic details:\n\n" f"Original answer: {original_answer}\n\n" "Concise medical answer:" "Rewrite this medical answer to be more concise while preserving all key medical information, clinical facts, and diagnostic details. Return only the rewritten answer without any introduction or commentary:\n\n" f"{original_answer}" ), "detailed": ( "You are a medical expert. Expand this medical answer with more detailed explanations, clinical context, and additional medical information while maintaining accuracy:\n\n" f"Original answer: {original_answer}\n\n" "Detailed medical answer:" "Expand this medical answer with more detailed explanations, clinical context, and additional medical information while maintaining accuracy. Return only the expanded answer without any introduction or commentary:\n\n" f"{original_answer}" ), "clinical": ( "You are a clinical specialist. Rewrite this answer using more formal clinical language, precise medical terminology, and professional medical communication style:\n\n" f"Original answer: {original_answer}\n\n" "Clinical medical answer:" "Rewrite this answer using more formal clinical language, precise medical terminology, and professional medical communication style. Return only the rewritten answer without any introduction or commentary:\n\n" f"{original_answer}" ), "patient_friendly": ( "You are a medical professional. Rewrite this medical answer in simpler, more patient-friendly language while keeping it medically accurate and informative:\n\n" f"Original answer: {original_answer}\n\n" "Patient-friendly medical answer:" "Rewrite this medical answer in simpler, more patient-friendly language while keeping it medically accurate and informative. Return only the rewritten answer without any introduction or commentary:\n\n" f"{original_answer}" ) } return prompts.get(strategy, f"Paraphrase this medical answer: {original_answer}")2 unmodified lines """Generate style-specific prompts for question enhancement with medical focus""" prompts = { "clarifying": ( "You are a medical professional. Rewrite this medical question to ask for clarification or more specific medical information:\n\n" f"Original question: {original_question}\n\n" "Clarifying medical question:" "Rewrite this medical question to ask for clarification or more specific medical information. Return only the rewritten question without any introduction or commentary:\n\n" f"{original_question}" ), "follow_up": ( "You are a medical professional. Create a follow-up question that a patient might ask after this medical question, focusing on related medical concerns:\n\n" f"Original question: {original_question}\n\n" "Follow-up medical question:" "Create a follow-up question that a patient might ask after this medical question, focusing on related medical concerns. Return only the follow-up question without any introduction or commentary:\n\n" f"{original_question}" ), "symptom_focused": ( "You are a medical professional. Rewrite this question to focus more on symptoms, their characteristics, and clinical presentation:\n\n" f"Original question: {original_question}\n\n" "Symptom-focused medical question:" "Rewrite this question to focus more on symptoms, their characteristics, and clinical presentation. Return only the rewritten question without any introduction or commentary:\n\n" f"{original_question}" ), "treatment_focused": ( "You are a medical professional. Rewrite this question to focus more on treatment options, management strategies, and therapeutic approaches:\n\n" f"Original question: {original_question}\n\n" "Treatment-focused medical question:" "Rewrite this question to focus more on treatment options, management strategies, and therapeutic approaches. Return only the rewritten question without any introduction or commentary:\n\n" f"{original_question}" ) } return prompts.get(strategy, f"Paraphrase this medical question: {original_question}")55 unmodified lines56575859606162636465665960686961707162636418 unmodified lines83848596868788891001011029091929355 unmodified lines if not text or len(text.strip()) < 10: return text prompt = f""" You are a medical data cleaning expert. Clean the following text by: 1. Remove conversational elements (greetings, pleasantries) 2. Remove non-medical small talk and social interactions 3. Keep only medically relevant information 4. Preserve clinical facts, symptoms, diagnoses, treatments, and medical advice 5. Maintain professional medical language 6. Return only cleaned medical content in 1-2 concise sentences suitable for dense retrieval embeddings. No lists, no headers. prompt = f"""Clean the following text by removing conversational elements (greetings, pleasantries), non-medical small talk, and social interactions. Keep only medically relevant information while preserving clinical facts, symptoms, diagnoses, treatments, and medical advice. Maintain professional medical language. Return only cleaned medical content in 1-2 concise sentences suitable for dense retrieval embeddings. No lists, no headers, no introduction or commentary:
Text to clean: {text}{text}"""
Cleaned medical content:"""
try: if self.is_local and self.medalpaca_client:18 unmodified lines if not question or not answer: return "" prompt = f"""You are a medical knowledge expert. Given a medical question and its answer, generate a brief relevant medical context that helps retrieval. Limit to 1–2 sentences, concise, avoid boilerplate, no enumerations. prompt = f"""Given a medical question and its answer, generate a brief relevant medical context that helps retrieval. Limit to 1–2 sentences, concise, avoid boilerplate, no enumerations. Return only the medical context without any introduction or commentary:
Question: {question}
Answer: {answer}
Generate a concise medical context:""" Answer: {answer}"""
try: if self.is_local and self.medalpaca_client:utils/augment.py+8-9
utils/cloud_llm.py+23-42
utils/local_llm.py+22-41
utils/processor.py+16-24
utils/rag.py+4-16