File size: 4,578 Bytes
14dc68f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from skills.skill import Skill
import openai
import requests
import os
import json
from urllib.parse import urlparse

class PlayMusic(Skill):
    name = 'play_music'
    description = "A skill that uses the Deezer API to search for music and play it with an html player."
    api_keys_required = ['openai']

    def __init__(self, api_keys, main_loop_function):
        super().__init__(api_keys, main_loop_function)
    
    def download_and_save_image(self, url, folder_path="public/static/images"):
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        
        response = requests.get(url)
        
        # Extract file name from URL
        parsed_url = urlparse(url)
        file_name = os.path.basename(parsed_url.path)
        
        # Save image to folder
        file_path = os.path.join(folder_path, file_name)
        with open(file_path, 'wb') as f:
            f.write(response.content)
        
        return file_path

    def generate_prompt(self, objective):
        prompt_generation_messages = [
            {"role": "system", "content": "You are a helpful assistant specialized in generating a query for Deezer's music API which is a query and search type. Search types are artist, album name, genre, and track name. Both query and search type should have single quotes, and separated by a comma"},
            {"role": "user", "content": "Play some Eminem"},
            {"role": "assistant", "content": "'Eminem','artist'"},
            {"role": "user", "content": "Play tropical house."},
            {"role": "assistant", "content": "'tropical house','genre'"},
            {"role": "user", "content": "Play 99 problems"},
            {"role": "assistant", "content": "'99 problems','track'"},
            {"role": "user", "content": "Play abbey road"},
            {"role": "assistant", "content": "'abbey road','album'"},
            {"role": "user", "content": objective}
        ]
        
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=prompt_generation_messages
        )
        
        return response.choices[0].message['content'].strip()

  
    def deezer_search(self, query, search_type):
        base_url = "https://api.deezer.com/search"
        
        params = {"q": f"{search_type}:'{query}'"}
        
        response = requests.get(base_url, params=params)
        
        if response.status_code != 200:
            print("Failed to get data:", response.status_code)
            return None
        
        results = json.loads(response.text)
        
        if not results['data']:
            print("No results found")
            return None
        
        first_result = results['data'][0]
        
        if search_type == "artist":
            artist_id = first_result['artist']['id']
            return f'<iframe title="deezer-widget" src="https://widget.deezer.com/widget/dark/artist/{artist_id}/top_tracks" width="100%" height="300" frameborder="0" allowtransparency="true" allow="encrypted-media; clipboard-write"></iframe>'
        elif search_type == "album":
            album_id = first_result['album']['id']
            return f'<iframe title="deezer-widget" src="https://widget.deezer.com/widget/dark/album/{album_id}" width="100%" height="300" frameborder="0" allowtransparency="true" allow="encrypted-media; clipboard-write"></iframe>'
        elif search_type == "track":
            track_id = first_result['id']
            return f'<iframe title="deezer-widget" src="https://widget.deezer.com/widget/dark/track/{track_id}" width="100%" height="300" frameborder="0" allowtransparency="true" allow="encrypted-media; clipboard-write"></iframe>'
        elif search_type == "genre":
            genre_id = first_result['id']
            return f'<iframe title="deezer-widget" src="https://widget.deezer.com/widget/dark/genre/{genre_id}" width="100%" height="300" frameborder="0" allowtransparency="true" allow="encrypted-media; clipboard-write"></iframe>'
        else:
            print("Unsupported search type")
            return None
    
    def execute(self, params, dependent_task_outputs, objective):
        if not self.valid:
            return
        print(f"Objective: {objective}")

        generated_prompt = self.generate_prompt(objective)
        print(f"generated_prompt: {generated_prompt}")

        query, search_type = [x.strip().strip("'") for x in generated_prompt.split(",")]

        response = self.deezer_search(query, search_type)  # Modified this line

        return response