Task Library BertQuestionAnswerer API, Bert मॉडल को लोड करता है और दिए गए पैसेज के कॉन्टेंट के आधार पर सवालों के जवाब देता है. ज़्यादा जानकारी के लिए, सवाल-जवाब वाले मॉडल का उदाहरण देखें.
BertQuestionAnswerer API की मुख्य सुविधाएं
यह सवाल और संदर्भ के तौर पर दो टेक्स्ट इनपुट लेता है और संभावित जवाबों की सूची दिखाता है.
यह इनपुट टेक्स्ट पर, Wordpiece या Sentencepiece टोकनाइज़ेशन को ग्राफ़ से बाहर ले जाकर लागू करता है.
BertQuestionAnswerer मॉडल के साथ काम करने वाले मॉडल
इन मॉडल के साथ BertNLClassifier API का इस्तेमाल किया जा सकता है.
BERT Question Answer के लिए TensorFlow Lite Model Maker की मदद से बनाए गए मॉडल.
कस्टम मॉडल, जो मॉडल के साथ काम करने की ज़रूरी शर्तों को पूरा करते हों.
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);
ज़्यादा जानकारी के लिए, सोर्स कोड देखें.
Swift में अनुमान लगाने की सुविधा इस्तेमाल करना
पहला चरण: 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 पैकेज इंस्टॉल करना
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 को कॉन्फ़िगर करने के ज़्यादा विकल्पों के लिए, सोर्स कोड देखें.
परिणामों के उदाहरण
ALBERT मॉडल के जवाब के नतीजों का एक उदाहरण यहां दिया गया है.
संदर्भ: "अमेज़न के वर्षावन को अंग्रेज़ी में अमेज़ोनिया या अमेज़न जंगल कहा जाता है. यह नमी वाले चौड़े पत्तों का उष्णकटिबंधीय वर्षावन है और अफ़्रीका की अमेज़न घाटी में ज़्यादातर जगह फैला है. इस बेसिन का क्षेत्रफल 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 एपीआई को TFLite मॉडल मेटाडेटा के साथ TFLite मॉडल की ज़रूरत होती है.
मेटाडेटा को इन ज़रूरी शर्तों को पूरा करना चाहिए:
input_process_unitsfor Wordpiece/Sentencepiece Tokenizerटोकनाइज़र के आउटपुट के लिए, "ids", "mask", और "segment_ids" नाम वाले तीन इनपुट टेंसर
"end_logits" और "start_logits" नाम वाले दो आउटपुट टेंसर, जवाब की रिलेटिव पोज़िशन को कॉन्टेक्स्ट में दिखाते हैं