Skip to content

Commit 1ccd91a

Browse files
committed
Created using Colaboratory
1 parent 4e73f45 commit 1ccd91a

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "01.Gradio_Transformer_translation.ipynb",
7+
"provenance": [],
8+
"authorship_tag": "ABX9TyN1Ic3+1wOaz80ygMA+1kZi",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"language_info": {
16+
"name": "python"
17+
}
18+
},
19+
"cells": [
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "view-in-github",
24+
"colab_type": "text"
25+
},
26+
"source": [
27+
"<a href=\"https://colab.research.google.com/github/gulabpatel/Python_Tutorials/blob/master/New_Python_Libraries/02.Gradio_Transformer_translation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"metadata": {
33+
"id": "VsVaB3G7CqPD"
34+
},
35+
"source": [
36+
"!pip install -q gradio\n",
37+
"!pip install -q transformers"
38+
],
39+
"execution_count": 1,
40+
"outputs": []
41+
},
42+
{
43+
"cell_type": "code",
44+
"metadata": {
45+
"id": "hNRnSxLFG6X0"
46+
},
47+
"source": [
48+
"import gradio as gr"
49+
],
50+
"execution_count": 2,
51+
"outputs": []
52+
},
53+
{
54+
"cell_type": "code",
55+
"metadata": {
56+
"id": "p_UbK4W3HDAQ"
57+
},
58+
"source": [
59+
"import transformers"
60+
],
61+
"execution_count": 3,
62+
"outputs": []
63+
},
64+
{
65+
"cell_type": "code",
66+
"metadata": {
67+
"colab": {
68+
"base_uri": "https://localhost:8080/",
69+
"height": 35
70+
},
71+
"id": "7yY7Wum9HOU8",
72+
"outputId": "dd92b6da-9d69-4422-d340-45c2f09d517e"
73+
},
74+
"source": [
75+
"transformers.__version__"
76+
],
77+
"execution_count": 4,
78+
"outputs": [
79+
{
80+
"output_type": "execute_result",
81+
"data": {
82+
"application/vnd.google.colaboratory.intrinsic+json": {
83+
"type": "string"
84+
},
85+
"text/plain": [
86+
"'4.11.1'"
87+
]
88+
},
89+
"metadata": {},
90+
"execution_count": 4
91+
}
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"metadata": {
97+
"id": "s7Ie2WlpHTPB"
98+
},
99+
"source": [
100+
"from transformers import pipeline"
101+
],
102+
"execution_count": 5,
103+
"outputs": []
104+
},
105+
{
106+
"cell_type": "code",
107+
"metadata": {
108+
"colab": {
109+
"base_uri": "https://localhost:8080/"
110+
},
111+
"id": "tEnwoBX-Ha1V",
112+
"outputId": "71343143-144e-41f1-9737-d20e0e323080"
113+
},
114+
"source": [
115+
"translation_pipe = pipeline(\"translation_en_to_fr\")"
116+
],
117+
"execution_count": 6,
118+
"outputs": [
119+
{
120+
"output_type": "stream",
121+
"name": "stderr",
122+
"text": [
123+
"No model was supplied, defaulted to t5-base (https://huggingface.co/t5-base)\n"
124+
]
125+
}
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"metadata": {
131+
"id": "xOpDrVmpH0ZT"
132+
},
133+
"source": [
134+
"result = translation_pipe(\"I like Mathematics and Computers\")"
135+
],
136+
"execution_count": 7,
137+
"outputs": []
138+
},
139+
{
140+
"cell_type": "code",
141+
"metadata": {
142+
"colab": {
143+
"base_uri": "https://localhost:8080/",
144+
"height": 35
145+
},
146+
"id": "MyFLuOOjIJ7b",
147+
"outputId": "94487eef-f2e2-4901-8735-0b6d7063fa38"
148+
},
149+
"source": [
150+
"result[0]['translation_text']"
151+
],
152+
"execution_count": 8,
153+
"outputs": [
154+
{
155+
"output_type": "execute_result",
156+
"data": {
157+
"application/vnd.google.colaboratory.intrinsic+json": {
158+
"type": "string"
159+
},
160+
"text/plain": [
161+
"\"J'aime les mathématiques et l'informatique\""
162+
]
163+
},
164+
"metadata": {},
165+
"execution_count": 8
166+
}
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"metadata": {
172+
"id": "ZfNNt-qTIKrC"
173+
},
174+
"source": [
175+
"def translate_gradio(input):\n",
176+
" result = translation_pipe(input)\n",
177+
" return result[0]['translation_text']"
178+
],
179+
"execution_count": 9,
180+
"outputs": []
181+
},
182+
{
183+
"cell_type": "code",
184+
"metadata": {
185+
"id": "4BPf0PnNJHwZ"
186+
},
187+
"source": [
188+
"translate_interface = gr.Interface(fn=translate_gradio, inputs=\"text\", outputs=\"text\")"
189+
],
190+
"execution_count": 10,
191+
"outputs": []
192+
},
193+
{
194+
"cell_type": "code",
195+
"metadata": {
196+
"colab": {
197+
"base_uri": "https://localhost:8080/",
198+
"height": 891
199+
},
200+
"id": "MfuC5N62JyOW",
201+
"outputId": "e2b8af58-edae-4a9c-8467-8d93f8493a5e"
202+
},
203+
"source": [
204+
"translate_interface.launch(debug=True)"
205+
],
206+
"execution_count": 11,
207+
"outputs": [
208+
{
209+
"output_type": "stream",
210+
"name": "stdout",
211+
"text": [
212+
"Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
213+
"This share link will expire in 72 hours. If you need a permanent link, visit: https://gradio.app/introducing-hosted\n",
214+
"Running on External URL: https://43675.gradio.app\n",
215+
"Interface loading below...\n"
216+
]
217+
},
218+
{
219+
"output_type": "display_data",
220+
"data": {
221+
"text/html": [
222+
"\n",
223+
" <iframe\n",
224+
" width=\"900\"\n",
225+
" height=\"500\"\n",
226+
" src=\"https://43675.gradio.app\"\n",
227+
" frameborder=\"0\"\n",
228+
" allowfullscreen\n",
229+
" ></iframe>\n",
230+
" "
231+
],
232+
"text/plain": [
233+
"<IPython.lib.display.IFrame at 0x7f9453248e10>"
234+
]
235+
},
236+
"metadata": {}
237+
},
238+
{
239+
"output_type": "error",
240+
"ename": "KeyboardInterrupt",
241+
"evalue": "ignored",
242+
"traceback": [
243+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
244+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
245+
"\u001b[0;32m<ipython-input-11-3db5863613ea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtranslate_interface\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
246+
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/gradio/interface.py\u001b[0m in \u001b[0;36mlaunch\u001b[0;34m(self, inline, inbrowser, share, debug, auth, auth_message, private_endpoint, prevent_thread_lock)\u001b[0m\n\u001b[1;32m 588\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 589\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 590\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 591\u001b[0m \u001b[0mis_in_interactive_mode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ps1'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minteractive\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 592\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mprevent_thread_lock\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_in_interactive_mode\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
247+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
248+
]
249+
}
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"metadata": {
255+
"id": "r4IdhWFLKGkr"
256+
},
257+
"source": [
258+
""
259+
],
260+
"execution_count": null,
261+
"outputs": []
262+
}
263+
]
264+
}

0 commit comments

Comments
 (0)