mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-17 11:29:58 +08:00
13 KiB
13 KiB
Example Usage of Gradio¶
Here is the code to define a model and train it. It may take a few minutes to train on a machine without GPUs
In [1]:
import numpy as np import tensorflow as tf import sklearn import gradio %load_ext autoreload %autoreload 2
In [2]:
mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train.reshape(-1, 784) / 255.0, x_test.reshape(-1, 784) / 255.0 model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation=tf.nn.relu), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=1) model.evaluate(x_test, y_test)
WARNING:tensorflow:From C:\Users\ALI\Anaconda3\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. WARNING:tensorflow:From C:\Users\ALI\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version. Instructions for updating: Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`. 60000/60000 [==============================] - 15s 256us/sample - loss: 0.2202 - acc: 0.9343 10000/10000 [==============================] - 0s 48us/sample - loss: 0.1078 - acc: 0.9659
Out[2]:
[0.10782068010494113, 0.9659]
Here, we simply take the trained model and pass it into gradio. When you run this, it should open up a new browser window and show allow you to interact with the model.
In [7]:
gradio.Interface(input='webcam',output='class',model=model, model_type='keras').launch()
Model available locally at: http://localhost:7860/templates/tmp_html.html Model available publicly for 8 hours at: http://b424b6a9.ngrok.io/templates/tmp_html.html
--------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-7-544d74ae98c8> in <module> ----> 1 gradio.Interface(input='webcam',output='class',model=model, model_type='keras').launch() ~\Desktop\gradiome\gradio.py in launch(self, share_link) 98 print("Model available publicly for 8 hours at: {}".format(ngrok_url + '/' + path_to_template)) 99 --> 100 asyncio.get_event_loop().run_until_complete(start_server) 101 try: 102 asyncio.get_event_loop().run_forever() ~\Anaconda3\lib\site-packages\nest_asyncio.py in run_until_complete(self, future) 59 while not f.done(): 60 run_once(self) ---> 61 return f.result() 62 else: 63 return self._run_until_complete_orig(future) ~\Anaconda3\lib\asyncio\futures.py in result(self) 176 self.__log_traceback = False 177 if self._exception is not None: --> 178 raise self._exception 179 return self._result 180 ~\Anaconda3\lib\asyncio\tasks.py in __step(***failed resolving arguments***) 221 # We use the `send` method directly, because coroutines 222 # don't have `__iter__` and `__next__` methods. --> 223 result = coro.send(None) 224 else: 225 result = coro.throw(exc) ~\Anaconda3\lib\asyncio\tasks.py in _wrap_awaitable(awaitable) 601 that will later be wrapped in a Task by ensure_future(). 602 """ --> 603 return (yield from awaitable.__await__()) 604 605 ~\Anaconda3\lib\site-packages\websockets\py35\server.py in __await_impl__(self) 11 # Duplicated with __iter__ because Python 3.7 requires an async function 12 # (as explained in __await__ below) which Python 3.4 doesn't support. ---> 13 server = await self._creating_server 14 self.ws_server.wrap(server) 15 return self.ws_server ~\Anaconda3\lib\asyncio\base_events.py in create_server(self, protocol_factory, host, port, family, flags, sock, backlog, ssl, reuse_address, reuse_port, ssl_handshake_timeout, start_serving) 1365 raise OSError(err.errno, 'error while attempting ' 1366 'to bind on address %r: %s' -> 1367 % (sa, err.strerror.lower())) from None 1368 completed = True 1369 finally: OSError: [Errno 10048] error while attempting to bind on address ('127.0.0.1', 9200): only one usage of each socket address (protocol/network address/port) is normally permitted