Improved spotify intent and added result (json) response in RESTAPI

This commit is contained in:
Mathieu B 2021-07-30 14:10:35 +02:00
parent 1695cd2903
commit d1ddb1664c
9 changed files with 55 additions and 30 deletions

View File

@ -7,7 +7,7 @@ from jarvis.skills.entertainement.jokes import JokesSkill
from jarvis.skills.entertainement.spotify import SpotifySkill from jarvis.skills.entertainement.spotify import SpotifySkill
from jarvis.skills.research.wikipedia import WikipediaSkill from jarvis.skills.research.wikipedia import WikipediaSkill
from jarvis.utils import languages_utils from jarvis.utils import languages_utils
from utils import config_utils, flask_utils, utils from utils import config_utils, flask_utils
app = Flask(__name__) app = Flask(__name__)
@ -19,7 +19,7 @@ def process_request():
if 'sentence' not in data or not data['sentence']: if 'sentence' not in data or not data['sentence']:
flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!')) flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!'))
return {} return jsonify(intent_manager.recognise(sentence=data['sentence']))
if __name__ == '__main__': if __name__ == '__main__':
@ -32,7 +32,7 @@ if __name__ == '__main__':
JokesSkill().register() JokesSkill().register()
SpotifySkill().register() SpotifySkill().register()
intent_manager.process_handlers() intent_manager.load_all_skills()
intent_manager.recognise("cherche sur wikipédia Elon Musk") # TO CHECK intent_manager.recognise("cherche sur wikipédia Elon Musk") # TO CHECK
intent_manager.recognise("c'est qui Elon Musk") # TO CHECK intent_manager.recognise("c'est qui Elon Musk") # TO CHECK

View File

@ -9,19 +9,26 @@ class SpotifySkill(Skill, metaclass=SkillRegistering):
@intent_file_handler("play_a_song.intent", "PlaySongWithSpotifyIntent") @intent_file_handler("play_a_song.intent", "PlaySongWithSpotifyIntent")
def handle_play_a_song(self, data): def handle_play_a_song(self, data):
print(data)
song_lists_matching = None
if 'song' in data: if 'song' in data:
song_lists_matching = spotify.query_song(data['song']) song_lists_matching = spotify.query_song(data['song'])
if 'singer' in data: if 'singer' in data:
song_lists_matching = spotify.query_song(data['song'], data['singer']) song_lists_matching = spotify.query_song(None, data['singer'])
if 'song' in data and 'singer' in data:
song_lists_matching = spotify.query_song(data['song'], data['singer'])
if song_lists_matching is not None and len(song_lists_matching) >= 1: if song_lists_matching is not None and len(song_lists_matching) >= 1:
print( print(
"[INFO INTENT] - Now playing : " + song_lists_matching[0]['uri'] + " / " + song_lists_matching[0][ "[INFO INTENT] - Now playing : " + song_lists_matching[0]['uri'] + " / " + song_lists_matching[0][
'name'] + " / " + 'name'] + " / " +
song_lists_matching[0]['artists'][0]['name']) song_lists_matching[0]['artists'][0]['name'])
spotify.get_spotify().add_to_queue(uri=song_lists_matching[0]['uri'])
spotify.get_spotify().next_track() spotify.get_spotify().add_to_queue(uri=song_lists_matching[0]['uri'])
# spotify.get_spotify().start_playback(context_uri=song_lists_matching[0]['uri']) spotify.get_spotify().next_track()
# spotify.get_spotify().start_playback(context_uri=song_lists_matching[0]['uri'])
@intent_file_handler("pause_music.intent", "PauseSpotifyIntent") @intent_file_handler("pause_music.intent", "PauseSpotifyIntent")
def pause_music(self, data): def pause_music(self, data):

View File

