Code Monkey home page Code Monkey logo

bert-squad's Introduction

BERT-SQuAD

Use google BERT to do SQuAD !

What is SQuAD?

Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span, from the corresponding reading passage, or the question might be unanswerable.

Requirements

  • python3
  • pip3 install -r requirements.txt

Result

model : bert-large-uncased-whole-word-masking

{"exact_match": 86.91579943235573, "f1": 93.1532499015869}

Pretrained model download from here

unzip and move files to model directory

Inference

from bert import QA

model = QA('model')

doc = "Victoria has a written constitution enacted in 1975, but based on the 1855 colonial constitution, passed by the United Kingdom Parliament as the Victoria Constitution Act 1855, which establishes the Parliament as the state's law-making body for matters coming under state responsibility. The Victorian Constitution can be amended by the Parliament of Victoria, except for certain 'entrenched' provisions that require either an absolute majority in both houses, a three-fifths majority in both houses, or the approval of the Victorian people in a referendum, depending on the provision."

q = 'When did Victoria enact its constitution?'

answer = model.predict(doc,q)

print(answer['answer'])
# 1975
print(answer.keys())
# dict_keys(['answer', 'start', 'end', 'confidence', 'document']))

model.predict(doc,q) return dict

{
"answer" : "answer text",
"start" : "start index",
"end" : "end index",
"confiednce" : "confidence of answer",
"document" : "tokenzied document , list"
}

Deploy REST-API

BERT QA model deployed as rest api

python api.py

API will be live at 0.0.0.0:8000 endpoint predict

cURL request

curl -X POST http://0.0.0.0:8000/predict -H 'Content-Type: application/json' -d '{ "document": "Victoria has a written constitution enacted in 1975, but based on the 1855 colonial constitution, passed by the United Kingdom Parliament as the Victoria Constitution Act 1855, which establishes the Parliament as the states law-making body for matters coming under state responsibility. The Victorian Constitution can be amended by the Parliament of Victoria, except for certain 'entrenched' provisions that require either an absolute majority in both houses, a three-fifths majority in both houses, or the approval of the Victorian people in a referendum, depending on the provision.","question":"When did Victoria enact its constitution?" }'

Output

{
    "result": {
        "answer": "1975",
        "confidence": 0.940746070672879,
        "document": [
            "Victoria",
            "has",
            "a",
            "written",
            "constitution",
            "enacted",
            "in",
            "1975,",
            "but",
            "based",
            "on",
            "the",
            "1855",
            "colonial",
            "constitution,",
            "passed",
            "by",
            "the",
            "United",
            "Kingdom",
            "Parliament",
            "as",
            "the",
            "Victoria",
            "Constitution",
            "Act",
            "1855,",
            "which",
            "establishes",
            "the",
            "Parliament",
            "as",
            "the",
            "states",
            "law-making",
            "body",
            "for",
            "matters",
            "coming",
            "under",
            "state",
            "responsibility.",
            "The",
            "Victorian",
            "Constitution",
            "can",
            "be",
            "amended",
            "by",
            "the",
            "Parliament",
            "of",
            "Victoria,",
            "except",
            "for",
            "certain",
            "entrenched",
            "provisions",
            "that",
            "require",
            "either",
            "an",
            "absolute",
            "majority",
            "in",
            "both",
            "houses,",
            "a",
            "three-fifths",
            "majority",
            "in",
            "both",
            "houses,",
            "or",
            "the",
            "approval",
            "of",
            "the",
            "Victorian",
            "people",
            "in",
            "a",
            "referendum,",
            "depending",
            "on",
            "the",
            "provision."
        ],
        "end": 7,
        "start": 7
    }
}

Postman

postman output image

bert-squad's People

Contributors

kamalkraj avatar ruqyai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.