Skip to content

Commit

Permalink
update jvm sample
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Oct 6, 2024
1 parent f47625d commit 52b6848
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.aallam.openai.sample.jvm

import kotlinx.io.files.Path

object Resources {
fun path(resource: String): Path {
return Path(path = getPath(resource))
}

private fun getPath(resource: String): String {
return Resources::class.java.getResource("/$resource")?.path
?: throw IllegalStateException("Resource $resource not found")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import com.aallam.openai.api.audio.TranslationRequest
import com.aallam.openai.api.file.FileSource
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import kotlinx.io.files.Path

suspend fun whisper(openAI: OpenAI) {
println("\n>️ Create transcription...")
val transcriptionRequest = TranscriptionRequest(
audio = FileSource(path = Path("micro-machines.wav")),
audio = FileSource(path = Resources.path("micro-machines.wav")),
model = ModelId("whisper-1"),
)
val transcription = openAI.transcription(transcriptionRequest)
println(transcription)

println("\n>️ Create translation...")
val translationRequest = TranslationRequest(
audio = FileSource(path = Path("multilingual.wav")),
audio = FileSource(path = Resources.path("multilingual.wav")),
model = ModelId("whisper-1"),
)
val translation = openAI.translation(translationRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.aallam.openai.api.image.ImageCreation
import com.aallam.openai.api.image.ImageEdit
import com.aallam.openai.api.image.ImageSize
import com.aallam.openai.client.OpenAI
import kotlinx.io.files.Path

suspend fun images(openAI: OpenAI) {
println("\n> Create images...")
Expand All @@ -20,8 +19,8 @@ suspend fun images(openAI: OpenAI) {

println("\n> Edit images...")
val imageEdit = ImageEdit(
image = FileSource(path = Path("image.png")),
mask = FileSource(path = Path("image.png")),
image = FileSource(path = Resources.path("image.png")),
mask = FileSource(path = Resources.path("image.png")),
prompt = "a sunlit indoor lounge area with a pool containing a flamingo",
n = 1,
size = ImageSize.is1024x1024,
Expand Down
11 changes: 5 additions & 6 deletions sample/native/src/nativeMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
import okio.FileSystem
import okio.Path.Companion.toPath
import kotlinx.io.files.Path
import platform.posix.getenv

fun main(): Unit = runBlocking {
Expand Down Expand Up @@ -62,8 +61,8 @@ fun main(): Unit = runBlocking {

println("\n> Edit images...")
val imageEdit = ImageEdit(
image = FileSource(path = "$resourcesPrefix/image.png".toPath(), fileSystem = FileSystem.SYSTEM),
mask = FileSource(path = "$resourcesPrefix/mask.png".toPath(), fileSystem = FileSystem.SYSTEM),
image = FileSource(path = Path(resourcesPrefix, "image.png")),
mask = FileSource(path = Path(resourcesPrefix, "mask.png")),
prompt = "a sunlit indoor lounge area with a pool containing a flamingo",
n = 1,
size = ImageSize.is1024x1024,
Expand Down Expand Up @@ -96,15 +95,15 @@ fun main(): Unit = runBlocking {

println("\n>️ Create transcription...")
val transcriptionRequest = TranscriptionRequest(
audio = FileSource(path = "$resourcesPrefix/micro-machines.wav".toPath(), fileSystem = FileSystem.SYSTEM),
audio = FileSource(path = Path(resourcesPrefix, "micro-machines.wav")),
model = ModelId("whisper-1"),
)
val transcription = openAI.transcription(transcriptionRequest)
println(transcription)

println("\n>️ Create translation...")
val translationRequest = TranslationRequest(
audio = FileSource(path = "$resourcesPrefix/multilingual.wav".toPath(), fileSystem = FileSystem.SYSTEM),
audio = FileSource(path = Path(resourcesPrefix, "multilingual.wav")),
model = ModelId("whisper-1"),
)
val translation = openAI.translation(translationRequest)
Expand Down

0 comments on commit 52b6848

Please sign in to comment.