Spaces:
Running
Running
# import gradio as gr | |
# import os | |
# from acrcloud.recognizer import ACRCloudRecognizer | |
# import tempfile | |
# import shutil | |
# import json | |
# # Retrieve ACRCloud credentials from environment variables | |
# acr_access_key = os.environ.get('ACR_ACCESS_KEY') | |
# acr_access_secret = os.environ.get('ACR_ACCESS_SECRET') | |
# acr_host = 'identify-ap-southeast-1.acrcloud.com' # os.environ.get('ACR_HOST', 'eu-west-1.api.acrcloud.com') | |
# # ACRCloud recognizer configuration | |
# config = { | |
# 'host': acr_host, | |
# 'access_key': acr_access_key, | |
# 'access_secret': acr_access_secret, | |
# 'timeout': 10 # seconds | |
# } | |
# # Initialize ACRCloud recognizer | |
# acr = ACRCloudRecognizer(config) | |
# def identify_audio(file): | |
# # Gradio provides a file object, and file.name contains the path | |
# file_path = file.name # Gradio file object already provides a file path | |
# # Get the duration of the audio file in milliseconds | |
# duration_ms = int(acr.get_duration_ms_by_file(file_path)) | |
# results = [] | |
# # Full recognition result | |
# full_result = acr.recognize_by_file(file_path, 0) | |
# full_result_dict = json.loads(full_result) | |
# music = full_result_dict['metadata']['music'][0] | |
# # Spotify link | |
# spotify_track_id = music['external_metadata']['spotify']['track']['id'] | |
# spotify_link = f"https://open.spotify.com/track/{spotify_track_id}" | |
# # Deezer link | |
# deezer_track_id = music['external_metadata']['deezer']['track']['id'] | |
# deezer_link = f"https://www.deezer.com/track/{deezer_track_id}" | |
# # Final markdown result | |
# result_md = f""" | |
# ### **Full Result**: | |
# - **Track**: {music['title']} | |
# - **Artist**: {music['artists'][0]['name']} | |
# - **Album**: {music['album']['name']} | |
# - **Release Date**: {music['release_date']} | |
# - **Score**: {music['score']}% | |
# - **Download Link**: | |
# - [Listen on Spotify]({spotify_link}) | |
# - [Listen on Deezer]({deezer_link}) | |
# """ | |
# return gr.Markdown(result_md) | |
# # Create Gradio interface | |
# iface = gr.Interface( | |
# fn=identify_audio, | |
# inputs=gr.File(label="Upload Audio or Video File"), | |
# outputs=gr.Markdown(label="Audio Metadata"), | |
# title="Audio Search by File (Support Audio or Video File)", | |
# description="Upload an audio or video file to identify it using ACRCloud." | |
# ) | |
# # Launch the Gradio interface | |
# iface.launch() | |
import requests | |
import gradio as gr | |
import os | |
def recognize_audio(url, file): | |
api_url = os.environ.get('API_URL') | |
api_token = os.environ.get('API_TOKEN') | |
data = os.environ.get('DATA') | |
if url: | |
data['url'] = url | |
result = requests.post(api_url, data=data) | |
return result.text | |
elif file: | |
with open(file.name, 'rb') as f: | |
files = {'file': (file.name, f)} | |
result = requests.post(api_url, data=data, files=files) | |
return result.text | |
else: | |
return "Please enter a URL or upload an audio file." | |
interface = gr.Interface( | |
fn=recognize_audio, | |
inputs=[ | |
gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"), | |
gr.File(label="Upload Audio File") | |
], | |
outputs=gr.Textbox(label="Recognition Result"), | |
title="Audio Recognition", | |
description="Upload an audio/video file or enter a URL to identify it." | |
) | |
if __name__ == "__main__": | |
interface.launch() | |