Spaces:
Sleeping
Sleeping
Upload tool
Browse files- app.py +5 -0
- requirements.txt +1 -0
- tool.py +105 -0
app.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import PediatricDosageCalculatorTool
|
| 3 |
+
|
| 4 |
+
tool = PediatricDosageCalculatorTool()
|
| 5 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
class PediatricDosageCalculatorTool(Tool):
|
| 6 |
+
name = "pediatric_dosage_calculator"
|
| 7 |
+
description = """
|
| 8 |
+
This tool calculates pediatric medication dosages based on weight and age.
|
| 9 |
+
It provides dosage recommendations for common medications used in children.
|
| 10 |
+
For educational purposes only - always consult healthcare professionals for actual medical decisions.
|
| 11 |
+
"""
|
| 12 |
+
inputs = {'medication': {'type': 'string', 'description': 'Name of the medication (supported: acetaminophen, ibuprofen, amoxicillin)'}, 'weight_kg': {'type': 'number', 'description': "Child's weight in kilograms"}, 'age_years': {'type': 'integer', 'description': "Child's age in years"}}
|
| 13 |
+
output_type = "string"
|
| 14 |
+
|
| 15 |
+
def forward(self, medication: str, weight_kg: float, age_years: int) -> str:
|
| 16 |
+
# All imports must be inside the method for hub sharing
|
| 17 |
+
import math
|
| 18 |
+
|
| 19 |
+
# Validate inputs
|
| 20 |
+
if weight_kg <= 0 or age_years <= 0:
|
| 21 |
+
return "Error: Weight and age must be positive values."
|
| 22 |
+
|
| 23 |
+
if age_years > 18:
|
| 24 |
+
return "Error: This tool is designed for pediatric patients (18 years and under)."
|
| 25 |
+
|
| 26 |
+
medication = medication.lower().strip()
|
| 27 |
+
|
| 28 |
+
# Dosage calculations based on standard pediatric guidelines
|
| 29 |
+
dosage_guidelines = {
|
| 30 |
+
"acetaminophen": {
|
| 31 |
+
"mg_per_kg": [10, 15], # 10-15 mg/kg every 4-6 hours
|
| 32 |
+
"max_single_dose": 650, # mg
|
| 33 |
+
"max_daily_dose": 75, # mg/kg/day
|
| 34 |
+
"frequency": "every 4-6 hours",
|
| 35 |
+
},
|
| 36 |
+
"ibuprofen": {
|
| 37 |
+
"mg_per_kg": [5, 10], # 5-10 mg/kg every 6-8 hours
|
| 38 |
+
"max_single_dose": 400, # mg
|
| 39 |
+
"max_daily_dose": 40, # mg/kg/day
|
| 40 |
+
"frequency": "every 6-8 hours",
|
| 41 |
+
},
|
| 42 |
+
"amoxicillin": {
|
| 43 |
+
"mg_per_kg": [20, 40], # 20-40 mg/kg/day divided into 2-3 doses
|
| 44 |
+
"max_single_dose": 500, # mg
|
| 45 |
+
"max_daily_dose": 3000, # mg absolute max
|
| 46 |
+
"frequency": "twice daily (every 12 hours)",
|
| 47 |
+
},
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
if medication not in dosage_guidelines:
|
| 51 |
+
available_meds = ", ".join(dosage_guidelines.keys())
|
| 52 |
+
return f"Error: Medication '{medication}' not supported. Available medications: {available_meds}"
|
| 53 |
+
|
| 54 |
+
guideline = dosage_guidelines[medication]
|
| 55 |
+
|
| 56 |
+
# Calculate dosage (using middle of the range)
|
| 57 |
+
if medication == "acetaminophen":
|
| 58 |
+
dose_mg = weight_kg * 12.5 # Middle of 10-15 mg/kg range
|
| 59 |
+
elif medication == "ibuprofen":
|
| 60 |
+
dose_mg = weight_kg * 7.5 # Middle of 5-10 mg/kg range
|
| 61 |
+
elif medication == "amoxicillin":
|
| 62 |
+
daily_dose = weight_kg * 30 # Middle of 20-40 mg/kg range
|
| 63 |
+
dose_mg = daily_dose / 2 # Divided into 2 doses per day
|
| 64 |
+
|
| 65 |
+
# Apply maximum limits
|
| 66 |
+
dose_mg = min(dose_mg, guideline["max_single_dose"])
|
| 67 |
+
|
| 68 |
+
# Round to nearest 5mg for practical dosing
|
| 69 |
+
dose_mg = round(dose_mg / 5) * 5
|
| 70 |
+
|
| 71 |
+
# Safety checks
|
| 72 |
+
daily_dose_check = dose_mg * (
|
| 73 |
+
24 / 6
|
| 74 |
+
if medication == "acetaminophen"
|
| 75 |
+
else 24 / 8 if medication == "ibuprofen" else 2
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
max_daily = (
|
| 79 |
+
guideline["max_daily_dose"]
|
| 80 |
+
if medication == "amoxicillin"
|
| 81 |
+
else weight_kg * guideline["max_daily_dose"]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if daily_dose_check > max_daily:
|
| 85 |
+
return f"Warning: Calculated dose exceeds safe daily limits for {medication}. Please consult a healthcare provider."
|
| 86 |
+
|
| 87 |
+
# Format response
|
| 88 |
+
response = f"""
|
| 89 |
+
Pediatric Dosage Calculation for {medication.title()}:
|
| 90 |
+
- Patient: {age_years} years old, {weight_kg} kg
|
| 91 |
+
- Recommended dose: {dose_mg} mg {guideline['frequency']}
|
| 92 |
+
- Calculation: {weight_kg} kg × dosing guideline
|
| 93 |
+
|
| 94 |
+
⚠️ IMPORTANT DISCLAIMERS:
|
| 95 |
+
- This is for educational purposes only
|
| 96 |
+
- Always verify with current medical references
|
| 97 |
+
- Consult healthcare providers for actual prescriptions
|
| 98 |
+
- Consider patient-specific factors (allergies, conditions, etc.)
|
| 99 |
+
- Dosing may vary based on indication and severity
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
return response.strip()
|
| 103 |
+
|
| 104 |
+
def __init__(self, *args, **kwargs):
|
| 105 |
+
self.is_initialized = False
|