diff --git a/dockerfiles/web.dockerfile b/dockerfiles/web.dockerfile new file mode 100644 index 0000000..57a5740 --- /dev/null +++ b/dockerfiles/web.dockerfile @@ -0,0 +1,28 @@ +FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime + +WORKDIR / + +RUN apt update && \ + apt install --no-install-recommends -y build-essential gcc && \ + apt clean && rm -rf /var/lib/apt/lists/* + +RUN apt-get update && apt-get install -y nano + +COPY toxic_comments/slowapi/requirements.txt / + +RUN pip install -r requirements.txt --no-cache-dir + +COPY toxic_comments/ toxic_comments/ +COPY /toxic_comments/slowapi /slowapi +COPY models/bert-toxic-classifier/models_bert-toxic-classifier_epoch=1-val_loss=0.15.ckpt models/bert-toxic-classifier/ + +ENV PYTHONPATH=/ + +EXPOSE 5000 + +#ENTRYPOINT [ "python" ] +#CMD [ "slowapi/ask.py" ] + +#docker run -p 5000:5000 name_of_your_image +#docker run -p 8000:80 hosting uvicorn slowapi.main:app --host 0.0.0.0 --port 80 +#change run.app(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/flaskbuild.yaml b/flaskbuild.yaml new file mode 100644 index 0000000..2a4f428 --- /dev/null +++ b/flaskbuild.yaml @@ -0,0 +1,5 @@ +steps: + - name: 'gcr.io/cloud-builders/docker' + args: ['build', '-f', './dockerfiles/web.dockerfile', '-t', 'gcr.io/propane-facet-410709/flask-inference', '.', '--no-cache'] + - name: 'gcr.io/cloud-builders/docker' + args: ['push', 'gcr.io/propane-facet-410709/flask-inference'] \ No newline at end of file diff --git a/toxic_comments/models/config/default.yaml b/toxic_comments/models/config/default.yaml index fb3c8c6..86ad012 100644 --- a/toxic_comments/models/config/default.yaml +++ b/toxic_comments/models/config/default.yaml @@ -14,7 +14,7 @@ train: predict: checkpoint_path: models/bert-toxic-classifier/epoch=1-val_loss=0.15.ckpt - + # In config_train.yaml hydra: run: diff --git a/toxic_comments/predict_model.py b/toxic_comments/predict_model.py index d2445c3..229b71e 100644 --- a/toxic_comments/predict_model.py +++ b/toxic_comments/predict_model.py @@ -104,10 +104,25 @@ def predict_user_input(config): # Save results labels_list = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"] + print(user_input) + print(result[0]) r_df = pd.DataFrame(result[0], columns=labels_list) r_df.to_csv("outputs/predictions.csv") +@hydra.main(version_base="1.3", config_name="default.yaml", config_path="models/config") +def predict_user_input_hosting(config): + user_input = [config.text] + result = predict(user_input, config) + # Save results + labels_list = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"] + print(user_input) + print(result[0]) + r_df = pd.DataFrame(result[0], columns=labels_list) + r_df.to_csv("outputs/predictions.csv") + return r_df + + @hydra.main(version_base="1.3", config_name="default.yaml", config_path="models/config") def predict_file_input(config): """Predict input from a file and save the results to a CSV file.""" diff --git a/toxic_comments/slowapi/__init__.py b/toxic_comments/slowapi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/toxic_comments/slowapi/ask.py b/toxic_comments/slowapi/ask.py new file mode 100644 index 0000000..48936ae --- /dev/null +++ b/toxic_comments/slowapi/ask.py @@ -0,0 +1,31 @@ +from flask import Flask, render_template, request +from toxic_comments.predict_model import predict_user_input_hosting +from omegaconf import OmegaConf +# import pandas as pd + + +app = Flask(__name__) + + +@app.route("/") +def index(): + return render_template("1.html") + + +@app.route("/upload", methods=["POST"]) +def upload(): + config = OmegaConf.load("toxic_comments/models/config/default.yaml") # Update with the actual path + text_input = request.form.get("text_input") + config.text = text_input + + # file = request.files['file_input'] + # file.save(f"uploads/{file.filename}") # Save the uploaded file to a folder named 'uploads' + + print(f"Text Input: {text_input}") + # print(f"File Uploaded: {file.filename}") + df = predict_user_input_hosting(config) + return render_template("index.html", table=df.to_html(classes="table table-striped")) + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/toxic_comments/slowapi/requirements.txt b/toxic_comments/slowapi/requirements.txt new file mode 100644 index 0000000..78d2169 --- /dev/null +++ b/toxic_comments/slowapi/requirements.txt @@ -0,0 +1,14 @@ +omegaconf==2.3.0 +pydantic==2.5.3 +hydra-core==1.3.2 +numpy==1.26.3 +omegaconf==2.3.0 +pandas==2.1.4 +pytorch_lightning==2.1.2 +scikit_learn==1.3.2 +torch==2.1.2 +transformers==4.36.2 +tensorboard==2.15.1 +wandb==0.16.2 +flask==3.0.0 +requests==2.26.0 \ No newline at end of file diff --git a/toxic_comments/slowapi/templates/1.html b/toxic_comments/slowapi/templates/1.html new file mode 100644 index 0000000..8da539b --- /dev/null +++ b/toxic_comments/slowapi/templates/1.html @@ -0,0 +1,25 @@ + + + + + + Flask Template - Text and File Input + + +

Flask Template - Text and File Input

+ +
+ + + +
+ + + + +

+ + +
+ + diff --git a/toxic_comments/slowapi/templates/index.html b/toxic_comments/slowapi/templates/index.html new file mode 100644 index 0000000..35f6b54 --- /dev/null +++ b/toxic_comments/slowapi/templates/index.html @@ -0,0 +1,18 @@ + + + + + + + DataFrame Display + + + + +
+

DataFrame Display

+ + {{ table | safe }} +
+ + diff --git a/toxic_comments/train_model.py b/toxic_comments/train_model.py index 01c2ff4..909cd7d 100644 --- a/toxic_comments/train_model.py +++ b/toxic_comments/train_model.py @@ -67,6 +67,7 @@ def train(config): # Train the model trainer.fit(model) + # save logged data logger.save() # Test the model