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 (2)

Collapse
 
voncartergriffen profile image
Von Carter Griffen

Love how you turned Docker nightmares into Cloud Run therapy; 91% accuracy is solid, though now I'm suspicious of every Linear Regression demo I've ever seen.

Collapse
 
austin_deyan_6c9b2445aed6 profile image
Austin Deyan

Haha, 'Cloud Run therapy' is the perfect way to put it! ๐Ÿ˜‚ It definitely saved me some gray hairs after wrestling with those containers. And yeah, Linear Regression is a great baseline, but Gradient Boosting is where the real magic happens for messy real-world data. Thanks for checking it out!