Adityadn commited on
Commit
aabd712
·
verified ·
1 Parent(s): 985fbba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -68
app.py CHANGED
@@ -1,72 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- import os
3
- from acrcloud.recognizer import ACRCloudRecognizer
4
- import tempfile
5
- import shutil
6
- import json
7
-
8
- # Retrieve ACRCloud credentials from environment variables
9
- acr_access_key = os.environ.get('ACR_ACCESS_KEY')
10
- acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
11
- acr_host = 'identify-ap-southeast-1.acrcloud.com' # os.environ.get('ACR_HOST', 'eu-west-1.api.acrcloud.com')
12
-
13
- # ACRCloud recognizer configuration
14
- config = {
15
- 'host': acr_host,
16
- 'access_key': acr_access_key,
17
- 'access_secret': acr_access_secret,
18
- 'timeout': 10 # seconds
19
- }
20
-
21
- # Initialize ACRCloud recognizer
22
- acr = ACRCloudRecognizer(config)
23
-
24
- def identify_audio(file):
25
- # Gradio provides a file object, and file.name contains the path
26
- file_path = file.name # Gradio file object already provides a file path
27
-
28
- # Get the duration of the audio file in milliseconds
29
- duration_ms = int(acr.get_duration_ms_by_file(file_path))
30
-
31
- results = []
32
-
33
- # Full recognition result
34
- full_result = acr.recognize_by_file(file_path, 0)
35
- full_result_dict = json.loads(full_result)
36
 
37
- music = full_result_dict['metadata']['music'][0]
38
-
39
- # Spotify link
40
- spotify_track_id = music['external_metadata']['spotify']['track']['id']
41
- spotify_link = f"https://open.spotify.com/track/{spotify_track_id}"
42
-
43
- # Deezer link
44
- deezer_track_id = music['external_metadata']['deezer']['track']['id']
45
- deezer_link = f"https://www.deezer.com/track/{deezer_track_id}"
46
-
47
- # Final markdown result
48
- result_md = f"""
49
- ### **Full Result**:
50
- - **Track**: {music['title']}
51
- - **Artist**: {music['artists'][0]['name']}
52
- - **Album**: {music['album']['name']}
53
- - **Release Date**: {music['release_date']}
54
- - **Score**: {music['score']}%
55
- - **Download Link**:
56
- - [Listen on Spotify]({spotify_link})
57
- - [Listen on Deezer]({deezer_link})
58
- """
59
-
60
- return gr.Markdown(result_md)
61
-
62
- # Create Gradio interface
63
- iface = gr.Interface(
64
- fn=identify_audio,
65
- inputs=gr.File(label="Upload Audio or Video File"),
66
- outputs=gr.Markdown(label="Audio Metadata"),
67
- title="Audio Search by File (Support Audio or Video File)",
68
- description="Upload an audio or video file to identify it using ACRCloud."
69
  )
70
 
71
- # Launch the Gradio interface
72
- iface.launch()
 
 
1
+ # import gradio as gr
2
+ # import os
3
+ # from acrcloud.recognizer import ACRCloudRecognizer
4
+ # import tempfile
5
+ # import shutil
6
+ # import json
7
+
8
+ # # Retrieve ACRCloud credentials from environment variables
9
+ # acr_access_key = os.environ.get('ACR_ACCESS_KEY')
10
+ # acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
11
+ # acr_host = 'identify-ap-southeast-1.acrcloud.com' # os.environ.get('ACR_HOST', 'eu-west-1.api.acrcloud.com')
12
+
13
+ # # ACRCloud recognizer configuration
14
+ # config = {
15
+ # 'host': acr_host,
16
+ # 'access_key': acr_access_key,
17
+ # 'access_secret': acr_access_secret,
18
+ # 'timeout': 10 # seconds
19
+ # }
20
+
21
+ # # Initialize ACRCloud recognizer
22
+ # acr = ACRCloudRecognizer(config)
23
+
24
+ # def identify_audio(file):
25
+ # # Gradio provides a file object, and file.name contains the path
26
+ # file_path = file.name # Gradio file object already provides a file path
27
+
28
+ # # Get the duration of the audio file in milliseconds
29
+ # duration_ms = int(acr.get_duration_ms_by_file(file_path))
30
+
31
+ # results = []
32
+
33
+ # # Full recognition result
34
+ # full_result = acr.recognize_by_file(file_path, 0)
35
+ # full_result_dict = json.loads(full_result)
36
+
37
+ # music = full_result_dict['metadata']['music'][0]
38
+
39
+ # # Spotify link
40
+ # spotify_track_id = music['external_metadata']['spotify']['track']['id']
41
+ # spotify_link = f"https://open.spotify.com/track/{spotify_track_id}"
42
+
43
+ # # Deezer link
44
+ # deezer_track_id = music['external_metadata']['deezer']['track']['id']
45
+ # deezer_link = f"https://www.deezer.com/track/{deezer_track_id}"
46
+
47
+ # # Final markdown result
48
+ # result_md = f"""
49
+ # ### **Full Result**:
50
+ # - **Track**: {music['title']}
51
+ # - **Artist**: {music['artists'][0]['name']}
52
+ # - **Album**: {music['album']['name']}
53
+ # - **Release Date**: {music['release_date']}
54
+ # - **Score**: {music['score']}%
55
+ # - **Download Link**:
56
+ # - [Listen on Spotify]({spotify_link})
57
+ # - [Listen on Deezer]({deezer_link})
58
+ # """
59
+
60
+ # return gr.Markdown(result_md)
61
+
62
+ # # Create Gradio interface
63
+ # iface = gr.Interface(
64
+ # fn=identify_audio,
65
+ # inputs=gr.File(label="Upload Audio or Video File"),
66
+ # outputs=gr.Markdown(label="Audio Metadata"),
67
+ # title="Audio Search by File (Support Audio or Video File)",
68
+ # description="Upload an audio or video file to identify it using ACRCloud."
69
+ # )
70
+
71
+ # # Launch the Gradio interface
72
+ # iface.launch()
73
+
74
+ import requests
75
  import gradio as gr
76
+
77
+ def recognize_audio(url, file):
78
+ api_url = os.environ.get('API_URL')
79
+ api_token = os.environ.get('API_TOKEN')
80
+ data = os.environ.get('DATA')
81
+
82
+ if url:
83
+ data['url'] = url
84
+ elif file:
85
+ files = {'file': (file.name, open(file.name, 'rb'))}
86
+ result = requests.post(api_url, data=data, files=files)
87
+ return result.text
88
+ else:
89
+ return "Silakan masukkan URL atau unggah file audio."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ result = requests.post(api_url, data=data)
92
+ return result.text
93
+
94
+ interface = gr.Interface(
95
+ fn=recognize_audio,
96
+ inputs=[
97
+ gr.Textbox(label="Masukkan URL Audio", placeholder="https://example.com/audio.mp3"),
98
+ gr.File(label="Unggah File Audio")
99
+ ],
100
+ outputs=gr.Textbox(label="Hasil Pengenalan"),
101
+ title="Audio Recognition",
102
+ description="Upload audio/video or enter a URL to identify."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  )
104
 
105
+ if __name__ == "__main__":
106
+ interface.launch()
107
+