Skip to content

Commit

Permalink
Merge pull request #16 from e19166/updates_after_review
Browse files Browse the repository at this point in the history
Add suggested changes
  • Loading branch information
NipunaRanasinghe authored Aug 21, 2024
2 parents 1097092 + 25393bf commit 674d4ba
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 88 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ import ballerinax/openai.audio;
Create a `audio:ConnectionConfig` with the obtained API Key and initialize the connector.

```ballerina
configurable string apiKey = ?;
configurable string token = ?;
final images:Client openAIAudio = check new ({
auth: {
token: apiKey
token
}
});
```
Expand Down
4 changes: 2 additions & 2 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ import ballerinax/openai.audio;
Create a `audio:ConnectionConfig` with the obtained API Key and initialize the connector.

```ballerina
configurable string apiKey = ?;
configurable string token = ?;
final images:Client openAIAudio = check new ({
auth: {
token: apiKey
token
}
});
```
Expand Down
4 changes: 2 additions & 2 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ import ballerinax/openai.audio;
Create a `audio:ConnectionConfig` with the obtained API Key and initialize the connector.

```ballerina
configurable string apiKey = ?;
configurable string token = ?;
final images:Client openAIAudio = check new ({
auth: {
token: apiKey
token
}
});
```
Expand Down
13 changes: 5 additions & 8 deletions ballerina/tests/mock_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ballerina/http;
import ballerina/io;

listener http:Listener ep0 = new (9090);

