-
-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of getting bytes instead of saving directly to file for speech #177
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
|
||
import '../../../../dart_openai.dart'; | ||
|
||
|
@@ -13,6 +14,14 @@ abstract class CreateInterface { | |
Directory? outputDirectory, | ||
}); | ||
|
||
Future<Uint8List> createSpeechBytes({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. couldn't create an overload with the same param names, rename if unsuitable. |
||
required String model, | ||
required String input, | ||
required String voice, | ||
OpenAIAudioSpeechResponseFormat? responseFormat, | ||
double? speed, | ||
}); | ||
|
||
Future<OpenAIAudioModel> createTranscription({ | ||
required File file, | ||
required String model, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import "dart:async"; | ||
import "dart:convert"; | ||
import "dart:io"; | ||
import "dart:typed_data"; | ||
import "package:dart_openai/src/core/constants/config.dart"; | ||
import "package:dart_openai/src/core/utils/extensions.dart"; | ||
|
||
|
@@ -206,6 +207,55 @@ abstract class OpenAINetworkingClient { | |
Map<String, dynamic>? body, | ||
http.Client? client, | ||
}) async { | ||
|
||
var response = await postAndGetResponse(to: to, body: body, client: client); | ||
|
||
final fileTypeHeader = "content-type"; | ||
|
||
final fileExtensionFromBodyResponseFormat = | ||
response.headers[fileTypeHeader]?.split("/").last ?? "mp3"; | ||
|
||
final fileName = | ||
outputFileName + "." + fileExtensionFromBodyResponseFormat; | ||
|
||
File file = File( | ||
"${outputDirectory != null ? outputDirectory.path : ''}" + | ||
"/" + | ||
fileName, | ||
); | ||
|
||
OpenAILogger.creatingFile(fileName); | ||
|
||
await file.create(); | ||
OpenAILogger.fileCreatedSuccessfully(fileName); | ||
OpenAILogger.writingFileContent(fileName); | ||
|
||
file = await file.writeAsBytes( | ||
response.bodyBytes, | ||
mode: FileMode.write, | ||
); | ||
|
||
OpenAILogger.fileContentWrittenSuccessfully(fileName); | ||
|
||
return onFileResponse(file); | ||
} | ||
|
||
|
||
static Future<Uint8List> postAndGetBytes({ | ||
required String to, | ||
Map<String, dynamic>? body, | ||
http.Client? client, | ||
}) async { | ||
|
||
var response = await postAndGetResponse(to: to, body: body, client: client); | ||
return response.bodyBytes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case just get the bytes, should mean we can play with AudioPlayer |
||
} | ||
|
||
static Future<http.Response> postAndGetResponse({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separated the basics of getting the response with bytes data so we still have the error handling/logging |
||
required String to, | ||
Map<String, dynamic>? body, | ||
http.Client? client, | ||
}) async { | ||
OpenAILogger.logStartRequest(to); | ||
|
||
final uri = Uri.parse(to); | ||
|
@@ -255,34 +305,7 @@ abstract class OpenAINetworkingClient { | |
|
||
OpenAILogger.requestFinishedSuccessfully(); | ||
|
||
final fileTypeHeader = "content-type"; | ||
|
||
final fileExtensionFromBodyResponseFormat = | ||
response.headers[fileTypeHeader]?.split("/").last ?? "mp3"; | ||
|
||
final fileName = | ||
outputFileName + "." + fileExtensionFromBodyResponseFormat; | ||
|
||
File file = File( | ||
"${outputDirectory != null ? outputDirectory.path : ''}" + | ||
"/" + | ||
fileName, | ||
); | ||
|
||
OpenAILogger.creatingFile(fileName); | ||
|
||
await file.create(); | ||
OpenAILogger.fileCreatedSuccessfully(fileName); | ||
OpenAILogger.writingFileContent(fileName); | ||
|
||
file = await file.writeAsBytes( | ||
response.bodyBytes, | ||
mode: FileMode.write, | ||
); | ||
|
||
OpenAILogger.fileContentWrittenSuccessfully(fileName); | ||
|
||
return onFileResponse(file); | ||
return response; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was automatic, please revert if incorrect