Skip to content

Instantly share code, notes, and snippets.

@itsuncheng
Created January 19, 2021 09:15
Show Gist options
  • Save itsuncheng/8a4ce7deb1d4965211e07c3ecb593b6c to your computer and use it in GitHub Desktop.
Save itsuncheng/8a4ce7deb1d4965211e07c3ecb593b6c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question Answering in English"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"from transformers import pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"question_answering = pipeline(\"question-answering\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"context = \"\"\"Machine learning (ML) is the study of computer algorithms that improve automatically through experience. It is seen as a part of artificial intelligence. Machine learning algorithms build a model based on sample data, known as \"training data\", in order to make predictions or decisions without being explicitly programmed to do so. Machine learning algorithms are used in a wide variety of applications, such as email filtering and computer vision, where it is difficult or unfeasible to develop conventional algorithms to perform the needed tasks.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"question = \"What are machine learning models based on?\""
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/raymond/.conda/envs/movie_script_generation/lib/python3.6/site-packages/transformers/tokenization_utils_base.py:1423: FutureWarning: The `max_len` attribute has been deprecated and will be removed in a future version, use `model_max_length` instead.\n",
" FutureWarning,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer: sample data,\n",
"Score: 0.8846667408943176\n"
]
}
],
"source": [
"result = question_answering(question=question, context=context)\n",
"\n",
"print(\"Answer:\", result['answer'])\n",
"print(\"Score:\", result['score'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question Answering in Another Language Example"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"question_answering = pipeline(\"question-answering\", model=\"mrm8488/bert-multi-cased-finetuned-xquadv1\",\n",
" tokenizer=\"mrm8488/bert-multi-cased-finetuned-xquadv1\")"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/raymond/.conda/envs/movie_script_generation/lib/python3.6/site-packages/transformers/tokenization_utils_base.py:1423: FutureWarning: The `max_len` attribute has been deprecated and will be removed in a future version, use `model_max_length` instead.\n",
" FutureWarning,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer: 机器学习是人工智能的一个分支。\n",
"Score: 0.9717233777046204\n"
]
}
],
"source": [
"context = \"\"\"机器学习是人工智能的一个分支。 是个人很热门的专业。\"\"\"\n",
"question = \"机器学习是什么的分支?\"\n",
"\n",
"result = question_answering(question=question, context=context)\n",
"\n",
"print(\"Answer:\", result['answer'])\n",
"print(\"Score:\", result['score'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment