Skip to content

Commit

Permalink
Merge pull request #25 from AssemblyAI/fern-bot/03-20-2024-0126AM
Browse files Browse the repository at this point in the history
🌿 Fern Regeneration -- March 20, 2024
  • Loading branch information
Swimburger committed Mar 21, 2024
2 parents 28b3504 + 8b347da commit 0577669
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ test/
.rubocop.yml
.github

<!-- Manual customized files client -->
lib/assemblyai/files/client.rb

<!-- Allow for the polling client -->
lib/assemblyai.rb
lib/assemblyai/transcripts/polling_client.rb
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ transcript = client.transcripts.submit(
<summary>Transcribe a local audio file</summary>

```ruby
data = File.open('/path/to/your/file').read
encoded = Base64.encode64(data)
uploaded_file = client.files.upload(request: encoded)
uploaded_file = client.files.upload(file: '/path/to/your/file')
# You can also pass an IO object or base64 string
# uploaded_file = client.files.upload(file: File.new('/path/to/your/file')

transcript = client.transcripts.transcribe(audio_url: uploaded_file.upload_url)
puts transcript.text
Expand Down
2 changes: 1 addition & 1 deletion assemblyai.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require_relative "lib/gemconfig"

Gem::Specification.new do |spec|
spec.name = "assemblyai"
spec.version = "1.0.0-beta.5"
spec.version = "1.0.0-beta.6"
spec.authors = AssemblyAI::Gemconfig::AUTHORS
spec.email = AssemblyAI::Gemconfig::EMAIL
spec.summary = AssemblyAI::Gemconfig::SUMMARY
Expand Down
58 changes: 52 additions & 6 deletions lib/assemblyai/files/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,39 @@ def initialize(request_client:)

# Upload your media file directly to the AssemblyAI API if it isn't accessible via a URL already.
#
# @param request [String] Base64 encoded bytes
# @param file [String, IO] File path, Base64 String, or an IO object (e.g. Faraday::UploadIO, etc.)
# @param request_options [RequestOptions]
# @return [Files::UploadedFile]
def upload(request:, request_options: nil)
def upload(file:, request_options: nil)
response = @request_client.conn.post("/v2/upload") do |req|
if file.is_a? String
begin
path = Pathname.new(file)
rescue StandardError
file_data = file
end
unless path.nil?
if path.file?
file_data = File.new(file)
elsif path.directory?
raise "file path has to be a path to file, but is a path to directory"
else
raise "file at path does not exist"
end
end
else
file_data = file
end
req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
req.headers["Authorization"] = request_options.api_key unless request_options&.api_key.nil?
req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
req.headers["Content-Type"] = "application/octet-stream"
req.body = { **(request || {}), **(request_options&.additional_body_parameters || {}) }.compact
if file.is_a? File
req.headers["Content-Length"] = File.size(file_data.path).to_s
else
req.headers["Transfer-Encoding"] = "chunked"
end
req.body = file_data
end
Files::UploadedFile.from_json(json_object: response.body)
end
Expand All @@ -44,17 +67,40 @@ def initialize(request_client:)

# Upload your media file directly to the AssemblyAI API if it isn't accessible via a URL already.
#
# @param request [String] Base64 encoded bytes
# @param file [String, IO] File path, Base64 String, or an IO object (e.g. Faraday::UploadIO, etc.)
# @param request_options [RequestOptions]
# @return [Files::UploadedFile]
def upload(request:, request_options: nil)
def upload(file:, request_options: nil)
Async do
response = @request_client.conn.post("/v2/upload") do |req|
if file.is_a? String
begin
path = Pathname.new(file)
rescue StandardError
file_data = file
end
unless path.nil?
if path.file?
file_data = File.new(file)
elsif path.directory?
raise "file path has to be a path to file, but is a path to directory"
else
raise "file at path does not exist"
end
end
else
file_data = file
end
req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
req.headers["Authorization"] = request_options.api_key unless request_options&.api_key.nil?
req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
req.headers["Content-Type"] = "application/octet-stream"
req.body = { **(request || {}), **(request_options&.additional_body_parameters || {}) }.compact
if file.is_a? File
req.headers["Content-Length"] = File.size(file_data.path).to_s
else
req.headers["Transfer-Encoding"] = "chunked"
end
req.body = file_data
end
Files::UploadedFile.from_json(json_object: response.body)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(api_key:, environment: Environment::DEFAULT, max_retries: nil, ti
@headers = {
"X-Fern-Language": "Ruby",
"X-Fern-SDK-Name": "assemblyai",
"X-Fern-SDK-Version": "1.0.0-beta.5",
"X-Fern-SDK-Version": "1.0.0-beta.6",
"Authorization": api_key.to_s
}
@conn = Faraday.new(@base_url, headers: @headers) do |faraday|
Expand All @@ -46,7 +46,7 @@ def initialize(api_key:, environment: Environment::DEFAULT, max_retries: nil, ti
@headers = {
"X-Fern-Language": "Ruby",
"X-Fern-SDK-Name": "assemblyai",
"X-Fern-SDK-Version": "1.0.0-beta.5",
"X-Fern-SDK-Version": "1.0.0-beta.6",
"Authorization": api_key.to_s
}
@conn = Faraday.new(@base_url, headers: @headers) do |faraday|
Expand Down
Binary file added test/gore-short.wav
Binary file not shown.
39 changes: 33 additions & 6 deletions test/test_assemblyai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,41 @@

# Basic AssemblyAI tests
class TestAssemblyAI < Minitest::Test
# @return [String] ASSEMBLYAI API key
def api_key
ENV.fetch("ASSEMBLYAI_API_KEY", nil)
end

def test_upload_file_with_file
skip("Integration tests not enabled")
client = AssemblyAI::Client.new(api_key: api_key)
file = File.new("./test/gore-short.wav")
uploaded_file = client.files.upload(file: file)
assert !uploaded_file.upload_url.nil?
end

def test_upload_file_with_path
skip("Integration tests not enabled")
client = AssemblyAI::Client.new(api_key: api_key)
uploaded_file = client.files.upload(file: "./test/gore-short.wav")
assert !uploaded_file.upload_url.nil?
end

def test_upload_file_with_base64_string
skip("Integration tests not enabled")
client = AssemblyAI::Client.new(api_key: api_key)
uploaded_file = client.files.upload(file: File.read("./test/gore-short.wav"))
assert !uploaded_file.upload_url.nil?
end

def test_init
# this test verifies that the SDK can be imported/initialized
AssemblyAI::Client.new(api_key: "YOUR API KEY")
AssemblyAI::Client.new(api_key: "dummy")
end

def test_pagination
skip("Integration tests not enabled")
client = AssemblyAI::Client.new(api_key: "YOUR API KEY")
client = AssemblyAI::Client.new(api_key: api_key)
transcript_list = client.transcripts.list

count = 0
Expand All @@ -36,11 +63,11 @@ def test_pagination

def test_polling
skip("Integration tests not enabled")
client = AssemblyAI::Client.new(api_key: "YOUR API KEY")
client = AssemblyAI::Client.new(api_key: api_key)
transcript = client.transcripts.transcribe(audio_url: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a")
assert transcript.status == AssemblyAI::Transcripts::TranscriptStatus::COMPLETED

client = AssemblyAI::AsyncClient.new(api_key: "YOUR API KEY")
client = AssemblyAI::AsyncClient.new(api_key: api_key)
Sync do
transcript_task = client.transcripts.transcribe(audio_url: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a")
assert transcript_task.is_a? Async::Task
Expand All @@ -52,7 +79,7 @@ def test_polling
def test_transcribe
skip("Integration tests not enabled")
# Transcribe
client = AssemblyAI::Client.new(api_key: "YOUR API KEY")
client = AssemblyAI::Client.new(api_key: api_key)

transcript_submission = client.transcripts.submit(audio_url: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a")
assert !transcript_submission.id.nil?
Expand All @@ -69,7 +96,7 @@ def test_transcribe

def test_lemur
skip("Integration tests not enabled")
client = AssemblyAI::Client.new(api_key: "YOUR API KEY")
client = AssemblyAI::Client.new(api_key: api_key)
assert !client.lemur.summary(transcript_ids: ["369849ed-b5a1-4add-9dde-ac936d3e7b99"]).response.nil?

assert !client.lemur.question_answer(transcript_ids: ["369849ed-b5a1-4add-9dde-ac936d3e7b99"],
Expand Down

0 comments on commit 0577669

Please sign in to comment.