guest docs: improve extractor logic even more

This commit is contained in:
androidlover5842
2026-01-31 03:37:12 +05:30
parent b5aacfc8c6
commit 73f7d41619

View File

@@ -215,83 +215,105 @@ class GuestDocuments(
"${aiBaseUrl}/properties/$propertyId/guests/$guestId/documents/${document.id}/file?token=$token" "${aiBaseUrl}/properties/$propertyId/guests/$guestId/documents/${document.id}/file?token=$token"
val results = linkedMapOf<String, String>() val results = linkedMapOf<String, String>()
results["isVehiclePhoto"] = llamaClient.ask( val detections = listOf(
imageUrl, Detection(
"IS THIS A VEHICLE NUMBER PLATE PHOTO? Answer YES or NO only." detect = {
) results["isVehiclePhoto"] = llamaClient.ask(
imageUrl,
if (isYes(results["isVehiclePhoto"])) { "IS THIS A VEHICLE NUMBER PLATE PHOTO? Answer YES or NO only."
results["vehicleNumber"] = llamaClient.ask( )
imageUrl, isYes(results["isVehiclePhoto"])
"VEHICLE NUMBER PLATE? Reply only number or NONE." },
) handle = {
} results["vehicleNumber"] = llamaClient.ask(
results["hasAadhar"] = llamaClient.ask(imageUrl, "CONTAINS AADHAAR? Answer YES or NO only.") imageUrl,
results["hasUidai"] = llamaClient.ask(imageUrl, "CONTAINS UIDAI? Answer YES or NO only.") "VEHICLE NUMBER PLATE? Reply only number or NONE."
val hasAadhar = isYes(results["hasAadhar"]) || isYes(results["hasUidai"]) )
if (hasAadhar) {
val aadharQuestions = linkedMapOf(
"hasAddress" to "POSTAL ADDRESS PRESENT? Answer YES or NO only.",
"hasDob" to "DOB? Reply YES or NO.",
"hasGenderMentioned" to "GENDER MENTIONED? Reply YES or NO."
)
for ((key, question) in aadharQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
val hasAddress = isYes(results["hasAddress"])
if (hasAddress) {
val addressQuestions = linkedMapOf(
DocumentPrompts.PIN_CODE,
DocumentPrompts.ADDRESS
)
for ((key, question) in addressQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
} }
} ),
val hasDob = isYes(results["hasDob"]) Detection(
val hasGender = isYes(results["hasGenderMentioned"]) detect = {
if (hasDob && hasGender) { results["hasAadhar"] = llamaClient.ask(
val aadharFrontQuestions = linkedMapOf( imageUrl,
DocumentPrompts.NAME, "CONTAINS AADHAAR? Answer YES or NO only."
DocumentPrompts.DOB, )
DocumentPrompts.ID_NUMBER, results["hasUidai"] = llamaClient.ask(
DocumentPrompts.GENDER imageUrl,
) "CONTAINS UIDAI? Answer YES or NO only."
for ((key, question) in aadharFrontQuestions) { )
results[key] = llamaClient.ask(imageUrl, question) isYes(results["hasAadhar"]) || isYes(results["hasUidai"])
},
handle = {
val aadharQuestions = linkedMapOf(
"hasAddress" to "POSTAL ADDRESS PRESENT? Answer YES or NO only.",
"hasDob" to "DOB? Reply YES or NO.",
"hasGenderMentioned" to "GENDER MENTIONED? Reply YES or NO."
)
for ((key, question) in aadharQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
val hasAddress = isYes(results["hasAddress"])
if (hasAddress) {
val addressQuestions = linkedMapOf(
DocumentPrompts.PIN_CODE,
DocumentPrompts.ADDRESS
)
for ((key, question) in addressQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
}
val hasDob = isYes(results["hasDob"])
val hasGender = isYes(results["hasGenderMentioned"])
if (hasDob && hasGender) {
val aadharFrontQuestions = linkedMapOf(
DocumentPrompts.NAME,
DocumentPrompts.DOB,
DocumentPrompts.ID_NUMBER,
DocumentPrompts.GENDER
)
for ((key, question) in aadharFrontQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
}
} }
} ),
Detection(
} else { detect = {
results["hasDrivingLicence"] = llamaClient.ask( results["hasDrivingLicence"] = llamaClient.ask(
imageUrl, imageUrl,
"CONTAINS DRIVING LICENCE? Answer YES or NO only." "CONTAINS DRIVING LICENCE? Answer YES or NO only."
) )
results["hasTransportDept"] = llamaClient.ask( results["hasTransportDept"] = llamaClient.ask(
imageUrl, imageUrl,
"CONTAINS TRANSPORT DEPARTMENT? Answer YES or NO only." "CONTAINS TRANSPORT DEPARTMENT? Answer YES or NO only."
) )
val isDriving = isYes(results["hasDrivingLicence"]) || isYes(results["hasTransportDept"]) isYes(results["hasDrivingLicence"]) || isYes(results["hasTransportDept"])
if (isDriving) { },
val drivingQuestions = linkedMapOf( handle = {
DocumentPrompts.NAME, val drivingQuestions = linkedMapOf(
DocumentPrompts.DOB, DocumentPrompts.NAME,
"idNumber" to "DL NUMBER? Reply only number or NONE.", DocumentPrompts.DOB,
DocumentPrompts.ADDRESS, "idNumber" to "DL NUMBER? Reply only number or NONE.",
DocumentPrompts.PIN_CODE, DocumentPrompts.ADDRESS,
DocumentPrompts.CITY, DocumentPrompts.PIN_CODE,
DocumentPrompts.GENDER, DocumentPrompts.CITY,
DocumentPrompts.NATIONALITY DocumentPrompts.GENDER,
) DocumentPrompts.NATIONALITY
for ((key, question) in drivingQuestions) { )
results[key] = llamaClient.ask(imageUrl, question) for ((key, question) in drivingQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
} }
} else { ),
results["hasElectionCommission"] = llamaClient.ask( Detection(
imageUrl, detect = {
"CONTAINS ELECTION COMMISSION OF INDIA? Answer YES or NO only." results["hasElectionCommission"] = llamaClient.ask(
) imageUrl,
if (isYes(results["hasElectionCommission"])) { "CONTAINS ELECTION COMMISSION OF INDIA? Answer YES or NO only."
)
isYes(results["hasElectionCommission"])
},
handle = {
val voterQuestions = linkedMapOf( val voterQuestions = linkedMapOf(
DocumentPrompts.NAME, DocumentPrompts.NAME,
DocumentPrompts.DOB, DocumentPrompts.DOB,
@@ -305,66 +327,86 @@ class GuestDocuments(
for ((key, question) in voterQuestions) { for ((key, question) in voterQuestions) {
results[key] = llamaClient.ask(imageUrl, question) results[key] = llamaClient.ask(imageUrl, question)
} }
} else { }
),
Detection(
detect = {
results["hasIncomeTaxDept"] = llamaClient.ask( results["hasIncomeTaxDept"] = llamaClient.ask(
imageUrl, imageUrl,
"CONTAINS INCOME TAX DEPARTMENT? Answer YES or NO only." "CONTAINS INCOME TAX DEPARTMENT? Answer YES or NO only."
) )
if (isYes(results["hasIncomeTaxDept"])) { isYes(results["hasIncomeTaxDept"])
val panQuestions = linkedMapOf( },
DocumentPrompts.NAME, handle = {
DocumentPrompts.DOB, val panQuestions = linkedMapOf(
"idNumber" to "PAN NUMBER? Reply only number or NONE.", DocumentPrompts.NAME,
DocumentPrompts.ADDRESS, DocumentPrompts.DOB,
DocumentPrompts.PIN_CODE, "idNumber" to "PAN NUMBER? Reply only number or NONE.",
DocumentPrompts.CITY, DocumentPrompts.ADDRESS,
DocumentPrompts.GENDER, DocumentPrompts.PIN_CODE,
DocumentPrompts.NATIONALITY DocumentPrompts.CITY,
) DocumentPrompts.GENDER,
for ((key, question) in panQuestions) { DocumentPrompts.NATIONALITY
results[key] = llamaClient.ask(imageUrl, question) )
} for ((key, question) in panQuestions) {
} else { results[key] = llamaClient.ask(imageUrl, question)
results["hasPassport"] = llamaClient.ask(
imageUrl,
"CONTAINS PASSPORT? Answer YES or NO only."
)
if (isYes(results["hasPassport"])) {
val passportQuestions = linkedMapOf(
DocumentPrompts.NAME,
DocumentPrompts.DOB,
"idNumber" to "PASSPORT NUMBER? Reply only number or NONE.",
DocumentPrompts.ADDRESS,
DocumentPrompts.PIN_CODE,
DocumentPrompts.CITY,
DocumentPrompts.GENDER,
DocumentPrompts.NATIONALITY
)
for ((key, question) in passportQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
} else {
val generalQuestions = linkedMapOf(
DocumentPrompts.NAME,
DocumentPrompts.DOB,
DocumentPrompts.ID_NUMBER,
DocumentPrompts.ADDRESS,
DocumentPrompts.VEHICLE_NUMBER,
DocumentPrompts.PIN_CODE,
DocumentPrompts.CITY,
DocumentPrompts.GENDER,
DocumentPrompts.NATIONALITY
)
for ((key, question) in generalQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
}
} }
} }
),
Detection(
detect = {
results["hasPassport"] = llamaClient.ask(
imageUrl,
"CONTAINS PASSPORT? Answer YES or NO only."
)
isYes(results["hasPassport"])
},
handle = {
val passportQuestions = linkedMapOf(
DocumentPrompts.NAME,
DocumentPrompts.DOB,
"idNumber" to "PASSPORT NUMBER? Reply only number or NONE.",
DocumentPrompts.ADDRESS,
DocumentPrompts.PIN_CODE,
DocumentPrompts.CITY,
DocumentPrompts.GENDER,
DocumentPrompts.NATIONALITY
)
for ((key, question) in passportQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
}
}
)
)
var handled = false
for (detection in detections) {
if (detection.detect()) {
detection.handle()
handled = true
break
}
}
if (!handled) {
val generalQuestions = linkedMapOf(
DocumentPrompts.NAME,
DocumentPrompts.DOB,
DocumentPrompts.ID_NUMBER,
DocumentPrompts.ADDRESS,
DocumentPrompts.VEHICLE_NUMBER,
DocumentPrompts.PIN_CODE,
DocumentPrompts.CITY,
DocumentPrompts.GENDER,
DocumentPrompts.NATIONALITY
)
for ((key, question) in generalQuestions) {
results[key] = llamaClient.ask(imageUrl, question)
} }
} }
results["docType"] = when { results["docType"] = when {
!handled -> "GENERAL"
isYes(results["hasCourt"]) || isYes(results["hasCourt"]) ||
isYes(results["hasHighCourt"]) || isYes(results["hasHighCourt"]) ||
isYes(results["hasSupremeCourt"]) || isYes(results["hasSupremeCourt"]) ||
@@ -505,6 +547,11 @@ private fun isYes(value: String?): Boolean {
return value.orEmpty().contains("YES", ignoreCase = true) return value.orEmpty().contains("YES", ignoreCase = true)
} }
private data class Detection(
val detect: () -> Boolean,
val handle: () -> Unit
)
private fun cleanedValue(value: String?): String? { private fun cleanedValue(value: String?): String? {
val trimmed = value?.trim().orEmpty() val trimmed = value?.trim().orEmpty()
if (trimmed.isBlank()) return null if (trimmed.isBlank()) return null