{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import gradio as gr\n", "import os\n", "\n", "os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'\n", "import requests\n", "\n", "from spacy import displacy" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "\n", "def compute_ner(input_text_message):\n", " endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'\n", "\n", " headers = {\n", " 'Authorization': 'Bearer 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',\n", " 'Content-Type': 'application/json',\n", " }\n", "\n", " json_data = {\n", " 'inputs': input_text_message,\n", " }\n", "\n", " response = requests.post(endpoint_url, headers=headers, json=json_data)\n", "\n", " tokens = response.json()\n", "\n", " entities = []\n", "\n", " for token in tokens:\n", " label = token[\"entity\"]\n", "\n", " if label == \"I-Observation\" or label == \"B-Observation\":\n", " label = \"Observation\"\n", " token[\"label\"] = label\n", " entities.append(token)\n", "\n", " if label == \"I-Evaluation\" or label == \"B-Evaluation\":\n", " label = \"Evaluation\"\n", " token[\"label\"] = label\n", " entities.append(token)\n", "\n", " params = [{\"text\": input_text_message,\n", " \"ents\": entities,\n", " \"title\": None}]\n", "\n", " return displacy.render(params, style=\"ent\", manual=True, options={\n", " \"colors\": {\n", " \"Observation\": \"#9bddff\",\n", " \"Evaluation\": \"#f08080\",\n", " },\n", " })\n", "\n", "\n", "examples = ['You are dick',\n", " 'My dad is an asshole and took his anger out on my mom by verbally abusing her','He eventually moved on to my brother']\n", "\n", "iface = gr.Interface(fn=compute_ner, inputs=gr.inputs.Textbox(lines=5, placeholder=\"Enter your text here\",label='Check your text on NVC rules'),\n", " outputs=\"html\", examples=examples,)\n", "iface.launch(debug=True)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'\n", "\n", "headers = {\n", " 'Authorization': 'Bearer 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',\n", " 'Content-Type': 'application/json',\n", "}\n", "\n", "json_data = {\n", " 'inputs': 'you are dick',\n", "}\n", "\n", "response = requests.post(endpoint_url, headers=headers, json=json_data)\n", "\n", "tokens = response.json()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "tokens" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import spacy\n", "from spacy import displacy" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "!python -m spacy download en_core_web_sm" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "\n", "text = \"When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously.\"\n", "\n", "nlp = spacy.load(\"en_core_web_sm\")\n", "doc = nlp(text)\n", "displacy.serve(doc, style=\"ent\")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "name": "torch", "language": "python", "display_name": "torch" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }