Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
3 |
from acrcloud.recognizer import ACRCloudRecognizer
|
4 |
-
import tempfile
|
5 |
-
import shutil
|
6 |
|
7 |
# Retrieve ACRCloud credentials from environment variables
|
8 |
acr_access_key = os.environ.get('ACR_ACCESS_KEY')
|
9 |
acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
|
10 |
-
acr_host = 'identify-ap-southeast-1.acrcloud.com'
|
11 |
|
12 |
# ACRCloud recognizer configuration
|
13 |
config = {
|
@@ -37,34 +36,37 @@ def identify_audio(file):
|
|
37 |
# Full recognition result
|
38 |
full_result = acr.recognize_by_file(file_path, 0)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Recognize using file buffer
|
41 |
with open(file_path, 'rb') as f:
|
42 |
buf = f.read()
|
43 |
buffer_result = acr.recognize_by_filebuffer(buf, 0)
|
44 |
|
45 |
-
# Parse and structure the results in a more readable format
|
46 |
-
partial_results = "\n".join(results)
|
47 |
-
full_result_parsed = f"Title: {full_result['metadata']['music'][0]['title']}\n" \
|
48 |
-
f"Artist: {full_result['metadata']['music'][0]['artists'][0]['name']}\n" \
|
49 |
-
f"Album: {full_result['metadata']['music'][0]['album']['name']}\n" \
|
50 |
-
f"Release Date: {full_result['metadata']['music'][0]['release_date']}\n" \
|
51 |
-
f"Duration: {full_result['metadata']['music'][0]['duration_ms'] // 1000}s"
|
52 |
-
|
53 |
-
buffer_result_parsed = f"Track ID: {buffer_result['metadata']['music'][0]['external_metadata']['spotify']['track']['id']}\n" \
|
54 |
-
f"Track Name: {buffer_result['metadata']['music'][0]['external_metadata']['spotify']['track']['name']}\n" \
|
55 |
-
f"Artist: {buffer_result['metadata']['music'][0]['external_metadata']['spotify']['track']['artists'][0]['name']}\n"
|
56 |
-
|
57 |
return {
|
58 |
-
"Partial Results":
|
59 |
-
"Full Result":
|
60 |
-
"Buffer Result":
|
61 |
}
|
62 |
|
63 |
# Create Gradio interface
|
64 |
iface = gr.Interface(
|
65 |
fn=identify_audio,
|
66 |
inputs=gr.File(label="Upload Audio File"),
|
67 |
-
outputs=
|
68 |
title="Audio Search by File",
|
69 |
description="Upload an audio file to identify it using ACRCloud."
|
70 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import json
|
4 |
from acrcloud.recognizer import ACRCloudRecognizer
|
|
|
|
|
5 |
|
6 |
# Retrieve ACRCloud credentials from environment variables
|
7 |
acr_access_key = os.environ.get('ACR_ACCESS_KEY')
|
8 |
acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
|
9 |
+
acr_host = 'identify-ap-southeast-1.acrcloud.com' # Default host, can be adjusted
|
10 |
|
11 |
# ACRCloud recognizer configuration
|
12 |
config = {
|
|
|
36 |
# Full recognition result
|
37 |
full_result = acr.recognize_by_file(file_path, 0)
|
38 |
|
39 |
+
# Parse full_result from string to JSON
|
40 |
+
full_result_parsed = json.loads(full_result)
|
41 |
+
|
42 |
+
# Extract desired details from parsed result
|
43 |
+
if 'metadata' in full_result_parsed and 'music' in full_result_parsed['metadata']:
|
44 |
+
music_data = full_result_parsed['metadata']['music'][0]
|
45 |
+
title = music_data.get('title', 'Unknown Title')
|
46 |
+
artists = ', '.join([artist['name'] for artist in music_data.get('artists', [])])
|
47 |
+
album = music_data.get('album', {}).get('name', 'Unknown Album')
|
48 |
+
release_date = music_data.get('release_date', 'Unknown Release Date')
|
49 |
+
|
50 |
+
full_result_text = f"Title: {title}\nArtists: {artists}\nAlbum: {album}\nRelease Date: {release_date}"
|
51 |
+
else:
|
52 |
+
full_result_text = "No music metadata found in the result."
|
53 |
+
|
54 |
# Recognize using file buffer
|
55 |
with open(file_path, 'rb') as f:
|
56 |
buf = f.read()
|
57 |
buffer_result = acr.recognize_by_filebuffer(buf, 0)
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
return {
|
60 |
+
"Partial Results": results,
|
61 |
+
"Full Result": full_result_text,
|
62 |
+
"Buffer Result": buffer_result
|
63 |
}
|
64 |
|
65 |
# Create Gradio interface
|
66 |
iface = gr.Interface(
|
67 |
fn=identify_audio,
|
68 |
inputs=gr.File(label="Upload Audio File"),
|
69 |
+
outputs=gr.JSON(label="Audio Metadata"),
|
70 |
title="Audio Search by File",
|
71 |
description="Upload an audio file to identify it using ACRCloud."
|
72 |
)
|