DEV Community

Cover image for 🌾 How I Built & Deployed a Crop Yield Prediction API in the Cloud
Austin Deyan
Austin Deyan

Posted on

🌾 How I Built & Deployed a Crop Yield Prediction API in the Cloud

Hey devs! 👋

I just wrapped up a super interesting project and wanted to share the entire journey—wins, fails, and everything in between.

What I Built

An AI-powered crop yield prediction system that:

  • Predicts harvest yields with 91% accuracy
  • Serves predictions via REST API
  • Runs on Google Cloud Run
  • Has a beautiful web UI

The Stack

Backend:  Python + Flask + Scikit-learn
DevOps:   Docker + Google Cloud Run
Frontend: Vanilla JS + HTML/CSS (keeping it simple!)
ML:       Gradient Boosting Regressor
Enter fullscreen mode Exit fullscreen mode

The Journey (Story Time! 📖)

Week 1: Data Exploration Hell 😅

Started with messy agricultural data. Spent days just cleaning and understanding it. Pro tip: ALWAYS look at your data distributions first!

Week 2: Model Selection Drama 🤖

Trained 7 models. Linear Regression? Terrible. Decision Trees? Overfitting. Random Forest? Better but slow. Gradient Boosting? Chef's kiss 👌

Here's the comparison:

Model               | R² Score | MAE
--------------------|----------|--------
Gradient Boosting   | 0.913    | 0.31
Random Forest       | 0.895    | 0.35
Linear Regression   | 0.623    | 0.89
Enter fullscreen mode Exit fullscreen mode

Week 3: Docker Nightmares 🐳

"Works on my machine" → Real problem.

Issue #1: Model files not loading in container
Solution: Load model at module level, not in if __name__ == '__main__'

Issue #2: CORS blocking requests
Solution: pip install flask-cors saved my life

Week 4: Cloud Deployment Victory! ☁️

Google Cloud Run = Amazing for ML models

  • Serverless (scales to zero!)
  • Easy Docker deployment
  • Built-in HTTPS
  • Pay per request

Code Snippets

Here's the prediction endpoint (simplified):

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()

    # Preprocessing
    input_df = pd.DataFrame([data])
    input_encoded = pd.get_dummies(input_df)
    input_scaled = scaler.transform(input_encoded)

    # Prediction
    prediction = model.predict(input_scaled)[0]

    return jsonify({
        'predicted_yield': round(prediction, 2),
        'unit': 'tons_per_hectare'
    })
Enter fullscreen mode Exit fullscreen mode

Biggest Learnings

  1. Data > Models: Feature engineering mattered more than model selection
  2. Deployment is Hard: Spend time on DevOps early
  3. UI Matters: Built a simple HTML interface—users loved it
  4. Documentation: Write it as you code, not after!

Try It Yourself

🔗 More info on Medium

What's Next?

Planning to add:

  • [ ] Time-series forecasting
  • [ ] Weather API integration
  • [ ] Mobile app
  • [ ] Model retraining pipeline

Questions?

Drop them in the comments! Happy to discuss anything about ML deployment, Docker, or agricultural AI! 👇

Top comments (0)