Spaces:
Sleeping
Sleeping
Update app.py
Browse files42 unmodified lines4344454647484924 unmodified lines747576777879808182116 unmodified lines19920020120220320420520620720820921021121221321421521621721821922022122222322442 unmodified linesdef clean_output(text):"""Remove reasoning artifacts and extract only the actual report"""# Remove common reasoning patternspatterns_to_remove = [24 unmodified linestext = text[idx:]break# Clean up extra whitespace and duplicate asteriskstext = re.sub(r'\*{3,}', '**', text)text = re.sub(r'\n{3,}', '\n\n', text)text = text.strip()return text116 unmodified lines# Clean up the outputoutput_text = clean_output(output_text)# Validationrequired_sections = ["Executive Summary","System-Specific Analysis","Personalized Action Plan","Interaction Alerts","Tabular Mapping","Enhanced AI Insights"]missing_sections = [s for s in required_sections if s not in output_text]if len(output_text) < 800 or len(missing_sections) >= 3:warning = "\n\n⚠️ **Model Performance Issue Detected**\n\n"warning += f"Generated text length: {len(output_text)} characters\n"warning += f"Missing sections: {', '.join(missing_sections) if missing_sections else 'None'}\n\n"warning += "**This model may not be suitable for this task. Consider:**\n"warning += "1. Using Llama-3-8B-Instruct or Mistral-7B-Instruct\n"warning += "2. Fine-tuning on medical report generation\n"warning += "3. Using API-based models (GPT-4, Claude, etc.)\n"output_text += warningreturn output_text42 unmodified lines4344454647484924 unmodified lines7475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137116 unmodified lines25425525625725825926026126226326426526626726826927027127227327427527627727827928028128242 unmodified linesdef clean_output(text):"""Remove reasoning artifacts and clean formatting"""# Remove common reasoning patternspatterns_to_remove = [24 unmodified linestext = text[idx:]break# Fix common formatting issues from the modelreplacements = {# Fix typos'Albumen': 'Albumin','Creatinin': 'Creatinine','Nomal': 'Normal','Lympho': 'Lymphocytes','Strea‑ngths': 'Strengths','Priorities': 'Priorities','Heath': 'Health','Kidnee': 'Kidney','Meta‑bolic': 'Metabolic','Health*': 'Health**','Personelized': 'Personalized','Action': 'Action','Plan': 'Plan','Interaction': 'Interaction','Tabular': 'Tabular','Mapping': 'Mapping','Biomarker': 'Biomarker','Value': 'Value','Status': 'Status','Clinical': 'Clinical','Insight': 'Insight','Recommentation': 'Recommendation','Longe‑timal': 'Longitudinal','Longe-Term': 'Long-Term','Insigh ts': 'Insights','A.I.': 'AI','Immune': 'Immune',# Fix units'gdL': 'g/dL','mgl/dL': 'mg/dL','mg/mL': 'mg/L','ui/l': 'U/L','kc/ml': 'K/uL','fl': 'fL',# Clean up weird unicode characters'': '','': '','‑': '-','–': '-','—': '-','│': '|','├─': '|','•': '-','‐': '-',}for old, new in replacements.items():text = text.replace(old, new)# Clean up extra whitespace and duplicate asteriskstext = re.sub(r'\*{3,}', '**', text)text = re.sub(r'\n{3,}', '\n\n', text)text = re.sub(r' {2,}', ' ', text)text = text.strip()return text116 unmodified lines# Clean up the outputoutput_text = clean_output(output_text)# Improved validation with flexible matchingrequired_sections = {"Executive Summary": ["Executive Summary", "Executive Sum", "Executive Sum"],"System-Specific Analysis": ["System-Specific Analysis", "System‑Specific", "Sys‐tem‑Specific"],"Personalized Action Plan": ["Personalized Action Plan", "Personelized Action", "Action Plan"],"Interaction Alerts": ["Interaction Alerts", "Interaction Alerts"],"Tabular Mapping": ["Tabular Mapping", "Tabular Mapping", "| Bioma"],"Enhanced AI Insights": ["Enhanced AI Insights", "Enhanced A.I. Insigh", "Longe‑ti"]}missing_sections = []for section, variants in required_sections.items():if not any(variant in output_text for variant in variants):missing_sections.append(section)# Only warn if truly incomplete (very short OR missing most sections)if len(output_text) < 500 or len(missing_sections) >= 4:warning = "\n\n⚠️ **Warning: Incomplete Output**\n\n"warning += f"Generated: {len(output_text)} characters | Missing: {', '.join(missing_sections) if missing_sections else 'None'}\n\n"warning += "**Suggestions:**\n"warning += "- Try regenerating with different parameter values\n"warning += "- Consider using a larger model (Llama-3-8B, Mistral-7B)\n"warning += "- Or use API-based models for guaranteed quality\n"output_text += warningreturn output_text
app.py+77-19