-
Notifications
You must be signed in to change notification settings - Fork 0
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 #26 from MLOps-essi-upc/dev
Merging dev into main
- Loading branch information
Showing
22 changed files
with
574 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
name: Run tests CI | ||
on: | ||
push: | ||
branches: | ||
- main | ||
- dev | ||
pull_request: | ||
branches: | ||
- main | ||
- dev | ||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Use python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
|
||
- name: Install dependencies | ||
run: pip install -r requirements.txt | ||
|
||
- name: show cwd | ||
run : python -c "import os; print('\n',os.getcwd())" | ||
|
||
- name : pull data from dvc | ||
run : python -m dvc pull -r origin | ||
|
||
- name: Run tests | ||
run: python -m pytest | ||
|
||
cd: | ||
runs-on: ubuntu-latest | ||
needs : ci | ||
steps : | ||
- name : Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Docker login | ||
run: docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_TOKEN }} | ||
|
||
- name: Build | ||
run: docker build -t sentibites . | ||
- name: Tags | ||
run: | | ||
docker tag sentibites ${{ secrets.DOCKER_HUB_USERNAME }}/sentibites:${{ github.sha }} | ||
docker tag sentibites ${{ secrets.DOCKER_HUB_USERNAME }}/sentibites:latest | ||
- name: Push | ||
run: | | ||
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/sentibites:${{ github.sha }} | ||
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/sentibites:latest |
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 |
---|---|---|
|
@@ -162,3 +162,6 @@ cython_debug/ | |
# Report | ||
out/ | ||
reports/ | ||
|
||
# DS_STORE | ||
.DS_Store |
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,15 @@ | ||
FROM python:3.10 | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
|
||
COPY requirements.txt . | ||
|
||
WORKDIR /app | ||
|
||
COPY . /app/ | ||
|
||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
EXPOSE 5000 | ||
|
||
CMD ["python3", "run_servers.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
Empty file.
Empty file.
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,13 @@ | ||
from subprocess import Popen | ||
|
||
# Start uvicorn | ||
uvicorn_cmd = ["uvicorn", "--host", "0.0.0.0", "--port", "8000", "src.app.api:app"] | ||
uvicorn_process = Popen(uvicorn_cmd) | ||
|
||
# Start gunicorn | ||
gunicorn_cmd = ["gunicorn", "-b", "0.0.0.0:5000", "src.app.app:app"] | ||
gunicorn_process = Popen(gunicorn_cmd) | ||
|
||
# Wait for the processes to finish | ||
uvicorn_process.wait() | ||
gunicorn_process.wait() |
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,103 @@ | ||
"""Main script: it includes our API initialization and endpoints.""" | ||
|
||
import pickle | ||
from datetime import datetime | ||
from functools import wraps | ||
from http import HTTPStatus | ||
from typing import List | ||
import os | ||
import sys | ||
|
||
from contextlib import asynccontextmanager | ||
from fastapi import FastAPI, HTTPException, Request | ||
from src.models.predict_model import SentiBites | ||
from src.app.schemas import Review | ||
|
||
# Get the parent directory | ||
parent_dir = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
# Add the parent directory to sys.path | ||
sys.path.append(parent_dir) | ||
|
||
model = None | ||
|
||
# Define application | ||
app = FastAPI( | ||
title="SentiBites", | ||
description="This API lets you make predictions on sentiment analysis of Amazon food reviews.", | ||
version="0.1", | ||
) | ||
|
||
|
||
def construct_response(f): | ||
"""Construct a JSON response for an endpoint's results.""" | ||
|
||
@wraps(f) | ||
def wrap(request: Request, *args, **kwargs): | ||
results = f(request, *args, **kwargs) | ||
|
||
# Construct response | ||
response = { | ||
"message": results["message"], | ||
"method": request.method, | ||
"status-code": results["status-code"], | ||
"timestamp": datetime.now().isoformat(), | ||
"url": request.url._url, | ||
} | ||
|
||
# Add data | ||
if "data" in results: | ||
response["data"] = results["data"] | ||
|
||
return response | ||
|
||
return wrap | ||
|
||
|
||
@app.on_event("startup") | ||
def _load_model(): | ||
"""Loads the model""" | ||
|
||
global model | ||
model = SentiBites("models/SentiBites/") | ||
|
||
|
||
@app.get("/", tags=["General"]) # path operation decorator | ||
@construct_response | ||
def _index(request: Request): | ||
"""Root endpoint.""" | ||
|
||
response = { | ||
"message": HTTPStatus.OK.phrase, | ||
"status-code": HTTPStatus.OK, | ||
"data": {"message": "Welcome to SentiBites! Please, read the `/docs`!"}, | ||
} | ||
return response | ||
|
||
@app.post("/models/", tags=["Prediction"]) | ||
@construct_response | ||
def _predict(request: Request, payload: Review): | ||
"""Performs sentiment analysis based on the food review.""" | ||
|
||
if model: | ||
prediction,scores = model.predict(payload.msg) | ||
|
||
response = { | ||
"message": HTTPStatus.OK.phrase, | ||
"status-code": HTTPStatus.OK, | ||
"data": { | ||
"model-type": "RoBERTaSB", | ||
"payload": payload.msg, | ||
"prediction": prediction, | ||
"Scores" : { | ||
"positive" : scores['positive'], | ||
"neutral" : scores['neutral'], | ||
"negative" : scores['negative'] | ||
} | ||
}, | ||
} | ||
else: | ||
raise HTTPException( | ||
status_code=HTTPStatus.BAD_REQUEST, detail="Model not found" | ||
) | ||
return response |
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,35 @@ | ||
from flask import Flask, render_template, request, jsonify | ||
import requests | ||
|
||
app = Flask(__name__, static_folder="static") | ||
|
||
# The URL of FastAPI API | ||
api_url = "http://127.0.0.1:8000/models/" | ||
|
||
@app.route("/", methods=["GET", "POST"]) | ||
def index(): | ||
if request.method == "POST": | ||
# Get the text entered by the user | ||
text = request.form.get("text") | ||
|
||
# Build the URL with the text as a parameter | ||
payload = {'msg':text} | ||
|
||
# Call your FastAPI API | ||
response = requests.post(api_url,json=payload) | ||
|
||
if response.status_code == 200: | ||
# Get the results from the API | ||
data = response.json() | ||
prediction = data["data"]["prediction"] | ||
model_type = data["data"]["model-type"] | ||
|
||
return render_template("index.html", prediction=prediction, model_type=model_type, text=text) | ||
else: | ||
error_message = "Error when calling API." | ||
return render_template("index.html", error_message=error_message, text=text) | ||
|
||
return render_template("index.html") | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=False) |
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,4 @@ | ||
from pydantic import BaseModel | ||
|
||
class Review(BaseModel): | ||
msg : str |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.