-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from deeppavlov/dev
Release 0.4.0
- Loading branch information
Showing
134 changed files
with
5,606 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ requests==2.22.0 | |
sentry-sdk[asgi]==1.3.1 | ||
jinja2<=3.0.3 | ||
Werkzeug<=2.0.3 | ||
importlib-metadata<5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FROM pytorch/pytorch:1.5-cuda10.1-cudnn7-runtime | ||
|
||
WORKDIR /src | ||
|
||
ARG PRETRAINED_MODEL_NAME_OR_PATH | ||
ENV PRETRAINED_MODEL_NAME_OR_PATH ${PRETRAINED_MODEL_NAME_OR_PATH} | ||
ARG SERVICE_PORT | ||
ENV SERVICE_PORT ${SERVICE_PORT} | ||
|
||
COPY ./requirements.txt /src/requirements.txt | ||
RUN pip install -r /src/requirements.txt | ||
RUN pip install sentencepiece | ||
|
||
RUN python -c "from transformers import XLMRobertaTokenizer; XLMRobertaTokenizer.from_pretrained('${PRETRAINED_MODEL_NAME_OR_PATH}');" | ||
RUN python -c "from transformers import XLMRobertaForSequenceClassification; XLMRobertaForSequenceClassification.from_pretrained('${PRETRAINED_MODEL_NAME_OR_PATH}');" | ||
|
||
COPY . /src | ||
|
||
CMD gunicorn --workers=1 server:app -b 0.0.0.0:${SERVICE_PORT} --timeout=300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Coming soon ;) |
10 changes: 10 additions & 0 deletions
10
annotators/MultilingualSentimentClassification/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
transformers==4.20.1 | ||
flask==1.1.1 | ||
gunicorn==19.9.0 | ||
requests==2.22.0 | ||
sentry-sdk[flask]==0.14.1 | ||
healthcheck==1.3.3 | ||
itsdangerous==2.0.1 | ||
jinja2<=3.0.3 | ||
Werkzeug<=2.0.3 | ||
torch==1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import logging | ||
import time | ||
import os | ||
|
||
import sentry_sdk | ||
import torch | ||
from flask import Flask, request, jsonify | ||
from sentry_sdk.integrations.flask import FlaskIntegration | ||
from transformers import XLMRobertaForSequenceClassification, XLMRobertaTokenizer | ||
|
||
sentry_sdk.init(dsn=os.getenv("SENTRY_DSN"), integrations=[FlaskIntegration()]) | ||
|
||
|
||
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
PRETRAINED_MODEL_NAME_OR_PATH = os.environ.get("PRETRAINED_MODEL_NAME_OR_PATH") | ||
logging.info(f"PRETRAINED_MODEL_NAME_OR_PATH = {PRETRAINED_MODEL_NAME_OR_PATH}") | ||
columns = ["negative", "neutral", "positive"] | ||
|
||
try: | ||
tokenizer = XLMRobertaTokenizer.from_pretrained(PRETRAINED_MODEL_NAME_OR_PATH) | ||
model = XLMRobertaForSequenceClassification.from_pretrained(PRETRAINED_MODEL_NAME_OR_PATH) | ||
model.eval() | ||
if torch.cuda.is_available(): | ||
model.to("cuda") | ||
logger.info("sentiment-classification is set to run on cuda") | ||
|
||
logger.info("sentiment-classification model is ready") | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
logger.exception(e) | ||
raise e | ||
|
||
app = Flask(__name__) | ||
logging.getLogger("werkzeug").setLevel("WARNING") | ||
|
||
|
||
def classify_sentences(sentences): | ||
try: | ||
inputs = tokenizer(sentences, return_tensors="pt", truncation=True, padding=True) | ||
with torch.no_grad(): | ||
if torch.cuda.is_available(): | ||
inputs = inputs.to("cuda") | ||
outputs = model(**inputs)[0] | ||
model_output = torch.nn.functional.softmax(outputs, dim=-1).cpu() | ||
result = [] | ||
|
||
for i, cla in zip(sentences, model_output): | ||
result += [{columns[id_column]: float(cla[id_column]) for id_column in range(len(columns))}] | ||
|
||
except Exception as exc: | ||
logger.exception(exc) | ||
sentry_sdk.capture_exception(exc) | ||
result = [{column: 0.0 for column in columns}] * len(sentences) | ||
return result | ||
|
||
|
||
@app.route("/respond", methods=["POST"]) | ||
def respond(): | ||
st_time = time.time() | ||
sentences = request.json.get("sentences", []) | ||
result = classify_sentences(sentences) | ||
total_time = time.time() - st_time | ||
logger.info(f"sentiment-classification exec time: {total_time:.3f}s") | ||
|
||
return jsonify(result) | ||
|
||
|
||
@app.route("/respond_batch", methods=["POST"]) | ||
def respond_batch(): | ||
st_time = time.time() | ||
sentences = request.json.get("sentences", []) | ||
result = classify_sentences(sentences) | ||
total_time = time.time() - st_time | ||
logger.info(f"sentiment-classification exec time: {total_time:.3f}s") | ||
|
||
return jsonify([{"batch": result}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import requests | ||
|
||
|
||
def test_respond(): | ||
url = "http://0.0.0.0:8024/respond" | ||
|
||
sentences = ["Hola! Como estas?", "örnek metin", "Болван несчастный"] | ||
gold = [ | ||
{"negative": 0.0274, "neutral": 0.706, "positive": 0.2666}, | ||
{"negative": 0.29077, "neutral": 0.33038, "positive": 0.37885}, | ||
{"negative": 0.94606, "neutral": 0.03936, "positive": 0.01458}, | ||
] | ||
request_data = {"sentences": sentences} | ||
result = requests.post(url, json=request_data).json() | ||
assert [{i: round(j[i], 5) for i in j} for j in result] == gold, f"Got\n{result}" | ||
print("Success!") | ||
|
||
|
||
if __name__ == "__main__": | ||
test_respond() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
python test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM pytorch/pytorch:1.5-cuda10.1-cudnn7-runtime | ||
|
||
WORKDIR /src | ||
|
||
ARG PRETRAINED_MODEL_NAME_OR_PATH | ||
ENV PRETRAINED_MODEL_NAME_OR_PATH ${PRETRAINED_MODEL_NAME_OR_PATH} | ||
ARG SERVICE_PORT | ||
ENV SERVICE_PORT ${SERVICE_PORT} | ||
|
||
COPY ./requirements.txt /src/requirements.txt | ||
RUN pip install -r /src/requirements.txt | ||
|
||
#RUN python -c "import torch; torch.hub.load_state_dict_from_url('${PRETRAINED_MODEL_NAME_OR_PATH}')" | ||
#RUN python -c "from transformers import AutoModelForSequenceClassification; AutoModelForSequenceClassification.from_pretrained('${PRETRAINED_MODEL_NAME_OR_PATH}');" | ||
|
||
COPY . /src | ||
|
||
CMD gunicorn --workers=1 server:app -b 0.0.0.0:${SERVICE_PORT} --timeout=300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Coming soon ;) |
Binary file added
BIN
+431 Bytes
annotators/MultilingualToxicClassification/multilingual_toxic_xlm_r
Binary file not shown.
11 changes: 11 additions & 0 deletions
11
annotators/MultilingualToxicClassification/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
transformers==4.20.1 | ||
sentencepiece==0.1.94 | ||
flask==1.1.1 | ||
gunicorn==19.9.0 | ||
requests==2.22.0 | ||
sentry-sdk[flask]==0.14.1 | ||
healthcheck==1.3.3 | ||
itsdangerous==2.0.1 | ||
jinja2<=3.0.3 | ||
Werkzeug<=2.0.3 | ||
torch==1.8 |
Oops, something went wrong.