import gradio as gr import os from acrcloud.recognizer import ACRCloudRecognizer import tempfile import shutil # 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 = 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): # Create a temporary file path temp_dir = tempfile.mkdtemp() file_path = os.path.join(temp_dir, file.name) # Open the uploaded file and save it to the temporary directory with open(file_path, 'wb') as f: f.write(file.read()) # Write the content of the uploaded file to disk # 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) # Clean up the temporary file shutil.rmtree(temp_dir) 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()