feat: Sentiment model download and upload to vendor (#7526)

This commit is contained in:
Tejaswini Chile
2023-07-18 12:17:50 +05:30
committed by GitHub
parent 3a77e672f8
commit 5b480f563d
3 changed files with 55 additions and 3 deletions

View File

@@ -27,8 +27,11 @@ class Enterprise::SentimentAnalysisJob < ApplicationJob
# Model initializes OnnxRuntime::Model, with given file for inference session and to create the tensor # Model initializes OnnxRuntime::Model, with given file for inference session and to create the tensor
def model def model
model_path = ENV.fetch('SENTIMENT_FILE_PATH', nil) model = save_and_open_sentiment_file
Informers::SentimentAnalysis.new(model_path) if model_path.present?
return if File.empty?(model)
Informers::SentimentAnalysis.new(model)
end end
def label_val(sentiment) def label_val(sentiment)
@@ -38,4 +41,18 @@ class Enterprise::SentimentAnalysisJob < ApplicationJob
def valid_incoming_message?(message) def valid_incoming_message?(message)
!message.incoming? || message.private? !message.incoming? || message.private?
end 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 end

View File

@@ -6,7 +6,7 @@ RSpec.describe Enterprise::SentimentAnalysisJob do
let(:message) { build(:message, content_type: nil, account: account) } let(:message) { build(:message, content_type: nil, account: account) }
context 'when update the message sentiments' do 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 } let(:model) { double }
before do before do
@@ -50,6 +50,41 @@ RSpec.describe Enterprise::SentimentAnalysisJob do
end end
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 context 'when does not update the message sentiments' do
it 'with outgoing message' do it 'with outgoing message' do
message.update(message_type: :outgoing) message.update(message_type: :outgoing)

BIN
vendor/db/sentiment-analysis.onnx vendored Normal file

Binary file not shown.