File size: 3,946 Bytes
dce5a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/parth-pai/Learners_Space_2023_NLP/blob/main/LS_Gradio_Run_Final.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yOSyP10ss3nE"
      },
      "source": [
        "##**Mounting Drive**\n",
        "First we mount the google drive here to access the required files"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3iod2CiGnm9C",
        "outputId": "f5662837-44f7-4d45-dbf6-3f09a8cbb1ab"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive', force_remount=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UftTXc3ctEH8"
      },
      "source": [
        "Install required libraries again here as done in Training code"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "34OZ1dPEn_0k"
      },
      "outputs": [],
      "source": [
        "!pip install -q gradio transformers sentencepiece"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ssIvx0O_XeW5"
      },
      "source": [
        "##**Displaying using Gradio**\n",
        "Now we create an interactive app environment using `gradio`. We can see language translation happening with this good looking and interactive app interface."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "aXNwmCQmofdl"
      },
      "outputs": [],
      "source": [
        "from transformers import AutoModelForSeq2SeqLM, pipeline, AutoTokenizer\n",
        "import gradio as gr"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3xbtLJmntR1e"
      },
      "source": [
        "First input the model, the tokenizer and translation pipeline. Then define the function that gradio will be using and input required arguments into gradio giving title and description. Then launch the interface using `iface.launch()`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dPtYKVNCsa_5"
      },
      "outputs": [],
      "source": [
        "model_checkpoint = AutoModelForSeq2SeqLM.from_pretrained('/content/drive/MyDrive/LS_NLP_Sameer')\n",
        "tokenizer=AutoTokenizer.from_pretrained('/content/drive/MyDrive/LS_NLP_Sameer')\n",
        "translator = pipeline(\"translation\", model=model_checkpoint)\n",
        "def translation(text):\n",
        "  return translator(text)[0]['translation_text']\n",
        "iface = gr.Interface(\n",
        "    fn=translation,\n",
        "    inputs=gr.inputs.Textbox(label=\"Input English Text\"),\n",
        "    outputs=gr.outputs.Textbox(label=\"Translated Italian Text\"),\n",
        "    title=\"English to Italian Translation\",\n",
        "    description=\"Translate English text to Italian using a fine-tuned model.\",\n",
        ")\n",
        "\n",
        "iface.launch()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "include_colab_link": true,
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}