import ast import gradio as gr from ShazamAPI import Shazam async def detect(audio): mp3_file_content_to_recognize = open(audio, 'rb').read() shazam = Shazam( mp3_file_content_to_recognize ) recognize_generator = shazam.recognizeSong() results = '' while True: try: print(recognize_generator) results += str(next(recognize_generator)) data_tuple = ast.literal_eval(results) track_info = data_tuple[1] title = track_info['track']['title'] subtitle = track_info['track']['subtitle'] background_image = track_info['track']['images']['background'] url = track_info['track']['url'] return 'Title: ' + title + '\nArtist: ' + subtitle + '\nURL: ' + url, background_image except: return 'No results found.', None title = "Music finder using Shazam" description = "Find the name and other useful informations of your audio file's Music using this Space!" iface = gr.Interface(fn=detect, title=title, description=description, inputs=[gr.File(label="Upload Audio or Video")], outputs=[gr.Text(label="Info"), gr.Image(label="Cover")]) iface.launch()