|
|
|
|
|
|
|
import gradio as gr |
|
import pandas as pd |
|
import numpy as np |
|
from sklearn.preprocessing import StandardScaler |
|
import json |
|
from datetime import datetime, timedelta |
|
|
|
class SimplePrecisionMedicineSystem: |
|
def __init__(self): |
|
self.scaler = StandardScaler() |
|
|
|
def calculate_risk_score(self, genetic_score, lifestyle_score, age, symptom_severity): |
|
"""Calculate overall risk score""" |
|
try: |
|
|
|
age_normalized = age / 100.0 |
|
|
|
|
|
weights = { |
|
'genetic': 0.4, |
|
'lifestyle': 0.3, |
|
'age': 0.2, |
|
'symptoms': 0.1 |
|
} |
|
|
|
|
|
risk_score = ( |
|
genetic_score * weights['genetic'] + |
|
(1 - lifestyle_score) * weights['lifestyle'] + |
|
age_normalized * weights['age'] + |
|
(symptom_severity / 10) * weights['symptoms'] |
|
) |
|
|
|
return round(risk_score, 2) |
|
except Exception as e: |
|
print(f"Error in risk calculation: {str(e)}") |
|
return 0.5 |
|
|
|
def get_treatment_recommendation(self, risk_score, current_medication, symptom_severity): |
|
"""Generate treatment recommendations based on risk score and symptoms""" |
|
try: |
|
base_recommendations = [] |
|
|
|
|
|
if risk_score < 0.3: |
|
base_recommendations.append("Low risk profile - Continue monitoring") |
|
elif risk_score < 0.6: |
|
base_recommendations.append("Moderate risk - Regular check-ups recommended") |
|
else: |
|
base_recommendations.append("High risk - Intensive monitoring required") |
|
|
|
|
|
if symptom_severity <= 3: |
|
base_recommendations.append("Mild symptoms - Maintain current treatment") |
|
elif symptom_severity <= 7: |
|
base_recommendations.append("Moderate symptoms - Consider treatment adjustment") |
|
else: |
|
base_recommendations.append("Severe symptoms - Immediate medical review required") |
|
|
|
|
|
if current_medication.lower().strip() in ['none', 'no', '']: |
|
if risk_score > 0.5: |
|
base_recommendations.append("Consider initiating preventive treatment") |
|
else: |
|
base_recommendations.append("Review current medications at next appointment") |
|
|
|
return base_recommendations |
|
except Exception as e: |
|
print(f"Error in treatment recommendation: {str(e)}") |
|
return ["Unable to generate recommendations due to error"] |
|
|
|
def generate_report(self, patient_data): |
|
"""Generate patient report""" |
|
try: |
|
|
|
risk_score = self.calculate_risk_score( |
|
patient_data['genetic_score'], |
|
patient_data['lifestyle_score'], |
|
patient_data['age'], |
|
patient_data['symptom_severity'] |
|
) |
|
|
|
|
|
recommendations = self.get_treatment_recommendation( |
|
risk_score, |
|
patient_data['current_medication'], |
|
patient_data['symptom_severity'] |
|
) |
|
|
|
|
|
report = { |
|
"Patient Information": { |
|
"Age": patient_data['age'], |
|
"Gender": patient_data['gender'], |
|
"Current Medications": patient_data['current_medication'] |
|
}, |
|
"Risk Assessment": { |
|
"Overall Risk Score": risk_score, |
|
"Risk Category": "High" if risk_score > 0.6 else "Moderate" if risk_score > 0.3 else "Low", |
|
"Genetic Risk": patient_data['genetic_score'], |
|
"Lifestyle Score": patient_data['lifestyle_score'] |
|
}, |
|
"Current Status": { |
|
"Symptom Severity": f"{patient_data['symptom_severity']}/10", |
|
}, |
|
"Recommendations": recommendations, |
|
"Next Steps": { |
|
"Follow-up": "Within 1 week" if risk_score > 0.6 else "Within 1 month" if risk_score > 0.3 else "Within 3 months", |
|
"Monitoring": "Daily" if patient_data['symptom_severity'] > 7 else "Weekly" if patient_data['symptom_severity'] > 4 else "Monthly" |
|
}, |
|
"Report Generated": datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
} |
|
|
|
return json.dumps(report, indent=2) |
|
except Exception as e: |
|
print(f"Error in report generation: {str(e)}") |
|
return json.dumps({"error": "Failed to generate report", "message": str(e)}) |
|
|
|
def create_gradio_interface(): |
|
"""Create Gradio interface""" |
|
system = SimplePrecisionMedicineSystem() |
|
|
|
def process_patient(age, gender, genetic_score, lifestyle_score, current_medication, symptom_severity): |
|
try: |
|
|
|
patient_data = { |
|
'age': age, |
|
'gender': gender, |
|
'genetic_score': genetic_score, |
|
'lifestyle_score': lifestyle_score, |
|
'current_medication': current_medication, |
|
'symptom_severity': symptom_severity |
|
} |
|
|
|
|
|
return system.generate_report(patient_data) |
|
|
|
except Exception as e: |
|
return json.dumps({ |
|
"error": "An error occurred while processing the patient data", |
|
"details": str(e) |
|
}, indent=2) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_patient, |
|
inputs=[ |
|
gr.Number(label="Age", minimum=0, maximum=120), |
|
gr.Dropdown(choices=["Male", "Female", "Other"], label="Gender"), |
|
gr.Slider(minimum=0, maximum=1, step=0.1, label="Genetic Risk Score (0-1)"), |
|
gr.Slider(minimum=0, maximum=1, step=0.1, label="Lifestyle Score (0-1)"), |
|
gr.Textbox(label="Current Medications (comma-separated or 'None')"), |
|
gr.Slider(minimum=0, maximum=10, step=1, label="Symptom Severity (0-10)") |
|
], |
|
outputs=gr.JSON(label="Medical Report"), |
|
title="Precision Medicine System", |
|
description="Enter patient information to generate personalized medical recommendations", |
|
examples=[ |
|
[45, "Female", 0.6, 0.7, "Metformin 500mg, Lisinopril 10mg", 4], |
|
[72, "Male", 0.8, 0.4, "Warfarin 5mg, Atorvastatin 40mg", 7], |
|
[28, "Other", 0.2, 0.9, "None", 2] |
|
] |
|
) |
|
return iface |
|
|
|
|
|
if __name__ == "__main__": |
|
interface = create_gradio_interface() |
|
interface.launch() |