File size: 2,198 Bytes
98e6025
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: question-answering"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio torch transformers"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline\n", "\n", "model_name = \"deepset/roberta-base-squad2\"\n", "\n", "nlp = pipeline(\"question-answering\", model=model_name, tokenizer=model_name)\n", "\n", "context = \"The Amazon rainforest, also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.\"\n", "question = \"Which continent is the Amazon rainforest in?\"\n", "\n", "\n", "def predict(context, question):\n", "    res = nlp({\"question\": question, \"context\": context})\n", "    return res[\"answer\"], res[\"score\"]\n", "\n", "\n", "gr.Interface(\n", "    predict,\n", "    inputs=[\n", "        gr.Textbox(lines=7, value=context, label=\"Context Paragraph\"),\n", "        gr.Textbox(lines=2, value=question, label=\"Question\"),\n", "    ],\n", "    outputs=[gr.Textbox(label=\"Answer\"), gr.Textbox(label=\"Score\")],\n", ").launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}