Add fuzzy and text-classification light model #1
4 changed files with 87 additions and 26 deletions
13
main.py
13
main.py
|
@ -10,7 +10,8 @@ from src.ratatouille import Ratatouille
|
||||||
from src.mpd import Mpd
|
from src.mpd import Mpd
|
||||||
from src.hass import Hass
|
from src.hass import Hass
|
||||||
from src.httpServer import get_server
|
from src.httpServer import get_server
|
||||||
from src.intent import AlexaIntent
|
from src.fuzzy import fuzz_predict
|
||||||
|
# from src.intent import AlexaIntent
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=10,
|
level=10,
|
||||||
|
@ -22,16 +23,18 @@ PORT = 5555
|
||||||
|
|
||||||
walle = Hass(config.hass_url, config.hass_token)
|
walle = Hass(config.hass_url, config.hass_token)
|
||||||
yoda = None # Rhasspy(config.rhasspy_url)
|
yoda = None # Rhasspy(config.rhasspy_url)
|
||||||
mopidy = Mpd('10.10.10.10', yoda)
|
# mopidy = Mpd('10.10.10.10', yoda)
|
||||||
ratatouille = Ratatouille(yoda, walle, mopidy, schedule)
|
ratatouille = Ratatouille(yoda, walle, mopidy, schedule)
|
||||||
alexa = AlexaIntent() # we are not doing any request to the evil amazon but we are using one of its dataset
|
# alexa = AlexaIntent() # we are not doing any request to the evil amazon but we are using one of its dataset
|
||||||
|
|
||||||
|
|
||||||
def answer(sentence):
|
def answer(sentence):
|
||||||
return ratatouille.parseAlexa(alexa.predict(sentence))
|
# return ratatouille.parse_alexa(alexa.predict(sentence))
|
||||||
|
return ratatouille.parse_fuzzy(fuzz_predict(sentence))
|
||||||
# return "42"
|
# return "42"
|
||||||
|
|
||||||
|
|
||||||
server = get_server(IP, PORT, answer)
|
server = get_server(IP, PORT, answer)
|
||||||
|
|
||||||
logging.info('Running server on '+IP+':'+str(PORT))
|
logging.info('Running server on '+IP+':'+str(PORT))
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
|
|
||||||
|
|
|
@ -2,5 +2,6 @@ paho-mqtt<1.6
|
||||||
requests<2.26
|
requests<2.26
|
||||||
schedule<1.1.0
|
schedule<1.1.0
|
||||||
python-mpd2<3.1
|
python-mpd2<3.1
|
||||||
transformers<4.28.0
|
#transformers<4.28.0
|
||||||
torch<2.1.0
|
#torch<2.1.0
|
||||||
|
rapidfuzz<4.0.0
|
44
src/fuzzy.py
Normal file
44
src/fuzzy.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
from rapidfuzz import process, fuzz, utils
|
||||||
|
|
||||||
|
|
||||||
|
ROOMS = ["cuisine", "salon", "chambre", "bureau"]
|
||||||
|
|
||||||
|
|
||||||
|
def compute_sentences_lamp_on():
|
||||||
|
return ["allume la lumière de la" + room for room in ROOMS]
|
||||||
|
|
||||||
|
|
||||||
|
def compute_sentences_lamp_off():
|
||||||
|
return ["éteins la lumière de la" + room for room in ROOMS]
|
||||||
|
|
||||||
|
|
||||||
|
SENTENCES_HOUR = ["quel heure est-il ?"]
|
||||||
|
SENTENCES_LAMP_ON = compute_sentences_lamp_on()
|
||||||
|
SENTENCES_LAMP_OFF = compute_sentences_lamp_off()
|
||||||
|
# SENTENCES_MUSIQUE = computes_sentences
|
||||||
|
|
||||||
|
|
||||||
|
def fuzz_predict(text):
|
||||||
|
choices = SENTENCES_HOUR + SENTENCES_LAMP_ON + SENTENCES_LAMP_OFF
|
||||||
|
result = process.extractOne(
|
||||||
|
text, choices, scorer=fuzz.WRatio, processor=utils.default_process)
|
||||||
|
|
||||||
|
choosen_sentence = result[0]
|
||||||
|
if choosen_sentence in SENTENCES_HOUR:
|
||||||
|
return {'intentName': 'GetTime'}
|
||||||
|
|
||||||
|
if choosen_sentence in SENTENCES_LAMP_ON:
|
||||||
|
return {'intentName': 'LightOn', 'intentArg': [find_matching(ROOMS, text)]}
|
||||||
|
|
||||||
|
if choosen_sentence in SENTENCES_LAMP_OFF:
|
||||||
|
return {'intentName': 'LightOff', 'intentArg': [find_matching(ROOMS, text)]}
|
||||||
|
|
||||||
|
return {'intentName': 'Unknown'}
|
||||||
|
|
||||||
|
|
||||||
|
def find_matching(list_str, text):
|
||||||
|
for search in list_str:
|
||||||
|
if search in text:
|
||||||
|
return search
|
||||||
|
|
||||||
|
return None
|
|
@ -11,6 +11,7 @@ from src.tools.parse_entities import parse_entities
|
||||||
|
|
||||||
from src.const.temperature_keyword import TEMPERATURE_KEYWORD
|
from src.const.temperature_keyword import TEMPERATURE_KEYWORD
|
||||||
|
|
||||||
|
|
||||||
class Ratatouille():
|
class Ratatouille():
|
||||||
|
|
||||||
def __init__(self, yoda, walle, mopidy, schedule):
|
def __init__(self, yoda, walle, mopidy, schedule):
|
||||||
|
@ -44,7 +45,7 @@ class Ratatouille():
|
||||||
self.yoda.say(response)
|
self.yoda.say(response)
|
||||||
return
|
return
|
||||||
|
|
||||||
def parseAlexa(self,payload):
|
def parse_alexa(self, payload):
|
||||||
print(payload)
|
print(payload)
|
||||||
intent = payload['intents'][0]['label']
|
intent = payload['intents'][0]['label']
|
||||||
entities = payload['entities']
|
entities = payload['entities']
|
||||||
|
@ -59,6 +60,15 @@ class Ratatouille():
|
||||||
return self.weather_query(parse_entities(entities))
|
return self.weather_query(parse_entities(entities))
|
||||||
return '42'
|
return '42'
|
||||||
|
|
||||||
|
def parse_fuzzy(self, payload):
|
||||||
|
command = payload['intentName']
|
||||||
|
if command == 'GetTime':
|
||||||
|
return self.send_hour()
|
||||||
|
elif command == "LightOn":
|
||||||
|
self.walle.light_on(payload['intentArg'][0])
|
||||||
|
elif command == "LightOff":
|
||||||
|
self.walle.light_off(payload['intentArg'][0])
|
||||||
|
|
||||||
def weather_query(self, entities):
|
def weather_query(self, entities):
|
||||||
if any(a in entities['B-weather_descriptor'] for a in TEMPERATURE_KEYWORD):
|
if any(a in entities['B-weather_descriptor'] for a in TEMPERATURE_KEYWORD):
|
||||||
res = self.send_temperature()
|
res = self.send_temperature()
|
||||||
|
@ -99,7 +109,8 @@ class Ratatouille():
|
||||||
def send_temperature(self):
|
def send_temperature(self):
|
||||||
logging.info('Send temperature')
|
logging.info('Send temperature')
|
||||||
data = self.walle.get('weather.toulouse')
|
data = self.walle.get('weather.toulouse')
|
||||||
temperature = str(data['attributes']['temperature']).replace('.',' virgule ')
|
temperature = str(data['attributes']['temperature']
|
||||||
|
).replace('.', ' virgule ')
|
||||||
return 'il fait '+temperature+' degrés'
|
return 'il fait '+temperature+' degrés'
|
||||||
|
|
||||||
def light_off(self, entities):
|
def light_off(self, entities):
|
||||||
|
@ -132,8 +143,8 @@ class Ratatouille():
|
||||||
else:
|
else:
|
||||||
return 'J\'ai pas pu allumer ' + int_to_str(number_error, 'f') + ' lampes'
|
return 'J\'ai pas pu allumer ' + int_to_str(number_error, 'f') + ' lampes'
|
||||||
|
|
||||||
|
|
||||||
# -- -- hibernate -- --
|
# -- -- hibernate -- --
|
||||||
|
|
||||||
def can_hibernate(self):
|
def can_hibernate(self):
|
||||||
lamp_desk = self.walle.get('switch.prise_bureau_switch')
|
lamp_desk = self.walle.get('switch.prise_bureau_switch')
|
||||||
if lamp_desk:
|
if lamp_desk:
|
||||||
|
@ -153,11 +164,13 @@ class Ratatouille():
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
else:
|
else:
|
||||||
self.schedule.clear('hourly-hibernate')
|
self.schedule.clear('hourly-hibernate')
|
||||||
self.schedule.every(3).minutes.do(self.hibernate).tag('hourly-hibernate')
|
self.schedule.every(3).minutes.do(
|
||||||
|
self.hibernate).tag('hourly-hibernate')
|
||||||
|
|
||||||
logging.info('retry to hibernate in 3 minutes')
|
logging.info('retry to hibernate in 3 minutes')
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def clear_hibernate(self):
|
def clear_hibernate(self):
|
||||||
self.schedule.clear('hourly-hibernate')
|
self.schedule.clear('hourly-hibernate')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue