Skip to content
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

Facing issue in ImageVariation call #83

Closed
hassanmirza444 opened this issue Jan 25, 2023 · 7 comments
Closed

Facing issue in ImageVariation call #83

hassanmirza444 opened this issue Jan 25, 2023 · 7 comments

Comments

@hassanmirza444
Copy link

hassanmirza444 commented Jan 25, 2023

I had integrate this library in my project. It is Working fine for content generation("v1/completions") and Image generation("v1/images/generations"). But When I start using ImageVariation ("v1/images/variations"), I had faced issue that I am unable to fix,

D 1-->file:///storage/emulated/0/DCIM/Camera/IMG_20230125_180236381.jpg 2023-01-25 18:02:38.499 8356-8356 hssn com.sparklab.ai D 2-->file:///data/user/0/com.sparklab.ai/files/sparklab/sparklab_hssnmirza.png 2023-01-25 18:02:38.975 8356-8396 OpenGLRenderer com.sparklab.ai D endAllActiveAnimators on 0xb4000076461a44d0 (RippleDrawable) with handle 0xb400007556108220 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I HttpClient: REQUEST: https://api.openai.com/v1/images/variations 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I METHOD: HttpMethod(value=POST) 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I COMMON HEADERS 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I -> Accept: application/json 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I -> Accept-Charset: UTF-8 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I -> Authorization: Bearer <apiKey> 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I CONTENT HEADERS 2023-01-25 18:02:40.404 8356-8583 System.out com.sparklab.ai I -> Content-Type: multipart/form-data; boundary=-651c0b63-3b3247d1-5ce2608bf515725-441b30e6-4507d4a361a52508-1fc4734e- 2023-01-25 18:02:40.535 8356-8585 TrafficStats com.sparklab.ai D tagSocket(96) with statsTag=0xffffffff, statsUid=-1 2023-01-25 18:02:42.753 8356-8585 System.out com.sparklab.ai I HttpClient: RESPONSE: 400 BAD REQUEST 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I METHOD: HttpMethod(value=POST) 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I FROM: https://api.openai.com/v1/images/variations 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I COMMON HEADERS 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> access-control-allow-origin: * 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> connection: keep-alive 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> content-length: 161 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> content-type: application/json 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> date: Wed, 25 Jan 2023 13:02:40 GMT 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> openai-organization: user-zinuwdnhn5tnrbfqaxqlrm8u 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> openai-processing-ms: 147 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> openai-version: 2020-10-01 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> strict-transport-security: max-age=15724800; includeSubDomains 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> x-android-received-millis: 1674651762739 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> x-android-response-source: NETWORK 400 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> x-android-selected-protocol: http/1.1 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> x-android-sent-millis: 1674651761388 --------- beginning of crash 2023-01-25 18:02:42.754 8356-8585 System.out com.sparklab.ai I -> x-request-id: 8ba8f7abb134b74cd3ae5b2d6f41c489 2023-01-25 18:02:42.798 8356-8592 AndroidRuntime com.sparklab.ai E FATAL EXCEPTION: DefaultDispatcher-worker-8 Process: com.sparklab.ai, PID: 8356 com.aallam.openai.api.exception.OpenAIHttpException at com.aallam.openai.client.internal.http.HttpTransport.perform(HttpTransport.kt:24) at com.aallam.openai.client.internal.http.HttpTransport$perform$1.invokeSuspend(Unknown Source:15) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@9c8cf2c, Dispatchers.Default] Caused by: com.aallam.openai.api.exception.OpenAIAPIException: (statusCode=400, body='{ "error": { "code": null, "message": "Uploaded image must be a PNG and less than 4 MB.", "param": null, "type": "invalid_request_error" } } ') at com.aallam.openai.client.internal.http.HttpTransport.bodyOrThrow(HttpTransport.kt:32) at com.aallam.openai.client.internal.http.HttpTransport.perform(HttpTransport.kt:22) ... 9 more 2023-01-25 18:02:42.832 8356-8592 Process com.sparklab.ai I Sending signal. PID: 8356 SIG: 9
My Image is already in .png format and size is in KBs. So, Please let me know how can I fix it.

@hassanmirza444
Copy link
Author

hassanmirza444 commented Jan 25, 2023

Logs in organised way.

