Add OpenAI fallback for Aadhaar extraction
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s

This commit is contained in:
androidlover5842
2026-01-31 05:03:28 +05:30
parent d90b0bb260
commit bd2bca9f33
4 changed files with 100 additions and 22 deletions

View File

@@ -12,22 +12,23 @@ import java.util.UUID
@org.springframework.stereotype.Component
class DocumentExtractionService(
private val llamaClient: LlamaClient,
private val openAIVisionClient: OpenAIVisionClient,
private val guestRepo: GuestRepo,
private val guestVehicleRepo: GuestVehicleRepo,
private val propertyRepo: PropertyRepo
) {
fun extractAndApply(imageUrl: String, document: GuestDocument, propertyId: UUID): ExtractionResult {
fun extractAndApply(localImageUrl: String, publicImageUrl: String, document: GuestDocument, propertyId: UUID): ExtractionResult {
val results = linkedMapOf<String, String>()
val detections = listOf(
Detection(
detect = {
results["isVehiclePhoto"] = llamaClient.ask(
imageUrl,
localImageUrl,
"IS THIS A VEHICLE NUMBER PLATE PHOTO? Answer YES or NO only."
)
if (!isYes(results["isVehiclePhoto"])) return@Detection false
val candidate = llamaClient.ask(
imageUrl,
localImageUrl,
"VEHICLE NUMBER PLATE? Reply only number or NONE."
)
val cleaned = cleanedValue(candidate)
@@ -45,11 +46,11 @@ class DocumentExtractionService(
Detection(
detect = {
results["hasAadhar"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS AADHAAR? Answer YES or NO only."
)
results["hasUidai"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS UIDAI? Answer YES or NO only."
)
isYes(results["hasAadhar"]) || isYes(results["hasUidai"])
@@ -61,7 +62,7 @@ class DocumentExtractionService(
"hasGenderMentioned" to "GENDER MENTIONED? Reply YES or NO."
)
for ((key, question) in aadharQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
val hasAddress = isYes(results["hasAddress"])
if (hasAddress) {
@@ -70,7 +71,7 @@ class DocumentExtractionService(
DocumentPrompts.ADDRESS
)
for ((key, question) in addressQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
}
val hasDob = isYes(results["hasDob"])
@@ -83,20 +84,20 @@ class DocumentExtractionService(
DocumentPrompts.GENDER
)
for ((key, question) in aadharFrontQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
ensureAadhaarId(imageUrl, results)
ensureAadhaarId(localImageUrl, publicImageUrl, results)
}
}
),
Detection(
detect = {
results["hasDrivingLicence"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS DRIVING LICENCE? Answer YES or NO only."
)
results["hasTransportDept"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS TRANSPORT DEPARTMENT? Answer YES or NO only."
)
isYes(results["hasDrivingLicence"]) || isYes(results["hasTransportDept"])
@@ -113,14 +114,14 @@ class DocumentExtractionService(
DocumentPrompts.NATIONALITY
)
for ((key, question) in drivingQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
}
),
Detection(
detect = {
results["hasElectionCommission"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS ELECTION COMMISSION OF INDIA? Answer YES or NO only."
)
isYes(results["hasElectionCommission"])
@@ -137,14 +138,14 @@ class DocumentExtractionService(
DocumentPrompts.NATIONALITY
)
for ((key, question) in voterQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
}
),
Detection(
detect = {
results["hasIncomeTaxDept"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS INCOME TAX DEPARTMENT? Answer YES or NO only."
)
isYes(results["hasIncomeTaxDept"])
@@ -161,14 +162,14 @@ class DocumentExtractionService(
DocumentPrompts.NATIONALITY
)
for ((key, question) in panQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
}
),
Detection(
detect = {
results["hasPassport"] = llamaClient.ask(
imageUrl,
localImageUrl,
"CONTAINS PASSPORT? Answer YES or NO only."
)
isYes(results["hasPassport"])
@@ -185,7 +186,7 @@ class DocumentExtractionService(
DocumentPrompts.NATIONALITY
)
for ((key, question) in passportQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
}
)
@@ -213,7 +214,7 @@ class DocumentExtractionService(
DocumentPrompts.NATIONALITY
)
for ((key, question) in generalQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
results[key] = llamaClient.ask(localImageUrl, question)
}
}
@@ -270,7 +271,7 @@ class DocumentExtractionService(
}
}
private fun ensureAadhaarId(imageUrl: String, results: MutableMap<String, String>) {
private fun ensureAadhaarId(localImageUrl: String, publicImageUrl: String, results: MutableMap<String, String>) {
val key = DocumentPrompts.ID_NUMBER.first
val current = cleanedValue(results[key])
val normalized = normalizeDigits(current)
@@ -279,12 +280,19 @@ class DocumentExtractionService(
return
}
val retry = llamaClient.ask(
imageUrl,
localImageUrl,
"AADHAAR NUMBER (12 digits). Read extremely carefully. Reply ONLY the 12 digits or NONE."
)
val retryNormalized = normalizeDigits(cleanedValue(retry))
if (retryNormalized != null && isValidAadhaar(retryNormalized)) {
results[key] = formatAadhaar(retryNormalized)
return
}
val fallback = openAIVisionClient.extractAadhaarNumber(publicImageUrl) ?: ""
val fallbackNormalized = normalizeDigits(cleanedValue(fallback))
if (fallbackNormalized != null && isValidAadhaar(fallbackNormalized)) {
results[key] = formatAadhaar(fallbackNormalized)
} else {
results[key] = "NONE"
}