From 45020fdc0fe3e0f24036d1eafb63c7f948194479 Mon Sep 17 00:00:00 2001 From: Raul Del Pozo Moreno Date: Wed, 13 Jul 2022 18:44:20 +0200 Subject: [PATCH] Added docker deployment --- Dockerfile | 12 +++++++ actions/Dockerfile | 14 +++++++++ actions/actions.py | 67 ++++++++++++++++++++-------------------- actions/requirements.txt | 1 + docker-compose.yml | 22 +++++++++++++ endpoints.yml | 2 +- 6 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 Dockerfile create mode 100644 actions/Dockerfile create mode 100644 actions/requirements.txt create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..78639f0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM rasa/rasa:3.1.1 +WORKDIR '/app' +COPY . /app +USER root +# WORKDIR /app +# COPY . /app +COPY ./data /app/data +RUN rasa train +VOLUME /app +VOLUME /app/data +VOLUME /app/models +CMD ["run","-m","/app/models","--enable-api","--cors","*","--debug" ,"--endpoints", "endpoints.yml", "--log-file", "out.log", "--debug"] \ No newline at end of file diff --git a/actions/Dockerfile b/actions/Dockerfile new file mode 100644 index 0000000..b9a546c --- /dev/null +++ b/actions/Dockerfile @@ -0,0 +1,14 @@ +FROM rasa/rasa-sdk:3.1.1 +WORKDIR /app +COPY requirements.txt requirements.txt +USER root + +RUN pip install --upgrade pip +RUN pip install --verbose -r requirements.txt + +RUN apt-get update && apt-get install -y locales +RUN locale-gen es_ES +RUN locale-gen es_ES.utf8 + +EXPOSE 5055 +USER 1001 \ No newline at end of file diff --git a/actions/actions.py b/actions/actions.py index 014f7b9..ca61aee 100644 --- a/actions/actions.py +++ b/actions/actions.py @@ -4,6 +4,7 @@ from rasa_sdk.types import DomainDict from rasa_sdk.executor import CollectingDispatcher from rasa_sdk import Tracker, FormValidationAction +from os import path import unidecode # acentos import datetime import pathlib @@ -13,22 +14,20 @@ locale.setlocale(locale.LC_ALL, 'es_ES.utf8') -names = pathlib.Path("data/diccionarios/nombres.txt").read_text().split("\n") +data_path = "/app/actions" +if not path.exists(data_path): + data_path = "." + +names = pathlib.Path( + data_path+"/data/diccionarios/nombres.txt").read_text().split("\n") malsonantes = pathlib.Path( - "data/diccionarios/malsonante.txt").read_text().split("\n") + data_path+"/data/diccionarios/malsonante.txt").read_text().split("\n") DIAS_SEMANA = ["lunes", "martes", "miercoles", "jueves", "viernes", "sabado", "domingo"] CATEGORIA_MENU = ["entrantes", "carnes", "pescados", "postres", "bebidas"] -def get_data_plato(tipo): - f = open(pathlib.Path("data/menu/"+tipo+".json")) - data = json.load(f) - f.close() - return data - - def get_data_generic(path): f = open(pathlib.Path(path)) data = json.load(f) @@ -36,9 +35,9 @@ def get_data_generic(path): return data -horario = get_data_generic("data/menu/horario.json") -mesas = get_data_generic("data/tables/mesas.json") -horas = get_data_generic("data/tables/horas.json") +horario = get_data_generic(data_path+"/data/menu/horario.json") +mesas = get_data_generic(data_path+"/data/tables/mesas.json") +horas = get_data_generic(data_path+"/data/tables/horas.json") # --------------------------------------------------------------------- @@ -193,20 +192,20 @@ def run(self, dispatcher, tracker, domain): elif intent == "horario_concreto": dia = tracker.get_slot("dia") if dia is not None: - dia = dia.lower() - - if dia == "hoy": - now = datetime.datetime.now() - dia = now.strftime("%A") - elif dia == "mañana": - now = (datetime.datetime.now() + - datetime.timedelta(1)).strftime("%A") - dia = now - elif dia == "pasado mañana": - now = (datetime.datetime.now() + - datetime.timedelta(2)).strftime("%A") - dia = now - dia = unidecode.unidecode(dia) + dia = dia.lower() + + if dia == "hoy": + now = datetime.datetime.now() + dia = now.strftime("%A") + elif dia == "mañana": + now = (datetime.datetime.now() + + datetime.timedelta(1)).strftime("%A") + dia = now + elif dia == "pasado mañana": + now = (datetime.datetime.now() + + datetime.timedelta(2)).strftime("%A") + dia = now + dia = unidecode.unidecode(dia) if dia not in DIAS_SEMANA: message = "Ese dia de la semana es incorrecto." @@ -317,7 +316,7 @@ def name(self): return 'MenuGet' def get_menu_plato(Action, tipo): - data = get_data_plato(tipo) + data = get_data_generic(data_path+"/data/menu/"+tipo+".json") message = tipo.capitalize() + ":" + "\n" for it in data[tipo]: message += str(it['id']) + ": " + it['nombre'] + \ @@ -325,7 +324,7 @@ def get_menu_plato(Action, tipo): return message def get_submenu_botones(Action, tipo): - data = get_data_plato(tipo) + data = get_data_generic(data_path+"/data/menu/"+tipo+".json") buttons = [] for it in data[tipo]: buttons.append({"title": "{}".format( @@ -340,7 +339,7 @@ def run(self, dispatcher, tracker, domain): if intent == "menu_completo": message = "" for it in CATEGORIA_MENU: - data = get_data_plato(it) + data = get_data_generic(data_path+"/data/menu/"+it+".json") message += it.capitalize() + ":\n" for d in data[it]: message += "- " + d['nombre'] + \ @@ -524,7 +523,8 @@ async def extract_menu_plato_id( menu_plato_categoria = it break - data = get_data_plato(menu_plato_categoria) + data = get_data_generic( + data_path+"/data/menu/"+menu_plato_categoria+".json") for it in data[menu_plato_categoria]: if it['id'] == str(menu_plato_id): return {"menu_plato_id": menu_plato_id} @@ -552,7 +552,8 @@ def validate_menu_plato_id( menu_plato_categoria = it break - data = get_data_plato(menu_plato_categoria) + data = get_data_generic( + data_path+"/data/menu/"+menu_plato_categoria+".json") for it in data[menu_plato_categoria]: if it['id'] == str(menu_plato_id): message = "Guardado plato " + menu_plato_categoria + \ @@ -650,8 +651,8 @@ def run(self, dispatcher, tracker, domain): dispatcher.utter_message(text=message) if tracker.get_slot("menu_establecido"): - dispatcher.utter_message(text=f'Tu menu esta compuesto por:') - MenuGetUser.run(self, dispatcher, tracker, domain) + dispatcher.utter_message(text=f'Tu menu esta compuesto por:') + MenuGetUser.run(self, dispatcher, tracker, domain) return "" diff --git a/actions/requirements.txt b/actions/requirements.txt new file mode 100644 index 0000000..d5c37bc --- /dev/null +++ b/actions/requirements.txt @@ -0,0 +1 @@ +Unidecode \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c0b0083 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3.0' +services: + rasa: + container_name: rasa-server + build: + context: . + ports: + - 5005:5005 + volumes: + - ./:/app + command: + - run + depends_on: + - action_server + action_server: + container_name: rasa-actions + build: + context: actions + volumes: + - ./actions:/app/actions + ports: + - 5055:5055 diff --git a/endpoints.yml b/endpoints.yml index c5576e1..5f3402a 100644 --- a/endpoints.yml +++ b/endpoints.yml @@ -11,7 +11,7 @@ # https://rasa.com/docs/rasa/custom-actions action_endpoint: - url: "http://localhost:5055/webhook" + url: "http://action_server:5055/webhook" # Tracker store which is used to store the conversations. # By default the conversations are stored in memory.