Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Load model and label classes
|
| 7 |
+
pipeline = joblib.load("nutrition_pipeline.joblib")
|
| 8 |
+
le_plan_classes = np.load("le_plan_classes.npy", allow_pickle=True)
|
| 9 |
+
|
| 10 |
+
# Meal suggestions
|
| 11 |
+
meal_ideas = {
|
| 12 |
+
'High iron diet + calcium': ['Spinach lentils', 'Fortified cereals', 'Dried apricots'],
|
| 13 |
+
'Iron-rich mashed foods': ['Mashed peas + beef', 'Sweet potato + iron drops'],
|
| 14 |
+
'Leafy greens + iron': ['Kale soup', 'Iron-fortified breads'],
|
| 15 |
+
'Low GI carbs + protein': ['Oats + eggs', 'Barley porridge', 'Greek yogurt'],
|
| 16 |
+
'Fiber-rich baby cereals': ['Oats with banana', 'Barley with milk'],
|
| 17 |
+
'Balanced, no added sugars': ['Brown rice', 'Vegetable stew'],
|
| 18 |
+
'Iodine + selenium rich': ['Iodized salt meals', 'Brazil nuts, eggs'],
|
| 19 |
+
'Iodized foods + fish': ['Fish puree', 'Iodized cereal mash'],
|
| 20 |
+
'Moderate iodine intake': ['Seafood weekly', 'Cooked greens'],
|
| 21 |
+
'Balanced diet + hydration': ['Fruits, grains, milk', 'Soup and veggies'],
|
| 22 |
+
'Gentle solids + breastmilk': ['Rice mash', 'Carrot puree'],
|
| 23 |
+
'Regular diet, nutrient dense': ['Family meals with fruits', 'Whole grains, milk'],
|
| 24 |
+
'General balanced diet': ['Seasonal veggies + rice', 'Multigrain porridge']
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Prediction function
|
| 28 |
+
def predict(age, region, stage, health):
|
| 29 |
+
try:
|
| 30 |
+
input_df = pd.DataFrame([[region, stage, health]], columns=['region', 'breastfeeding_stage', 'health_condition'])
|
| 31 |
+
encoded_input = pd.DataFrame({
|
| 32 |
+
'age': [age],
|
| 33 |
+
'region': pipeline.named_steps['region'].encoder.transform(input_df['region']),
|
| 34 |
+
'stage': pipeline.named_steps['stage'].encoder.transform(input_df['breastfeeding_stage']),
|
| 35 |
+
'health': pipeline.named_steps['health'].encoder.transform(input_df['health_condition']),
|
| 36 |
+
})
|
| 37 |
+
prediction = pipeline.named_steps['classifier'].predict(encoded_input)[0]
|
| 38 |
+
plan = le_plan_classes[prediction]
|
| 39 |
+
meals = meal_ideas.get(plan, ['Mixed vegetables', 'Simple lentils'])
|
| 40 |
+
return f"Recommended Plan: {plan}", meals
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error: {str(e)}", []
|
| 43 |
+
|
| 44 |
+
# Gradio interface
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=predict,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Slider(20, 45, value=30, label="Age"),
|
| 49 |
+
gr.Dropdown(['South Asia', 'Africa', 'Europe', 'Middle East'], label="Region"),
|
| 50 |
+
gr.Dropdown(['Lactation', 'Weaning', 'Extended'], label="Breastfeeding Stage"),
|
| 51 |
+
gr.Dropdown(['Anemia', 'Diabetes', 'Thyroid', 'None'], label="Health Condition")
|
| 52 |
+
],
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Textbox(label="Nutrition Plan"),
|
| 55 |
+
gr.List(label="Meal Ideas")
|
| 56 |
+
],
|
| 57 |
+
title="Meal Plan Recommender for Nursing Mothers",
|
| 58 |
+
description="Get a customized meal plan based on age, region, stage, and health condition."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|