Add fuzzy and text-classification light model #1

Merged
Tjiho merged 18 commits from fuzzy into master 2025-02-01 01:35:11 +01:00
4 changed files with 87 additions and 26 deletions
Showing only changes of commit a89634a231 - Show all commits

19
main.py
View file

@ -10,7 +10,8 @@ from src.ratatouille import Ratatouille
from src.mpd import Mpd
from src.hass import Hass
from src.httpServer import get_server
from src.intent import AlexaIntent
from src.fuzzy import fuzz_predict
# from src.intent import AlexaIntent
logging.basicConfig(
level=10,
@ -21,17 +22,19 @@ IP = "10.10.10.11"
PORT = 5555
walle = Hass(config.hass_url, config.hass_token)
yoda = None #Rhasspy(config.rhasspy_url)
mopidy = Mpd('10.10.10.10', yoda)
yoda = None # Rhasspy(config.rhasspy_url)
# mopidy = Mpd('10.10.10.10', yoda)
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):
return ratatouille.parseAlexa(alexa.predict(sentence))
#return "42"
# return ratatouille.parse_alexa(alexa.predict(sentence))
return ratatouille.parse_fuzzy(fuzz_predict(sentence))
# return "42"
server = get_server(IP,PORT,answer)
server = get_server(IP, PORT, answer)
logging.info('Running server on '+IP+':'+str(PORT))
server.serve_forever()

View file

@ -2,5 +2,6 @@ paho-mqtt<1.6
requests<2.26
schedule<1.1.0
python-mpd2<3.1
transformers<4.28.0
torch<2.1.0
#transformers<4.28.0
#torch<2.1.0
rapidfuzz<4.0.0

44
src/fuzzy.py Normal file
View 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

View file

@ -11,9 +11,10 @@ from src.tools.parse_entities import parse_entities
from src.const.temperature_keyword import TEMPERATURE_KEYWORD
class Ratatouille():
def __init__(self,yoda, walle, mopidy, schedule):
def __init__(self, yoda, walle, mopidy, schedule):
self.yoda = yoda
self.walle = walle
self.mopidy = mopidy
@ -24,7 +25,7 @@ class Ratatouille():
# yoda.say('Ratatouille a bien démmaré')
logging.info('loaded')
def parse_rhasspy_command(self,payload):
def parse_rhasspy_command(self, payload):
command = payload['intent']['intentName']
print(command)
response = ''
@ -40,11 +41,11 @@ class Ratatouille():
response = self.pause_music()
elif command == 'StartMusic':
response = self.start_music()
self.yoda.say(response)
return
def parseAlexa(self,payload):
def parse_alexa(self, payload):
print(payload)
intent = payload['intents'][0]['label']
entities = payload['entities']
@ -59,12 +60,21 @@ class Ratatouille():
return self.weather_query(parse_entities(entities))
return '42'
def weather_query(self,entities):
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):
if any(a in entities['B-weather_descriptor'] for a in TEMPERATURE_KEYWORD):
res = self.send_temperature()
return res
def play_genre(self,genre):
def play_genre(self, genre):
try:
self.mopidy.play_genre(genre)
except Exception as e:
@ -99,10 +109,11 @@ class Ratatouille():
def send_temperature(self):
logging.info('Send temperature')
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'
def light_off(self,entities):
def light_off(self, entities):
number_error = 0
for lamp in entities:
try:
@ -115,9 +126,9 @@ class Ratatouille():
if number_error == 0:
return 'Et voila !'
else:
return 'J\'ai pas pu eteindre ' + int_to_str(number_error,'f') + ' lampes'
return 'J\'ai pas pu eteindre ' + int_to_str(number_error, 'f') + ' lampes'
def light_on(self,entities):
def light_on(self, entities):
number_error = 0
for lamp in entities:
try:
@ -130,19 +141,19 @@ class Ratatouille():
if number_error == 0:
return 'Et voila !'
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 -- --
def can_hibernate(self):
lamp_desk = self.walle.get('switch.prise_bureau_switch')
if lamp_desk:
desk_is_on = lamp_desk['attributes'].get('state','OFF') == 'ON'
desk_is_on = lamp_desk['attributes'].get('state', 'OFF') == 'ON'
lamp_bathroom = self.walle.get('light.chihiro_chihiro')
if lamp_bathroom:
bathroom_is_on = lamp_bathroom.get('state','OFF') == 'on'
#bathroom_is_on = self.walle.get('switch.prise_bureau_switch')['attributes']['state'] == 'ON'
bathroom_is_on = lamp_bathroom.get('state', 'OFF') == 'on'
# bathroom_is_on = self.walle.get('switch.prise_bureau_switch')['attributes']['state'] == 'ON'
return lamp_desk and lamp_bathroom and not desk_is_on and not bathroom_is_on
def hibernate(self):
@ -153,11 +164,13 @@ class Ratatouille():
time.sleep(5)
else:
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')
return
def clear_hibernate(self):
self.schedule.clear('hourly-hibernate')