nutrition-api / app.py
ayeshaqamar's picture
Create app.py
3b0bbe6 verified
import gradio as gr
import joblib
import numpy as np
import pandas as pd
# Load model and label classes
pipeline = joblib.load("nutrition_pipeline.joblib")
le_plan_classes = np.load("le_plan_classes.npy", allow_pickle=True)
# Meal suggestions
meal_ideas = {
'High iron diet + calcium': ['Spinach lentils', 'Fortified cereals', 'Dried apricots'],
'Iron-rich mashed foods': ['Mashed peas + beef', 'Sweet potato + iron drops'],
'Leafy greens + iron': ['Kale soup', 'Iron-fortified breads'],
'Low GI carbs + protein': ['Oats + eggs', 'Barley porridge', 'Greek yogurt'],
'Fiber-rich baby cereals': ['Oats with banana', 'Barley with milk'],
'Balanced, no added sugars': ['Brown rice', 'Vegetable stew'],
'Iodine + selenium rich': ['Iodized salt meals', 'Brazil nuts, eggs'],
'Iodized foods + fish': ['Fish puree', 'Iodized cereal mash'],
'Moderate iodine intake': ['Seafood weekly', 'Cooked greens'],
'Balanced diet + hydration': ['Fruits, grains, milk', 'Soup and veggies'],
'Gentle solids + breastmilk': ['Rice mash', 'Carrot puree'],
'Regular diet, nutrient dense': ['Family meals with fruits', 'Whole grains, milk'],
'General balanced diet': ['Seasonal veggies + rice', 'Multigrain porridge']
}
# Prediction function
def predict(age, region, stage, health):
try:
input_df = pd.DataFrame([[region, stage, health]], columns=['region', 'breastfeeding_stage', 'health_condition'])
encoded_input = pd.DataFrame({
'age': [age],
'region': pipeline.named_steps['region'].encoder.transform(input_df['region']),
'stage': pipeline.named_steps['stage'].encoder.transform(input_df['breastfeeding_stage']),
'health': pipeline.named_steps['health'].encoder.transform(input_df['health_condition']),
})
prediction = pipeline.named_steps['classifier'].predict(encoded_input)[0]
plan = le_plan_classes[prediction]
meals = meal_ideas.get(plan, ['Mixed vegetables', 'Simple lentils'])
return f"Recommended Plan: {plan}", meals
except Exception as e:
return f"Error: {str(e)}", []
# Gradio interface
demo = gr.Interface(
fn=predict,
inputs=[
gr.Slider(20, 45, value=30, label="Age"),
gr.Dropdown(['South Asia', 'Africa', 'Europe', 'Middle East'], label="Region"),
gr.Dropdown(['Lactation', 'Weaning', 'Extended'], label="Breastfeeding Stage"),
gr.Dropdown(['Anemia', 'Diabetes', 'Thyroid', 'None'], label="Health Condition")
],
outputs=[
gr.Textbox(label="Nutrition Plan"),
gr.List(label="Meal Ideas")
],
title="Meal Plan Recommender for Nursing Mothers",
description="Get a customized meal plan based on age, region, stage, and health condition."
)
demo.launch()