Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sentence tokenization to process longer texts. #71

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from bert import Ner

import nltk.data
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')

app = Flask(__name__)
CORS(app)

Expand All @@ -11,12 +14,25 @@
@app.route("/predict",methods=['POST'])
def predict():
text = request.json["text"]
sentences = sent_detector.tokenize(text)

try:
out = model.predict(text)
return jsonify({"result":out})
ner_tagged_sentences = []
for sentence in sentences:
if len(sentence) < 512:
tagged_sentence = model.predict(sentence)
ner_tagged_sentences.extend(tagged_sentence)
else:
chunks = [sentence[x:x+512] for x in range(0, len(sentence), 512)]
tagged_sentence = []
for chunk in chunks:
ner_chunk = model.predict(chunk)
tagged_sentence.extend(ner_chunk)
ner_tagged_sentences.extend(tagged_sentence)
return jsonify({"result":ner_tagged_sentences})
except Exception as e:
print(e)
return jsonify({"result":"Model Failed"})

if __name__ == "__main__":
app.run('0.0.0.0',port=8000)
app.run('0.0.0.0',port=8000)