diff --git a/src/main.py b/src/main.py index 3a505a5..6e99efb 100644 --- a/src/main.py +++ b/src/main.py @@ -11,9 +11,6 @@ monitor = DialogStateMonitor() # DSM dialog_policy = DialogPolicy() # DP language_generation = NaturalLanguageGeneration(templates) # NLG -agent = PipelineAgent(nlu=nlu, dst=monitor, policy=None, nlg=language_generation, name='sys') -resp = agent.response("Dzień dobry") -print(resp) # Main loop dial_num = 0 print("CTRL+C aby zakończyć program.") @@ -35,8 +32,7 @@ while True: system_action = dialog_policy.predict(monitor) # NLG - act, slots = parse_frame(frame) - response = language_generation.generate(act, slots) + response = language_generation.generate(frame) print(response) if frame.act == "bye": diff --git a/src/service/dialog_policy.py b/src/service/dialog_policy.py index a40bb16..18597b1 100644 --- a/src/service/dialog_policy.py +++ b/src/service/dialog_policy.py @@ -1,6 +1,6 @@ from collections import defaultdict from model.frame import Frame -from src.model.slot import Slot +from model.slot import Slot class DialogPolicy: diff --git a/src/service/dialog_state_monitor.py b/src/service/dialog_state_monitor.py index 73705e8..629e6de 100644 --- a/src/service/dialog_state_monitor.py +++ b/src/service/dialog_state_monitor.py @@ -1,4 +1,4 @@ -from src.model.frame import Frame +from model.frame import Frame import copy import json @@ -9,7 +9,7 @@ def normalize(value): class DialogStateMonitor: - def __init__(self, initial_state_file: str = '../attributes.json'): + def __init__(self, initial_state_file: str = 'attributes.json'): with open(initial_state_file) as file: constants = json.load(file) self.__initial_state = dict(belief_state={ diff --git a/src/service/natural_languag_understanding.py b/src/service/natural_languag_understanding.py index a76f2b5..0ca6c8d 100644 --- a/src/service/natural_languag_understanding.py +++ b/src/service/natural_languag_understanding.py @@ -1,8 +1,7 @@ -from convlab.nlu.nlu import NLU from flair.models import SequenceTagger from utils.nlu_utils import predict_single, predict_and_annotate from model.frame import Frame, Slot - +import random """ ACTS: inform/order @@ -42,7 +41,7 @@ SLOTS: sauce """ -class NaturalLanguageUnderstanding(NLU): +class NaturalLanguageUnderstanding(): def __init__(self): print("\n========================================================") print("Models are loading, it may take a moment, please wait...") @@ -86,8 +85,15 @@ class NaturalLanguageUnderstanding(NLU): return slots - def predict(self, text: str, context: list): + def predict(self, text: str): act = self.__predict_intention(text) slots = self.__predict_slot(text) frame = Frame(source = 'user', act = act, slots = slots) + # uncomment to quickly mock the response + # frames = [ + # Frame(source="user", act = "inform/order", slots=[Slot(name="pizza", value="barcelona")]), + # Frame(source="user", act = "welcomemsg", slots=[]), + # Frame(source="user", act = "request/menu", slots=[]), + # ] + # return random.choice(frames) return frame \ No newline at end of file diff --git a/src/service/natural_language_generation.py b/src/service/natural_language_generation.py index 14c37ba..8ab6875 100644 --- a/src/service/natural_language_generation.py +++ b/src/service/natural_language_generation.py @@ -1,26 +1,26 @@ -import re -from service.template_selector import select_template -import random -from convlab.nlg.nlg import NLG - -# from service.templates import templates - -def parse_frame(frame): - if not hasattr(frame, 'act') or not hasattr(frame, 'slots'): - raise TypeError("Expected a Frame object with 'act' and 'slots' attributes.") - - act = frame.act - slots = [{"name": slot.name, "value": slot.value} for slot in frame.slots] - - return act, slots - -class NaturalLanguageGeneration(NLG): - def __init__(self, templates): - self.templates = templates - - def generate(self, act, slots): - template = select_template(act, slots) - if template == "default/template": - template = random.choice(self.templates["default/template"]) - slot_dict = {slot['name']: slot['value'] for slot in slots} +import re +from service.template_selector import select_template +import random + +# from service.templates import templates + +def parse_frame(frame): + if not hasattr(frame, 'act') or not hasattr(frame, 'slots'): + raise TypeError("Expected a Frame object with 'act' and 'slots' attributes.") + + act = frame.act + slots = [{"name": slot.name, "value": slot.value} for slot in frame.slots] + + return act, slots + +class NaturalLanguageGeneration(): + def __init__(self, templates): + self.templates = templates + + def generate(self, frame): + act, slots = parse_frame(frame) + template = select_template(act, slots) + if template == "default/template": + template = random.choice(self.templates["default/template"]) + slot_dict = {slot['name']: slot['value'] for slot in slots} return template.format(**slot_dict) \ No newline at end of file