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

13
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,
@ -22,16 +23,18 @@ PORT = 5555
walle = Hass(config.hass_url, config.hass_token)
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)
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 ratatouille.parse_alexa(alexa.predict(sentence))
return ratatouille.parse_fuzzy(fuzz_predict(sentence))
# return "42"
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,6 +11,7 @@ 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):
@ -44,7 +45,7 @@ class Ratatouille():
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,6 +60,15 @@ class Ratatouille():
return self.weather_query(parse_entities(entities))
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):
if any(a in entities['B-weather_descriptor'] for a in TEMPERATURE_KEYWORD):
res = self.send_temperature()
@ -99,7 +109,8 @@ 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):
@ -132,8 +143,8 @@ class Ratatouille():
else:
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:
@ -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')