mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-28 17:52:39 +00:00
feat: Sentiment model download and upload to vendor (#7526)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
BIN
vendor/db/sentiment-analysis.onnx
vendored
Normal file
BIN
vendor/db/sentiment-analysis.onnx
vendored
Normal file
Binary file not shown.
Reference in New Issue
Block a user