diff --git a/.idea/deployment.xml b/.idea/deployment.xml
deleted file mode 100644
index 36ea77f8f1..0000000000
--- a/.idea/deployment.xml
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/gradiome.iml b/.idea/gradiome.iml
deleted file mode 100644
index 7c9d48f0fd..0000000000
--- a/.idea/gradiome.iml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index ba24381645..0000000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index 5fc9b91518..0000000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7f4c..0000000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index add3aaf50d..0000000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,640 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Edit
- button
- see an ex
- jumobo
- jumbotron
- Silicon s
- lead
- draw a
- see an
- pip ins
- Developed
- Usage
- server
- zip
- resize_
- base
- ws
- <em
- 6002
- INITIAL_WEBSOCKET_PORT
- print(
- inputs.
- subprocess
- psutil
- tmp
- tmp_h
- JS_PATH_TEMP
- JS_PATH_LO
- path_to_server
- /
-
-
- 400
- ../css/
- Source
- //
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- true
- DEFINITION_ORDER
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1545238786403
-
-
- 1545238786403
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Test Notebook.ipynb b/Test Notebook.ipynb
index 8abe277d16..68d0071ec5 100644
--- a/Test Notebook.ipynb
+++ b/Test Notebook.ipynb
@@ -2,7 +2,17 @@
"cells": [
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "%load_ext autoreload\n",
+ "%autoreload 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -11,7 +21,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -21,7 +31,7 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -30,20 +40,13 @@
"text": [
"NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu\n",
"Model available locally at: http://localhost:7862/interface.html\n",
- "Model available publicly for 8 hours at: http://ae6f8734.ngrok.io/interface.html\n"
+ "Model available publicly for 8 hours at: http://bf3590aa.ngrok.io/interface.html\n"
]
}
],
"source": [
"gradio.Interface(input=\"textbox\", output=\"textbox\", model_type=\"func\", model=test).launch()"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
diff --git a/gradio/__init__.py b/gradio/__init__.py
index adcf0b9675..fc501c0e8a 100644
--- a/gradio/__init__.py
+++ b/gradio/__init__.py
@@ -1,157 +1 @@
-import asyncio
-import websockets
-import nest_asyncio
-import webbrowser
-import pkg_resources
-from bs4 import BeautifulSoup
-from gradio import inputs
-from gradio import outputs
-from gradio import networking
-import os
-import shutil
-import tempfile
-
-nest_asyncio.apply()
-
-LOCALHOST_IP = '127.0.0.1'
-INITIAL_WEBSOCKET_PORT = 9200
-TRY_NUM_PORTS = 100
-
-BASE_TEMPLATE = pkg_resources.resource_filename('gradio', 'templates/all_io.html')
-JS_PATH_LIB = pkg_resources.resource_filename('gradio', 'js/')
-CSS_PATH_LIB = pkg_resources.resource_filename('gradio', 'css/')
-JS_PATH_TEMP = 'js/'
-CSS_PATH_TEMP = 'css/'
-TEMPLATE_TEMP = 'interface.html'
-BASE_JS_FILE = 'js/all-io.js'
-
-
-class Interface():
- """
- """
-
- def __init__(self, input, output, model, model_type, preprocessing_fn=None, postprocessing_fn=None):
- """
- :param model_type: what kind of trained model, can be 'keras' or 'sklearn'.
- :param model_obj: the model object, such as a sklearn classifier or keras model.
- :param model_params: additional model parameters.
- """
- self.input_interface = inputs.registry[input](preprocessing_fn)
- self.output_interface = outputs.registry[output](postprocessing_fn)
- self.model_type = model_type
- self.model_obj = model
-
- def _build_template(self, temp_dir):
- input_template_path = pkg_resources.resource_filename(
- 'gradio', self.input_interface._get_template_path())
- output_template_path = pkg_resources.resource_filename(
- 'gradio', self.output_interface._get_template_path())
- input_page = open(input_template_path)
- output_page = open(output_template_path)
- input_soup = BeautifulSoup(input_page.read(), features="html.parser")
- output_soup = BeautifulSoup(output_page.read(), features="html.parser")
-
- all_io_page = open(BASE_TEMPLATE)
- all_io_soup = BeautifulSoup(all_io_page.read(), features="html.parser")
- input_tag = all_io_soup.find("div", {"id": "input"})
- output_tag = all_io_soup.find("div", {"id": "output"})
-
- input_tag.replace_with(input_soup)
- output_tag.replace_with(output_soup)
-
- f = open(os.path.join(temp_dir, TEMPLATE_TEMP), "w")
- f.write(str(all_io_soup.prettify))
-
- self._copy_files(JS_PATH_LIB, os.path.join(temp_dir, JS_PATH_TEMP))
- self._copy_files(CSS_PATH_LIB, os.path.join(temp_dir, CSS_PATH_TEMP))
- return
-
- def _copy_files(self, src_dir, dest_dir):
- if not os.path.exists(dest_dir):
- os.makedirs(dest_dir)
- src_files = os.listdir(src_dir)
- for file_name in src_files:
- full_file_name = os.path.join(src_dir, file_name)
- if os.path.isfile(full_file_name):
- shutil.copy(full_file_name, dest_dir)
-
- def _set_socket_url_in_js(self, temp_dir, socket_url):
- with open(os.path.join(temp_dir, BASE_JS_FILE)) as fin:
- lines = fin.readlines()
- lines[0] = 'var NGROK_URL = "{}"\n'.format(socket_url.replace('http', 'ws'))
-
- with open(os.path.join(temp_dir, BASE_JS_FILE), 'w') as fout:
- for line in lines:
- fout.write(line)
-
- def _set_socket_port_in_js(self, temp_dir, socket_port):
- with open(os.path.join(temp_dir, BASE_JS_FILE)) as fin:
- lines = fin.readlines()
- lines[1] = 'var SOCKET_PORT = {}\n'.format(socket_port)
-
- with open(os.path.join(temp_dir, BASE_JS_FILE), 'w') as fout:
- for line in lines:
- fout.write(line)
-
- def predict(self, array):
- if self.model_type=='sklearn':
- return self.model_obj.predict(array)
- elif self.model_type=='keras':
- return self.model_obj.predict(array)
- elif self.model_type=='func':
- return self.model_obj(array)
- else:
- raise ValueError('model_type must be one of: "sklearn" or "keras" or "func".')
-
- async def communicate(self, websocket, path):
- """
- Method that defines how this interface communicates with the websocket.
- :param websocket: a Websocket object used to communicate with the interface frontend
- :param path: ignored
- """
- while True:
- try:
- msg = await websocket.recv()
- processed_input = self.input_interface._pre_process(msg)
- prediction = self.predict(processed_input)
- processed_output = self.output_interface._post_process(prediction)
- await websocket.send(str(processed_output))
- except websockets.exceptions.ConnectionClosed:
- pass
-
- def launch(self, share_link=True):
- """
- Standard method shared by interfaces that launches a websocket at a specified IP address.
- """
- networking.kill_processes([4040, 4041])
- output_directory = tempfile.mkdtemp()
-
- server_port = networking.start_simple_server(output_directory)
- path_to_server = 'http://localhost:{}/'.format(server_port)
- self._build_template(output_directory)
-
- ports_in_use = networking.get_ports_in_use()
- for i in range(TRY_NUM_PORTS):
- if not ((INITIAL_WEBSOCKET_PORT + i) in ports_in_use):
- break
- else:
- raise OSError("All ports from {} to {} are in use. Please close a port.".format(
- INITIAL_WEBSOCKET_PORT, INITIAL_WEBSOCKET_PORT + TRY_NUM_PORTS))
-
- start_server = websockets.serve(self.communicate, LOCALHOST_IP, INITIAL_WEBSOCKET_PORT + i)
- self._set_socket_port_in_js(output_directory, INITIAL_WEBSOCKET_PORT + i)
-
- if share_link:
- site_ngrok_url = networking.setup_ngrok(server_port)
- socket_ngrok_url = networking.setup_ngrok(INITIAL_WEBSOCKET_PORT, api_url=networking.NGROK_TUNNELS_API_URL2)
- self._set_socket_url_in_js(output_directory, socket_ngrok_url)
- print("NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu")
- print("Model available locally at: {}".format(path_to_server + TEMPLATE_TEMP))
- print("Model available publicly for 8 hours at: {}".format(site_ngrok_url + '/' + TEMPLATE_TEMP))
- asyncio.get_event_loop().run_until_complete(start_server)
- try:
- asyncio.get_event_loop().run_forever()
- except RuntimeError: # Runtime errors are thrown in jupyter notebooks because of async.
- pass
-
- webbrowser.open(path_to_server + TEMPLATE_TEMP)
+from interface import Interface # This makes Interface importable as gradio.Interface.
diff --git a/gradio/interface.py b/gradio/interface.py
new file mode 100644
index 0000000000..adcf0b9675
--- /dev/null
+++ b/gradio/interface.py
@@ -0,0 +1,157 @@
+import asyncio
+import websockets
+import nest_asyncio
+import webbrowser
+import pkg_resources
+from bs4 import BeautifulSoup
+from gradio import inputs
+from gradio import outputs
+from gradio import networking
+import os
+import shutil
+import tempfile
+
+nest_asyncio.apply()
+
+LOCALHOST_IP = '127.0.0.1'
+INITIAL_WEBSOCKET_PORT = 9200
+TRY_NUM_PORTS = 100
+
+BASE_TEMPLATE = pkg_resources.resource_filename('gradio', 'templates/all_io.html')
+JS_PATH_LIB = pkg_resources.resource_filename('gradio', 'js/')
+CSS_PATH_LIB = pkg_resources.resource_filename('gradio', 'css/')
+JS_PATH_TEMP = 'js/'
+CSS_PATH_TEMP = 'css/'
+TEMPLATE_TEMP = 'interface.html'
+BASE_JS_FILE = 'js/all-io.js'
+
+
+class Interface():
+ """
+ """
+
+ def __init__(self, input, output, model, model_type, preprocessing_fn=None, postprocessing_fn=None):
+ """
+ :param model_type: what kind of trained model, can be 'keras' or 'sklearn'.
+ :param model_obj: the model object, such as a sklearn classifier or keras model.
+ :param model_params: additional model parameters.
+ """
+ self.input_interface = inputs.registry[input](preprocessing_fn)
+ self.output_interface = outputs.registry[output](postprocessing_fn)
+ self.model_type = model_type
+ self.model_obj = model
+
+ def _build_template(self, temp_dir):
+ input_template_path = pkg_resources.resource_filename(
+ 'gradio', self.input_interface._get_template_path())
+ output_template_path = pkg_resources.resource_filename(
+ 'gradio', self.output_interface._get_template_path())
+ input_page = open(input_template_path)
+ output_page = open(output_template_path)
+ input_soup = BeautifulSoup(input_page.read(), features="html.parser")
+ output_soup = BeautifulSoup(output_page.read(), features="html.parser")
+
+ all_io_page = open(BASE_TEMPLATE)
+ all_io_soup = BeautifulSoup(all_io_page.read(), features="html.parser")
+ input_tag = all_io_soup.find("div", {"id": "input"})
+ output_tag = all_io_soup.find("div", {"id": "output"})
+
+ input_tag.replace_with(input_soup)
+ output_tag.replace_with(output_soup)
+
+ f = open(os.path.join(temp_dir, TEMPLATE_TEMP), "w")
+ f.write(str(all_io_soup.prettify))
+
+ self._copy_files(JS_PATH_LIB, os.path.join(temp_dir, JS_PATH_TEMP))
+ self._copy_files(CSS_PATH_LIB, os.path.join(temp_dir, CSS_PATH_TEMP))
+ return
+
+ def _copy_files(self, src_dir, dest_dir):
+ if not os.path.exists(dest_dir):
+ os.makedirs(dest_dir)
+ src_files = os.listdir(src_dir)
+ for file_name in src_files:
+ full_file_name = os.path.join(src_dir, file_name)
+ if os.path.isfile(full_file_name):
+ shutil.copy(full_file_name, dest_dir)
+
+ def _set_socket_url_in_js(self, temp_dir, socket_url):
+ with open(os.path.join(temp_dir, BASE_JS_FILE)) as fin:
+ lines = fin.readlines()
+ lines[0] = 'var NGROK_URL = "{}"\n'.format(socket_url.replace('http', 'ws'))
+
+ with open(os.path.join(temp_dir, BASE_JS_FILE), 'w') as fout:
+ for line in lines:
+ fout.write(line)
+
+ def _set_socket_port_in_js(self, temp_dir, socket_port):
+ with open(os.path.join(temp_dir, BASE_JS_FILE)) as fin:
+ lines = fin.readlines()
+ lines[1] = 'var SOCKET_PORT = {}\n'.format(socket_port)
+
+ with open(os.path.join(temp_dir, BASE_JS_FILE), 'w') as fout:
+ for line in lines:
+ fout.write(line)
+
+ def predict(self, array):
+ if self.model_type=='sklearn':
+ return self.model_obj.predict(array)
+ elif self.model_type=='keras':
+ return self.model_obj.predict(array)
+ elif self.model_type=='func':
+ return self.model_obj(array)
+ else:
+ raise ValueError('model_type must be one of: "sklearn" or "keras" or "func".')
+
+ async def communicate(self, websocket, path):
+ """
+ Method that defines how this interface communicates with the websocket.
+ :param websocket: a Websocket object used to communicate with the interface frontend
+ :param path: ignored
+ """
+ while True:
+ try:
+ msg = await websocket.recv()
+ processed_input = self.input_interface._pre_process(msg)
+ prediction = self.predict(processed_input)
+ processed_output = self.output_interface._post_process(prediction)
+ await websocket.send(str(processed_output))
+ except websockets.exceptions.ConnectionClosed:
+ pass
+
+ def launch(self, share_link=True):
+ """
+ Standard method shared by interfaces that launches a websocket at a specified IP address.
+ """
+ networking.kill_processes([4040, 4041])
+ output_directory = tempfile.mkdtemp()
+
+ server_port = networking.start_simple_server(output_directory)
+ path_to_server = 'http://localhost:{}/'.format(server_port)
+ self._build_template(output_directory)
+
+ ports_in_use = networking.get_ports_in_use()
+ for i in range(TRY_NUM_PORTS):
+ if not ((INITIAL_WEBSOCKET_PORT + i) in ports_in_use):
+ break
+ else:
+ raise OSError("All ports from {} to {} are in use. Please close a port.".format(
+ INITIAL_WEBSOCKET_PORT, INITIAL_WEBSOCKET_PORT + TRY_NUM_PORTS))
+
+ start_server = websockets.serve(self.communicate, LOCALHOST_IP, INITIAL_WEBSOCKET_PORT + i)
+ self._set_socket_port_in_js(output_directory, INITIAL_WEBSOCKET_PORT + i)
+
+ if share_link:
+ site_ngrok_url = networking.setup_ngrok(server_port)
+ socket_ngrok_url = networking.setup_ngrok(INITIAL_WEBSOCKET_PORT, api_url=networking.NGROK_TUNNELS_API_URL2)
+ self._set_socket_url_in_js(output_directory, socket_ngrok_url)
+ print("NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu")
+ print("Model available locally at: {}".format(path_to_server + TEMPLATE_TEMP))
+ print("Model available publicly for 8 hours at: {}".format(site_ngrok_url + '/' + TEMPLATE_TEMP))
+ asyncio.get_event_loop().run_until_complete(start_server)
+ try:
+ asyncio.get_event_loop().run_forever()
+ except RuntimeError: # Runtime errors are thrown in jupyter notebooks because of async.
+ pass
+
+ webbrowser.open(path_to_server + TEMPLATE_TEMP)