Using a Code Snippet to Perform Transcribing via OpenAI API

This guide will help you understand how to use a SurveyToGo code snippet to perform audio transcribing using the OpenAI API. We'll walk through the code, explain how it works, and show you how to adapt it to your needs.


Overview

In this guide we will discuss how to create a survey that uses OpenAI integration with user attachments.

1. Setting up a survey
2. Copy code to Advanced Scripts
3. Setting Up the OpenAI API Key
4. Using the script with Prompt and Image
5. Code overview and explanation
6. Customizing the request
Final Notes

Step-by-Step Guide

1. Setting up a survey

Before implementing the OCR functionality, you need to set up your survey in SurveyToGo with the following three questions:

  1. First Question: Multimedia Question

    • Type: Multimedia Question
    • Purpose: To capture an audio recording that will be transcribed.
    • Identifier: Assign a unique identifier (e.g., Q_1) to reference this question in your code.
  2. Second Question: Waiting screen - Empty Question

    • Type: Empty Question (Will show a waiting message)
    • Purpose: Runs the transcribing function and shows the result.
    • Identifier: Assign a unique identifier (e.g., Q_2) to reference this question in your code.
  3. Third Question: Single choice for silent recording

    • Type: Single Choicen (e.g., Single Choice, Open-Ended)

    • Purpose: Capture silent recording for transcribing
    • Identifier: Assign a unique identifier (e.g., Q_3) if you need to manipulate it further in your code.
  4. Fourth Question: Waiting screen - Empty Question
      • Type: Empty Question (Will show a waiting message)
      • Purpose: Runs the transcribing function and shows the result.
      • Identifier: Assign a unique identifier (e.g., Q_4) if you need to manipulate it further in your code.

Survey structure


 

2. Copy code to Advanced Scripts

This code should be placed in the Advanced Scripts. 

The advanced scripts will hold the function definition and callback functions neeeded for the implementation.

var secret = 'your_openai_api_key'; 

function Transcribe(QuestionID) {
  var filename = GetAttachedFiles(QuestionID)[0];
var options = CreateWSOptions();
options.ContentType = "multipart/form-data";
options.Headers["Authorization"] = "Bearer " + secret;
options.Headers["Accept"] = "*/*";
options.FormData["model"] = "whisper-1";
options.FormData["file"] = "file://" + filename;
var res = WebServicePostSync("https://api.openai.com/v1/audio/transcriptions", null, options);
return res.Result
}

 

3. Setting Up the OpenAI API Key

First, you need to set your OpenAI API key to authenticate your requests with OpenAI.
Instructions available Here.

Need to configure Billing in OpenAI, and add the model under the Project Limits (whisper-1) in this example.
Then insert the API Key you've created into the "secret" variable in the advanced scripts
// Replace 'your_openai_api_key' with your actual API key
var secret = 'your_openai_api_key';

Note: Keep your API key secure. Do not share it publicly or include it in code repositories.


 

4. Using Transcribe in a script

Add this script to the second question's Start Script.
The Transcribe funtion will transcribe the file associated with the first attachment of the question.

// Example usage of the Transcribe function
var res = Transcribe(Q_1);
Prompt(res);
  • Q_1: This is the identifier for the question that holds the recording.

5. Code overview and explanation

5.1 The PromptImageGPT Function

This first part of the function retrieves the first attachment.

function Transcribe(QuestionID)
{
var filename = GetAttachedFiles(QuestionID)[0];
  • Explanation:
    • Retrieves the recording file associated with QuestionID.

5.2 Reading and Encoding the Image

Create a webservice options object that contains the OpenAI request information.

var options = CreateWSOptions();

options.ContentType = "multipart/form-data";
options.Headers["Authorization"] = "Bearer " + secret;
options.Headers["Accept"] = "*/*";
options.FormData["model"] = "whisper-1";
options.FormData["file"] = "file://" + filename;
  • Explanation:
    • OpenAI Whisper requires a multipart POST request
    • Set the model to "whisper-1"
    • Associate the attachment file
    •  

5.4 Sending the Request

We send the composed message to the OpenAI API.

 var res = WebServicePostSync("https://api.openai.com/v1/audio/transcriptions", null, options);
return res.Result
  • Explanation:
    • Sends a POST request to the API endpoint with the options object and no body.

6. Customizing the request

OpenAI supports a few more options when sending the request, for example:

1. Verbose results. This requests extra information besides just the text. For example the detected language, duration and timestamps of the individual words.

options.FormData["response_format"] = "verbose_json";

........

var jsonObject= ParseJson(res);
Prompt (asdfasdf.language);

2. Specify the language in the recording, could be used with selected survey language.
OpenAI supports just the 2 letter language code according to ISO 639-1

options.FormData["language"] = "es"; //specify spanish

3. Open AI supports translation to english with a different endpoint with the same parameters.

var res = WebServicePostSync("https://api.openai.com/v1/audio/translations", null, options);

 

Final Notes

  • Testing: Try processing different images to see how the AI responds to various prompts.
  • Error Handling: Enhance the code with additional error checks as needed.

By following this guide, you can adapt the code snippet to perform Transcribing tasks tailored to your specific requirements.


 

Additional Resources

  • An example survey is linked at the end of the article
  • OpenAI API Documentation: OpenAI Speech-To-Text API Reference
  • Best Practices:
    • Securely manage your API keys.
    • Implement proper error handling.
    • Optimize code for asynchronous operations.
  • Minimum supported version:
    • Studio 1.32.673
    • Playstore Android 2.0.614
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Article is closed for comments.