File size: 3,835 Bytes
aabd712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da3e61e
a98d34a
30b04a4
3e5c391
 
 
f9a0d06
d2237c6
 
 
 
3e5c391
 
 
 
 
 
f9a0d06
3e5c391
92f3382
 
f9a0d06
3e5c391
f9a0d06
aabd712
3e5c391
 
 
aabd712
3e5c391
aabd712
 
 
3e5c391
a98d34a
f9a0d06
aabd712
a98d34a
aabd712
92f3382
da3e61e
 
3e5c391
aabd712
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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
import json

# Function to recognize audio from URL or uploaded file
def recognize_audio(choice, url, file):
    api_url = os.getenv("API_URL", "https://api.audd.io/")
    params = {
        "return": "apple_music,spotify"
        "api_token": os.getenv("API_TOKEN", "your_api_token_here")
    }

    if choice == "URL":
        if not url:
            return "Please enter a valid URL."
        params['url'] = url
        response = requests.post(api_url, data=params)

    elif choice == "Upload File":
        if not file:
            return "Please upload a valid audio file."
        with open(file, "rb") as f:
            response = requests.post(api_url, data=params, files={'file': f})

    else:
        return "Please select a method (URL or Upload File)."

    return response.text

# Gradio Interface
interface = gr.Interface(
    fn=recognize_audio,
    inputs=[
        gr.Radio(["URL", "Upload File"], label="Select Input Method"),
        gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
        gr.File(label="Upload Audio File", type="filepath")  # Menggunakan filepath agar sesuai dengan Gradio
    ],
    outputs=gr.Textbox(label="Recognition Result"),
    title="Audio Recognition",
    description="Choose a method: Upload an audio file or enter a URL to identify the song."
)

# Run Gradio App
if __name__ == "__main__":
    interface.launch()