facebookresearch / BLINK

Entity Linker solution
MIT License
1.17k stars 232 forks source link

JSON Web API #45

Open EntilZha opened 3 years ago

EntilZha commented 3 years ago

Hi, we've been using BLINK for some work at UMD and have been finding it helpful to create web API wrappers around various entity linkers to make it easier for multiple projects to use. Earlier this week I implemented the first pass on one for BLINK, and wanted to check if there is interest in me creating a PR for it. Thanks!

The code is here: https://github.com/EntilZha/BLINK/blob/master/blink/main_api.py

and run with python blink/main_api.py --fast --mode api

image

The response looks like this:

{
  "samples": [
    {
      "label": "unknown",
      "label_id": -1,
      "context_left": "",
      "context_right": " was a british computer scientist known for creating the turing machine.",
      "mention": "alan turing",
      "start_pos": 0,
      "end_pos": 11,
      "sent_idx": 0
    },
    {
      "label": "unknown",
      "label_id": -1,
      "context_left": "alan turing was a ",
      "context_right": " computer scientist known for creating the turing machine.",
      "mention": "british",
      "start_pos": 18,
      "end_pos": 25,
      "sent_idx": 0
    },
    {
      "label": "unknown",
      "label_id": -1,
      "context_left": "alan turing was a british computer scientist known for creating the ",
      "context_right": ".",
      "mention": "turing machine",
      "start_pos": 68,
      "end_pos": 82,
      "sent_idx": 0
    }
  ],
  "linked_entities": [
    {
      "idx": 0,
      "sample": {
        "label": "unknown",
        "label_id": -1,
        "context_left": "",
        "context_right": " was a british computer scientist known for creating the turing machine.",
        "mention": "alan turing",
        "start_pos": 0,
        "end_pos": 11,
        "sent_idx": 0
      },
      "entity_id": 308,
      "entity_title": "Alan Turing",
      "entity_text": " Alan Mathison Turing (; 23 June 1912 – 7 June 1954) was an English mathematician, computer scientist, logician, cryptanalyst, philosopher and theoretical biologist. Turing was highly influential in the development of theoretical computer science, providing a formalisation of the concepts of algorithm and computation with the Turing machine, which can be considered a model of a general-purpose computer. Turing is widely considered to be the father of theoretical computer science and artificial intelligence. Despite these accomplishments, he was never fully recognised in his home country during his lifetime, due to his homosexuality, which was then a crime in the UK, and because his work was covered by the Official Secrets Act.  During the Second World War, Turing worked for the Government Code and Cypher School (GC&CS) at Bletchley Park, Britain's codebreaking centre that produced Ultra intelligence. For a time he led Hut 8, the section that was responsible for German naval cryptanalysis. Here, he devised a number of techniques for speeding the breaking of German ciphers, including improvements to the pre-war Polish bombe method, an electromechanical machine that could find settings for the Enigma machine.  Turing played a pivotal role in cracking intercepted coded messages that enabled the Allies to defeat the Nazis in many crucial engagements, including the Battle of the Atlantic, and in so doing helped win the war. Due to the problems of counterfactual history, it's hard to estimate what effect Ultra intelligence had on the war, but at the upper end it has been estimated that this work shortened the",
      "url": "https://en.wikipedia.org/wiki?curid=1208",
      "crossencoder": false
    },
    {
      "idx": 1,
      "sample": {
        "label": "unknown",
        "label_id": -1,
        "context_left": "alan turing was a ",
        "context_right": " computer scientist known for creating the turing machine.",
        "mention": "british",
        "start_pos": 18,
        "end_pos": 25,
        "sent_idx": 0
      },
      "entity_id": 15734,
      "entity_title": "United Kingdom",
      "entity_text": " The United Kingdom of Great Britain and Northern Ireland, commonly known as the United Kingdom (UK) or Britain, is a sovereign country located off the north-western coast of the European mainland. The United Kingdom includes the island of Great Britain, the north-eastern part of the island of Ireland, and many smaller islands. Northern Ireland is the only part of the United Kingdom that shares a land border with another sovereign state, the Republic of Ireland. Apart from this land border, the United Kingdom is surrounded by the Atlantic Ocean, with the North Sea to the east, the English Channel to the south and the Celtic Sea to the south-west, giving it the 12th-longest coastline in the world. The Irish Sea lies between Great Britain and Ireland. The United Kingdom's were home to an estimated 66.0 million inhabitants in 2017.  The United Kingdom is a unitary parliamentary democracy and constitutional monarchy. The current monarch is Queen Elizabeth II, who has reigned since 1952, making her the world's longest-serving current head of state. The United Kingdom's capital and largest city is London, a global city and financial centre with an urban area population of 10.3 million. Other major cities include Birmingham, Manchester, Glasgow, Leeds and Liverpool.  The United Kingdom consists of four constituent countries: England, Scotland, Wales, and Northern Ireland. Their capitals are London, Edinburgh, Cardiff, and Belfast, respectively. Apart from England, the countries have their own devolved governments, each with varying powers, but such power is delegated by the Parliament of the United Kingdom,",
      "url": "https://en.wikipedia.org/wiki?curid=31717",
      "crossencoder": false
    },
    {
      "idx": 2,
      "sample": {
        "label": "unknown",
        "label_id": -1,
        "context_left": "alan turing was a british computer scientist known for creating the ",
        "context_right": ".",
        "mention": "turing machine",
        "start_pos": 68,
        "end_pos": 82,
        "sent_idx": 0
      },
      "entity_id": 15078,
      "entity_title": "Turing machine",
      "entity_text": " A Turing machine is a mathematical model of computation that defines an abstract machine, which manipulates symbols on a strip of tape according to a table of rules. Despite the model's simplicity, given any computer algorithm, a Turing machine capable of simulating that algorithm's logic can be constructed.  The machine operates on an infinite memory tape divided into discrete \"cells\". The machine positions its \"head\" over a cell and \"reads\" or \"scans\" the symbol there. Then, as per the symbol and its present place in a \"finite table\" of user-specified instructions, the machine (i) writes a symbol (e.g., a digit or a letter from a finite alphabet) in the cell (some models allowing symbol erasure or no writing), then (ii) either moves the tape one cell left or right (some models allow no motion, some models move the head), then (iii) (as determined by the observed symbol and the machine's place in the table) either proceeds to a subsequent instruction or halts the computation.  The Turing machine was invented in 1936 by Alan Turing, who called it an \"a-machine\" (automatic machine). With this model, Turing was able to answer two questions in the negative: (1) Does a machine exist that can determine whether any arbitrary machine on its tape is \"circular\" (e.g., freezes, or fails to continue its computational task); similarly, (2) does a machine exist that can determine whether any arbitrary machine on its tape ever prints a given symbol. Thus by providing a mathematical description of a very simple device capable",
      "url": "https://en.wikipedia.org/wiki?curid=30403",
      "crossencoder": false
    }
  ]
}
ledw commented 3 years ago

@EntilZha This is super cool! Yes please send a PR and we're happy to review it.