ruby-on-rails 操作作业似乎找不到操作存储附件,错误为:错误::ENOENT(No such file or directory @ rb_sysopen

zpqajqem  于 2023-05-19  发布在  Ruby
关注(0)|答案(1)|浏览(164)

我有一个Rails 7.0.4应用程序,其中有一个简单的TranscriptionController,一旦提交了包含标题、描述和audio_file的表单,就会启动一个Action Job,使用openai gem处理音频转录。我一直得到这个错误,尽管产生的URL指向我想要转码的audio_file:Errno::ENOENT (No such file or directory @ rb_sysopen
下面是转录模型:

class Transcription < ApplicationRecord
    has_one_attached :audio_file

    def audio_file_url
        Rails.application.routes.url_helpers.url_for(audio_file) if audio_file.attached?
    end 
end

下面是TranscriptionController中的create操作:

def create
    @transcription = Transcription.new(transcription_params)

    respond_to do |format|
      if @transcription.save
        # Process audio file using OpenAI's Whisper API
        TranscriptionJob.perform_later(@transcription)

        format.html { redirect_to transcription_url(@transcription), notice: "Transcription was successfully created. Check back later for transcription text." }
        format.json { render :show, status: :created, location: @transcription }
        
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @transcription.errors, status: :unprocessable_entity }
      end
    end
  end

这里是最重要的部分,称为transcription_job.rb的活动作业:

class TranscriptionJob < ApplicationJob
  queue_as :default
  require 'openai'

  def perform(transcription)
    # Do something later
    client = OpenAI::Client.new(access_token:  Rails.application.credentials.dig(:openai, :api_key))
    logger.info "SMC DEBUG: OpenAI Key set"

    file_path = transcription.audio_file_url

    logger.info "SMC DEBUG: Audio file path is: #{file_path}"
    
    response = client.transcribe(
        parameters: {
            model: "whisper-1",
            file: File.open(file_path, "rb")
        })
    logger.info "SMC DEBUG: transcription sent"
    # Update the transcription object with the returned text
    transcription.transcriptionresult = response['text']
    logger.info "SMC DEBUG: TranscriptionResult is: #{transcription.transcriptionresult}"

        # Save the updated transcription object
    transcription.save
  end
end

下面是来自Action Job的Rails日志的一些相关反馈:

15:21:58 web.1  | [ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 7b3600ec-ee85-4be6-8faa-18f467fc719a) to Async(default) with arguments: #<GlobalID:0x00000001073c6880 @uri=#<URI::GID gid://transcriptionservice/ActiveStorage::Blob/59>>
15:21:58 web.1  | [ActiveJob] Enqueued TranscriptionJob (Job ID: 974566f7-7d83-48ee-b3ae-d6f02a006efb) to Async(default) with arguments: #<GlobalID:0x00000001073d7ba8 @uri=#<URI::GID gid://transcriptionservice/Transcription/61>>
15:21:58 web.1  | Redirected to http://localhost:3000/transcriptions/61
15:21:58 web.1  | Completed 302 Found in 60ms (ActiveRecord: 13.1ms | Allocations: 24625)
15:21:58 web.1  | 
15:21:58 web.1  | 
15:21:58 web.1  | Started GET "/transcriptions/61" for ::1 at 2023-05-14 15:21:58 -0400
15:21:58 web.1  | [ActiveJob] [ActiveStorage::AnalyzeJob] [7b3600ec-ee85-4be6-8faa-18f467fc719a]   ActiveStorage::Blob Load (2.2ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 59], ["LIMIT", 1]]
15:21:58 web.1  | [ActiveJob] [ActiveStorage::AnalyzeJob] [7b3600ec-ee85-4be6-8faa-18f467fc719a] Performing ActiveStorage::AnalyzeJob (Job ID: 7b3600ec-ee85-4be6-8faa-18f467fc719a) from Async(default) enqueued at 2023-05-14T19:21:58Z with arguments: #<GlobalID:0x0000000107426ac8 @uri=#<URI::GID gid://transcriptionservice/ActiveStorage::Blob/59>>
15:21:58 web.1  | [ActiveJob] [ActiveStorage::AnalyzeJob] [7b3600ec-ee85-4be6-8faa-18f467fc719a]   Disk Storage (0.6ms) Downloaded file from key: 49zqmrx6g54pk1pzuc3y2uyvtiyx
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   Transcription Load (0.3ms)  SELECT "transcriptions".* FROM "transcriptions" WHERE "transcriptions"."id" = $1 LIMIT $2  [["id", 61], ["LIMIT", 1]]
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb] Performing TranscriptionJob (Job ID: 974566f7-7d83-48ee-b3ae-d6f02a006efb) from Async(default) enqueued at 2023-05-14T19:21:58Z with arguments: #<GlobalID:0x000000010743ca80 @uri=#<URI::GID gid://transcriptionservice/Transcription/61>>
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb] SMC DEBUG: OpenAI Key set
15:21:58 web.1  | Processing by TranscriptionsController#show as TURBO_STREAM
15:21:58 web.1  |   Parameters: {"id"=>"61"}
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   ActiveStorage::Attachment Load (7.0ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 61], ["record_type", "Transcription"], ["name", "audio_file"], ["LIMIT", 1]]
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   ↳ app/models/transcription.rb:5:in `audio_file_url'
15:21:58 web.1  |   Transcription Load (1.7ms)  SELECT "transcriptions".* FROM "transcriptions" WHERE "transcriptions"."id" = $1 LIMIT $2  [["id", 61], ["LIMIT", 1]]
15:21:58 web.1  |   ↳ app/controllers/transcriptions_controller.rb:83:in `set_transcription'
15:21:58 web.1  |   Rendering layout layouts/application.html.erb
15:21:58 web.1  |   Rendering transcriptions/show.html.erb within layouts/application
15:21:58 web.1  |   Rendered transcriptions/_transcription.html.erb (Duration: 0.0ms | Allocations: 25)
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   ActiveStorage::Blob Load (1.4ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 59], ["LIMIT", 1]]
15:21:58 web.1  |   Rendered transcriptions/show.html.erb within layouts/application (Duration: 1.1ms | Allocations: 843)
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   ↳ app/models/transcription.rb:5:in `audio_file_url'
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb] SMC DEBUG: Audio file path is: http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBRQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--495ff1b4e7c2001a5ca50886bfd85e2ad6847c7b/test_audio.mp3
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb] Error performing TranscriptionJob (Job ID: 974566f7-7d83-48ee-b3ae-d6f02a006efb) from Async(default) in 25.03ms: Errno::ENOENT (No such file or directory @ rb_sysopen - http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBRQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--495ff1b4e7c2001a5ca50886bfd85e2ad6847c7b/test_audio.mp3):

当我尝试将该URL放入浏览器时,它会下载正确的文件。
我尝试使用以下内容来引用路径而不是URL:

file_path = Rails.application.routes.url_helpers.rails_blob_path(transcription.audio_file, only_path: true)

但这给了我一个类似的错误,指向路径而不是url:

15:38:35 web.1  | [ActiveJob] [TranscriptionJob] [854d3393-ed0b-41b7-8f5f-7d6469feb8a1] Error performing TranscriptionJob (Job ID: 854d3393-ed0b-41b7-8f5f-7d6469feb8a1) from Async(default) in 134.93ms: Errno::ENOENT (No such file or directory @ rb_sysopen - /rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBRUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f6270379699bf876802dd28de4cb4414cc7f96ba/test_audio.mp3):

我不知道如何解决这个问题。我很想从智囊团那里找到答案。

ogsagwnx

ogsagwnx1#

感谢@Chiperific向我指出这个边缘指南:https://edgeguides.rubyonrails.org/active_storage_overview.html#downloading-files这帮助我重写了我的代码如下.关键是将文件下载到系统上的临时文件位置,然后在openai API中指向该本地路径。

class TranscriptionJob < ApplicationJob
  queue_as :default
  require 'openai'

  def perform(transcription)
    # Provide the openai API key and initialize an instance of the openai client
    client = OpenAI::Client.new(access_token:  Rails.application.credentials.dig(:openai, :api_key))

    # Initialize the transcriptionresult variable
    transcription.transcriptionresult = ''
    
    # It is necessary to download the file to temporary storage location first and then submit with the openai API.

    transcription.audio_file.open do |file|
      # I created a folder in the app root /tmp called “transcribe”
      system '/tmp/transcribe', file.path
      
      response = client.transcribe(
        parameters: {
            model: "whisper-1",
            file: File.open(file.path, "rb")
        })

      # Make the transcriptionresult equal to the response['text'] produced by the openai transcription.
      transcription.transcriptionresult = response['text']
      
      # Save the updated transcription object
      transcription.save
    end
  end
end

相关问题