File size: 2,838 Bytes
da3e61e
 
e22d140
e60bc15
 
 
da3e61e
 
 
 
e60bc15
e22d140
c22cb5d
 
 
 
 
 
 
 
e22d140
 
da3e61e
 
b90cfad
 
da3e61e
e22d140
 
 
 
 
 
 
 
e60bc15
e22d140
 
 
 
 
 
 
 
da3e61e
d8ff09c
 
 
 
 
 
 
 
e60bc15
d8ff09c
 
 
 
 
 
 
 
 
 
 
 
 
 
e60bc15
 
da3e61e
 
 
 
 
e60bc15
da3e61e
 
 
 
e22d140
da3e61e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 = []

    # 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)

    # Convert JSON results to dictionary
    full_result_dict = json.loads(full_result)
    buffer_result_dict = json.loads(buffer_result)

    # Format the results as markdown
    result_md = f"""
    ### **Partial Results**:
    {'\n'.join(results)}

    ### **Full Result**:
    - **Track**: {full_result_dict['metadata']['music'][0]['title']}
    - **Artist**: {full_result_dict['metadata']['music'][0]['artists'][0]['name']}
    - **Album**: {full_result_dict['metadata']['music'][0]['album']['name']}
    - **Release Date**: {full_result_dict['metadata']['music'][0]['release_date']}
    - **Score**: {full_result_dict['metadata']['music'][0]['score']}%
    
    ### **Buffer Result**:
    - **Track**: {buffer_result_dict['metadata']['music'][0]['title']}
    - **Artist**: {buffer_result_dict['metadata']['music'][0]['artists'][0]['name']}
    - **Album**: {buffer_result_dict['metadata']['music'][0]['album']['name']}
    - **Release Date**: {buffer_result_dict['metadata']['music'][0]['release_date']}
    - **Score**: {buffer_result_dict['metadata']['music'][0]['score']}%
    """

    return gr.Markdown(result_md)

# Create Gradio interface
iface = gr.Interface(
    fn=identify_audio,
    inputs=gr.File(label="Upload Audio File"),
    outputs=gr.Markdown(label="Audio Metadata"),
    title="Audio Search by File",
    description="Upload an audio file to identify it using ACRCloud."
)

# Launch the Gradio interface
iface.launch()