Created
July 18, 2024 02:41
-
-
Save jaeyson/4a7d9301227eaf570a0a0d2e0d359c14 to your computer and use it in GitHub Desktop.
File Uploads Using the Req Elixir Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Uploader do | |
@moduledoc """ | |
Source: https://samrat.me/til-file-uploads-using-the-req-elixir-library | |
v0.4.5 of Req don't support file upload. Might as well use multipart | |
""" | |
def transcribe_audio(file_path, token) do | |
model = "whisper-1" | |
filename = Path.basename(file_path) | |
{:ok, file_contents} = File.read(file_path) | |
multipart = | |
Multipart.new() | |
|> Multipart.add_part(Multipart.Part.text_field(model, "model")) | |
|> Multipart.add_part( | |
Multipart.Part.file_content_field(filename, file_contents, :file, filename: filename) | |
) | |
content_length = Multipart.content_length(multipart) | |
content_type = Multipart.content_type(multipart, "multipart/form-data") | |
headers = [ | |
{"authorization", "Bearer #{token}"}, | |
{"Content-Type", content_type}, | |
{"Content-Length", to_string(content_length)} | |
] | |
Req.post( | |
"https://api.openai.com/v1/audio/transcriptions", | |
headers: headers, | |
body: Multipart.body_stream(multipart) | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment