feat(ee): Add transcription support for audio messages (#11670)
<img width="419" alt="Screenshot 2025-06-03 at 4 25 37 PM" src="https://github.com/user-attachments/assets/4b6ddd11-9b91-4981-a571-83746cc4d40b" /> Fixes https://github.com/chatwoot/chatwoot/issues/10182 --------- Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -49,10 +49,24 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob
|
||||
|
||||
def message_content(message)
|
||||
return message.content if message.content.present?
|
||||
return 'User has shared a message without content' unless message.attachments.any?
|
||||
|
||||
return 'User has shared an attachment' if message.attachments.any?
|
||||
audio_transcriptions = extract_audio_transcriptions(message.attachments)
|
||||
return audio_transcriptions if audio_transcriptions.present?
|
||||
|
||||
'User has shared a message without content'
|
||||
'User has shared an attachment'
|
||||
end
|
||||
|
||||
def extract_audio_transcriptions(attachments)
|
||||
audio_attachments = attachments.where(file_type: :audio)
|
||||
return '' if audio_attachments.blank?
|
||||
|
||||
transcriptions = ''
|
||||
audio_attachments.each do |attachment|
|
||||
result = Messages::AudioTranscriptionService.new(attachment).perform
|
||||
transcriptions += result[:transcriptions] if result[:success]
|
||||
end
|
||||
transcriptions
|
||||
end
|
||||
|
||||
def determine_role(message)
|
||||
|
||||
13
enterprise/app/jobs/messages/audio_transcription_job.rb
Normal file
13
enterprise/app/jobs/messages/audio_transcription_job.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class Messages::AudioTranscriptionJob < ApplicationJob
|
||||
queue_as :low
|
||||
|
||||
def perform(attachment_id)
|
||||
attachment = Attachment.find_by(id: attachment_id)
|
||||
return if attachment.blank?
|
||||
|
||||
Messages::AudioTranscriptionService.new(attachment).perform
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "Error in AudioTranscriptionJob: #{e.message}"
|
||||
ChatwootExceptionTracker.new(e).capture_exception
|
||||
end
|
||||
end
|
||||
15
enterprise/app/models/enterprise/concerns/attachment.rb
Normal file
15
enterprise/app/models/enterprise/concerns/attachment.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module Enterprise::Concerns::Attachment
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
after_create_commit :enqueue_audio_transcription
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def enqueue_audio_transcription
|
||||
return unless file_type.to_sym == :audio
|
||||
|
||||
Messages::AudioTranscriptionJob.perform_later(id)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,67 @@
|
||||
class Messages::AudioTranscriptionService < Llm::BaseOpenAiService
|
||||
attr_reader :attachment, :message, :account
|
||||
|
||||
def initialize(attachment)
|
||||
super()
|
||||
@attachment = attachment
|
||||
@message = attachment.message
|
||||
@account = message.account
|
||||
end
|
||||
|
||||
def perform
|
||||
return { error: 'Transcription limit exceeded' } unless can_transcribe?
|
||||
return { error: 'Message not found' } if message.blank?
|
||||
|
||||
begin
|
||||
transcriptions = transcribe_audio
|
||||
Rails.logger.info "Audio transcription successful: #{transcriptions}"
|
||||
{ success: true, transcriptions: transcriptions }
|
||||
rescue StandardError => e
|
||||
ChatwootExceptionTracker.new(e).capture_exception
|
||||
Rails.logger.error "Audio transcription failed: #{e.message}"
|
||||
{ error: "Transcription failed: #{e.message}" }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def can_transcribe?
|
||||
account.audio_transcriptions.present? && account.usage_limits[:captain][:responses][:current_available].positive?
|
||||
end
|
||||
|
||||
def fetch_audio_file
|
||||
temp_dir = Rails.root.join('tmp/uploads')
|
||||
FileUtils.mkdir_p(temp_dir)
|
||||
temp_file_path = File.join(temp_dir, attachment.file.filename.to_s)
|
||||
File.write(temp_file_path, attachment.file.download, mode: 'wb')
|
||||
temp_file_path
|
||||
end
|
||||
|
||||
def transcribe_audio
|
||||
transcribed_text = attachment.meta&.[]('transcribed_text') || ''
|
||||
return transcribed_text if transcribed_text.present?
|
||||
|
||||
temp_file_path = fetch_audio_file
|
||||
|
||||
response = @client.audio.transcribe(
|
||||
parameters: {
|
||||
model: 'whisper-1',
|
||||
file: File.open(temp_file_path),
|
||||
temperature: 0.4
|
||||
}
|
||||
)
|
||||
|
||||
FileUtils.rm_f(temp_file_path)
|
||||
|
||||
update_transcription(response['text'])
|
||||
response['text']
|
||||
end
|
||||
|
||||
def update_transcription(transcribed_text)
|
||||
return if transcribed_text.blank?
|
||||
|
||||
attachment.update!(meta: { transcribed_text: transcribed_text })
|
||||
message.reload.send_update_event
|
||||
message.account.increment_response_usage
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user