{ "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": [ "\"Open" ] }, { "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": [] } ] }