File size: 1,774 Bytes
05f5f1a
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: text_analysis\n", "### This simple demo takes advantage of Gradio's HighlightedText, JSON and HTML outputs to create a clear NER segmentation.\n", "        "]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio spacy"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import os\n", "os.system('python -m spacy download en_core_web_sm')\n", "import spacy\n", "from spacy import displacy\n", "\n", "nlp = spacy.load(\"en_core_web_sm\")\n", "\n", "def text_analysis(text):\n", "    doc = nlp(text)\n", "    html = displacy.render(doc, style=\"dep\", page=True)\n", "    html = (\n", "        \"<div style='max-width:100%; max-height:360px; overflow:auto'>\"\n", "        + html\n", "        + \"</div>\"\n", "    )\n", "    pos_count = {\n", "        \"char_count\": len(text),\n", "        \"token_count\": 0,\n", "    }\n", "    pos_tokens = []\n", "\n", "    for token in doc:\n", "        pos_tokens.extend([(token.text, token.pos_), (\" \", None)])\n", "\n", "    return pos_tokens, pos_count, html\n", "\n", "demo = gr.Interface(\n", "    text_analysis,\n", "    gr.Textbox(placeholder=\"Enter sentence here...\"),\n", "    [\"highlight\", \"json\", \"html\"],\n", "    examples=[\n", "        [\"What a beautiful morning for a walk!\"],\n", "        [\"It was the best of times, it was the worst of times.\"],\n", "    ],\n", ")\n", "\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}