Skip to content

Commit

Permalink
Merge pull request #153 from deepmipt/dev
Browse files Browse the repository at this point in the history
Release v0.1.6
  • Loading branch information
dilyararimovna committed May 5, 2022
2 parents 48872a6 + dba07ac commit ed42f0c
Show file tree
Hide file tree
Showing 22 changed files with 713 additions and 269 deletions.
5 changes: 5 additions & 0 deletions common/dff/integration/condition.py
Expand Up @@ -11,6 +11,7 @@
import common.dff.integration.context as int_ctx
from common.acknowledgements import GENERAL_ACKNOWLEDGEMENTS
from common.constants import CAN_CONTINUE_SCENARIO, CAN_NOT_CONTINUE
from .facts_utils import provide_facts_request

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -280,3 +281,7 @@ def set_conf_and_can_cont_by_universal_policy(ctx: Context, actor: Actor):

int_ctx.set_can_continue(ctx, actor, can_continue_flag)
int_ctx.set_confidence(ctx, actor, confidence)


def facts(ctx, actor):
return provide_facts_request(ctx, actor)
Expand Up @@ -7,7 +7,7 @@
import requests
import sentry_sdk
import common.constants as common_constants
import common.dialogflow_framework.utils.state as state_utils
import common.dff.integration.context as context