D loaded /vendor/lib64/egl/libGLES_mali.so D maxBitmapSize: 3243 D Uri scheme: file D Image size: [393:524] I Should crop: true D 1-->file:///storage/emulated/0/DCIM/Camera/IMG_20230125_180236381.jpg D 2-->file:///data/user/0/com.sparklab.ai/files/sparklab/sparklab_hssnmirza.png D endAllActiveAnimators on 0xb4000076461a44d0 (RippleDrawable) with handle 0xb400007556108220 I HttpClient: REQUEST: https://api.openai.com/v1/images/variations I METHOD: HttpMethod(value=POST) I COMMON HEADERS I -> Accept: application/json I -> Accept-Charset: UTF-8 I -> Authorization: Bearer <apiKey> I CONTENT HEADERS I -> Content-Type: multipart/form-data; boundary=-651c0b63-3b3247d1-5ce2608bf515725-441b30e6-4507d4a361a52508-1fc4734e- D tagSocket(96) with statsTag=0xffffffff, statsUid=-1 I HttpClient: RESPONSE: 400 BAD REQUEST I METHOD: HttpMethod(value=POST) I FROM: https://api.openai.com/v1/images/variations I COMMON HEADERS I -> access-control-allow-origin: * I -> connection: keep-alive I -> content-length: 161 I -> content-type: application/json I -> date: Wed, 25 Jan 2023 13:02:40 GMT I -> openai-organization: user-zinuwdnhn5tnrbfqaxqlrm8u I -> openai-processing-ms: 147 I -> openai-version: 2020-10-01 I -> strict-transport-security: max-age=15724800; includeSubDomains I -> x-android-received-millis: 1674651762739 I -> x-android-response-source: NETWORK 400 I -> x-android-selected-protocol: http/1.1 I -> x-android-sent-millis: 1674651761388 --------- beginning of crash I -> x-request-id: 8ba8f7abb134b74cd3ae5b2d6f41c489 E FATAL EXCEPTION: DefaultDispatcher-worker-8 Process: com.sparklab.ai, PID: 8356 com.aallam.openai.api.exception.OpenAIHttpException at com.aallam.openai.client.internal.http.HttpTransport.perform(HttpTransport.kt:24) at com.aallam.openai.client.internal.http.HttpTransport$perform$1.invokeSuspend(Unknown Source:15) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@9c8cf2c, Dispatchers.Default] Caused by: com.aallam.openai.api.exception.OpenAIAPIException: (statusCode=400, body='{ "error": { "code": null, "message": "Uploaded image must be a PNG and less than 4 MB.", "param": null, "type": "invalid_request_error" } } ') at com.aallam.openai.client.internal.http.HttpTransport.bodyOrThrow(HttpTransport.kt:32) at com.aallam.openai.client.internal.http.HttpTransport.perform(HttpTransport.kt:22) ... 9 more I Sending signal. PID: 8356 SIG: 9

@aallam
Copy link
Owner

aallam commented Jan 25, 2023

Hi @hassanmirza444, I think you are trying to upload a jpg image, while the API only accepts png:

  1. The error from openai API is: Uploaded image must be a PNG and less than 4 MB.
  2. The image you are trying to upload is: IMG_20230125_180236381.jpg

PS: I've deleted your API key from the stacktraces above (and edit history)

@hassanmirza444
Copy link
Author

Yes brother, What I am doing is I select a file from gallery, and Logged it as 1.
1-->file:///storage/emulated/0/DCIM/Camera/IMG_20230125_180236381.jpg
Then I had converted it in png. Also, I have show it in an imageView. its completely fine.
2-->file:///data/user/0/com.sparklab.ai/files/sparklab/sparklab_hssnmirza.png

@aallam
Copy link
Owner

aallam commented Jan 25, 2023

Can you share how did you convert it from jpg to png please ?

@hassanmirza444
Copy link
Author

hassanmirza444 commented Jan 25, 2023

@aallam Here is code, on Image selected from gallery and conversion to png

` override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
//Image Uri will not be null for RESULT_OK

        Log.d("hssn", "1-->" + data?.data!!.toString())
        seletedPngFileUri = convertJpgUriToPngUri(data.data!!, "hssnmirza")!!
        Log.d("hssn", "2-->" + seletedPngFileUri.toString())
        binding.ivInput.setImageURI(seletedPngFileUri)



    } else if (resultCode == ImagePicker.RESULT_ERROR) {
        Toast.makeText(requireContext(), ImagePicker.getError(data), Toast.LENGTH_SHORT)
            .show()
    } else {
        Toast.makeText(requireContext(), "Task Cancelled", Toast.LENGTH_SHORT).show()
    }
}

 private fun convertJpgUriToPngUri(sourceuri: Uri, fName: String): Uri? {
    val sourceFilename = sourceuri.path
    val folderpath = context?.getFilesDir().toString() + File.separator + "sparklab"
    val folder = File(folderpath)
    var destinationFilename = File(folderpath + File.separator + "sparklab_" + fName + ".png")
    try {
        // Make sure the Pictures directory exists.
        folder.mkdirs()
        destinationFilename.createNewFile()
    } catch (e: java.lang.Exception) {
        Log.d("hssn---0", e.toString())
    }


    var bis: BufferedInputStream? = null
    var bos: BufferedOutputStream? = null
    try {
        bis = BufferedInputStream(FileInputStream(sourceFilename))
        bos = BufferedOutputStream(FileOutputStream(destinationFilename, false))
        val buf = ByteArray(1024)
        bis.read(buf)
        do {
            bos.write(buf)
        } while (bis.read(buf) != -1)
    } catch (e: IOException) {
        e.printStackTrace()
        Log.d("hssn", e.toString())
        // destinationFilename = null
    } finally {
        try {
            bis?.close()
            bos?.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
    return Uri.fromFile(destinationFilename)
}

`

@aallam
Copy link
Owner

aallam commented Jan 25, 2023

Correct me if I am wrong, in this code snippet you make a copy of the file with different extension, not actually converting it, right ?

@hassanmirza444
Copy link
Author

No , I am wrong. You are right. Issue was on my side. Now, it is fixed using below function.

` private fun convertJpgToPng(sourceuri: Uri): Uri {
val bitmap = MediaStore.Images.Media.getBitmap(context?.getContentResolver(), sourceuri);

    //folder and file creation
    val folderpath = context?.getFilesDir().toString() + File.separator + "sparklab"
    val folder = File(folderpath)
    val destinationFilename =
        File(folderpath + File.separator + "sparklab_" + "hssn_mirza" + ".png")
    try {
        // Make sure the directory exists.
        folder.mkdirs()
        destinationFilename.createNewFile()
    } catch (e: java.lang.Exception) {
        Log.d("hssn---10", e.toString())
    }

    val outputStream = FileOutputStream(destinationFilename)
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
    outputStream.close()

    return Uri.fromFile(destinationFilename)
}`


Thanks you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants