A comprehensive Flask-based WhatsApp chatbot specifically designed for farmers and agricultural communities. The bot integrates with Google's Gemini AI to provide intelligent agricultural advice, crop disease identification, market information, and government scheme details. Built with conversation memory and multilingual support to serve farmers effectively.
- 🌾 Agricultural Intelligence: Specialized AI responses for farming queries, crop management, and agricultural best practices
- 🔍 Crop Disease Detection: Image analysis for plant disease identification and treatment recommendations
- 📊 Market Price Information: Real-time crop price data and market trends
- 🏛️ Government Schemes: Information about agricultural subsidies, schemes, and farmer benefits
- 🗣️ Voice Message Support: Audio message processing with automatic transcription
- 🌐 Multilingual Support: Translation services for regional language support
- 📚 Knowledge Base: Comprehensive agricultural knowledge database
- 💬 Conversation Memory: Maintains chat history for personalized farming advice
- 📱 WhatsApp Integration: Seamless integration with WhatsApp Business API
- 🔒 Secure Communication: HMAC signature verification for secure webhooks
- 🎯 Response Validation: Quality assurance for AI-generated agricultural advice
KisanAI/
├── app/
│ ├── __init__.py # Flask app factory
│ ├── config.py # Configuration management
│ ├── views.py # Webhook endpoints
│ ├── decorators/
│ │ └── security.py # Security decorators
│ ├── services/
│ │ ├── gemini_service.py # Gemini AI integration for agricultural queries
│ │ ├── conversation_service.py # Conversation history management
│ │ ├── knowledge_base_service.py # Agricultural knowledge database
│ │ ├── prompt_manager.py # Specialized agricultural prompts
│ │ ├── response_validator.py # Quality validation for farming advice
│ │ ├── translation_service.py # Multilingual support
│ │ ├── speech_service.py # Speech-to-text processing
│ │ └── openai_service.py # Alternative AI service
│ └── utils/
│ └── whatsapp_utils.py # WhatsApp utilities and message processing
├── data/
│ ├── crop_diseases.json # Crop disease database
│ ├── government_schemes.json # Agricultural schemes and subsidies
│ ├── kisan_knowledge_base.json # Comprehensive farming knowledge
│ └── market_prices.json # Market price information
├── start/ # Quickstart examples and testing tools
├── debug_whatsapp.py # API troubleshooting tool
├── run.py # Application entry point
├── requirements.txt # Dependencies
├── .env.sample # Environment template
├── .gitignore # Git ignore rules
└── README.md # This file
- Python 3.7+
- WhatsApp Business API account
- Google Gemini API key (or OpenAI API key)
- ngrok (for local development)
- ffmpeg (for audio processing) - Download here
-
Clone the repository
git clone https://github.com/mandarwagh9/KisanAI.git cd KisanAI/AI
-
Install dependencies
pip install -r requirements.txt
-
Environment Configuration Copy the sample environment file and configure it:
cp .env.sample .env
Edit the
.env
file with your credentials:# WhatsApp Business API ACCESS_TOKEN=your_whatsapp_access_token PHONE_NUMBER_ID=your_phone_number_id VERIFY_TOKEN=your_verify_token VERSION=v18.0 # AI Configuration GEMINI_API_KEY=your_gemini_api_key GEMINI_MODEL=gemini-pro GEMINI_VISION_MODEL=gemini-pro-vision # Optional: OpenAI Configuration OPENAI_API_KEY=your_openai_api_key OPENAI_ASSISTANT_ID=your_assistant_id # Application Settings FLASK_ENV=development DEBUG=True
-
Run the application
python run.py
-
Test your setup Use the debugging tool to verify your WhatsApp API configuration:
python debug_whatsapp.py
- Go to Meta for Developers
- Create a new app for WhatsApp Business
- Set webhook URL:
https://your-domain.com/webhook
- Set verify token (same as in your
.env
file) - Subscribe to
messages
webhook field
- Access Token: From WhatsApp Business API settings
- Phone Number ID: From WhatsApp Business API settings
- App Secret: From App Settings > Basic
The bot uses Google's Gemini AI with specialized agricultural prompts and knowledge base:
- Get API key from Google AI Studio
- Add
GEMINI_API_KEY
to your.env
file - The system uses specialized prompts for:
- Crop disease identification
- Agricultural best practices
- Market price queries
- Government scheme information
- Weather-related farming advice
The bot includes comprehensive data for Indian agriculture:
- Crop Diseases: 500+ disease entries with symptoms and treatments
- Government Schemes: Latest agricultural subsidies and farmer benefits
- Market Prices: Real-time crop pricing information
- Farming Knowledge: Best practices, seasonal advice, and cultivation tips
To use OpenAI instead:
- Uncomment the OpenAI import in
whatsapp_utils.py
- Comment out the Gemini import
- Set up OpenAI credentials in
.env
- Note: Agricultural specialization may require additional prompt engineering
The bot supports audio message processing with automatic format conversion:
-
Google Cloud Speech (recommended for best quality):
- Enable Google Cloud Speech-to-Text API
- Set up service account credentials
- Supports OGG, WAV, FLAC formats directly
-
SpeechRecognition (free tier fallback):
- Uses Google's free speech recognition service
- Automatically converts audio formats using ffmpeg
-
Audio Format Support:
- OGG (WhatsApp's default format)
- WAV, FLAC, MP3
- Automatic conversion for unsupported formats
Use the built-in debugging script to test your WhatsApp API setup:
python debug_whatsapp.py
This tool will:
- ✅ Test API connectivity and permissions
- 🔑 Validate access tokens and scopes
- 📱 Verify phone number configuration
- 📤 Send test messages (optional)
- 🔍 Provide detailed error diagnostics
- 401 Unauthorized: Token expired or invalid - generate new token
- Missing scopes: Ensure token has
whatsapp_business_messaging
permission - Audio processing errors: Install ffmpeg for audio format conversion
- Rate limiting: Implement proper request throttling
- Crop Advisory: Specialized advice for different crops and farming stages
- Disease Diagnosis: AI-powered plant disease identification from images
- Weather Integration: Weather-aware farming recommendations
- Seasonal Guidance: Timely advice based on agricultural seasons
- Soil Management: Soil health and fertilizer recommendations
- Price Tracking: Real-time market prices for various crops
- Market Trends: Historical price analysis and forecasting
- Best Selling Times: Optimal timing for crop sales
- Regional Variations: Location-specific market information
- Scheme Discovery: Find relevant government schemes for farmers
- Eligibility Checking: Determine qualification for various programs
- Application Guidance: Step-by-step application assistance
- Subsidy Information: Details about agricultural subsidies
- Stores farming history and preferences per user
- Maintains context across multiple conversations
- Personalized recommendations based on farmer's profile
- Tracks crop cycles and provides timely reminders
- Translation Services: Automatic translation for regional languages
- Voice Processing: Speech-to-text in multiple Indian languages
- Cultural Context: Culturally appropriate farming advice
- Local Terminology: Uses region-specific agricultural terms
- Crop Health Assessment: Visual analysis of plant conditions
- Disease Detection: Automated identification of plant diseases
- Nutrient Deficiency: Detection of nutritional deficiencies in crops
- Growth Stage Identification: Determining crop maturity levels
- Response Validation: Ensures agricultural advice accuracy
- Knowledge Base Verification: Cross-references with agricultural databases
- Expert Review Integration: Option for expert validation of critical advice
- Continuous Learning: Improves responses based on farmer feedback
Webhook verification endpoint for WhatsApp Business API
Receives incoming WhatsApp messages and processes them
# Install ngrok
# Run your Flask app
python run.py
# In another terminal, expose local server
ngrok http 8000
# Use the ngrok URL as your webhook URL in Meta Developer Console
The bot includes specialized quickstart examples:
gemini_quickstart.py
- Test agricultural AI integrationwhatsapp_quickstart.py
- Test WhatsApp APIassistants_quickstart.py
- Test OpenAI Assistant
Test agricultural features:
# Test crop disease detection
python -c "from app.services.gemini_service import generate_response_with_image; print('Disease detection ready')"
# Test knowledge base
python -c "from app.services.knowledge_base_service import search_knowledge_base; print('Knowledge base loaded')"
# Test translation service
python -c "from app.services.translation_service import translation_service; print('Translation service ready')"
Use the debugging tool for comprehensive API testing:
python debug_whatsapp.py
- Use environment variables for all sensitive data
- Enable HTTPS for webhook endpoints
- Configure proper logging and monitoring for farming queries
- Set up error tracking and alerting for critical agricultural advice
- Use a production WSGI server (e.g., Gunicorn)
- Implement rate limiting for API calls
- Set up proper audio and image file cleanup
- Regular updates to agricultural knowledge base
- Backup conversation histories and farmer data
- Implement data privacy measures for farmer information
# Using Gunicorn for production
gunicorn -w 4 -b 0.0.0.0:8000 run:app
# Using Docker (create Dockerfile)
docker build -t kisanai-whatsapp .
docker run -p 8000:8000 kisanai-whatsapp
# For high availability with agricultural data
gunicorn -w 4 -b 0.0.0.0:8000 --timeout 120 run:app
- Crop Disease Identification: "My tomato plants have yellow spots. What's wrong?"
- Market Price Queries: "What's the current price of wheat in Maharashtra?"
- Government Scheme Information: "Are there any subsidies for drip irrigation?"
- Weather-Based Advice: "Should I plant rice this week given the weather?"
- Pest Management: "How do I control aphids in my cotton crop?"
- Quick Reference: Access to comprehensive agricultural database
- Visual Diagnosis: Image-based crop problem identification
- Scheme Updates: Latest information on government programs
- Multi-language Support: Communicate with farmers in local languages
- Market Intelligence: Real-time crop price monitoring
- Farmer Engagement: Direct communication channel with farming community
- Product Recommendations: Context-aware agricultural product suggestions
- Data Collection: Insights into farming challenges and needs
- Hindi (हिंदी)
- Marathi (मराठी)
- Gujarati (ગુજરાતી)
- Punjabi (ਪੰਜਾਬੀ)
- Bengali (বাংলা)
- Tamil (தமிழ்)
- Telugu (తెలుగు)
- Kannada (ಕನ್ನಡ)
- Malayalam (മലയാളം)
- English
- Crop Varieties: 200+ crop varieties with cultivation details
- Disease Database: 500+ diseases with symptoms and treatments
- Pest Information: Common agricultural pests and control measures
- Government Schemes: Updated database of central and state schemes
- Market Data: Integration with agricultural market platforms
- Weather Integration: Connection with meteorological services
- State-specific crop recommendations
- Regional market price variations
- Local government scheme availability
- Climate-zone appropriate advice
- Traditional farming practice integration
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support:
- Create an issue on GitHub
- Check the WhatsApp Business API documentation
- Review Google Gemini AI documentation
- Google for the Gemini AI API and agricultural AI capabilities
- Meta for the WhatsApp Business API
- Flask community for the excellent framework
- Indian Council of Agricultural Research (ICAR) for agricultural data
- State Agricultural Universities for regional knowledge
- Farmer communities for feedback and real-world testing
- Agricultural extension workers for domain expertise
- OpenAI for alternative AI integration options
Note: This agricultural AI assistant is designed to support farmers with information and advice. Always consult with local agricultural experts for critical farming decisions. The bot aims to democratize access to agricultural knowledge while respecting traditional farming wisdom and local practices.
- Add the WhatsApp Number: Save the bot's WhatsApp number to your contacts
- Send a Message: Start with "Hi" or "नमस्ते" to begin conversation
- Ask Questions: Use simple language to ask about farming topics
- Send Images: Take photos of crops, diseases, or pests for analysis
- Voice Messages: Speak in your preferred language for voice queries
Crop Disease Inquiry:
Farmer: "My tomato plants have brown spots on leaves"
KisanAI: "This appears to be early blight disease. Here's what you can do:
🍅 Remove affected leaves immediately
💧 Reduce overhead watering
🌿 Apply copper-based fungicide
📅 Spray every 7-10 days until symptoms disappear"
Market Price Query:
Farmer: "wheat price today"
KisanAI: "Current wheat prices (MSP 2024-25):
🌾 Common Wheat: ₹2,275/quintal
🌾 Durum Wheat: ₹2,425/quintal
📍 Nearest mandi: Kharif Market, 15km
📈 Price trend: Stable (+2% from last month)"
Government Scheme Information:
Farmer: "subsidy for solar pump"
KisanAI: "Available solar pump subsidies:
☀️ PM-KUSUM Scheme: 60% subsidy
📋 Eligibility: All farmers with agricultural land
💰 Max subsidy: ₹4.8 lakh for 10 HP pump
📝 Apply at: Nearest agriculture office
📞 Helpline: 1800-180-1551"
Watch our project demonstration: KisanAI Demo