Add OpenAI fallback for Aadhaar extraction
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package com.android.trisolarisserver.component
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.http.HttpEntity
|
||||
import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.client.RestTemplate
|
||||
|
||||
@Component
|
||||
class OpenAIVisionClient(
|
||||
private val restTemplate: RestTemplate,
|
||||
private val objectMapper: ObjectMapper,
|
||||
@Value("\${openai.apiKey:}")
|
||||
private val apiKey: String,
|
||||
@Value("\${openai.baseUrl:https://api.openai.com/v1/responses}")
|
||||
private val baseUrl: String,
|
||||
@Value("\${openai.model:gpt-5-mini}")
|
||||
private val model: String
|
||||
) {
|
||||
fun extractAadhaarNumber(imageUrl: String): String? {
|
||||
if (apiKey.isBlank()) return null
|
||||
|
||||
val payload = mapOf(
|
||||
"model" to model,
|
||||
"input" to listOf(
|
||||
mapOf(
|
||||
"role" to "user",
|
||||
"content" to listOf(
|
||||
mapOf(
|
||||
"type" to "input_text",
|
||||
"text" to "Read extremely carefully. Aadhaar number = 12 digits. Reply ONLY the 12 digits or NONE."
|
||||
),
|
||||
mapOf(
|
||||
"type" to "input_image",
|
||||
"image_url" to imageUrl
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
val headers = HttpHeaders()
|
||||
headers.contentType = MediaType.APPLICATION_JSON
|
||||
headers.setBearerAuth(apiKey)
|
||||
val entity = HttpEntity(payload, headers)
|
||||
val response = restTemplate.postForEntity(baseUrl, entity, String::class.java)
|
||||
val body = response.body ?: return null
|
||||
val node = objectMapper.readTree(body)
|
||||
|
||||
val outputText = node.path("output_text").asText()
|
||||
if (outputText.isNotBlank()) return outputText
|
||||
|
||||
val outputArray = node.path("output")
|
||||
if (outputArray.isArray && outputArray.size() > 0) {
|
||||
val content = outputArray[0].path("content")
|
||||
if (content.isArray && content.size() > 0) {
|
||||
val text = content[0].path("text").asText()
|
||||
if (text.isNotBlank()) return text
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user