public type OkByteArray record {|
*http:Ok;
byte[] body;
Expand All @@ -28,23 +29,20 @@ const AUDIO_FILE_PATH = "tests/resources/audioClip.mp3";
readonly & byte[] content = check io:fileReadBytes(AUDIO_FILE_PATH);

service / on ep0 {

# Converts a given text to speech.
#
#
# + return - OkByteArray if the operation is successful.
resource function post audio/speech(@http:Payload CreateSpeechRequest payload) returns OkByteArray|error {

return {
body: content,
headers: { "Transfer-Encoding": "chunked" }
headers: {"Transfer-Encoding": "chunked"}
};
}

# Transcribes a given audio file.
#
#
# + return - CreateTranscriptionResponse if the operation is successful.
resource function post audio/transcriptions(http:Request request) returns CreateTranscriptionResponse {

CreateTranscriptionResponseJson mockTranscriptionResponse = {
text: "The"
};
Expand All @@ -53,10 +51,9 @@ service / on ep0 {
}

# Translates a given text.
#
#
# + return - CreateTranslationResponse if the operation is successful.
resource function post audio/translations(http:Request request) returns CreateTranslationResponse {

CreateTranslationResponseJson mockTranslationResponse = {
text: "The"
};
Expand Down
39 changes: 19 additions & 20 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;
import ballerina/mime;

import ballerina/io;
import ballerina/mime;
import ballerina/os;
import ballerina/test;

configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";
configurable string apiKey = isLiveServer ? os:getEnv("OPENAI_API_KEY") : "test";
configurable string serviceUrl = isLiveServer ? "https://api.openai.com/v1" : "http://localhost:9090";

const AUDIO_FILE_PATH1 = "tests/resources/audioClip.mp3";

final ConnectionConfig config = {
auth: {
token:apiKey
token: apiKey
}
};
final Client openAIAudio = check new(config,serviceUrl);

const AUDIO_FILE_PATH1 = "tests/resources/audioClip.mp3";
final Client openAIAudio = check new (config, serviceUrl);

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testCreateTranslation() returns error? {

isolated function testCreateTranslation() returns error? {
byte[] audioContent = check io:fileReadBytes(AUDIO_FILE_PATH1);

CreateTranslationRequest payload = {
file: {
fileContent: audioContent, fileName: "speech.mp3"},
model: "whisper-1"
fileContent: audioContent,
fileName: "speech.mp3"
},
model: "whisper-1"
};
CreateTranslationResponse result = check openAIAudio->/audio/translations.post(payload);
test:assertEquals(result.text, "The");
Expand All @@ -52,32 +52,31 @@ isolated function testCreateTranslation() returns error? {
@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testCreateSpeech() returns error? {
isolated function testCreateSpeech() returns error? {
CreateSpeechRequest requestPayload = {
model: "tts-1",
input: "The",
voice: "alloy",
response_format: "mp3",
speed: 1.0
};
};
byte[] response = check openAIAudio->/audio/speech.post(requestPayload);
test:assertTrue(response.length() > 2000, msg = "Expected response length to be greater than 2000, but found " + response.length().toString());
}


@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testCreateTranscription() returns error? {
isolated function testCreateTranscription() returns error? {
byte[] audioContent = check io:fileReadBytes(AUDIO_FILE_PATH1);

CreateTranscriptionRequest payload = {
file: {fileContent: audioContent, fileName: "./speech.mp3"},
model: "whisper-1"
CreateTranscriptionRequest payload = {
file: {fileContent: audioContent, fileName: "./speech.mp3"},
model: "whisper-1"
};
map<string> headers = {
"Content-Type": mime:MULTIPART_FORM_DATA
};
"Content-Type": mime:MULTIPART_FORM_DATA
};
CreateTranscriptionResponse result = check openAIAudio->/audio/transcriptions.post(payload, headers);
test:assertEquals(result.text, "The");
}
Expand Down
Binary file modified docs/setup/resources/api-key-dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/setup/resources/create-new-secret-key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/setup/resources/navigate-api-key-dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/setup/resources/saved-key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions docs/spec/sanitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ These changes are done in order to improve the overall usability, and as workaro
- **Updated**:
- Removed the `default` parameter

- **Reason**: This change is done as a workaround for ballerina openapi tool not allowing to generate the client.
- **Reason**: This change is done as a workaround for ballerina openapi tool not allowing to generate the client until the openAPI tool supports v3.1.x schemas

2. **Modified the specifications to use named schemas instead of inline schemas**

Expand All @@ -28,30 +28,36 @@ These changes are done in order to improve the overall usability, and as workaro
`/audio/transcriptions`

- **Original**:
```bash
- oneOf:
- $ref: "#/components/schemas/CreateTranscriptionResponseJson"
- $ref: "#/components/schemas/CreateTranscriptionResponseVerboseJson"

- oneOf:
- $ref: "#/components/schemas/CreateTranslationResponseJson"
- $ref: "#/components/schemas/CreateTranslationResponseVerboseJson"
```

- **Updated**:
- Created a new `schema` in the `components` field and added the reference in the `/audio/transcription` path definition
```bash
- $ref: "#/components/schemas/CreateTranscriptionResponse"

- CreateTranscriptionResponse:
oneOf:
- oneOf:
- $ref: "#/components/schemas/CreateTranscriptionResponseJson"
- $ref: "#/components/schemas/CreateTranscriptionResponseVerboseJson"
```

- Created a new `schema` in the `components` field and added the reference in the `/audio/translation` path definition
```bash
- $ref: "#/components/schemas/CreateTranslationResponse"
- CreateTranslationResponse:
oneOf:
- oneOf:
- $ref: "#/components/schemas/CreateTranslationResponseJson"
- $ref: "#/components/schemas/CreateTranslationResponseVerboseJson"
```

- **Reason**: This modification has been implemented to enhance the readability and user-friendliness of record naming conventions.

Expand Down
80 changes: 40 additions & 40 deletions examples/international-news-translator/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,48 @@ const DOWNLOADED_NEWS_TEXT_FILE_PATH = "./news.txt";
const TRANSLATED_NEWS_AUDIO_FILE = "englishAudio.mp3";

public function main(string newsTextUrl) returns error? {
// Creates an HTTP client to download the text file
http:Client textEP = check new(newsTextUrl);
http:Response response = check textEP->/get();
string fileContent = check response.getTextPayload();
check io:fileWriteString(DOWNLOADED_NEWS_TEXT_FILE_PATH, fileContent);
// Creates an HTTP client to download the text file
http:Client textEP = check new (newsTextUrl);
http:Response response = check textEP->/get();
string fileContent = check response.getTextPayload();
check io:fileWriteString(DOWNLOADED_NEWS_TEXT_FILE_PATH, fileContent);

// Creates an OpenAI audio client
audio:Client openAIAudio = check new({auth: {token: openAIKey}});

// Creates a request to convert the text into audio
audio:CreateSpeechRequest speechRequest = {
voice: "alloy",
input: io:fileReadString(DOWNLOADED_NEWS_TEXT_FILE_PATH),
model: "whisper-1"
};
// Creates an OpenAI audio client
audio:Client openAIAudio = check new ({auth: {token: openAIKey}});

// Convert the text to audio
byte[] speechResponse = check openAIAudio->/audio/speech.post(speechRequest);

// Creates a request to translate the audio to English
audio:CreateTranslationRequest translationRequest = {
file: {
fileContent: speechResponse,
fileName: TRANSLATED_NEWS_AUDIO_FILE
// Creates a request to convert the text into audio
audio:CreateSpeechRequest speechRequest = {
voice: "alloy",
input: io:fileReadString(DOWNLOADED_NEWS_TEXT_FILE_PATH),
model: "whisper-1"
};

// Convert the text to audio
byte[] speechResponse = check openAIAudio->/audio/speech.post(speechRequest);

// Creates a request to translate the audio to English
audio:CreateTranslationRequest translationRequest = {
file: {
fileContent: speechResponse,
fileName: TRANSLATED_NEWS_AUDIO_FILE
},
model: "whisper-1"
};
// Translate the audio to English
audio:CreateTranslationResponse translationResponse = check openAIAudio->/audio/translations.post(translationRequest);
// Creates a request to convert the translated english text to audio
audio:CreateSpeechRequest targetedSpeechRequest = {
voice: "alloy",
input: translationResponse.text,
model: "whisper-1"
};
model: "whisper-1"
};

// Translate the audio to English
audio:CreateTranslationResponse translationResponse = check openAIAudio->/audio/translations.post(translationRequest);

// Creates a request to convert the translated english text to audio
audio:CreateSpeechRequest targetedSpeechRequest = {
voice: "alloy",
input: translationResponse.text,
model: "whisper-1"
};

// Translate the english text to audio
byte[] englishAudio = check openAIAudio->/audio/speech.post(targetedSpeechRequest);
// Translate the english text to audio
byte[] englishAudio = check openAIAudio->/audio/speech.post(targetedSpeechRequest);

// Get the translated audio and save it in a '.mp3' file
check io:fileWriteBytes(ENGLISH_NEWS_AUDIO_FILE_PATH,englishAudio);
io:println("Translated news saved as translated_news.mp3");
}
// Get the translated audio and save it in a '.mp3' file
check io:fileWriteBytes(ENGLISH_NEWS_AUDIO_FILE_PATH, englishAudio);
io:println("Translated news saved as translated_news.mp3");
}
22 changes: 11 additions & 11 deletions examples/meeting-transcriber-and-translator/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public function main(string meetingAudioUrl) returns error? {

// Creates a transcription request
audio:CreateTranscriptionRequest transcriptionRequest = {
file: {
fileContent: check io:fileReadBytes(MEETING_AUDIO_FILE_PATH),
fileName: TRANSCRIBED_MEETING_AUDIO_FILE
},
model: "whisper-1"
file: {
fileContent: check io:fileReadBytes(MEETING_AUDIO_FILE_PATH),
fileName: TRANSCRIBED_MEETING_AUDIO_FILE
},
model: "whisper-1"
};

// Transcribes the audio file
Expand All @@ -55,12 +55,12 @@ public function main(string meetingAudioUrl) returns error? {

//Creates a translation request
audio:CreateTranslationRequest translationRequest = {
file: {
fileContent: check io:fileReadBytes(MEETING_AUDIO_FILE_PATH),
fileName: TRANSLATED_MEETING_AUDIO_FILE
},
model: "whisper-1"
};
file: {
fileContent: check io:fileReadBytes(MEETING_AUDIO_FILE_PATH),
fileName: TRANSLATED_MEETING_AUDIO_FILE
},
model: "whisper-1"
};

// Translates the audio file
audio:CreateTranslationResponse translationResponse = check openAIAudio->/audio/translations.post(translationRequest);
Expand Down

0 comments on commit 674d4ba

Please sign in to comment.