Skip to content

Commit

Permalink
Receptionist vector database (#242)
Browse files Browse the repository at this point in the history
* feat: add name and drink vector dbs

* feat: receptionist command similarity matcher
  • Loading branch information
m-barker authored Jul 6, 2024
1 parent f518ec5 commit d59478f
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 17 deletions.
34 changes: 17 additions & 17 deletions tasks/gpsr/src/gpsr/states/command_similarity_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def execute(self, userdata):
sm = smach.StateMachine(outcomes=["succeeded", "failed"])
with sm:
sm.userdata.tts_phrase = "Please tell me your command."
# smach.StateMachine.add(
# "ASK_FOR_COMMAND",
# AskAndListen(),
# transitions={"succeeded": "COMMAND_SIMILARITY_MATCHER", "failed": "failed"},
# remapping={"transcribed_speech": "command"},
# )
smach.StateMachine.add(
"ASK_FOR_COMMAND",
AskAndListen(),
transitions={"succeeded": "COMMAND_SIMILARITY_MATCHER", "failed": "failed"},
remapping={"transcribed_speech": "command"},
)
sm.add(
"LISTEN",
Listen(),
Expand All @@ -71,18 +71,18 @@ def execute(self, userdata):
sm.add(
"COMMAND_SIMILARITY_MATCHER",
CommandSimilarityMatcher([1177943] * 10),
transitions={"succeeded": "LISTEN"},
transitions={"succeeded": "SAY_MATCHED_COMMAND", "failed": "failed"},
)
sm.add(
"SAY_MATCHED_COMMAND",
Say(),
transitions={
"succeeded": "ASK_FOR_COMMAND",
"aborted": "failed",
"preempted": "failed",
},
remapping={"text": "matched_command"},
)
# smach.StateMachine.add(
# "SAY_MATCHED_COMMAND",
# Say(),
# transitions={
# "succeeded": "ASK_FOR_COMMAND",
# "aborted": "failed",
# "preempted": "failed",
# },
# remapping={"text": "matched_command"},
# )

sm.execute()
rospy.spin()
1 change: 1 addition & 0 deletions tasks/receptionist/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ include_directories(
catkin_install_python(PROGRAMS
scripts/main.py
scripts/test_find_and_look_at.py
scripts/create_name_and_drink_vector_db.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Expand Down
Binary file added tasks/receptionist/data/name_and_drink.index
Binary file not shown.
70 changes: 70 additions & 0 deletions tasks/receptionist/data/name_and_drink_vector_db.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
My name is Adel and my favorite drink is cola
My name is Adel and my favorite drink is iced tea
My name is Adel and my favorite drink is juice pack
My name is Adel and my favorite drink is milk
My name is Adel and my favorite drink is orange juice
My name is Adel and my favorite drink is red wine
My name is Adel and my favorite drink is tropical juice
My name is Angel and my favorite drink is cola
My name is Angel and my favorite drink is iced tea
My name is Angel and my favorite drink is juice pack
My name is Angel and my favorite drink is milk
My name is Angel and my favorite drink is orange juice
My name is Angel and my favorite drink is red wine
My name is Angel and my favorite drink is tropical juice
My name is Axel and my favorite drink is cola
My name is Axel and my favorite drink is iced tea
My name is Axel and my favorite drink is juice pack
My name is Axel and my favorite drink is milk
My name is Axel and my favorite drink is orange juice
My name is Axel and my favorite drink is red wine
My name is Axel and my favorite drink is tropical juice
My name is Charlie and my favorite drink is cola
My name is Charlie and my favorite drink is iced tea
My name is Charlie and my favorite drink is juice pack
My name is Charlie and my favorite drink is milk
My name is Charlie and my favorite drink is orange juice
My name is Charlie and my favorite drink is red wine
My name is Charlie and my favorite drink is tropical juice
My name is Jane and my favorite drink is cola
My name is Jane and my favorite drink is iced tea
My name is Jane and my favorite drink is juice pack
My name is Jane and my favorite drink is milk
My name is Jane and my favorite drink is orange juice
My name is Jane and my favorite drink is red wine
My name is Jane and my favorite drink is tropical juice
My name is Jules and my favorite drink is cola
My name is Jules and my favorite drink is iced tea
My name is Jules and my favorite drink is juice pack
My name is Jules and my favorite drink is milk
My name is Jules and my favorite drink is orange juice
My name is Jules and my favorite drink is red wine
My name is Jules and my favorite drink is tropical juice
My name is Morgan and my favorite drink is cola
My name is Morgan and my favorite drink is iced tea
My name is Morgan and my favorite drink is juice pack
My name is Morgan and my favorite drink is milk
My name is Morgan and my favorite drink is orange juice
My name is Morgan and my favorite drink is red wine
My name is Morgan and my favorite drink is tropical juice
My name is Paris and my favorite drink is cola
My name is Paris and my favorite drink is iced tea
My name is Paris and my favorite drink is juice pack
My name is Paris and my favorite drink is milk
My name is Paris and my favorite drink is orange juice
My name is Paris and my favorite drink is red wine
My name is Paris and my favorite drink is tropical juice
My name is Robin and my favorite drink is cola
My name is Robin and my favorite drink is iced tea
My name is Robin and my favorite drink is juice pack
My name is Robin and my favorite drink is milk
My name is Robin and my favorite drink is orange juice
My name is Robin and my favorite drink is red wine
My name is Robin and my favorite drink is tropical juice
My name is Simone and my favorite drink is cola
My name is Simone and my favorite drink is iced tea
My name is Simone and my favorite drink is juice pack
My name is Simone and my favorite drink is milk
My name is Simone and my favorite drink is orange juice
My name is Simone and my favorite drink is red wine
My name is Simone and my favorite drink is tropical juice
48 changes: 48 additions & 0 deletions tasks/receptionist/scripts/create_name_and_drink_vector_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from typing import List
import rospy
import rospkg
from lasr_vector_databases_msgs.srv import TxtIndex, TxtIndexRequest


def create_txt_file(output_path: str) -> None:
"""Creates a txt file containing all permutations of
"My name is <name> and my favorite drink is <drink>"
Args:
output_path (str): Path to the output txt file
"""

names: List[str] = rospy.get_param("/priors/names")
drinks: List[str] = rospy.get_param("/priors/drinks")

with open(output_path, "w") as f:
for name in names:
for drink in drinks:
f.write(f"My name is {name} and my favorite drink is {drink}\n")


def create_vector_db(txt_path: str, output_path: str) -> None:
"""Creates a vector database from a txt file containing
all permutations of "My name is <name> and my favorite drink is <drink>"
Args:
txt_path (str): Path to the txt file
output_path (str): Path to the output vector database
"""

rospy.wait_for_service("lasr_faiss/txt_index")
txt_index = rospy.ServiceProxy("lasr_faiss/txt_index", TxtIndex)

request = TxtIndexRequest()
request.txt_paths = [txt_path]
request.index_paths = [output_path]
request.index_factory_string = "Flat"
txt_index(request)


if __name__ == "__main__":
data_dir = os.path.join(rospkg.RosPack().get_path("receptionist"), "data")
txt_path = os.path.join(data_dir, "name_and_drink_vector_db.txt")
output_path = os.path.join(data_dir, "name_and_drink.index")
create_vector_db(txt_path, output_path)
82 changes: 82 additions & 0 deletions tasks/receptionist/src/receptionist/states/match_name_and_drink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3
import os
import rospy
import rospkg
import smach
from lasr_skills import AskAndListen, Say
from lasr_vector_databases_msgs.srv import TxtQuery, TxtQueryRequest


class MatchNameAndDrink(smach.State):
def __init__(self):
smach.State.__init__(
self,
outcomes=["succeeded", "failed"],
input_keys=["sequence"],
output_keys=["matched_name", "matched_drink", "sequence"],
)

self._query_service = rospy.ServiceProxy("lasr_faiss/txt_query", TxtQuery)
self._text_path = os.path.join(
rospkg.RosPack().get_path("receptionist"),
"data",
"name_and_drink_vector_db.txt",
)
self._index_path = os.path.join(
rospkg.RosPack().get_path("receptionist"), "data", "name_and_drink.index"
)

def execute(self, userdata):
rospy.loginfo(f"Received transcript: {userdata.sequence}")
request = TxtQueryRequest()
request.txt_paths = [self._text_path]
request.index_paths = [self._index_path]
request.query_sentence = userdata.sequence
request.k = 1
response = self._query_service(request)
matched_name, matched_drink = response.closest_sentences[0].split(
" and my favorite drink is "
)
matched_name = matched_name.split("My name is ")[1]
userdata.matched_name = matched_name
userdata.matched_drink = matched_drink
rospy.loginfo(
f"Matched name: {matched_name} and matched drink: {matched_drink}"
)
userdata.sequence = (
f"Hello {matched_name}, I see that your favorite drink is {matched_drink}."
)
return "succeeded"


if __name__ == "__main__":
rospy.init_node("match_name_and_drink")
sm = smach.StateMachine(outcomes=["succeeded", "failed"])
with sm:
smach.StateMachine.add(
"ASK_FOR_NAME_AND_DRINK",
AskAndListen(
tts_phrase="Hello, please tell me your name and favorite drink."
),
transitions={"succeeded": "MATCH_NAME_AND_DRINK", "failed": "failed"},
remapping={"transcribed_speech": "sequence"},
)
smach.StateMachine.add(
"MATCH_NAME_AND_DRINK",
MatchNameAndDrink(),
transitions={"succeeded": "SAY_MATCHED_NAME_AND_DRINK", "failed": "failed"},
remapping={"sequence": "sequence"},
)
smach.StateMachine.add(
"SAY_MATCHED_NAME_AND_DRINK",
Say(),
transitions={
"succeeded": "succeeded",
"aborted": "failed",
"preempted": "failed",
},
remapping={
"text": "sequence",
},
)
sm.execute()

0 comments on commit d59478f

Please sign in to comment.