Action Job can't seem to find an Action Storage attachment with error: Errno::ENOENT (No such file or directory @ rb_sysopen

huangapple go评论58阅读模式
英文:

Action Job can't seem to find an Action Storage attachment with error: Errno::ENOENT (No such file or directory @ rb_sysopen

问题

It seems like you're encountering an issue with the file path when trying to transcribe audio using the openai gem in your Rails application. The error message "Errno::ENOENT (No such file or directory @ rb_sysopen)" indicates that the system is unable to find the file at the specified path.

In your code, you are using transcription.audio_file_url to obtain the file URL, and then trying to open it using File.open(file_path, "rb"). However, the File.open method expects a local file path, not a URL.

To resolve this issue, you should download the file from the URL and then pass the local path to File.open. Here's a modified version of your TranscriptionJob perform method to achieve this:

class TranscriptionJob < ApplicationJob
  queue_as :default
  require 'openai'
  require 'open-uri'

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

    file_url = transcription.audio_file_url

    begin
      # Download the file from the URL to a temporary location
      temp_file = Tempfile.new(['audio', '.mp3'])
      temp_file.binmode
      open(file_url, 'rb') do |url_file|
        temp_file.write(url_file.read)
      end
      temp_file.rewind

      response = client.transcribe(
        parameters: {
          model: "whisper-1",
          file: temp_file
        })
      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
    ensure
      temp_file.close
      temp_file.unlink
    end
  end
end

This code downloads the file from the URL, saves it to a temporary location, and then opens it using File.open. Make sure to require 'open-uri' at the beginning of your code to use the open method.

This should resolve the "No such file or directory" error and allow you to transcribe the audio correctly.

英文:

I have a Rails 7.0.4 app in which I have a simple TranscriptionController that once a form with the title, description and audio_file are submitted, an Action Job is kicked off to process an audio transcription using the openai gem. I keep getting this error in spite of the URL being produced as pointing to the audio_file I want transcoded:
Errno::ENOENT (No such file or directory @ rb_sysopen

Here is the Transcription model:

    class Transcription &lt; 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

Here is the create action in the TranscriptionController:

def create
    @transcription = Transcription.new(transcription_params)

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

        format.html { redirect_to transcription_url(@transcription), notice: &quot;Transcription was successfully created. Check back later for transcription text.&quot; }
        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

And here is the most important part, the Active Job called transcription_job.rb:

class TranscriptionJob &lt; ApplicationJob
  queue_as :default
  require &#39;openai&#39;

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

    file_path = transcription.audio_file_url

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

        # Save the updated transcription object
    transcription.save
  end
end

Here is some relevant feedback from the Rails logs for the Action Job:

15:21:58 web.1  | [ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 7b3600ec-ee85-4be6-8faa-18f467fc719a) to Async(default) with arguments: #&lt;GlobalID:0x00000001073c6880 @uri=#&lt;URI::GID gid://transcriptionservice/ActiveStorage::Blob/59&gt;&gt;
15:21:58 web.1  | [ActiveJob] Enqueued TranscriptionJob (Job ID: 974566f7-7d83-48ee-b3ae-d6f02a006efb) to Async(default) with arguments: #&lt;GlobalID:0x00000001073d7ba8 @uri=#&lt;URI::GID gid://transcriptionservice/Transcription/61&gt;&gt;
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 &quot;/transcriptions/61&quot; 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 &quot;active_storage_blobs&quot;.* FROM &quot;active_storage_blobs&quot; WHERE &quot;active_storage_blobs&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 59], [&quot;LIMIT&quot;, 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: #&lt;GlobalID:0x0000000107426ac8 @uri=#&lt;URI::GID gid://transcriptionservice/ActiveStorage::Blob/59&gt;&gt;
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 &quot;transcriptions&quot;.* FROM &quot;transcriptions&quot; WHERE &quot;transcriptions&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 61], [&quot;LIMIT&quot;, 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: #&lt;GlobalID:0x000000010743ca80 @uri=#&lt;URI::GID gid://transcriptionservice/Transcription/61&gt;&gt;
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: {&quot;id&quot;=&gt;&quot;61&quot;}
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   ActiveStorage::Attachment Load (7.0ms)  SELECT &quot;active_storage_attachments&quot;.* FROM &quot;active_storage_attachments&quot; WHERE &quot;active_storage_attachments&quot;.&quot;record_id&quot; = $1 AND &quot;active_storage_attachments&quot;.&quot;record_type&quot; = $2 AND &quot;active_storage_attachments&quot;.&quot;name&quot; = $3 LIMIT $4  [[&quot;record_id&quot;, 61], [&quot;record_type&quot;, &quot;Transcription&quot;], [&quot;name&quot;, &quot;audio_file&quot;], [&quot;LIMIT&quot;, 1]]
15:21:58 web.1  | [ActiveJob] [TranscriptionJob] [974566f7-7d83-48ee-b3ae-d6f02a006efb]   ↳ app/models/transcription.rb:5:in `audio_file_url&#39;
15:21:58 web.1  |   Transcription Load (1.7ms)  SELECT &quot;transcriptions&quot;.* FROM &quot;transcriptions&quot; WHERE &quot;transcriptions&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 61], [&quot;LIMIT&quot;, 1]]
15:21:58 web.1  |   ↳ app/controllers/transcriptions_controller.rb:83:in `set_transcription&#39;
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 &quot;active_storage_blobs&quot;.* FROM &quot;active_storage_blobs&quot; WHERE &quot;active_storage_blobs&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 59], [&quot;LIMIT&quot;, 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&#39;
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):

When I try to put that url in my browser it downloads the correct file.

I have tried to use the following to reference the path instead of the URL:

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

But this gives me a similar error pointing to the path instead of the 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):

I am at a loss for how to address this problem. I would love to find an answer from the brain trust.

答案1

得分: 0

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

class TranscriptionJob &lt; ApplicationJob
  queue_as :default
  require 'openai'

  def perform(transcription)
    # 提供openai API密钥并初始化openai客户端的实例
    client = OpenAI::Client.new(access_token:  Rails.application.credentials.dig(:openai, :api_key))

    # 初始化transcriptionresult变量
    transcription.transcriptionresult = ''

    # 必须首先将文件下载到临时存储位置,然后使用openai API提交。

    transcription.audio_file.open do |file|
      # 我在应用根目录创建了一个名为“transcribe”的文件夹
      system '/tmp/transcribe', file.path

      response = client.transcribe(
        parameters: {
            model: "whisper-1",
            file: File.open(file.path, "rb")
        })

      # 使transcriptionresult等于openai转录产生的response['text']。
      transcription.transcriptionresult = response['text']

      # 保存更新后的transcription对象
      transcription.save
    end
  end
end
英文:

Thank you for @Chiperific pointing me to this edge guide: https://edgeguides.rubyonrails.org/active_storage_overview.html#downloading-files This helped me to rewrite my code as follows. The key is to download the file to a temporary file location on the system and then point to that local path in the openai API.

class TranscriptionJob &lt; ApplicationJob
  queue_as :default
  require &#39;openai&#39;

  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 = &#39;&#39;
    
    # 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 &#39;/tmp/transcribe&#39;, file.path
      
      response = client.transcribe(
        parameters: {
            model: &quot;whisper-1&quot;,
            file: File.open(file.path, &quot;rb&quot;)
        })

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

huangapple
  • 本文由 发表于 2023年5月15日 03:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定