Skip to content

Instantly share code, notes, and snippets.

@rmckeel
Last active August 30, 2021 23:08
Show Gist options
  • Save rmckeel/c22d614f9590d6d8760d06176da23e7f to your computer and use it in GitHub Desktop.
Save rmckeel/c22d614f9590d6d8760d06176da23e7f to your computer and use it in GitHub Desktop.
Simple tensorflow.js question & answer search over text
<!DOCTYPE html>
<html lang="en">
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/qna"></script>
<script>
const searchText = 'Afghanistan (/æfˈɡænɪstæn, æfˈɡɑːnɪstɑːn/ (About this soundlisten);[12] Pashto/Dari: افغانستان, Afġānestān [avɣɒnesˈtɒn]) is a landlocked country at the crossroads of Central and South Asia. It is bordered by Pakistan to the east and south, Iran to the west, Turkmenistan and Uzbekistan to the north, and Tajikistan and China to the northeast. Occupying 652,864 square kilometers (252,072 sq mi), the country is predominately mountainous with plains in the north and southwest. It is inhabited by 31.4 million people as of 2020, composed mostly of ethnic Pashtuns, Tajiks, Hazaras, and Uzbeks. Kabul serves as its capital and largest city.\n' +
'\n' +
'Human habitation in Afghanistan dates back to the Middle Paleolithic Era, and the country\'s strategic location along the Silk Road connected it to the cultures of the Middle East and other parts of Asia. The land has historically been home to various peoples and has witnessed numerous military campaigns, including those by Alexander the Great, Mauryas, Muslim Arabs, Mongols, British, Soviets, and in 2001 by the United States with NATO-allied countries. It has been called "unconquerable"[13][14] and nicknamed the "graveyard of empires",[15] though it has been occupied during several different periods of its history. The land also served as the source from which the Greco-Bactrians, Kushans, Hephthalites, Samanids, Saffarids, Ghaznavids, Ghorids, Khaljis, Mughals, Hotaks, Durranis, and others have risen to form major empires.[16]\n' +
'\n' +
'The modern state of Afghanistan began with the Hotak and Durrani dynasties in the 18th century. In the late 19th century, Afghanistan became a buffer state in the "Great Game" between British India and the Russian Empire. Following the Third Anglo-Afghan War in 1919, the country became free of foreign dominance, eventually becoming the Kingdom of Afghanistan in June 1926 under King Amanullah. This kingdom lasted almost fifty years, until King Zahir was overthrown and a republic was established in July 1973. In 1978, after a second coup, Afghanistan became a socialist state, provoking the Soviet–Afghan War in the 1980s against mujahideen rebels. By 1996 most of Afghanistan was captured by the Islamic fundamentalist group, the Taliban, who ruled most of the country as a totalitarian regime for over five years. The Taliban were removed from power after the US invasion in 2001 but still controlled a significant portion of the country. The twenty-year-long war between the government and the Taliban reached a climax with the 2021 Taliban offensive and the resulting fall of Kabul which returned the Taliban to power.\n' +
'\n' +
'The country has high levels of terrorism, poverty, child malnutrition, and corruption. It is a member of the United Nations, the Organisation of Islamic Cooperation, the South Asian Association for Regional Cooperation, the Group of 77, the Economic Cooperation Organization, and the Non-Aligned Movement. Afghanistan\'s economy is the world\'s 96th largest, with a gross domestic product (GDP) of $72.9 billion by purchasing power parity; the country fares much worse in terms of per-capita GDP (PPP), ranking 169th out of 186 countries as of 2018.'
const questions = [ 'When did Afghanistan start?',
'Is Afghanistan corrupt?',
'How big is Afghanistan?' // fail
]
const init = () => {
document.getElementById( 'answers' ).innerHTML = 'Loading...'
qna.load().then( async model => {
let response = ''
for ( let i = 0; i < questions.length; i++ ) {
response += `<br><br>Q: ${questions[ i ]}<br>A: `
const answers = await model.findAnswers( questions[ i ], searchText )
response += answers.length ? answers[ 0 ].text : 'Johnny Five does not compute'
}
document.getElementById( 'answers' ).innerHTML = response
} )
}
document.addEventListener( 'DOMContentLoaded', () => {
init()
} )
</script>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="answers"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment