A production-ready AI-powered nutrition and diet planning application that provides personalized meal recommendations using Google's Gemini AI. Built with FastAPI, Streamlit, and LangChain.
- Personalized Diet Plans: AI-generated meal plans based on individual health goals, preferences, and restrictions
- Interactive Chat Interface: Natural conversation flow for collecting user information
- Multiple Deployment Options: Both API and web interface available
- Session Management: Persistent conversations with memory across interactions
- Production Ready: Dockerized with proper error handling, logging, and security middleware
- Health Monitoring: Built-in health check endpoints for monitoring
- FastAPI API (
api.py): Production-ready REST API with session management - Streamlit App (
app.py): User-friendly web interface - AI Agent (
tool.py): LangChain-based conversational AI with structured prompting - Docker Support: Containerized deployment with optimized Python runtime
- Backend: FastAPI, Uvicorn, Gunicorn
- AI/ML: LangChain, Google Gemini AI, ConversationBufferMemory
- Frontend: Streamlit
- Containerization: Docker
- Data Validation: Pydantic
- Environment: Python 3.11
- Python 3.11+
- Google Gemini API Key (Get one here)
- Docker (optional, for containerized deployment)
- Clone the repository
git clone https://github.com/Kartavya-AI/Dietitian-Agent/
cd Dietitian-Agent- Install dependencies
pip install -r requirements.txt- Set up environment variables
# Create .env file
echo "GEMINI_API_KEY=your_gemini_api_key_here" > .env
# OR
echo "GOOGLE_API_KEY=your_gemini_api_key_here" > .envstreamlit run app.py- Access at:
http://localhost:8501 - Enter your Gemini API key in the sidebar
- Start chatting with the AI Dietitian
# Development
uvicorn api:app --reload --port 8080
# Production
gunicorn --bind 0.0.0.0:8080 --workers 2 --worker-class uvicorn.workers.UvicornWorker api:app- API Documentation:
http://localhost:8080/docs - Health Check:
http://localhost:8080/health
# Build the image
docker build -t ai-dietitian .
# Run the container
docker run -p 8080:8080 -e GEMINI_API_KEY=your_api_key_here ai-dietitianhttp://localhost:8080
GET /healthReturns server health status and uptime information.
POST /init-session
Content-Type: application/json
{
"session_id": "user123"
}POST /chat
Content-Type: application/json
{
"message": "I want to lose weight",
"session_id": "user123"
}DELETE /clear-session/{session_id}GET /sessionsimport requests
# Initialize session
response = requests.post("http://localhost:8080/init-session",
json={"session_id": "user123"})
# Chat with AI
response = requests.post("http://localhost:8080/chat",
json={
"message": "I want to gain muscle mass",
"session_id": "user123"
})
print(response.json()["response"])The AI follows a structured approach to create personalized diet plans:
-
Initial Greeting: Welcomes user and acknowledges their goal
-
Information Gathering: Asks specific questions one by one:
- Age, gender, height, weight
- Activity level
- Health goals
- Food allergies/dislikes
- Dietary preferences
- Meal frequency
- Medical conditions
-
Plan Generation: Creates a comprehensive diet plan including:
- Executive Summary
- Caloric & Macronutrient Goals
- 7-Day Sample Meal Plan
- Hydration Guidelines
- General Advice
- Medical Disclaimer
- Each session maintains conversation history
- Context is preserved across multiple interactions
- Session-based isolation for multiple users
| Variable | Description | Required |
|---|---|---|
GEMINI_API_KEY |
Google Gemini API key | Yes |
GOOGLE_API_KEY |
Alternative to GEMINI_API_KEY | Yes (if GEMINI_API_KEY not set) |
PORT |
Server port (default: 8080) | No |
- Visit Google AI Studio
- Create a new API key
- Copy the key (starts with 'AI')
- Set it in your environment variables
- Python 3.11 slim base image
- SQLite3 support for potential database needs
- Non-root user execution for security
- Optimized for production with Gunicorn + Uvicorn
- Multi-worker setup for better performance
# Build
docker build -t ai-dietitian .
# Run with environment file
docker run -p 8080:8080 --env-file .env ai-dietitian
# Run with inline environment
docker run -p 8080:8080 -e GEMINI_API_KEY=your_key ai-dietitianai-dietitian/
βββ api.py # FastAPI application
βββ app.py # Streamlit web interface
βββ tool.py # AI agent and prompt configuration
βββ requirements.txt # Python dependencies
βββ Dockerfile # Container configuration
βββ .env # Environment variables (create this)
βββ README.md # This file
- Input validation with Pydantic models
- Session ID sanitization
- CORS middleware configuration
- Trusted host middleware
- Non-root Docker user
- API key validation
- Error handling without information leakage
This application is for informational purposes only and should not replace professional medical advice. Users with medical conditions should consult healthcare providers before following any diet recommendations.
- Never commit API keys to version control
- Use environment variables for production
- Rotate keys regularly
- Monitor API usage and costs
-
Streamlit App:
- Start the app and enter API key
- Go through the complete conversation flow
- Verify diet plan generation
-
API Endpoints:
- Test health endpoint:
curl http://localhost:8080/health - Test chat flow with session management
- Verify error handling with invalid inputs
- Test health endpoint:
# Using curl for basic testing
curl -X POST "http://localhost:8080/chat" \
-H "Content-Type: application/json" \
-d '{"message": "Hello", "session_id": "test123"}'- Environment: Use production WSGI server (Gunicorn)
- Reverse Proxy: Nginx for static files and SSL termination
- Monitoring: Health checks and logging
- Scaling: Multiple worker processes
- Security: Proper CORS and host configuration
# Production environment variables
GEMINI_API_KEY=your_production_key
PORT=8080
PYTHONUNBUFFERED=1
PYTHONDONTWRITEBYTECODE=1- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions:
- Check the API documentation at
/docs - Review the health endpoint at
/health - Check logs for detailed error information
- Ensure API key is valid and has sufficient quota