A Flask-based API that uses OpenAI's models to provide plant diagnosis and chat functionality.
- Plant Diagnosis: Upload plant images for AI-powered health diagnosis
- Chat Support: Ask questions about plant care and get AI responses
- Cost-Effective: Uses the most affordable OpenAI models for each use case
- Multiple Model Support: Automatic fallback between models for reliability
-
Install Dependencies
pip install -r requirements.txt
-
Set up Environment Variables
- Copy
.env.exampleto.env - Add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here - Copy
-
Run the Application
python main.py
The API will be available at http://localhost:5000
Upload a plant image for AI-powered diagnosis.
Request:
- Method: POST
- Content-Type: multipart/form-data
- Fields:
image(required): Plant image file (PNG, JPG, JPEG, GIF, BMP, WEBP)additional_info(optional): Additional context about the plantlabel(optional): User-provided plant/crop name
Response:
{
"success": true,
"message": "Plant diagnosis completed successfully",
"diagnosis": {
"name": "Tomato",
"status": "nutrient_deficient",
"confidence": 85,
"problem": "Yellowing leaves on lower branches with brown edges",
"cause": "Nitrogen deficiency due to poor soil nutrition",
"treatment": "Apply balanced fertilizer every 2 weeks and ensure proper watering",
"prevention": "Maintain consistent watering schedule and soil pH between 6.0-6.8"
},
"model_used": "gpt-4o-mini",
"additional_info": "User provided context",
"user_label": "Tomato Plant"
}Ask questions about plant care and get AI responses.
Request:
- Method: POST
- Content-Type: application/json
- Body:
{
"message": "How often should I water my succulents?",
"conversation_history": [
{"role": "user", "content": "Previous question"},
{"role": "assistant", "content": "Previous answer"}
]
}Response:
{
"success": true,
"message": "Chat response generated successfully",
"response": "AI response to your question",
"conversation_history": [
{"role": "user", "content": "Your question"},
{"role": "assistant", "content": "AI response"}
],
"model_used": "gpt-3.5-turbo"
}The API uses different OpenAI models optimized for cost and performance:
- Diagnosis:
gpt-4o-mini(most affordable vision model) - Chat:
gpt-3.5-turbo(most affordable for conversations)
Fallback models are automatically used if the primary models fail.
The diagnosis endpoint returns health status with these possible values:
healthy: Plant appears to be in good conditionunhealthy: Plant shows signs of poor healthdiseased: Plant has visible disease symptomspest_infested: Plant is affected by pestsnutrient_deficient: Plant shows nutrient deficiency signsstressed: Plant is under stress (environmental, water, etc.)unknown: Unable to determine health status
The API includes comprehensive error handling for:
- Invalid file types
- Missing API keys
- OpenAI API errors
- File upload issues
- Malformed requests
curl -X POST http://localhost:5000/plant/diagnose \
-F "image=@path/to/plant_image.jpg" \
-F "label=Tomato Plant" \
-F "additional_info=My plant has been wilting for 3 days"curl -X POST http://localhost:5000/chat \
-H "Content-Type: application/json" \
-d '{"message": "What are the signs of overwatering in houseplants?"}'The project follows a modular structure:
services/openai_client.py: OpenAI integration and model managementroutes/diagnose/: Plant diagnosis endpointroutes/chat/: Chat endpointmain.py: Flask application setup
This project is for educational and development purposes.