from common.wiki_skill import (
find_page_title,
Expand All @@ -23,11 +23,13 @@
from common.universal_templates import CONTINUE_PATTERN
from common.utils import is_no, is_yes

nltk.download("punkt")

sentry_sdk.init(os.getenv("SENTRY_DSN"))
logger = logging.getLogger(__name__)
WIKI_FACTS_URL = os.getenv("WIKI_FACTS_URL")

with open("/src/common/dialogflow_framework/extensions/wikihow_cache.json", "r") as fl:
with open("/src/common/wikihow_cache.json", "r") as fl:
wikihow_cache = json.load(fl)

memory = {}
Expand Down Expand Up @@ -240,31 +242,30 @@ def preprocess_wikipedia_page(found_entity_substr, found_entity_types, article_c
return page_content_list


def provide_facts_request(vars):
def provide_facts_request(ctx, actor):
flag = False
shared_memory = state_utils.get_shared_memory(vars)
user_uttr = state_utils.get_last_human_utterance(vars)
wiki = ctx.misc.get("wiki", {})
user_uttr: dict = ctx.misc.get("agent", {}).get("dialog", {}).get("human_utterances", [{}])[-1]
isno = is_no(user_uttr)
cur_wiki_page = shared_memory.get("cur_wiki_page", "")
cur_wiki_page = wiki.get("cur_wiki_page", "")
if cur_wiki_page:
wiki_page_content_list = memory.get("wiki_page_content", [])
used_wiki_page_nums = shared_memory.get("used_wiki_page_nums", {}).get(cur_wiki_page, [])
used_wiki_page_nums = wiki.get("used_wiki_page_nums", {}).get(cur_wiki_page, [])
logger.info(f"request, used_wiki_page_nums {used_wiki_page_nums}")
logger.info(f"request, wiki_page_content_list {wiki_page_content_list[:3]}")
if len(wiki_page_content_list) > 0 and len(used_wiki_page_nums) < len(wiki_page_content_list) and not isno:
flag = True
return flag


def provide_facts_response(vars, page_source, wiki_page):
shared_memory = state_utils.get_shared_memory(vars)
user_uttr = state_utils.get_last_human_utterance(vars)
def provide_facts_response(ctx, actor, page_source, wiki_page):
wiki = ctx.misc.get("wiki", {})
user_uttr: dict = ctx.misc.get("agent", {}).get("dialog", {}).get("human_utterances", [{}])[-1]
isyes = is_yes(user_uttr) or re.findall(CONTINUE_PATTERN, user_uttr["text"])
response = ""
cur_wiki_page = shared_memory.get("cur_wiki_page", "")
cur_wiki_page = wiki.get("cur_wiki_page", "")
if not cur_wiki_page:
cur_wiki_page = wiki_page
state_utils.save_to_shared_memory(vars, cur_wiki_page=wiki_page)
wiki["cur_wiki_page"] = wiki_page
if page_source == "wikiHow":
page_content = get_wikihow_content(wiki_page, wikihow_cache)
wiki_page_content_list = preprocess_wikihow_page(page_content)
Expand All @@ -275,7 +276,7 @@ def provide_facts_response(vars, page_source, wiki_page):
memory["wiki_page_content"] = wiki_page_content_list
logger.info(f"wiki_page {wiki_page}")

used_wiki_page_nums_dict = shared_memory.get("used_wiki_page_nums", {})
used_wiki_page_nums_dict = wiki.get("used_wiki_page_nums", {})
used_wiki_page_nums = used_wiki_page_nums_dict.get(wiki_page, [])
wiki_page_content_list = memory.get("wiki_page_content", [])
logger.info(f"response, used_wiki_page_nums {used_wiki_page_nums}")
Expand All @@ -289,23 +290,30 @@ def provide_facts_response(vars, page_source, wiki_page):
response = f"{facts_str} {question}".strip().replace(" ", " ")
used_wiki_page_nums.append(num)
used_wiki_page_nums_dict[wiki_page] = used_wiki_page_nums
state_utils.save_to_shared_memory(vars, used_wiki_page_nums=used_wiki_page_nums_dict)
wiki["used_wiki_page_nums"] = used_wiki_page_nums_dict
break

if len(wiki_page_content_list) == len(used_wiki_page_nums):
if len(wiki_page_content_list) == len(used_wiki_page_nums):
state_utils.save_to_shared_memory(vars, wiki_page="")
wiki["wiki_page"] = ""
memory["wiki_page_content"] = []

logger.info(f"response, final {response}")
if response:
if isyes:
state_utils.set_confidence(vars, confidence=1.0)
state_utils.set_can_continue(vars, continue_flag=common_constants.MUST_CONTINUE)
context.set_confidence(ctx, actor, 1.0)
context.set_can_continue(ctx, actor, common_constants.MUST_CONTINUE)
else:
state_utils.set_confidence(vars, confidence=0.99)
state_utils.set_can_continue(vars, continue_flag=common_constants.CAN_CONTINUE_SCENARIO)
context.set_confidence(ctx, actor, 0.99)
context.set_can_continue(ctx, actor, common_constants.CAN_CONTINUE_SCENARIO)
else:
context.set_confidence(ctx, actor, 0.0)
context.set_can_continue(ctx, actor, common_constants.CAN_NOT_CONTINUE)
if hasattr(ctx, "a_s"):
processed_node = ctx.a_s.get("processed_node", ctx.a_s["next_node"])
processed_node.response = response
ctx.a_s["processed_node"] = processed_node
else:
state_utils.set_confidence(vars, confidence=0.0)
state_utils.set_can_continue(vars, continue_flag=common_constants.CAN_NOT_CONTINUE)
state_utils.add_acknowledgement_to_response_parts(vars)
return response
processed_node = ctx.framework_states["actor"].get("processed_node", ctx.framework_states["actor"]["next_node"])
processed_node.response = response
ctx.framework_states["actor"]["processed_node"] = processed_node
return ctx
34 changes: 33 additions & 1 deletion common/dff/integration/processing.py
@@ -1,8 +1,9 @@
import logging

from df_engine.core import Context, Actor

import common.constants as common_constants
from common.wiki_skill import extract_entity
from .facts_utils import provide_facts_response
from . import context


Expand All @@ -22,6 +23,37 @@ def save_slots_to_ctx_processing(
return save_slots_to_ctx_processing


def entities(**kwargs):
slot_info = list(kwargs.items())

def extract_entities(
ctx: Context,
actor: Actor,
*args,
**kwargs,
) -> Context:
for slot_name, slot_types in slot_info:
if isinstance(slot_types, str):
extracted_entity = extract_entity(ctx, slot_types)
if extracted_entity:
ctx = save_slots_to_ctx({slot_name: extracted_entity})(ctx, actor)
elif isinstance(slot_types, list):
for slot_type in slot_types:
extracted_entity = extract_entity(ctx, slot_type)
if extracted_entity:
ctx = save_slots_to_ctx({slot_name: extracted_entity})(ctx, actor)
return ctx

return extract_entities


def fact_provider(page_source, wiki_page):
def response(ctx: Context, actor: Actor, *args, **kwargs):
return provide_facts_response(ctx, actor, page_source, wiki_page)

return response


def fill_responses_by_slots():
def fill_responses_by_slots_processing(
ctx: Context,
Expand Down
67 changes: 0 additions & 67 deletions common/dialogflow_framework/extensions/custom_functions.py
Expand Up @@ -3,7 +3,6 @@
import nltk
import common.dialogflow_framework.utils.state as state_utils
from common.utils import is_yes
from common.wiki_skill import find_entity_by_types

logger = logging.getLogger(__name__)

Expand All @@ -23,72 +22,6 @@ def drawing_request(vars):
return flag


def extract_entity(vars, entity_type):
user_uttr = state_utils.get_last_human_utterance(vars)
annotations = user_uttr["annotations"]
if entity_type.startswith("tags"):
tag = entity_type.split("tags:")[1]
nounphrases = annotations.get("entity_detection", {}).get("labelled_entities", [])
for nounphr in nounphrases:
nounphr_text = nounphr.get("text", "")
nounphr_label = nounphr.get("label", "")
if nounphr_label == tag:
found_entity = nounphr_text
return found_entity
elif entity_type.startswith("wiki"):
wp_type = entity_type.split("wiki:")[1]
found_entity, *_ = find_entity_by_types(annotations, [wp_type])
if found_entity:
return found_entity
elif entity_type == "any_entity":
entities = annotations.get("entity_detection", {}).get("entities", [])
if entities:
return entities[0]
else:
res = re.findall(entity_type, user_uttr["text"])
if res:
return res[0]
return ""


def has_entities(entity_types):
def has_entities_func(vars):
flag = False
if isinstance(entity_types, str):
extracted_entity = extract_entity(vars, entity_types)
if extracted_entity:
flag = True
elif isinstance(entity_types, list):
for entity_type in entity_types:
extracted_entity = extract_entity(vars, entity_type)
if extracted_entity:
flag = True
break
return flag

return has_entities_func


def entities(**kwargs):
def extract_entities(vars):
shared_memory = state_utils.get_shared_memory(vars)
slot_values = shared_memory.get("slot_values", {})
for slot_name, slot_types in kwargs.items():
if isinstance(slot_types, str):
extracted_entity = extract_entity(vars, slot_types)
if extracted_entity:
slot_values[slot_name] = extracted_entity
state_utils.save_to_shared_memory(vars, slot_values=slot_values)
elif isinstance(slot_types, list):
for slot_type in slot_types:
extracted_entity = extract_entity(vars, slot_type)
if extracted_entity:
slot_values[slot_name] = extracted_entity
state_utils.save_to_shared_memory(vars, slot_values=slot_values)

return extract_entities


def speech_functions(*args):
def check_speech_function(vars):
flag = False
Expand Down
20 changes: 0 additions & 20 deletions common/dialogflow_framework/extensions/intents.py

This file was deleted.

8 changes: 0 additions & 8 deletions common/dialogflow_framework/extensions/providers.py

This file was deleted.

29 changes: 29 additions & 0 deletions common/wiki_skill.py
Expand Up @@ -618,6 +618,35 @@ def check_nounphr(annotations, nounphr_to_find):
return ""


def extract_entity(ctx, entity_type):
user_uttr: dict = ctx.misc.get("agent", {}).get("dialog", {}).get("human_utterances", [{}])[-1]
annotations = user_uttr.get("annotations", {})
logger.info(f"annotations {annotations}")
if entity_type.startswith("tags"):
tag = entity_type.split("tags:")[1]
nounphrases = annotations.get("entity_detection", {}).get("labelled_entities", [])
for nounphr in nounphrases:
nounphr_text = nounphr.get("text", "")
nounphr_label = nounphr.get("label", "")
if nounphr_label == tag:
found_entity = nounphr_text
return found_entity
elif entity_type.startswith("wiki"):
wp_type = entity_type.split("wiki:")[1]
found_entity, *_ = find_entity_by_types(annotations, [wp_type])
if found_entity:
return found_entity
elif entity_type == "any_entity":
entities = annotations.get("entity_detection", {}).get("entities", [])
if entities:
return entities[0]
else:
res = re.findall(entity_type, user_uttr["text"])
if res:
return res[0]
return ""


def if_user_dont_know_topic(user_uttr, bot_uttr):
flag = False
what_to_talk_about = re.findall(COMPILE_WHAT_TO_TALK_ABOUT, bot_uttr.get("text", ""))
Expand Down
1 change: 1 addition & 0 deletions common/wiki_skill_scenarios.py
Expand Up @@ -379,6 +379,7 @@
"answer": "Drawing gives you a mean to self-reflect and externalize your emotions.",
}
],
"conf": 0.99,
},
{
"utt": ["What kind of paintings do you like to draw: landscapes, portraits or something else?"],
Expand Down
File renamed without changes.
Expand Up @@ -177,10 +177,8 @@ def choose_best_with_scores(curr_cands_ids, curr_single_scores, candidates, bot_
def get_main_info_annotations(annotated_utterance):
intents = get_intents(annotated_utterance, which="all")
topics = get_topics(annotated_utterance, which="all")
named_entities = [ent[0]["text"] for ent in annotated_utterance.get("annotations", {}).get("ner", []) if ent]
nounphrases = [
nounph for nounph in annotated_utterance.get("annotations", {}).get("spacy_nounphrases", []) if nounph
]
named_entities = get_entities(annotated_utterance, only_named=True, with_labels=False)
nounphrases = get_entities(annotated_utterance, only_named=False, with_labels=False)
return intents, topics, named_entities, nounphrases


Expand Down Expand Up @@ -627,7 +625,7 @@ def tag_based_response_selection(dialog, candidates, scores, confidences, bot_ut
# -------------------- SUPER CONFIDENCE CASE HERE! --------------------
categorized_hyps = add_to_top1_category(cand_id, categorized_hyps, _is_require_action_intent)

if cand_uttr["skill_name"] == "dff_grounding_skill" and "acknowledgement" in cand_uttr.get(
if cand_uttr["skill_name"] == "dff_grounding_skill" and ["acknowledgement"] == cand_uttr.get(
"response_parts", []
):
acknowledgement_hypothesis = deepcopy(cand_uttr)
Expand Down
16 changes: 4 additions & 12 deletions skills/dff_art_skill/Dockerfile
Expand Up @@ -3,27 +3,19 @@ FROM python:3.9.1
# Do not change anything in this section
WORKDIR /src

COPY common/dialogflow_framework/requirements.txt .
COPY common/dff/requirements.txt .
RUN pip install -r requirements.txt

COPY common/dialogflow_framework/scripts /scripts

RUN bash /scripts/programy_logger_off.sh

# ###################### CUSTOM SECTION ######################################
# Here you can make changes
# Here you can make changes
RUN mkdir /global_data

ARG SERVICE_NAME
ENV SERVICE_NAME ${SERVICE_NAME}

COPY skills/${SERVICE_NAME}/requirements.txt .
RUN pip install -r requirements.txt

RUN python -m spacy download en
RUN python -m spacy download en_core_web_sm
RUN python -m spacy download en_core_web_md
RUN python -m nltk.downloader vader_lexicon
RUN python -m nltk.downloader punkt
RUN python -m nltk.downloader wordnet

COPY skills/${SERVICE_NAME}/ ./
COPY ./common/ ./common/
Expand Down

0 comments on commit ed42f0c

Please sign in to comment.