File size: 4,282 Bytes
53ff6ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "gradio_demos.ipynb",
      "private_outputs": true,
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyNhpeeMxUCUtvCsAPt4I1BZ",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/nlp-en-es/nlp-de-cero-a-cien/blob/main/6_demos/gradio_demos.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z5L1Z2AcwUPX"
      },
      "source": [
        "!pip install transformers gradio sentencepiece"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GC1HwQjqQRjS"
      },
      "source": [
        "# Example 1. Normal inference of a model"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AHRKQwWuwrw3"
      },
      "source": [
        "import gradio as gr\n",
        "\n",
        "from transformers import pipeline\n",
        "\n",
        "pipe = pipeline(\"translation\", model=\"Helsinki-NLP/opus-mt-en-es\")\n",
        "\n",
        "def predict(text):\n",
        "  return pipe(text)[0][\"translation_text\"]\n",
        "  \n",
        "title = \"Interactive demo: Helsinki-NLP English to Spanish Translation\"\n",
        "\n",
        "iface = gr.Interface(\n",
        "  fn=predict, \n",
        "  inputs=[gr.inputs.Textbox(label=\"text\", lines=3)],\n",
        "  outputs='text',\n",
        "  title=title,\n",
        "  examples=[[\"Hello! My name is Omar\"], [\"I like this workshop\"]]\n",
        ")\n",
        "\n",
        "iface.launch(debug=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PwTqI-t0QZtM"
      },
      "source": [
        "# Example 2. Using Inference API from Hugging Face"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EDkCXgXBx_MU"
      },
      "source": [
        "import gradio as gr\n",
        "\n",
        "description = \"BigGAN text-to-image demo.\"\n",
        "title = \"BigGAN ImageNet\"\n",
        "\n",
        "interface = gr.Interface.load(\n",
        "    \"huggingface/osanseviero/BigGAN-deep-128\", \n",
        "    description=description,\n",
        "    title = title,\n",
        "    examples=[[\"american robin\"], [\"chest\"], [\"soap bubble\"]]\n",
        ")\n",
        "\n",
        "interface.launch()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WffHZhIzURoW"
      },
      "source": [
        "# Example 3. Using series of models"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sS6iAMYKQpkH"
      },
      "source": [
        "import gradio as gr\n",
        "from gradio.mix import Series\n",
        "\n",
        "description = \"Generate your own D&D story!\"\n",
        "title = \"Spanish Story Generator using Opus MT and GPT-2\"\n",
        "\n",
        "translator_es = gr.Interface.load(\"huggingface/Helsinki-NLP/opus-mt-es-en\")\n",
        "story_gen = gr.Interface.load(\"huggingface/pranavpsv/gpt2-genre-story-generator\")\n",
        "translator_en = gr.Interface.load(\"huggingface/Helsinki-NLP/opus-mt-en-es\")\n",
        "\n",
        "examples = [[\"La aventura comienza en\"]]\n",
        "\n",
        "interface = Series(translator_es, story_gen, translator_en, description = description,\n",
        "      title = title,\n",
        "      examples=examples, \n",
        "      inputs = gr.inputs.Textbox(lines = 10)\n",
        ")\n",
        "\n",
        "interface.launch()"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}