#upload file to whisper

7 messages · Page 1 of 1 (latest)

sweet tangle
#

I need to upload a file to whisper, to do this i need a way to first get the file, how is th best approach in convex?
look at this example, the input is in a "local" audio.mp3

async function main() {
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("audio.mp3"),
model: "whisper-1",
});

cursive yachtBOT
#

Hi @sweet tangle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

In Convex, you can handle file uploads in a few steps. Here's a general approach:

  1. Generate an upload URL: Convex provides a method to generate an upload URL. This URL is where you will send your file data.
export const generateUploadUrl = mutation(async (ctx) => {
  return await ctx.storage.generateUploadUrl();
});

Source (https://stack.convex.dev/uploading-files-from-react-native-or-expo)

  1. Upload the file: Once you have the upload URL, you can send your file data there. In your case, you would read the contents of your audio.mp3 file into a Buffer or Blob, and then send that data to the upload URL.
const result = await fetch(postUrl, {
  method: "POST",
  headers: { "Content-Type": "audio/mp3" },
  body: audioBufferOrBlob,
});

Source (https://stack.convex.dev/uploading-files-from-react-native-or-expo)

  1. Store the file: After successfully uploading the file, you can get the storageId from the response. This ID is used to reference the file in Convex.
const { storageId } = await result.json();

Source (https://stack.convex.dev/uploading-files-from-react-native-or-expo)

  1. Use the file in Whisper: Once the file is stored in Convex, you can use the storageId to get the URL of the file and pass it to Whisper.
let fileUrl = (await ctx.storage.getUrl(storageId)) as string;

Source (https://stack.convex.dev/full-stack-ai-voice-note-taking-app#recording-a-voice-note)

Please note that the code snippets provided are for illustrative purposes and may need to be adapted to fit into your specific application.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@sweet tangle kapa.ai is still learning and improving, please let us know how it did by reacting below

sweet tangle
#

@cursive yacht as i explained i dont need the file url, but the real file

cursive yachtBOT
#

Let me check the knowledge base for you. One moment! :books:

#

In Convex, you can retrieve the actual file content (not just the URL) using the storage.get() method. This method accepts the file id and returns a Blob object representing the file content.

Here's an example of how you can use it:

const blob = await ctx.storage.get(storageId);

Source (https://docs.convex.dev/file-storage/serve-files#serving-files-from-http-actions)

Please note that the storage.get() method is typically used in server-side code (like HTTP actions or mutations), not on the client side. If you need to download the file content on the client side, you would typically use the file URL and make a fetch request.

Also, keep in mind that the Blob object represents the file data in binary format. Depending on your use case, you might need to convert this Blob into a format that your application can use (like an ArrayBuffer or a string).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: