PierreHanna's picture
Update app.py
dd3cb6b
raw
history blame
No virus
4.32 kB
from simbals_apis_public_clients.clients.services import SimbalsAPIClient
import time
import json
import os
import gradio as gr
SIMBALS_GLOBAL_DB = 1
SIMBALS_MAIN_DATABASE = 2
def process(input_path):
# setup the client
client = SimbalsAPIClient(os.environ['TOKEN'], debug=True)
parameters = client.prepare_request("31415", audio_file=input_path)
# add audio features service with an available database
#parameters = client.add_audio_features(parameters, SIMBALS_MAIN_DATABASE)
parameters = client.add_tags(parameters, SIMBALS_MAIN_DATABASE)
# 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)
break
time.sleep(1)
res = results[1]["response"]["tagging"]["2"]
dict_moods = {}
for m in res['moods']:
dict_moods[m['name']] = m['probability']
dict_desc={}
for d in res['music_descriptors']:
dict_desc[d['name']] = d['value']
genres={}
for d in res['genres']:
genres[d['name']] = d['probability']
themes = {}
for d in res['themes']:
themes[d['name']]= d['probability']
instruments = {}
for d in res['instruments']:
instruments[d['name']] = d['probability']
timbres = []
for d in res['timbres']:
timbres.append((d['name'],d['probability']))
print(timbres)
vocalgender = [res['vocal_gender'][0]['name'], res['vocal_gender'][0]['probability']]
print(vocalgender)
audioquality = [res['audio_quality'][0]['name'], res['audio_quality'][0]['probability']]
print(audioquality)
return dict_moods, genres, instruments, themes,
str(dict_desc['Electric/Acoustic']),
str(dict_desc['Danceability']),
str(dict_desc['Arousal']),
str(dict_desc['Vocal/Instrumental']),
str(dict_desc['Studio/Live']),
str(dict_desc['Music/Speech']),
str(dict_desc['Valence']),
str(dict_desc['Melodic']),
str(dict_desc['Articulation']),
str(dict_desc['RhythmicStability']),
str(dict_desc['Dissonance']),
str(dict_desc['Vocal/Instrumental']),
('BPM', 47), ('Binary', 0.794377), ('Key', 10), ('Mode', 1), ('TexturalStability', 0.312802)]
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
with gr.Row():
audio_input = gr.Audio(type="filepath", label='Audio Input')
with gr.Row():
analyze_btn = gr.Button('Analyze File')
with gr.Row():
with gr.Column():
gr.HTML("<h3>Moods</h3>")
dict_moods=gr.Label(label="Moods", show_label=False)
gr.HTML("<h3>Themes</h3>")
themes=gr.Label(label="Themes", show_label=False)
with gr.Column():
gr.HTML("<h3>Genres</h3>")
genres = gr.Label(label="Genres", show_label=False)
with gr.Column():
gr.HTML("<h3>Instruments</h3>")
instruments = gr.Label(label="Instruments", show_label=False)
with gr.Column():
gr.HTML("<h3> Descriptors</h3>")
#gr.HTML("<h5> Vocal/Instrumental</h5>")
vocalinstru = gr.Slider(label="Vocal/Instrumental", minimum=0, maximum=1.0)#, info="Information todo", show_label=False)
''''
with gr.Row():
with gr.Column():
gr.HTML("<h3>Themes</h3>")
themes=gr.Label(label="Themes", show_label=False)
'''
analyze_btn.click(process, inputs=[audio_input], outputs=[dict_moods, genres, instruments, themes, vocalinstru])
demo.launch(debug=False)