@ -12,6 +12,8 @@ sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope,
client_secret=config_utils.get_in_config("SPOTIFY_CLIENT_SECRET"), client_secret=config_utils.get_in_config("SPOTIFY_CLIENT_SECRET"),
redirect_uri='http://localhost:8888/callback/', redirect_uri='http://localhost:8888/callback/',
open_browser=False)) open_browser=False))
# TODO: Investigate the open_browser and automatic auth renewing without user interaction # TODO: Investigate the open_browser and automatic auth renewing without user interaction
@ -19,9 +21,12 @@ def get_spotify():
return sp return sp
def query_song(song, artist=None): def query_song(song=None, artist=None):
print(str(song) + " / " + str(artist))
if song is not None and artist is not None: if song is not None and artist is not None:
song_search = '*{}* artist:{}'.format(song, artist) song_search = '*{}* artist:{}'.format(song, artist)
elif song is None and artist is not None:
return query_artist(artist)
else: else:
song_search = song song_search = song
@ -45,6 +50,15 @@ def query_song(song, artist=None):
return data return data
def query_artist(artist):
tracks = get_spotify().search(q=("artist:" + artist), limit=10, type='track')['tracks']['items']
if len(tracks) > 0:
tracks.reverse() # Place best matches first
return tracks
return None
def best_confidence(title, query): def best_confidence(title, query):
"""Find best match for a title against a query. """Find best match for a title against a query.
Some titles include ( Remastered 2016 ) and similar info. This method Some titles include ( Remastered 2016 ) and similar info. This method

View File

@ -1,3 +0,0 @@
de
du groupe
d'

View File

@ -1,6 +0,0 @@
joue
mets
mets
fait moi écouter
écouter
joue le morceau

View File

@ -1,3 +0,0 @@
Spotify
spotify
spot

View File

@ -2,4 +2,5 @@ C'est quoi le nom du morceau actuel
On écoute quoi la On écoute quoi la
C'est quoi le titre de la chanson actuelle C'est quoi le titre de la chanson actuelle
C'est quoi cette chanson C'est quoi cette chanson
C'est quoi cette musique C'est quoi cette musique
c'est quoi la chanson actuelle

View File

@ -1,5 +1,6 @@
Joue {song} sur spotify Joue {song} sur spotify
Joue {song} Joue {song}
Joue la chanson {song}
Joue {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} Joue {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer}
Mets la chanson {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} Mets la chanson {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer}
Mets la chanson {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify Mets la chanson {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify
@ -22,4 +23,11 @@ Mets voir {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spo
Fait moi écouter {song} Fait moi écouter {song}
Fait moi écouter {song} sur spotify Fait moi écouter {song} sur spotify
Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer}
Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify Fait moi écouter {song} (de|des|du groupe|du chanteur|de la chanteuse) {singer} sur spotify
Joue quelque chose de {singer}
Mets de la musique du chanteur {singer}
Joue quelque chose du chanteur {singer}
Joue quelque chose de la chanteuse {singer}
Mets quelque chose de {singer}
Mets quelque chose de la chanteuse {singer}
Mets quelque chose du chanteur {singer}

View File

@ -1,3 +1,5 @@
import json
from adapt.engine import DomainIntentDeterminationEngine from adapt.engine import DomainIntentDeterminationEngine
from padatious import IntentContainer from padatious import IntentContainer
@ -35,7 +37,7 @@ def train_padatious():
padatious_intents_container.train() padatious_intents_container.train()
def process_handlers(): def load_all_skills():
for handler in intents_handlers_adapt: for handler in intents_handlers_adapt:
function_handler = intents_handlers_adapt.get(handler) function_handler = intents_handlers_adapt.get(handler)
intent_builder = getattr(function_handler[0], "_data", [])[0] intent_builder = getattr(function_handler[0], "_data", [])[0]
@ -87,8 +89,10 @@ def recognise(sentence):
# print(best_intent) # DEBUG # print(best_intent) # DEBUG
# TODO: add data for adapt
handle(best_intent['intent_type'], data={'utterance': sentence}) handle(best_intent['intent_type'], data={'utterance': sentence})
return best_intent
except StopIteration as e: except StopIteration as e:
pass pass
# print("No match... (Adapt)") # print("No match... (Adapt)")
@ -107,6 +111,9 @@ def recognise(sentence):
data['utterance'] = result.sent data['utterance'] = result.sent
data.update(result.matches) # adding the matches from padatious to the data data.update(result.matches) # adding the matches from padatious to the data
handle(result.name, data) handle(result.name, data)
return json.dumps(str(result))
else: else:
pass pass
# print("No match... (Padatious)") # print("No match... (Padatious)")