BERT से जुड़े सवाल का जवाब देने वाले व्यक्ति को इंटिग्रेट करें

टास्क लाइब्रेरी का BertQuestionAnswerer एपीआई, बर्ट मॉडल और जवाबों को लोड करता है दिए गए पैसेज के कॉन्टेंट पर आधारित सवाल. ज़्यादा जानकारी के लिए, देखें सवाल-जवाब वाले मॉडल का उदाहरण.

BertQuestionAnswerer एपीआई की मुख्य सुविधाएं

  • सवाल और कॉन्टेक्स्ट के तौर पर दो टेक्स्ट इनपुट लेता है. साथ ही, संभावित क्वेरी की एक सूची देता है जवाब.

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

काम करने वाले BertQuestionAnswerer मॉडल

ये मॉडल, BertNLClassifier API के साथ काम करते हैं.

Java में अनुमान चलाएं

पहला चरण: Gradle डिपेंडेंसी और अन्य सेटिंग इंपोर्ट करना

.tflite मॉडल फ़ाइल को, Android मॉड्यूल की ऐसेट डायरेक्ट्री में कॉपी करें जहां मॉडल को चलाया जाएगा. तय करें कि फ़ाइल कंप्रेस नहीं की जानी चाहिए, और मॉड्यूल की 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
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}

दूसरा चरण: एपीआई का इस्तेमाल करके अनुमान लगाना

// 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);

ज़्यादा जानकारी के लिए, सोर्स कोड देखें.

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

पहला चरण: CocoaPods को इंपोर्ट करना

Podfile में TensorFlowLiteTaskText पॉड जोड़ें

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

दूसरा चरण: एपीआई का इस्तेमाल करके अनुमान लगाना

// 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);

ज़्यादा जानकारी के लिए, सोर्स कोड देखें.

Python में इन्फ़रेंस चलाना

पहला चरण: पीआईपी पैकेज इंस्टॉल करना

pip install tflite-support

दूसरा चरण: मॉडल का इस्तेमाल करना

# 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 को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

परिणामों के उदाहरण

यहां के जवाब के नतीजों का एक उदाहरण दिया गया है ऐल्फ़ा मॉडल.

संदर्भ: "Amazon वर्षावन के साथ-साथ Amazon 7 को भी इसे माना जाता है अंग्रेज़ी के तौर पर Amazonia, Amazon का नम चौड़ी पत्ती वाला वर्षावन है यह बायोम, दक्षिण अमेरिका के ज़्यादातर अमेज़न बेसिन में उपलब्ध है. यह बेसिन 70,00,000 वर्ग किलोमीटर (27,00,000 वर्ग मील) तक फैला है, यह वर्षावन 55,00,000 कि॰मी॰ (21,00,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

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

  • "end_logits" नाम वाले 2 आउटपुट टेंसर और "start_logits" जो कॉन्टेक्स्ट में जवाब की मिलती-जुलती स्थिति