loomio/loomio

View on GitHub
lib/analyzers/transcription_analyzer.rb

Summary

Maintainability
A
0 mins
Test Coverage
# https://www.modern-rails.com/posts/activestorage-analyzers-and-the-openai-transcription-api/
class TranscriptionAnalyzer < ActiveStorage::Analyzer::AudioAnalyzer
  def metadata
    super.merge(text: @text, language: @language).compact
  end

  private

  def probe_from(file)
    super.tap do
      response = TranscriptionService.transcribe(file)
      @text = response["text"]
      @language = response["language"]
      record = blob.attachments.first.record
      # record.body += "<p>#{I18n.t('record_modal.audio_transcript', text: @text, locale: record.author.locale)}</p>"
      record.body += "<p>#{@text}</p>"
      record.save!
      MessageChannelService.publish_models(Array(record), group_id: record.group_id, user_id: record.author_id)
    end
  end
end