import streamlit as st import google.generativeai as genai from PIL import Image import os from dotenv import load_dotenv import PyPDF2 import io from datetime import datetime import pandas as pd from collections import defaultdict import re # Page configuration st.set_page_config( page_title="Cornea AI Pentacam Analyzer", page_icon="👁️", layout="wide", initial_sidebar_state="expanded" ) # Load environment variables load_dotenv() # Configure Gemini API genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) model = genai.GenerativeModel("gemini-2.0-flash-exp") # Custom CSS st.markdown(""" """, unsafe_allow_html=True) # System prompts CORNEA_ANALYSIS_PROMPT = """You are an expert ophthalmologist specializing in corneal diseases. Analyze these Pentacam scans and patient data with focus on: 1. Corneal Parameters Analysis: • Thickness mapping and progression • Topographic changes • Elevation data (anterior and posterior) • Keratoconus indices and classification 2. Disease Assessment: • ABCD Keratoconus staging • Fuchs Endothelial Corneal Dystrophy evaluation • Subclinical corneal edema (Sun criteria) • Risk assessment 3. Clinical Interpretation: • Pattern recognition • Disease progression markers • Treatment implications Please provide a detailed clinical assessment.""" PROGRESSION_ANALYSIS_PROMPT = """Analyze the progression of corneal parameters across multiple timepoints, focusing on: 1. Temporal Changes: • Progressive changes in corneal thickness • Evolution of topographic patterns • Changes in elevation maps • Progression of keratoconus indices 2. Rate of Progression: • Quantify changes between timepoints • Identify acceleration or stabilization periods • Compare with expected disease progression 3. Risk Assessment: • Current status evaluation • Future progression risk • Treatment recommendations 4. Timeline Analysis: • Key changes between each timepoint • Overall progression pattern • Critical periods of change Please provide a comprehensive progression analysis with clinical recommendations.""" def extract_patient_data(uploaded_file): """Extract and process patient data from uploaded file""" patient_data = {} if uploaded_file.type == "application/pdf": pdf_reader = PyPDF2.PdfReader(uploaded_file) text = "" for page in pdf_reader.pages: text += page.extract_text() patient_data['raw_text'] = text else: # Handle other file types if needed patient_data['raw_text'] = "File type not supported for detailed extraction" return patient_data def analyze_timepoint(images, date, patient_data=None): """Analyze a single timepoint""" prompt = f"{CORNEA_ANALYSIS_PROMPT}\n\nTimepoint: {date}\n" if patient_data: prompt += f"\nPatient Information:\n{patient_data}\n" prompt += "\nPlease analyze these corneal scans:" content = [prompt] + images response = model.generate_content(content) return response.text def analyze_progression(timepoints_data): """Analyze progression across multiple timepoints""" prompt = f"{PROGRESSION_ANALYSIS_PROMPT}\n\n" prompt += "Timepoints for analysis:\n" # Add all timepoints to the prompt all_images = [] for date, images in timepoints_data.items(): prompt += f"\n- {date}:" all_images.extend(images) prompt += "\n\nPlease analyze the progression across these timepoints:" content = [prompt] + all_images response = model.generate_content(content) return response.text def extract_date_from_filename(filename): """Extract date from filename using common patterns""" # Common date patterns (add more patterns if needed) patterns = [ r'(\d{4}[-_/]\d{2}[-_/]\d{2})', # YYYY-MM-DD, YYYY_MM_DD r'(\d{2}[-_/]\d{2}[-_/]\d{4})', # DD-MM-YYYY, DD_MM_YYYY r'(\d{8})', # YYYYMMDD ] for pattern in patterns: match = re.search(pattern, filename) if match: date_str = match.group(1) try: # Try different date formats for fmt in ['%Y-%m-%d', '%Y_%m_%d', '%d-%m-%Y', '%d_%m_%Y', '%Y%m%d']: try: return datetime.strptime(date_str.replace('/', '-'), fmt).strftime('%Y-%m-%d') except ValueError: continue except ValueError: continue return None def organize_scans_by_date(files): """Organize uploaded files by their dates""" organized_files = defaultdict(list) unorganized_files = [] for file in files: date = extract_date_from_filename(file.name) if date: organized_files[date].append(file) else: unorganized_files.append(file) return organized_files, unorganized_files def main(): # Header st.markdown("""
Advanced Corneal Analysis & Diagnostics
Developed by Dr. Verónica Gómez Calleja
Cornea Specialist
This advanced tool assists in the analysis of Pentacam scans and corneal conditions using state-of-the-art AI technology. It provides comprehensive analysis of corneal parameters and supports clinical decision-making in keratoconus, FECD, and other corneal conditions.
Note: This tool is for assistance only and should not replace professional medical judgment.