From b1f6904fc6fc21f622e5354b6643d45850d5dcab Mon Sep 17 00:00:00 2001 From: khush Date: Tue, 9 May 2023 09:36:08 +0300 Subject: [PATCH 1/3] solution --- autotests/app/Dockerfile | 12 ++ autotests/app/src/main.js | 193 +++++++++++++++++---------------- autotests/helm/values.yaml | 4 +- solution/helm/.dockerignore | 23 ++++ solution/helm/Dockerfile | 11 ++ solution/helm/app.py | 187 ++++++++++++++++++++++++++++++++ solution/helm/requirements.txt | 8 ++ 7 files changed, 340 insertions(+), 98 deletions(-) create mode 100644 autotests/app/Dockerfile create mode 100644 solution/helm/.dockerignore create mode 100644 solution/helm/Dockerfile create mode 100644 solution/helm/app.py create mode 100644 solution/helm/requirements.txt diff --git a/autotests/app/Dockerfile b/autotests/app/Dockerfile new file mode 100644 index 0000000..b57391c --- /dev/null +++ b/autotests/app/Dockerfile @@ -0,0 +1,12 @@ +FROM grafana/xk6 as xk6_builder +RUN xk6 build --output k6 --with github.com/szkiba/xk6-faker@latest \ + --with github.com/grafana/xk6-output-prometheus-remote@latest + + +FROM grafana/k6 + +WORKDIR /app/ +COPY src/main.js /app/ +COPY --from=xk6_builder /xk6/k6 /usr/bin/k6 + +ENTRYPOINT k6 run -o xk6-prometheus-rw main.js \ No newline at end of file diff --git a/autotests/app/src/main.js b/autotests/app/src/main.js index 3d63cde..4def6bc 100644 --- a/autotests/app/src/main.js +++ b/autotests/app/src/main.js @@ -1,110 +1,111 @@ -import http from 'k6/http'; -import { check } from 'k6'; -import { Faker } from "k6/x/faker" - -let f = new Faker(); - -export const options = { - // Tag for grafana dashboard - tags: { - testid: `${__ENV.PARTICIPANT_NAME}`, - }, - scenarios: { - does_it_even_work: { - executor: 'constant-arrival-rate', - rate: 1, - timeUnit: '1s', - duration: '1m', - preAllocatedVUs: 1, - maxVUs: 1, - exec: 'post_radom', - }, - - highload_scenario: { - executor: 'ramping-arrival-rate', - startTime: '61s', - preAllocatedVUs: 10, - maxVUs: 50, - timeUnit: '1s', - startRate: 1, - stages: [ - { target: 10, duration: '5s' }, - { target: 30, duration: '5s' }, - { target: 60, duration: '10s' }, - { target: 60, duration: '1m' }, - { target: 40, duration: '1m' }, - { target: 60, duration: '10s' }, - { target: 10, duration: '30s' }, - { target: 0, duration: '5s' }, - ], - exec: 'post_radom', - }, + import http from 'k6/http'; + import { check } from 'k6'; + import { Faker } from "k6/x/faker" - spike_scenario: { - executor: 'ramping-arrival-rate', - startTime: '247s', - preAllocatedVUs: 10, - maxVUs: 50, - timeUnit: '1s', - startRate: 1, - stages: [ - { target: 10, duration: '5s' }, - { target: 30, duration: '5s' }, - { target: 200, duration: '10s' }, - { target: 200, duration: '2m' }, - { target: 0, duration: '5s' }, - ], - exec: 'post_radom', - }, + let f = new Faker(); - stress_speed_test: { - executor: 'shared-iterations', - startTime: '392s', - vus: 10, - iterations: 1000000, - maxDuration: '2m', - exec: 'post_radom', + export const options = { + // Tag for grafana dashboard + tags: { + testid: `${__ENV.PARTICIPANT_NAME}`, }, - + scenarios: { + does_it_even_work: { + executor: 'constant-arrival-rate', + rate: 1, + timeUnit: '1s', + duration: '1m', + preAllocatedVUs: 1, + maxVUs: 1, + exec: 'post_radom', + }, + + highload_scenario: { + executor: 'ramping-arrival-rate', + startTime: '61s', + preAllocatedVUs: 10, + maxVUs: 50, + timeUnit: '1s', + startRate: 1, + stages: [ + { target: 10, duration: '5s' }, + { target: 30, duration: '5s' }, + { target: 60, duration: '10s' }, + { target: 60, duration: '1m' }, + { target: 40, duration: '1m' }, + { target: 60, duration: '10s' }, + { target: 10, duration: '30s' }, + { target: 0, duration: '5s' }, + ], + exec: 'post_radom', + }, + + spike_scenario: { + executor: 'ramping-arrival-rate', + startTime: '247s', + preAllocatedVUs: 10, + maxVUs: 50, + timeUnit: '1s', + startRate: 1, + stages: [ + { target: 10, duration: '5s' }, + { target: 30, duration: '5s' }, + { target: 200, duration: '10s' }, + { target: 200, duration: '2m' }, + { target: 0, duration: '5s' }, + ], + exec: 'post_radom', + }, + + stress_speed_test: { + executor: 'shared-iterations', + startTime: '392s', + vus: 10, + iterations: 1000000, + maxDuration: '2m', + exec: 'post_radom', + }, + + } } -} -export function post_radom() { - let text = "" - // 0.5 percent of requests will be empty - if (Math.random() < 0.005) { - text = "" - } else { - text = f.hipsterSentence(f.randomInt([0, 10])) + f.emoji() + f.url() + f.hipsterSentence(f.randomInt([0, 10])) + ". " + f.regex("[\x00-\x7F]{5,20}") + " " + f.bitcoinAddress() + f.emoji() + " " + f.hackerPhrase(f.randomInt([0, 10])) - } + export function post_radom() { + let text = "" + // 0.5 percent of requests will be empty + if (Math.random() < 0.005) { + text = "" + } else { + text = f.hipsterSentence(f.randomInt([0, 10])) + f.emoji() + f.url() + f.hipsterSentence(f.randomInt([0, 10])) + ". " + f.regex("[\x00-\x7F]{5,20}") + " " + f.bitcoinAddress() + f.emoji() + " " + f.hackerPhrase(f.randomInt([0, 10])) + } - console.log("mytext: " + text) + console.log("mytext: " + text) - // post json to the api - const res = http.post(`${__ENV.api_host}`, JSON.stringify(text), { - headers: { - 'Content-Type': 'application/json', - }, - }); - const languages_list = ["Arabic", "Basque", "Breton", "Catalan", "Chinese_China", "Chinese_Hongkong", "Chinese_Taiwan", "Chuvash", "Czech", "Dhivehi", "Dutch", "English", "Esperanto", "Estonian", "French", "Frisian", "Georgian", "German", "Greek", "Hakha_Chin", "Indonesian", "Interlingua", "Italian", "Japanese", "Kabyle", "Kinyarwanda", "Kyrgyz", "Latvian", "Maltese", "Mongolian", "Persian", "Polish", "Portuguese", "Romanian", "Romansh_Sursilvan", "Russian", "Sakha", "Slovenian", "Spanish", "Swedish", "Tamil", "Tatar", "Turkish", "Ukranian", "Welsh"] + // post json to the API + const res = http.post('http://localhost:8000/process', { text: text }, { + headers: { + 'Content-Type': 'application/json', + 'accept': 'application/json', + }, + }); + const languages_list = ["Arabic", "Basque", "Breton", "Catalan", "Chinese_China", "Chinese_Hongkong", "Chinese_Taiwan", "Chuvash", "Czech", "Dhivehi", "Dutch", "English", "Esperanto", "Estonian", "French", "Frisian", "Georgian", "German", "Greek", "Hakha_Chin", "Indonesian", "Interlingua", "Italian", "Japanese", "Kabyle", "Kinyarwanda", "Kyrgyz", "Latvian", "Maltese", "Mongolian", "Persian", "Polish", "Portuguese", "Romanian", "Romansh_Sursilvan", "Russian", "Sakha", "Slovenian", "Spanish", "Swedish", "Tamil", "Tatar", "Turkish", "Ukranian", "Welsh"] - check(res, { - 'is status 200': (r) => r.status === 200, - "model 1 score valid": res => res.json().cardiffnlp.score > 0, - "model 1 label valid": res => res.json().cardiffnlp.label === "POSITIVE" || res.json().cardiffnlp.label === "NEGATIVE" || res.json().cardiffnlp.label === "NEUTRAL", + check(res, { + 'is status 200': (r) => r.status === 200, + "model 1 score valid": res => res.json().cardiffnlp.score > 0, + "model 1 label valid": res => res.json().cardiffnlp.label === "POSITIVE" || res.json().cardiffnlp.label === "NEGATIVE" || res.json().cardiffnlp.label === "NEUTRAL", - "model 2 score valid": res => res.json().ivanlau.score > 0, - "model 2 label valid": res => languages_list.includes(res.json().ivanlau.label), + "model 2 score valid": res => res.json().ivanlau.score > 0, + "model 2 label valid": res => languages_list.includes(res.json().ivanlau.label), - "model 3 score valid": res => res.json().svalabs.score > 0, - "model 3 label valid": res => res.json().svalabs.label === "SPAM" || res.json().svalabs.label === "HAM", + "model 3 score valid": res => res.json().svalabs.score > 0, + "model 3 label valid": res => res.json().svalabs.label === "SPAM" || res.json().svalabs.label === "HAM", - "model 4 score valid": res => res.json().EIStakovskii.score > 0, - "model 4 label valid": res => res.json().EIStakovskii.label === "LABEL_0" || res.json().EIStakovskii.label === "LABEL_1", + "model 4 score valid": res => res.json().EIStakovskii.score > 0, + "model 4 label valid": res => res.json().EIStakovskii.label === "LABEL_0" || res.json().EIStakovskii.label === "LABEL_1", - "model 5 score valid": res => res.json().jy46604790.score > 0, - "model 5 label valid": res => res.json().jy46604790.label === "LABEL_0" || res.json().jy46604790.label === "LABEL_1", - }); -} + "model 5 score valid": res => res.json().jy46604790.score > 0, + "model 5 label valid": res => res.json().jy46604790.label === "LABEL_0" || res.json().jy46604790.label === "LABEL_1", + }); + } diff --git a/autotests/helm/values.yaml b/autotests/helm/values.yaml index cda6a5e..ea84816 100644 --- a/autotests/helm/values.yaml +++ b/autotests/helm/values.yaml @@ -25,8 +25,8 @@ global: activeDeadlineSeconds: 3600 # 1h env: - PARTICIPANT_NAME: - api_host: http://inca-smc-mlops-challenge-solution.default.svc.cluster.local/ + PARTICIPANT_NAME: Khush Patel + api_host: http://inca-smc-mlops-challenge-solution.default.svc.cluster.local/8000 # K6, do not edit! K6_PROMETHEUS_RW_SERVER_URL: http://kube-prometheus-stack-prometheus.monitoring.svc.cluster.local:9090/api/v1/write diff --git a/solution/helm/.dockerignore b/solution/helm/.dockerignore new file mode 100644 index 0000000..8b46eba --- /dev/null +++ b/solution/helm/.dockerignore @@ -0,0 +1,23 @@ +__pycache__ +*.pyc +*.pyo +*.pyd +.Python +env/ +venv/ +pip-log.txt +pip-delete-this-directory.txt +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.log +.git +.gitignore +.mypy_cache +.pytest_cache +.hypothesis +.idea \ No newline at end of file diff --git a/solution/helm/Dockerfile b/solution/helm/Dockerfile new file mode 100644 index 0000000..193cfed --- /dev/null +++ b/solution/helm/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.10.11-slim-bullseye + +WORKDIR /code + +COPY ./requirements.txt /code/requirements.txt + +COPY ./app.py /code/app.py + +RUN pip install -r requirements.txt + +CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/solution/helm/app.py b/solution/helm/app.py new file mode 100644 index 0000000..f597574 --- /dev/null +++ b/solution/helm/app.py @@ -0,0 +1,187 @@ +# Import required libraries +from fastapi import FastAPI, Request +from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig +import numpy as np +# import time +from scipy.special import softmax +from pydantic import BaseModel + +# Initialize a FastAPI app instance +app = FastAPI() + +# Define a default route +@app.get('/') +async def home(): + html = ( + 'Mlops Challenge' + ) + return html.format(format) + + +# Load the trained models +print("Model Loading Started...") + +# CardiffNLP Model +cardiffnlp_MODEL = f"cardiffnlp/twitter-xlm-roberta-base-sentiment" +cardiffnlp_tokenizer = AutoTokenizer.from_pretrained(cardiffnlp_MODEL) +cardiffnlp_model = AutoModelForSequenceClassification.from_pretrained(cardiffnlp_MODEL) +print("cardiffnlp Model Loaded!") + +# ivanlau Model +ivanlau_MODEL = f"ivanlau/language-detection-fine-tuned-on-xlm-roberta-base" +ivanlau_tokenizer = AutoTokenizer.from_pretrained(ivanlau_MODEL) +ivanlau_config = AutoConfig.from_pretrained(ivanlau_MODEL) +ivanlau_model = AutoModelForSequenceClassification.from_pretrained(ivanlau_MODEL) +print("ivanlau Model Loaded!") + +# svalabs Model +svalabs_MODEL = f"svalabs/twitter-xlm-roberta-crypto-spam" +svalabs_tokenizer = AutoTokenizer.from_pretrained(svalabs_MODEL) +svalabs_model = AutoModelForSequenceClassification.from_pretrained(svalabs_MODEL) +print("svalabs Model Loaded!") + +# EIStakovskii Model +EIStakovskii_MODEL = f"EIStakovskii/xlm_roberta_base_multilingual_toxicity_classifier_plus" +EIStakovskii_tokenizer = AutoTokenizer.from_pretrained(EIStakovskii_MODEL) +EIStakovskii_model = AutoModelForSequenceClassification.from_pretrained(EIStakovskii_MODEL) +print("EIStakovskii Model Loaded!") + +# jy46604790 Model +jy46604790_MODEL = f"jy46604790/Fake-News-Bert-Detect" +jy46604790_tokenizer = AutoTokenizer.from_pretrained(jy46604790_MODEL) +jy46604790_model = AutoModelForSequenceClassification.from_pretrained(jy46604790_MODEL) +print("jy46604790 Model Loaded!") + + +def preprocess(text: str) -> str: + new_text = [] + for t in text.split(" "): + t = '@user' if t.startswith('@') and len(t) > 1 else t + t = 'http' if t.startswith('http') else t + new_text.append(t) + return " ".join(new_text) + + +# Define a function for each of the trained models to preprocess the input and return the prediction +def cardiffnlp_precessing(text: str) -> dict: + encoded_input = cardiffnlp_tokenizer(text, return_tensors='pt') + outputs = cardiffnlp_model(**encoded_input) + outputs = outputs.logits.detach().numpy() + + scores = softmax(outputs)[0] + + max_index = scores.argmax() + + id2label = ["NEGATIVE","NEUTRAL","POSITIVE"] + + l = id2label[max_index] + s = scores[max_index] + result = { + "score": float(s), + "label": l + } + return result + +def ivanlau_precessing(text: str) -> dict: + encoded_input = ivanlau_tokenizer(text, return_tensors='pt') + outputs = ivanlau_model(**encoded_input) + outputs = outputs.logits.detach().numpy() + + scores = softmax(outputs)[0] + + max_index = scores.argmax() + + + l = ivanlau_config.id2label[max_index] + s = scores[max_index] + result = { + "score": float(s), + "label": l + } + return result + +def svalabs_precessing(text: str) -> dict: + encoded_input = svalabs_tokenizer(text, return_tensors='pt') + outputs = svalabs_model(**encoded_input) + outputs = outputs.logits.detach().numpy() + + scores = softmax(outputs)[0] + + id2label = ["HAM","SPAM"] + + max_index = scores.argmax() + + l = id2label[max_index] + s = scores[max_index] + result = { + "score": float(s), + "label": l + } + return result + +def EIStakovskii_precessing(text: str) -> dict: + encoded_input = EIStakovskii_tokenizer(text, return_tensors='pt') + outputs = EIStakovskii_model(**encoded_input) + outputs = outputs.logits.detach().numpy() + + scores = softmax(outputs)[0] + + max_index = scores.argmax() + + id2label = ["LABEL_0","LABEL_1"] + + l = id2label[max_index] + s = scores[max_index] + result = { + "score": float(s), + "label": l + } + + return result + +def jy46604790_precessing(text: str) -> dict: + encoded_input = jy46604790_tokenizer(text, return_tensors='pt') + outputs = jy46604790_model(**encoded_input) + outputs = outputs.logits.detach().numpy() + + scores = softmax(outputs)[0] + + max_index = scores.argmax() + + + id2label = ["LABEL_1","LABEL_0"] + + l = id2label[max_index] + s = scores[max_index] + result = { + "score": float(s), + "label": l + } + + return result + + +class TextIn(BaseModel): + text: str + +# Define a process route +@app.post("/process") +def predict(payload: TextIn): + # start = time.time() + text = preprocess(payload.text) + results_cardiffnlp = cardiffnlp_precessing(text) + results_ivanlau = ivanlau_precessing(text) + results_svalabs = svalabs_precessing(text) + results_EIStakovskii = EIStakovskii_precessing(text) + results_jy46604790 = jy46604790_precessing(text) + # end = time.time() + # total_time = end - start + # print(f"Total inference time with preprocessing: {total_time}") + result = { + "cardiffnlp": results_cardiffnlp, + "ivanlau":results_ivanlau, + "svalabs":results_svalabs, + "EIStakovskii":results_EIStakovskii, + "jy46604790":results_jy46604790, + } + return result \ No newline at end of file diff --git a/solution/helm/requirements.txt b/solution/helm/requirements.txt new file mode 100644 index 0000000..07cf214 --- /dev/null +++ b/solution/helm/requirements.txt @@ -0,0 +1,8 @@ +sentencepiece +transformers +torch +scipy +fastapi +uvicorn[standard] +pydantic +protobuf==3.13.0 \ No newline at end of file From 0b92bb4972742e5c27d7da673fb79b10a1e11614 Mon Sep 17 00:00:00 2001 From: khush Date: Tue, 9 May 2023 20:55:15 +0300 Subject: [PATCH 2/3] solution updated --- autotests/helm/values.yaml | 2 +- solution/{helm => }/.dockerignore | 0 solution/{helm => }/Dockerfile | 0 solution/{helm => }/app.py | 0 solution/{helm => }/requirements.txt | 0 5 files changed, 1 insertion(+), 1 deletion(-) rename solution/{helm => }/.dockerignore (100%) rename solution/{helm => }/Dockerfile (100%) rename solution/{helm => }/app.py (100%) rename solution/{helm => }/requirements.txt (100%) diff --git a/autotests/helm/values.yaml b/autotests/helm/values.yaml index ea84816..97aedb4 100644 --- a/autotests/helm/values.yaml +++ b/autotests/helm/values.yaml @@ -26,7 +26,7 @@ global: env: PARTICIPANT_NAME: Khush Patel - api_host: http://inca-smc-mlops-challenge-solution.default.svc.cluster.local/8000 + api_host: http://inca-smc-mlops-challenge-solution.default.svc.cluster.local/process # K6, do not edit! K6_PROMETHEUS_RW_SERVER_URL: http://kube-prometheus-stack-prometheus.monitoring.svc.cluster.local:9090/api/v1/write diff --git a/solution/helm/.dockerignore b/solution/.dockerignore similarity index 100% rename from solution/helm/.dockerignore rename to solution/.dockerignore diff --git a/solution/helm/Dockerfile b/solution/Dockerfile similarity index 100% rename from solution/helm/Dockerfile rename to solution/Dockerfile diff --git a/solution/helm/app.py b/solution/app.py similarity index 100% rename from solution/helm/app.py rename to solution/app.py diff --git a/solution/helm/requirements.txt b/solution/requirements.txt similarity index 100% rename from solution/helm/requirements.txt rename to solution/requirements.txt From be174784b66b4d6833fca6a3291b75b53fa28331 Mon Sep 17 00:00:00 2001 From: khush Date: Thu, 11 May 2023 00:49:34 +0300 Subject: [PATCH 3/3] updated endpoint --- autotests/app/src/main.js | 193 +++++++++++++++++++------------------- solution/app.py | 18 ++-- 2 files changed, 102 insertions(+), 109 deletions(-) diff --git a/autotests/app/src/main.js b/autotests/app/src/main.js index 4def6bc..f050fd9 100644 --- a/autotests/app/src/main.js +++ b/autotests/app/src/main.js @@ -1,111 +1,110 @@ - import http from 'k6/http'; - import { check } from 'k6'; - import { Faker } from "k6/x/faker" +import http from 'k6/http'; +import { check } from 'k6'; +import { Faker } from "k6/x/faker" + +let f = new Faker(); + +export const options = { + // Tag for grafana dashboard + tags: { + testid: `${__ENV.PARTICIPANT_NAME}`, + }, + scenarios: { + does_it_even_work: { + executor: 'constant-arrival-rate', + rate: 1, + timeUnit: '1s', + duration: '1m', + preAllocatedVUs: 1, + maxVUs: 1, + exec: 'post_radom', + }, + + highload_scenario: { + executor: 'ramping-arrival-rate', + startTime: '61s', + preAllocatedVUs: 10, + maxVUs: 50, + timeUnit: '1s', + startRate: 1, + stages: [ + { target: 10, duration: '5s' }, + { target: 30, duration: '5s' }, + { target: 60, duration: '10s' }, + { target: 60, duration: '1m' }, + { target: 40, duration: '1m' }, + { target: 60, duration: '10s' }, + { target: 10, duration: '30s' }, + { target: 0, duration: '5s' }, + ], + exec: 'post_radom', + }, - let f = new Faker(); + spike_scenario: { + executor: 'ramping-arrival-rate', + startTime: '247s', + preAllocatedVUs: 10, + maxVUs: 50, + timeUnit: '1s', + startRate: 1, + stages: [ + { target: 10, duration: '5s' }, + { target: 30, duration: '5s' }, + { target: 200, duration: '10s' }, + { target: 200, duration: '2m' }, + { target: 0, duration: '5s' }, + ], + exec: 'post_radom', + }, - export const options = { - // Tag for grafana dashboard - tags: { - testid: `${__ENV.PARTICIPANT_NAME}`, + stress_speed_test: { + executor: 'shared-iterations', + startTime: '392s', + vus: 10, + iterations: 1000000, + maxDuration: '2m', + exec: 'post_radom', }, - scenarios: { - does_it_even_work: { - executor: 'constant-arrival-rate', - rate: 1, - timeUnit: '1s', - duration: '1m', - preAllocatedVUs: 1, - maxVUs: 1, - exec: 'post_radom', - }, - - highload_scenario: { - executor: 'ramping-arrival-rate', - startTime: '61s', - preAllocatedVUs: 10, - maxVUs: 50, - timeUnit: '1s', - startRate: 1, - stages: [ - { target: 10, duration: '5s' }, - { target: 30, duration: '5s' }, - { target: 60, duration: '10s' }, - { target: 60, duration: '1m' }, - { target: 40, duration: '1m' }, - { target: 60, duration: '10s' }, - { target: 10, duration: '30s' }, - { target: 0, duration: '5s' }, - ], - exec: 'post_radom', - }, - - spike_scenario: { - executor: 'ramping-arrival-rate', - startTime: '247s', - preAllocatedVUs: 10, - maxVUs: 50, - timeUnit: '1s', - startRate: 1, - stages: [ - { target: 10, duration: '5s' }, - { target: 30, duration: '5s' }, - { target: 200, duration: '10s' }, - { target: 200, duration: '2m' }, - { target: 0, duration: '5s' }, - ], - exec: 'post_radom', - }, - - stress_speed_test: { - executor: 'shared-iterations', - startTime: '392s', - vus: 10, - iterations: 1000000, - maxDuration: '2m', - exec: 'post_radom', - }, - - } + } +} - export function post_radom() { - let text = "" - // 0.5 percent of requests will be empty - if (Math.random() < 0.005) { - text = "" - } else { - text = f.hipsterSentence(f.randomInt([0, 10])) + f.emoji() + f.url() + f.hipsterSentence(f.randomInt([0, 10])) + ". " + f.regex("[\x00-\x7F]{5,20}") + " " + f.bitcoinAddress() + f.emoji() + " " + f.hackerPhrase(f.randomInt([0, 10])) - } +export function post_radom() { + let text = "" + // 0.5 percent of requests will be empty + if (Math.random() < 0.005) { + text = "" + } else { + text = f.hipsterSentence(f.randomInt([0, 10])) + f.emoji() + f.url() + f.hipsterSentence(f.randomInt([0, 10])) + ". " + f.regex("[\x00-\x7F]{5,20}") + " " + f.bitcoinAddress() + f.emoji() + " " + f.hackerPhrase(f.randomInt([0, 10])) + } - console.log("mytext: " + text) + console.log("mytext: " + text) - // post json to the API - const res = http.post('http://localhost:8000/process', { text: text }, { - headers: { - 'Content-Type': 'application/json', - 'accept': 'application/json', - }, - }); - const languages_list = ["Arabic", "Basque", "Breton", "Catalan", "Chinese_China", "Chinese_Hongkong", "Chinese_Taiwan", "Chuvash", "Czech", "Dhivehi", "Dutch", "English", "Esperanto", "Estonian", "French", "Frisian", "Georgian", "German", "Greek", "Hakha_Chin", "Indonesian", "Interlingua", "Italian", "Japanese", "Kabyle", "Kinyarwanda", "Kyrgyz", "Latvian", "Maltese", "Mongolian", "Persian", "Polish", "Portuguese", "Romanian", "Romansh_Sursilvan", "Russian", "Sakha", "Slovenian", "Spanish", "Swedish", "Tamil", "Tatar", "Turkish", "Ukranian", "Welsh"] + // post json to the api + const res = http.post(`${__ENV.api_host}`, JSON.stringify(text), { + headers: { + 'Content-Type': 'application/json', + }, + }); + const languages_list = ["Arabic", "Basque", "Breton", "Catalan", "Chinese_China", "Chinese_Hongkong", "Chinese_Taiwan", "Chuvash", "Czech", "Dhivehi", "Dutch", "English", "Esperanto", "Estonian", "French", "Frisian", "Georgian", "German", "Greek", "Hakha_Chin", "Indonesian", "Interlingua", "Italian", "Japanese", "Kabyle", "Kinyarwanda", "Kyrgyz", "Latvian", "Maltese", "Mongolian", "Persian", "Polish", "Portuguese", "Romanian", "Romansh_Sursilvan", "Russian", "Sakha", "Slovenian", "Spanish", "Swedish", "Tamil", "Tatar", "Turkish", "Ukranian", "Welsh"] - check(res, { - 'is status 200': (r) => r.status === 200, - "model 1 score valid": res => res.json().cardiffnlp.score > 0, - "model 1 label valid": res => res.json().cardiffnlp.label === "POSITIVE" || res.json().cardiffnlp.label === "NEGATIVE" || res.json().cardiffnlp.label === "NEUTRAL", + check(res, { + 'is status 200': (r) => r.status === 200, + "model 1 score valid": res => res.json().cardiffnlp.score > 0, + "model 1 label valid": res => res.json().cardiffnlp.label === "POSITIVE" || res.json().cardiffnlp.label === "NEGATIVE" || res.json().cardiffnlp.label === "NEUTRAL", - "model 2 score valid": res => res.json().ivanlau.score > 0, - "model 2 label valid": res => languages_list.includes(res.json().ivanlau.label), + "model 2 score valid": res => res.json().ivanlau.score > 0, + "model 2 label valid": res => languages_list.includes(res.json().ivanlau.label), - "model 3 score valid": res => res.json().svalabs.score > 0, - "model 3 label valid": res => res.json().svalabs.label === "SPAM" || res.json().svalabs.label === "HAM", + "model 3 score valid": res => res.json().svalabs.score > 0, + "model 3 label valid": res => res.json().svalabs.label === "SPAM" || res.json().svalabs.label === "HAM", - "model 4 score valid": res => res.json().EIStakovskii.score > 0, - "model 4 label valid": res => res.json().EIStakovskii.label === "LABEL_0" || res.json().EIStakovskii.label === "LABEL_1", + "model 4 score valid": res => res.json().EIStakovskii.score > 0, + "model 4 label valid": res => res.json().EIStakovskii.label === "LABEL_0" || res.json().EIStakovskii.label === "LABEL_1", - "model 5 score valid": res => res.json().jy46604790.score > 0, - "model 5 label valid": res => res.json().jy46604790.label === "LABEL_0" || res.json().jy46604790.label === "LABEL_1", - }); - } + "model 5 score valid": res => res.json().jy46604790.score > 0, + "model 5 label valid": res => res.json().jy46604790.label === "LABEL_0" || res.json().jy46604790.label === "LABEL_1", + }); +} \ No newline at end of file diff --git a/solution/app.py b/solution/app.py index f597574..1f861ae 100644 --- a/solution/app.py +++ b/solution/app.py @@ -1,11 +1,9 @@ # Import required libraries -from fastapi import FastAPI, Request +from fastapi import FastAPI from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig -import numpy as np -# import time -from scipy.special import softmax -from pydantic import BaseModel - +import numpy as np +from scipy.special import softmax +from fastapi import Request, Body # Initialize a FastAPI app instance app = FastAPI() @@ -160,15 +158,11 @@ def jy46604790_precessing(text: str) -> dict: return result - -class TextIn(BaseModel): - text: str - # Define a process route @app.post("/process") -def predict(payload: TextIn): +async def predict(request: Request): # start = time.time() - text = preprocess(payload.text) + text = preprocess((await request.body()).decode()) results_cardiffnlp = cardiffnlp_precessing(text) results_ivanlau = ivanlau_precessing(text) results_svalabs = svalabs_precessing(text)