BERT प्रश्न उत्तरकर्ता को एकीकृत करें

टास्क लाइब्रेरी BertQuestionAnswerer API एक बर्ट मॉडल लोड करता है और दिए गए अनुच्छेद की सामग्री के आधार पर प्रश्नों का उत्तर देता है। अधिक जानकारी के लिए, प्रश्न-उत्तर मॉडल के लिए दस्तावेज़ यहां देखें।

BertQuestionAnswerer API की मुख्य विशेषताएं

  • प्रश्न और संदर्भ के रूप में दो टेक्स्ट इनपुट लेता है और संभावित उत्तरों की एक सूची आउटपुट करता है।

  • इनपुट टेक्स्ट पर आउट-ऑफ-ग्राफ वर्डपीस या सेंटेंसपीस टोकननाइजेशन निष्पादित करता है।

समर्थित BertQuestionAnswerer मॉडल

निम्नलिखित मॉडल BertNLClassifier API के साथ संगत हैं।

जावा में अनुमान चलाएँ

चरण 1: ग्रैडल निर्भरता और अन्य सेटिंग्स आयात करें

.tflite मॉडल फ़ाइल को एंड्रॉइड मॉड्यूल की संपत्ति निर्देशिका में कॉपी करें जहां मॉडल चलाया जाएगा। निर्दिष्ट करें कि फ़ाइल को संपीड़ित नहीं किया जाना चाहिए, और मॉड्यूल की build.gradle फ़ाइल में TensorFlow Lite लाइब्रेरी जोड़ें:

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import the Task Text Library dependency (NNAPI is included)
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}

चरण 2: एपीआई का उपयोग करके अनुमान चलाएँ

// Initialization
BertQuestionAnswererOptions options =
    BertQuestionAnswererOptions.builder()
        .setBaseOptions(BaseOptions.builder().setNumThreads(4).build())
        .build();
BertQuestionAnswerer answerer =
    BertQuestionAnswerer.createFromFileAndOptions(
        androidContext, modelFile, options);

// Run inference
List<QaAnswer> answers = answerer.answer(contextOfTheQuestion, questionToAsk);

अधिक विवरण के लिए स्रोत कोड देखें।

स्विफ्ट में अनुमान चलाएँ

चरण 1: कोकोपोड्स आयात करें

पॉडफाइल में TensorFlowLiteTaskText पॉड जोड़ें

target 'MySwiftAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskText', '~> 0.4.4'
end

चरण 2: एपीआई का उपयोग करके अनुमान चलाएँ

// Initialization
let mobileBertAnswerer = TFLBertQuestionAnswerer.questionAnswerer(
      modelPath: mobileBertModelPath)

// Run inference
let answers = mobileBertAnswerer.answer(
      context: context, question: question)

अधिक विवरण के लिए स्रोत कोड देखें।

C++ में अनुमान चलाएँ

// Initialization
BertQuestionAnswererOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<BertQuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();

// Run inference with your inputs, `context_of_question` and `question_to_ask`.
std::vector<QaAnswer> positive_results = answerer->Answer(context_of_question, question_to_ask);

अधिक विवरण के लिए स्रोत कोड देखें।

पायथन में अनुमान चलाएँ

चरण 1: पिप पैकेज स्थापित करें

pip install tflite-support

चरण 2: मॉडल का उपयोग करना

# Imports
from tflite_support.task import text

# Initialization
answerer = text.BertQuestionAnswerer.create_from_file(model_path)

# Run inference
bert_qa_result = answerer.answer(context, question)

BertQuestionAnswerer कॉन्फ़िगर करने के लिए अधिक विकल्पों के लिए स्रोत कोड देखें।

उदाहरण परिणाम

यहां अल्बर्ट मॉडल के उत्तर परिणामों का एक उदाहरण दिया गया है।

संदर्भ: "अमेज़ॅन वर्षावन, वैकल्पिक रूप से, अमेज़ॅन जंगल, जिसे अंग्रेजी में अमेजोनिया भी कहा जाता है, अमेज़ॅन बायोम में एक नम चौड़ी पत्ती वाला उष्णकटिबंधीय वर्षावन है जो दक्षिण अमेरिका के अधिकांश अमेज़ॅन बेसिन को कवर करता है। यह बेसिन 7,000,000 किमी 2 (2,700,000 वर्ग मील) में फैला है ), जिसमें से 5,500,000 किमी2 (2,100,000 वर्ग मील) वर्षावन से आच्छादित है। इस क्षेत्र में नौ देशों से संबंधित क्षेत्र शामिल हैं।"

प्रश्न: "अमेज़ॅन वर्षावन कहाँ है?"

उत्तर:

answer[0]:  'South America.'
logit: 1.84847, start_index: 39, end_index: 40
answer[1]:  'most of the Amazon basin of South America.'
logit: 1.2921, start_index: 34, end_index: 40
answer[2]:  'the Amazon basin of South America.'
logit: -0.0959535, start_index: 36, end_index: 40
answer[3]:  'the Amazon biome that covers most of the Amazon basin of South America.'
logit: -0.498558, start_index: 28, end_index: 40
answer[4]:  'Amazon basin of South America.'
logit: -0.774266, start_index: 37, end_index: 40

अपने स्वयं के मॉडल और परीक्षण डेटा के साथ BertQuestionAnswerer के लिए सरल सीएलआई डेमो टूल आज़माएं।

मॉडल अनुकूलता आवश्यकताएँ

BertQuestionAnswerer API अनिवार्य TFLite मॉडल मेटाडेटा के साथ एक TFLite मॉडल की अपेक्षा करता है।

मेटाडेटा को निम्नलिखित आवश्यकताओं को पूरा करना चाहिए:

  • वर्डपीस/सेंटेंसपीस टोकनाइज़र के लिए input_process_units

  • टोकननाइज़र के आउटपुट के लिए "आईडी", "मास्क" और "सेगमेंट_आईडी" नाम वाले 3 इनपुट टेंसर

  • संदर्भ में उत्तर की सापेक्ष स्थिति को इंगित करने के लिए "end_logits" और "start_logits" नाम वाले 2 आउटपुट टेंसर