Sha256: bc2867ed3f6a70058af28725136e896ad4f670bc13c626a1da4346a33106eed5

Contents?: true

Size: 1.03 KB

Versions: 13

Compression:

Stored size: 1.03 KB

Contents

module EasyML
  class PredictionsController < ApplicationController
    skip_before_action :verify_authenticity_token, only: [:create]

    def create
      unless params.key?(:input)
        return render json: { error: "Must provide key: input" }, status: :not_found
      end
      input = params[:input].permit!.to_h

      unless input.is_a?(Hash)
        return render json: { error: "Input must be a hash" }, status: :not_found
      end

      model_name = params[:model]
      unless EasyML::Model.find_by(name: model_name).present?
        return render json: { error: "Model not found" }, status: :not_found
      end

      prediction = EasyML::Predict.predict(model_name, input)

      render json: { prediction: EasyML::PredictionSerializer.new(prediction).serializable_hash.dig(:data, :attributes) }, status: :ok
    rescue ActiveRecord::RecordNotFound
      render json: { error: "Model not found" }, status: :not_found
    rescue StandardError => e
      render json: { error: e.message }, status: :unprocessable_entity
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
easy_ml-0.2.0.pre.rc52 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc51 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc50 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc49 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc48 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc47 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc46 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc45 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc44 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc43 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc41 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc40 app/controllers/easy_ml/predictions_controller.rb
easy_ml-0.2.0.pre.rc39 app/controllers/easy_ml/predictions_controller.rb