Adityadn commited on
Commit
e60bc15
·
verified ·
1 Parent(s): 568f753

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -27
app.py CHANGED
@@ -1,12 +1,14 @@
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 = {
@@ -31,45 +33,39 @@ def identify_audio(file):
31
  # Process audio in 10-second chunks
32
  for i in range(0, duration_ms // 1000, 10):
33
  res = acr.recognize_by_file(file_path, i, 10)
34
- results.append(f"**Time {i}s:** {res.strip()}")
35
 
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}\n\n" \
51
- f"**Artists**: {artists}\n\n" \
52
- f"**Album**: {album}\n\n" \
53
- f"**Release Date**: {release_date}\n"
54
- else:
55
- full_result_text = "No music metadata found in the result."
56
-
57
  # Recognize using file buffer
58
  with open(file_path, 'rb') as f:
59
  buf = f.read()
60
  buffer_result = acr.recognize_by_filebuffer(buf, 0)
61
 
62
- return {
63
- "Partial Results": "\n".join(results),
64
- "Full Result": full_result_text,
65
- "Buffer Result": buffer_result
66
- }
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # Create Gradio interface
69
  iface = gr.Interface(
70
  fn=identify_audio,
71
  inputs=gr.File(label="Upload Audio File"),
72
- outputs=gr.Markdown(label="Audio Metadata"), # Change output to Markdown
73
  title="Audio Search by File",
74
  description="Upload an audio file to identify it using ACRCloud."
75
  )
 
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 = {
 
33
  # Process audio in 10-second chunks
34
  for i in range(0, duration_ms // 1000, 10):
35
  res = acr.recognize_by_file(file_path, i, 10)
36
+ results.append(f"**Time {i}s**: {res.strip()}")
37
 
38
  # Full recognition result
39
  full_result = acr.recognize_by_file(file_path, 0)
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Recognize using file buffer
42
  with open(file_path, 'rb') as f:
43
  buf = f.read()
44
  buffer_result = acr.recognize_by_filebuffer(buf, 0)
45
 
46
+ # Convert results into Markdown format
47
+ result_md = f"""
48
+ ### Partial Results:
49
+ {"\n".join(results)}
50
+
51
+ ### Full Result:
52
+ ```json
53
+ {json.dumps(json.loads(full_result), indent=4)}
54
+ ```
55
+
56
+ ### Buffer Result:
57
+ ```json
58
+ {json.dumps(json.loads(buffer_result), indent=4)}
59
+ ```
60
+ """
61
+
62
+ return gr.Markdown(result_md)
63
 
64
  # Create Gradio interface
65
  iface = gr.Interface(
66
  fn=identify_audio,
67
  inputs=gr.File(label="Upload Audio File"),
68
+ outputs=gr.Markdown(label="Audio Metadata"),
69
  title="Audio Search by File",
70
  description="Upload an audio file to identify it using ACRCloud."
71
  )