File size: 5,184 Bytes
918d352
 
 
 
 
69eed8c
8e37fc1
 
 
918d352
 
 
 
e9ecdc7
 
 
69eed8c
 
 
 
 
 
8e37fc1
 
 
69eed8c
 
bab7cec
8e37fc1
 
 
 
 
 
 
 
 
0aec056
918d352
 
 
 
 
 
a08dc35
918d352
 
a08dc35
918d352
 
 
 
 
 
 
 
 
 
 
 
 
3393169
b4518cd
 
 
 
 
 
afd30f3
e9ecdc7
 
 
0aec056
6abacd1
2d5b51c
918d352
 
8e37fc1
918d352
 
afd30f3
918d352
 
 
8e37fc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bf36d3
001b9b0
8e37fc1
 
5236820
8e37fc1
 
5236820
8e37fc1
591f0f5
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from simbals_apis_public_clients.clients.services import SimbalsAPIClient
import time
import json
import os
import gradio as gr
import yt_dlp

YT_embed_html1 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'
YT_embed_html2 = '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'

SIMBALS_GLOBAL_DB = 1
SIMBALS_MAIN_DATABASE = 2

embed_html1 = '<iframe src="https://open.spotify.com/embed/track/'
embed_html2 = '" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>'

def download_audio_(link):
  with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': '%(title)s.mp3'}) as video:
    info_dict = video.extract_info(link, download = True)
    video_title = info_dict['title']
    video.download(link)    
    return video_title

def download_audio(id_video):
    id = id_video.split("?v=")[-1][:11]
    audio_file = download_audio_(id_video)
    audio_file = audio_file+'.mp3'
    embed_html_all = YT_embed_html1 + id + YT_embed_html2
    return audio_file, audio_file, embed_html_all

def process_url(input_path):
    audio_file, audio_file, embed_html_all = download_audio(input_path)
    return process(audio_file, embed_html_all)

def process_file(input_path):
    return process(input_path, '')

def process(input_path, embed_html_all):
    # setup the client
    client = SimbalsAPIClient(os.environ['TOKEN'], debug=True)
    # init the base parameters with your audio id and an audio url (or a local audio file)
    parameters = client.prepare_request("31415", audio_file=input_path)
    
    # add scanmatch service with full_matches option activated, against a chosen and available database
    #parameters = client.add_scanmatch_full_matches(parameters, SIMBALS_GLOBAL_DB)  
    # add scanmatch service with partial_matches option activated, against a chosen and available database
    #parameters = client.add_scanmatch_partial_matches(parameters, SIMBALS_GLOBAL_DB)
    parameters = client.add_scanmatch_partial_fast_matches(parameters, SIMBALS_GLOBAL_DB)   
    
    # launch the request and test for ok/code values
    ok, code, job_id = client.launch_request(parameters)
    # try to get the results with obtained job_id. If code is 4, job is not finished. In all other cases, job is finished
    for i in range(1000):
        results=client.get_results(job_id)
        if results[0] != 4:
            print(json.dumps(results[1], indent=1))
            #return json.dumps(results[1], indent=1)
            output = json.dumps(results[1], indent=1)
            try : 
                trackname = results[1]["response"]["scanmatch"]['1']['partial_fast_matches'][0]['track']['title']
                artistname = results[1]["response"]["scanmatch"]['1']['partial_fast_matches'][0]['artists'][0]['name']
                isrc = results[1]["response"]["scanmatch"]['1']['partial_fast_matches'][0]['track']['isrc']
                id_spotify = -1
                for x in results[1]["response"]["scanmatch"]['1']['partial_fast_matches'][0]['track']['other_ids'] :
                  if x['id_type_name'] == "spotify":
                    id_spotify = x['id']
                if id_spotify == -1:
                  id_spotify = "No Spotify ID found."
                output_text = trackname+" by "+artistname + " - ISRC : " + isrc #+ " - spotify ID : " + id_spotify

                output_html = embed_html1 + id_spotify + embed_html2
                
                return output_text, output_html, embed_html_all
            except (IndexError, TypeError):
                return "Not found. Please try again.","",""
        time.sleep(1)
    
'''
demo = gr.Interface(fn=process,
                    inputs=gr.Audio(type="filepath"),
                    outputs=[gr.outputs.Textbox(label="Identification"),"html"]
                    #examples=example_list,
                    #cache_examples=False
                    )
'''

with gr.Blocks() as demo:
    
    with gr.Row():

        with gr.Column():

            with gr.Row():
                #gr.HTML(embed_html)
                html = gr.HTML()
            
            with gr.Row():
                with gr.Column():
                    audio_url_input = gr.Textbox(placeholder='YouTube video URL', label='YouTube video URL')
                    analyze_url_btn = gr.Button('Search from URL')

            with gr.Row():
                with gr.Column():
                    audio_input_file = gr.Audio(type="filepath", label='Audio Input')
                    analyze_file_btn = gr.Button('Search from file')

            with gr.Row():
                with gr.Column():
                    id = gr.outputs.Textbox(label="Identification")        
                    id_html = gr.HTML()
                    
    analyze_url_btn.click(process_url, inputs=[audio_url_input], 
                      outputs=[id, id_html, html])

    analyze_file_btn.click(process_file, inputs=[audio_input_file], 
                      outputs=[id, id_html])
    
demo.queue(concurrency_count=2)
demo.launch(debug=True)