import streamlit as st from PIL import Image import os import base64 import io from dotenv import load_dotenv from groq import Groq from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as ReportLabImage from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.units import inch from datetime import datetime import re from reportlab.lib import colors import random import streamlit.components.v1 as components # ====================== # CONFIGURATION SETTINGS # ====================== PAGE_CONFIG = { "page_title": "Radiology Analyzer", "page_icon": "🩺", "layout": "wide", "initial_sidebar_state": "expanded" } ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg'] CSS_STYLES = """ """ # ====================== # CORE FUNCTIONS # ====================== def configure_application(): """Initialize application settings and styling""" st.set_page_config(**PAGE_CONFIG) st.markdown(CSS_STYLES, unsafe_allow_html=True) def initialize_groq_client(): """Create and validate Groq API client""" load_dotenv() api_key = os.getenv("GROQ_API_KEY") if not api_key: st.error("Groq API key not found. Please provide an API key.") return None return Groq(api_key=api_key) def encode_logo(image_path): """Encode logo image to base64""" try: with open(image_path, "rb") as img_file: return base64.b64encode(img_file.read()).decode("utf-8") except FileNotFoundError: # Return a placeholder image (blue medical technology icon) encoded as base64 return "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFLUlEQVR4nO2cfYhUVRTGf+uqlWmpZX6kaZpppX1YWpllEQVFEQVBEVFUpBVF0QdFRlEUVERBRNAfkRVZ9GVFpaXlRwpqlGlqpmZqaZa67q7r6qrr9kf30dxh9u7MnTvz5t6Z+8DLMDvv3nPOuWfOPe++e+6CEEIIIYQQ1cCVwBpgl8/2AnCa7UBjZDjwIbDXR4FUsjbg99zV7AZGpBJoZlwDfK+EeOAHYJTNIPuVkMAca2PghBs/AEcZMpdKSGD20IKJQCuvQSoYLSWjpJRiYzLCHgCeB5YDzTZicYGrgF+V7T10AG+kGcRLwPGWYnGFicA/SmgfeoAZaQ6yFhjmMxzXGAV8q6T24WdggB/DTcD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IlRDjiQ3AAVH2K0oIZTG5hP7ESCWkTzUoFJ3AEVlHYotJWQx0TYwNSoj5w2chKP3DibadnWk7SMepVkKMJ9qBWPocB2QdgG1eL0r7gQxwmBJSXvOeRJaXUcY/sMWExm4lxHhiPtBWRuhryjXeRQkJ1MWfVsb2HVZfpjrFbZ1aAMzJNaMLn3+35LbXwXJZR0IIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYRD/A9AH1PHpL17/AAAAABJRU5ErkJggg==" def process_image_data(uploaded_file): """Convert image to base64 encoded string""" try: image = Image.open(uploaded_file) buffer = io.BytesIO() image.save(buffer, format=image.format) return base64.b64encode(buffer.getvalue()).decode('utf-8'), image.format except Exception as e: st.error(f"Image processing error: {str(e)}") return None, None def generate_pdf_report(report_text, uploaded_file): """Generate professionally formatted PDF report with bold headers.""" buffer = io.BytesIO() doc = SimpleDocTemplate(buffer, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=72) # Create custom styles for different parts of the report styles = getSampleStyleSheet() # Custom styles for better formatting title_style = ParagraphStyle( 'ReportTitle', parent=styles['Title'], fontSize=16, alignment=1, # Center aligned spaceAfter=12 ) header_style = ParagraphStyle( 'SectionHeader', parent=styles['Heading2'], fontSize=12, fontName='Helvetica-Bold', textColor=colors.black, spaceBefore=12, spaceAfter=6 ) normal_style = ParagraphStyle( 'NormalText', parent=styles['BodyText'], fontSize=11, leading=14, spaceAfter=8 ) abnormal_style = ParagraphStyle( 'AbnormalText', parent=styles['BodyText'], fontSize=11, leading=14, textColor=colors.red, backColor=colors.lightgrey, borderPadding=5, spaceAfter=8 ) footer_style = ParagraphStyle( 'FooterText', parent=styles['Italic'], fontSize=9, alignment=1 # Center aligned ) # Begin building the report story = [] # Hospital/Institution Header header_text = "RADIOLOGY DEPARTMENT" header = Paragraph(header_text, title_style) story.append(header) # Report Title report_title = "RADIOLOGICAL EXAMINATION REPORT" title = Paragraph(report_title, title_style) story.append(title) # Add date and report ID date_text = f"Date: {datetime.now().strftime('%B %d, %Y')}" report_id = f"Report ID: RAD-{datetime.now().strftime('%Y%m%d')}-{random.randint(1000, 9999)}" date_para = Paragraph(date_text, normal_style) id_para = Paragraph(report_id, normal_style) story.append(date_para) story.append(id_para) story.append(Spacer(1, 12)) # Add the image to the PDF if uploaded_file: try: uploaded_file.seek(0) # Reset file pointer to beginning pil_image = Image.open(uploaded_file) img_width = 5 * inch aspect = float(pil_image.height) / float(pil_image.width) img_height = img_width * aspect img_temp = io.BytesIO() pil_image.save(img_temp, format=pil_image.format if pil_image.format else 'JPEG') img_temp.seek(0) img = ReportLabImage(img_temp, width=img_width, height=img_height) story.append(img) story.append(Spacer(1, 12)) # Add image caption caption = Paragraph("Figure 1: Radiological Image for Analysis", normal_style) story.append(caption) story.append(Spacer(1, 12)) except Exception as e: error_text = Paragraph(f"Image processing error: {str(e)}", normal_style) story.append(error_text) story.append(Spacer(1, 12)) # Clean the report text (remove markdown-style formatting and unwanted characters) cleaned_text = report_text.replace('**', '').replace('##', '').replace('*', '-') # Define section headers to identify section_headers = [ "DIAGNOSIS", "ETIOLOGY", "RISK FACTORS", "PATHOPHYSIOLOGY", "CLINICAL FEATURES", "SIGNS AND SYMPTOMS", "INVESTIGATIONS", "MANAGEMENT", "INITIAL STABILIZATION", "MEDICAL MANAGEMENT", "SURGICAL MANAGEMENT", "PROGNOSIS" ] # Split into lines for more precise processing lines = cleaned_text.split('\n') current_section = "" section_content = "" for i, line in enumerate(lines): line = line.strip() if not line: continue # Remove any "Step X:" prefixes line = re.sub(r'^Step \d+:\s*', '', line) # Check if this is a section header is_header = False for header in section_headers: if line.upper().startswith(header) or line.upper() == header + ":": is_header = True break # Also check if it's a short line ending with a colon (likely a header) if not is_header and len(line) < 60 and line.endswith(':'): is_header = True # If we found a header if is_header: # First add any accumulated content from previous section if section_content.strip(): # Check for severe abnormalities to highlight severe_abnormal_keywords = [ 'severe', 'critical', 'urgent', 'emergency', 'life-threatening', 'malignant', 'neoplasm', 'carcinoma', 'metastasis', 'hemorrhage', 'fracture', 'rupture', 'perforation', ] has_severe_issue = any(keyword in section_content.lower() for keyword in severe_abnormal_keywords) if current_section.upper().startswith("DIAGNOSIS") or current_section.upper().startswith("ABNORMAL"): # This is a diagnosis section - highlight abnormalities p = Paragraph(section_content, abnormal_style if has_severe_issue else normal_style) else: p = Paragraph(section_content, normal_style) story.append(p) story.append(Spacer(1, 6)) section_content = "" # Add the new section header - remove any trailing colon for cleaner look clean_header = line.strip() if clean_header.endswith(':'): clean_header = clean_header[:-1] current_section = clean_header p = Paragraph(f"{clean_header}", header_style) # Bold the header story.append(p) else: # This is content - append to the current section content if section_content: section_content += "
" + line else: section_content = line # Add any remaining content if section_content.strip(): p = Paragraph(section_content, normal_style) story.append(p) # Add conclusion if not present if not any("PROGNOSIS" in line.upper() for line in lines): conclusion_header = Paragraph("PROGNOSIS", header_style) story.append(conclusion_header) story.append(Spacer(1, 6)) conclusion_text = "Prognosis varies based on the extent and location of findings. Clinical correlation with the patient's symptoms and medical history is recommended." conclusion_para = Paragraph(conclusion_text, normal_style) story.append(conclusion_para) # Add footer with disclaimer story.append(Spacer(1, 24)) disclaimer = "This report was generated with AI assistance and should be reviewed by a qualified healthcare professional." footer = Paragraph(disclaimer, footer_style) story.append(footer) # Build PDF doc.build(story) buffer.seek(0) return buffer def generate_radiology_report_groq(uploaded_file, client): """Generate AI-powered radiology analysis using Groq API""" base64_image, img_format = process_image_data(uploaded_file) if not base64_image: return None image_url = f"data:image/{img_format.lower()};base64,{base64_image}" try: with st.spinner("Analyzing image..."): # Add progress bar for visual feedback progress_bar = st.progress(0) for i in range(100): # Update progress bar progress_bar.progress(i + 1) import time time.sleep(0.025) # Simulate processing time # Updated prompt to request the detailed, structured format response = client.chat.completions.create( model="meta-llama/llama-4-maverick-17b-128e-instruct", # Use Groq's model messages=[{ "role": "user", "content": [ {"type": "text", "text": ( """As a radiologist, analyze the following Medical report and provide a comprehensive report structured as follows: 1. **DIAGNOSIS**: Clearly state the primary diagnosis, including dimensions in mmIf where applicable (e.g., if a tumor is present). Use specific anatomical terms relevant to the body part being examined.If it is chest xray also mention if pneumonia is present or not. 2. **FINDINGS**: - Provide detailed observations from the Medical report, including: - The size, shape, and location of any lesions or abnormalities if applicable. - Description of the surrounding tissues and structures if applicable. - Any noted changes in signal intensity on various sequences (e.g., T1W, T2W, FLAIR) if applicable. - Mention of any associated findings, such as edema, mass effect, or midline shift if applicble. - Specific comments on vascular structures, if applicable. 3. **PATHOPHYSIOLOGY**: Briefly explain the disease mechanism related to the diagnosis, focusing on how it affects the specific body part. 4. **CLINICAL FEATURES**: Provide an overview of typical clinical presentations associated with this diagnosis, emphasizing symptoms that may arise from abnormalities in the specified anatomical area. 5. **SIGNS AND SYMPTOMS**: List common signs and symptoms relevant to the findings in the MRI report. Tailor this section to align with the specific anatomy being assessed. 6. **INVESTIGATIONS**: Mention diagnostic tests typically used for confirmation of the diagnosis, including imaging studies or laboratory tests pertinent to the body part. 7. **MANAGEMENT**: Outline the management plans in three parts: - Initial Stabilization: Describe immediate steps for patient care. - Medical Management: Outline pharmacological treatments and monitoring. - Surgical Management (if applicable): Discuss any surgical interventions specific to the diagnosis and body part. 8. **PROGNOSIS**: Describe expected outcomes and factors that may affect prognosis based on the diagnosis. Include considerations specific to the anatomical region and associated complications. Please ensure to focus on the following findings from the report: - Mention specific abnormalities based on the region (e.g., "T2/FLAIR hyperintensities in the right fronto-parietal region" for brain MRI). - Highlight any significant lesions or deviations from the norm. - Include any other abnormal findings noted in the report that are relevant to the specific anatomy. Format each section with appropriate headings and use bullet points for lists. Base your analysis on the provided MRI report details.""" )}, {"type": "image_url", "image_url": {"url": image_url}}, ] }], temperature=0.1, max_tokens=3000, # Increased token limit for more detailed response top_p=0.3 ) return response.choices[0].message.content except Exception as e: st.error(f"Groq API error: {str(e)}") return None def generate_radiology_report(uploaded_file, api_choice='groq'): client = initialize_groq_client() if client: return generate_radiology_report_groq(uploaded_file, client) else: st.error("Failed to initialize Groq client. Please check your API key.") return None # ====================== # UI COMPONENTS # ====================== def display_animated_logo(): """Display an animated medical logo""" logo_b64 = encode_logo("src/Round_image_depicting_a_futuristic_medical_image_a-1742282117033-photoaidcom-cropped.png") # If logo file doesn't exist, use the placeholder from encode_logo st.markdown( f"""
""", unsafe_allow_html=True ) def display_main_interface(): """Render primary application interface""" # Display animated logo and titles display_animated_logo() st.markdown('

Radiology Analyzer

', unsafe_allow_html=True) st.markdown('

Advanced Medical Imaging Analysis

', unsafe_allow_html=True) # Action buttons if st.session_state.get('analysis_result'): st.markdown( """
Analysis Complete
""", unsafe_allow_html=True ) col1, col2 = st.columns([1, 1]) with col1: pdf_report = generate_pdf_report(st.session_state.analysis_result, st.session_state.uploaded_file) st.download_button( label="📄 Download PDF Report", data=pdf_report, file_name="radiology_report.pdf", mime="application/pdf", use_container_width=True, help="Download formal PDF version of the report" ) with col2: if st.button("Clear Analysis 🗑️", use_container_width=True, help="Remove current results"): st.session_state.pop('analysis_result', None) st.session_state.pop('uploaded_file', None) st.rerun() # Display analysis results in a styled container st.markdown("### 🎯 Radiological Findings Report") st.markdown( f'
{st.session_state.analysis_result}
', unsafe_allow_html=True ) else: # Show a centered placeholder message st.markdown( """

Upload a medical image to begin analysis

""", unsafe_allow_html=True ) def render_sidebar(): """Create sidebar interface elements""" with st.sidebar: st.markdown('', unsafe_allow_html=True) st.markdown('', unsafe_allow_html=True) # Image Upload Section st.markdown('', unsafe_allow_html=True) # ====================== # APPLICATION ENTRYPOINT # ====================== def main(): """Primary application controller""" # Check if dark mode is in session state, default to true if 'dark_mode' not in st.session_state: st.session_state.dark_mode = True configure_application() render_sidebar() display_main_interface() if __name__ == "__main__": main()