diff --git a/enterprise/app/jobs/enterprise/sentiment_analysis_job.rb b/enterprise/app/jobs/enterprise/sentiment_analysis_job.rb index 08d345810..382584bc8 100644 --- a/enterprise/app/jobs/enterprise/sentiment_analysis_job.rb +++ b/enterprise/app/jobs/enterprise/sentiment_analysis_job.rb @@ -27,8 +27,11 @@ class Enterprise::SentimentAnalysisJob < ApplicationJob # Model initializes OnnxRuntime::Model, with given file for inference session and to create the tensor def model - model_path = ENV.fetch('SENTIMENT_FILE_PATH', nil) - Informers::SentimentAnalysis.new(model_path) if model_path.present? + model = save_and_open_sentiment_file + + return if File.empty?(model) + + Informers::SentimentAnalysis.new(model) end def label_val(sentiment) @@ -38,4 +41,18 @@ class Enterprise::SentimentAnalysisJob < ApplicationJob def valid_incoming_message?(message) !message.incoming? || message.private? end + + # returns the sentiment file from vendor folder else download it to the path from AWS-S3 + def save_and_open_sentiment_file + model_path = ENV.fetch('SENTIMENT_FILE_PATH', nil) + + sentiment_file = Rails.root.join('vendor/db/sentiment-analysis.onnx') + + return sentiment_file if File.exist?(sentiment_file) + + source_file = Down.download(model_path) # Download file from AWS-S3 + File.rename(source_file, sentiment_file) # Change the file path + + sentiment_file + end end diff --git a/spec/enterprise/jobs/enterprise/sentiment_analysis_job_spec.rb b/spec/enterprise/jobs/enterprise/sentiment_analysis_job_spec.rb index d56f1bef5..bb2a89395 100644 --- a/spec/enterprise/jobs/enterprise/sentiment_analysis_job_spec.rb +++ b/spec/enterprise/jobs/enterprise/sentiment_analysis_job_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Enterprise::SentimentAnalysisJob do let(:message) { build(:message, content_type: nil, account: account) } context 'when update the message sentiments' do - let(:model_path) { 'sentiment-analysis.onnx' } + let(:model_path) { Rails.root.join('vendor/db/sentiment-analysis.onnx') } let(:model) { double } before do @@ -50,6 +50,41 @@ RSpec.describe Enterprise::SentimentAnalysisJob do end end + context 'with download sentiment files' do + let(:model_path) { nil } + let(:model) { double } + + before do + allow(Informers::SentimentAnalysis).to receive(:new).with(model_path).and_return(model) + allow(model).to receive(:predict).and_return({ label: 'positive', score: '0.6' }) + end + + it 'fetch saved file in the server' do + with_modified_env SENTIMENT_FILE_PATH: 'sentiment-analysis.onnx' do + message.update(message_type: :incoming, content: 'I did not like your product') + + described_class.new(message).save_and_open_sentiment_file + + sentiment_file = Rails.root.join('vendor/db/sentiment-analysis.onnx') + expect(File).to exist(sentiment_file) + end + end + + it 'fetch file from the storage' do + with_modified_env SENTIMENT_FILE_PATH: 'sentiment-analysis.onnx' do + message.update(message_type: :incoming, content: 'I did not like your product') + allow(File).to receive(:exist?).and_return(false) + allow(Down).to receive(:download).and_return('./sentiment-analysis.onnx') + allow(File).to receive(:rename).and_return(100) + + described_class.new(message).save_and_open_sentiment_file + + sentiment_file = Rails.root.join('vendor/db/sentiment-analysis.onnx') + expect(sentiment_file).to be_present + end + end + end + context 'when does not update the message sentiments' do it 'with outgoing message' do message.update(message_type: :outgoing) diff --git a/vendor/db/sentiment-analysis.onnx b/vendor/db/sentiment-analysis.onnx new file mode 100644 index 000000000..1cb262354 Binary files /dev/null and b/vendor/db/sentiment-analysis.onnx differ