Skip to content

Commit

Permalink
Remove support for Python 2 (huggingface#233)
Browse files Browse the repository at this point in the history
* remove support for python <= 3.6

* removing python2-compat future imports and utf8 declarations

* version bump to 4.1.0

* formatting with black for uniformity

* remove u strings, add f strings

* more u strings to remove

* remove 2.7 from Travis CI
  • Loading branch information
svlandeg authored Jan 2, 2020
1 parent 84f29f4 commit 70a4614
Show file tree
Hide file tree
Showing 17 changed files with 1,999 additions and 970 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ group: travis_latest
language: python
cache: pip
python:
- 2.7
- 3.6
- 3.8
#- nightly
#- pypy
#- pypy3
Expand Down
73 changes: 42 additions & 31 deletions bin/cythonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
Note: this script does not check any of the dependent C++ libraries.
"""
from __future__ import print_function

import os
import sys
Expand All @@ -35,41 +34,49 @@
import argparse


HASH_FILE = 'cythonize.json'
HASH_FILE = "cythonize.json"


def process_pyx(fromfile, tofile):
print('Processing %s' % fromfile)
print("Processing %s" % fromfile)
try:
from Cython.Compiler.Version import version as cython_version
from distutils.version import LooseVersion
if LooseVersion(cython_version) < LooseVersion('0.19'):
raise Exception('Require Cython >= 0.19')

if LooseVersion(cython_version) < LooseVersion("0.19"):
raise Exception("Require Cython >= 0.19")

except ImportError:
pass

flags = ['--fast-fail']
if tofile.endswith('.cpp'):
flags += ['--cplus']
flags = ["--fast-fail"]
if tofile.endswith(".cpp"):
flags += ["--cplus"]

try:
try:
r = subprocess.call(['cython'] + flags + ['-o', tofile, fromfile],
env=os.environ) # See Issue #791
r = subprocess.call(
["cython"] + flags + ["-o", tofile, fromfile], env=os.environ
) # See Issue #791
if r != 0:
raise Exception('Cython failed')
raise Exception("Cython failed")
except OSError:
# There are ways of installing Cython that don't result in a cython
# executable on the path, see gh-2397.
r = subprocess.call([sys.executable, '-c',
'import sys; from Cython.Compiler.Main import '
'setuptools_main as main; sys.exit(main())'] + flags +
['-o', tofile, fromfile])
r = subprocess.call(
[
sys.executable,
"-c",
"import sys; from Cython.Compiler.Main import "
"setuptools_main as main; sys.exit(main())",
]
+ flags
+ ["-o", tofile, fromfile]
)
if r != 0:
raise Exception('Cython failed')
raise Exception("Cython failed")
except OSError:
raise OSError('Cython needs to be installed')
raise OSError("Cython needs to be installed")


def preserve_cwd(path, func, *args):
Expand All @@ -89,12 +96,12 @@ def load_hashes(filename):


def save_hashes(hash_db, filename):
with open(filename, 'w') as f:
with open(filename, "w") as f:
f.write(json.dumps(hash_db))


def get_hash(path):
return hashlib.md5(open(path, 'rb').read()).hexdigest()
return hashlib.md5(open(path, "rb").read()).hexdigest()


def hash_changed(base, path, db):
Expand All @@ -109,25 +116,27 @@ def hash_add(base, path, db):

def process(base, filename, db):
root, ext = os.path.splitext(filename)
if ext in ['.pyx', '.cpp']:
if hash_changed(base, filename, db) or not os.path.isfile(os.path.join(base, root + '.cpp')):
preserve_cwd(base, process_pyx, root + '.pyx', root + '.cpp')
hash_add(base, root + '.cpp', db)
hash_add(base, root + '.pyx', db)
if ext in [".pyx", ".cpp"]:
if hash_changed(base, filename, db) or not os.path.isfile(
os.path.join(base, root + ".cpp")
):
preserve_cwd(base, process_pyx, root + ".pyx", root + ".cpp")
hash_add(base, root + ".cpp", db)
hash_add(base, root + ".pyx", db)


def check_changes(root, db):
res = False
new_db = {}

setup_filename = 'setup.py'
hash_add('.', setup_filename, new_db)
if hash_changed('.', setup_filename, db):
setup_filename = "setup.py"
hash_add(".", setup_filename, new_db)
if hash_changed(".", setup_filename, db):
res = True

for base, _, files in os.walk(root):
for filename in files:
if filename.endswith('.pxd'):
if filename.endswith(".pxd"):
hash_add(base, filename, new_db)
if hash_changed(base, filename, db):
res = True
Expand All @@ -150,8 +159,10 @@ def run(root):
save_hashes(db, HASH_FILE)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Cythonize pyx files into C++ files as needed')
parser.add_argument('root', help='root directory')
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Cythonize pyx files into C++ files as needed"
)
parser.add_argument("root", help="root directory")
args = parser.parse_args()
run(args.root)
51 changes: 26 additions & 25 deletions examples/server.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Coreference resolution server example.
A simple server serving the coreference system.
"""
from __future__ import unicode_literals
from __future__ import print_function

import json
from wsgiref.simple_server import make_server
import falcon
import spacy
import neuralcoref

try:
unicode_ = unicode # Python 2
except NameError:
unicode_ = str # Python 3
# Python 3
unicode_ = str


class AllResource(object):
def __init__(self):
self.nlp = spacy.load('en')
self.nlp = spacy.load("en")
neuralcoref.add_to_pipe(self.nlp)
print("Server loaded")
self.response = None
Expand All @@ -35,28 +30,34 @@ def on_get(self, req, resp):
text = unicode_(text)
doc = self.nlp(text)
if doc._.has_coref:
mentions = [{'start': mention.start_char,
'end': mention.end_char,
'text': mention.text,
'resolved': cluster.main.text
}
for cluster in doc._.coref_clusters
for mention in cluster.mentions]
clusters = list(list(span.text for span in cluster)
for cluster in doc._.coref_clusters)
mentions = [
{
"start": mention.start_char,
"end": mention.end_char,
"text": mention.text,
"resolved": cluster.main.text,
}
for cluster in doc._.coref_clusters
for mention in cluster.mentions
]
clusters = list(
list(span.text for span in cluster)
for cluster in doc._.coref_clusters
)
resolved = doc._.coref_resolved
self.response['mentions'] = mentions
self.response['clusters'] = clusters
self.response['resolved'] = resolved
self.response["mentions"] = mentions
self.response["clusters"] = clusters
self.response["resolved"] = resolved

resp.body = json.dumps(self.response)
resp.content_type = 'application/json'
resp.append_header('Access-Control-Allow-Origin', "*")
resp.content_type = "application/json"
resp.append_header("Access-Control-Allow-Origin", "*")
resp.status = falcon.HTTP_200

if __name__ == '__main__':

if __name__ == "__main__":
RESSOURCE = AllResource()
APP = falcon.API()
APP.add_route('/', RESSOURCE)
HTTPD = make_server('0.0.0.0', 8000, APP)
APP.add_route("/", RESSOURCE)
HTTPD = make_server("0.0.0.0", 8000, APP)
HTTPD.serve_forever()
34 changes: 20 additions & 14 deletions neuralcoref/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
# coding: utf8
from __future__ import unicode_literals, absolute_import

import os
import shutil
import tarfile
import tempfile
import logging

# Filter Cython warnings that would force everybody to re-compile from source (like https://github.com/numpy/numpy/pull/432).
import warnings

warnings.filterwarnings("ignore", message="spacy.strings.StringStore size changed")

from neuralcoref.neuralcoref import NeuralCoref
from neuralcoref.file_utils import NEURALCOREF_MODEL_URL, NEURALCOREF_MODEL_PATH, NEURALCOREF_CACHE, cached_path
from neuralcoref.file_utils import (
NEURALCOREF_MODEL_URL,
NEURALCOREF_MODEL_PATH,
NEURALCOREF_CACHE,
cached_path,
)

__all__ = ['NeuralCoref', 'add_to_pipe']
__version__ = "4.0.0"
__all__ = ["NeuralCoref", "add_to_pipe"]
__version__ = "4.1.0"

logger = logging.getLogger(__name__)

if os.path.exists(NEURALCOREF_MODEL_PATH) and os.path.exists(os.path.join(NEURALCOREF_MODEL_PATH, "cfg")):
logger.info("Loading model from {}".format(NEURALCOREF_MODEL_PATH))
if os.path.exists(NEURALCOREF_MODEL_PATH) and os.path.exists(
os.path.join(NEURALCOREF_MODEL_PATH, "cfg")
):
logger.info(f"Loading model from {NEURALCOREF_MODEL_PATH}")
local_model = cached_path(NEURALCOREF_MODEL_PATH)
else:
if not os.path.exists(NEURALCOREF_MODEL_PATH):
os.makedirs(NEURALCOREF_MODEL_PATH)
logger.info("Getting model from {} or cache".format(NEURALCOREF_MODEL_URL))
logger.info(f"Getting model from {NEURALCOREF_MODEL_URL} or cache")
downloaded_model = cached_path(NEURALCOREF_MODEL_URL)

logger.info("extracting archive file {} to dir {}".format(downloaded_model, NEURALCOREF_MODEL_PATH))
with tarfile.open(downloaded_model, 'r:gz') as archive:
logger.info(
f"extracting archive file {downloaded_model} to dir {NEURALCOREF_MODEL_PATH}"
)
with tarfile.open(downloaded_model, "r:gz") as archive:
archive.extractall(NEURALCOREF_CACHE)


def add_to_pipe(nlp, **kwargs):
coref = NeuralCoref(nlp.vocab, **kwargs)
nlp.add_pipe(coref, name='neuralcoref')
nlp.add_pipe(coref, name="neuralcoref")
return nlp
Loading

0 comments on commit 70a4614

Please sign in to comment.