analyseIsSensitiveContent method

  1. @override
Future analyseIsSensitiveContent({
  1. Uint8List? receiptImageBytes,
  2. String text = '',
})
override

Classifies the content from both optional receiptImageBytes and text for sensitive categories using Gemini.

If receiptImageBytes is null, only the text is classified. If both are null or empty, it returns null.

Returns an AiClassificationResponse or null in case of error.

Implementation

@override
Future analyseIsSensitiveContent({
  Uint8List? receiptImageBytes,
  String text = '',
}) async {
  final systemPrompt = TextPart(textPrompt);
  final userPrompt = TextPart(text);
  DataPart? image;
  if (receiptImageBytes != null) {
    image = DataPart('image/jpeg', receiptImageBytes);
  }
  try {
    GenerateContentResponse response;
    if (image != null) {
      response = await _geminiModel.generateContent([
        Content.multi([systemPrompt, userPrompt, image]),
      ]);
    } else {
      response = await _geminiModel.generateContent([
        Content.multi([systemPrompt, userPrompt]),
      ]);
    }
    String? jsonString = response.text;

    if (jsonString == null || jsonString.isEmpty) return null;
    Map<String, dynamic> resultMap = jsonDecode(jsonString);

    final result = AiClassificationResponse.fromMap(resultMap);
    return result;
  } catch (e) {
    _logger.e('Error while analysing content: $e');
    return null;
  }
}