Spaces:
Running
Running
import gradio as gr | |
import os | |
from acrcloud.recognizer import ACRCloudRecognizer | |
# 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' | |
# 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): | |
# Save the uploaded file temporarily | |
file_path = "temp_audio_file" + os.path.splitext(file.name)[-1] # Preserve the file extension | |
# Write the uploaded file to disk | |
with open(file_path, 'wb') as f: | |
f.write(file.file.read()) # Write the content of the uploaded file | |
# Get the duration of the audio file in milliseconds | |
duration_ms = int(acr.get_duration_ms_by_file(file_path)) | |
results = [] | |
# Process audio in 10-second chunks | |
for i in range(0, duration_ms // 1000, 10): | |
res = acr.recognize_by_file(file_path, i, 10) | |
results.append(f"Time {i}s: {res.strip()}") | |
# Full recognition result | |
full_result = acr.recognize_by_file(file_path, 0) | |
# Recognize using file buffer | |
with open(file_path, 'rb') as f: | |
buf = f.read() | |
buffer_result = acr.recognize_by_filebuffer(buf, 0) | |
return { | |
"Partial Results": results, | |
"Full Result": full_result, | |
"Buffer Result": buffer_result | |
} | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=identify_audio, | |
inputs=gr.File(label="Upload Audio File"), | |
outputs=gr.JSON(label="Audio Metadata"), | |
title="Audio Search by File", | |
description="Upload an audio file to identify it using ACRCloud." | |
) | |
# Launch the Gradio interface | |
iface.launch() | |