Adityadn commited on
Commit
e22d140
·
verified ·
1 Parent(s): 785ccc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -1,24 +1,51 @@
1
  import gradio as gr
2
  import os
3
- from acrcloud import ACRCloud
4
 
5
  # Retrieve ACRCloud credentials from environment variables
6
  acr_access_key = os.environ.get('ACR_ACCESS_KEY')
7
  acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
 
8
 
9
- # Initialize ACRCloud client
10
- acr = ACRCloud('eu-west-1.api.acrcloud.com', acr_access_key, acr_access_secret)
 
 
 
 
 
 
 
 
11
 
12
  def identify_audio(file):
13
  # Save the uploaded file temporarily
14
- file_path = "temp_audio_file.ogg"
15
  file.save(file_path)
16
 
17
- # Identify the audio using ACRCloud
18
- metadata = acr.identify(file_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Return the metadata as the output
21
- return metadata
 
 
 
22
 
23
  # Create Gradio interface
24
  iface = gr.Interface(
@@ -29,5 +56,5 @@ iface = gr.Interface(
29
  description="Upload an audio file to identify it using ACRCloud."
30
  )
31
 
32
- # Launch the interface
33
  iface.launch()
 
1
  import gradio as gr
2
  import os
3
+ from acrcloud.recognizer import ACRCloudRecognizer
4
 
5
  # Retrieve ACRCloud credentials from environment variables
6
  acr_access_key = os.environ.get('ACR_ACCESS_KEY')
7
  acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
8
+ acr_host = os.environ.get('ACR_HOST', 'eu-west-1.api.acrcloud.com')
9
 
10
+ # ACRCloud recognizer configuration
11
+ config = {
12
+ 'host': acr_host,
13
+ 'access_key': acr_access_key,
14
+ 'access_secret': acr_access_secret,
15
+ 'timeout': 10 # seconds
16
+ }
17
+
18
+ # Initialize ACRCloud recognizer
19
+ acr = ACRCloudRecognizer(config)
20
 
21
  def identify_audio(file):
22
  # Save the uploaded file temporarily
23
+ file_path = file.name
24
  file.save(file_path)
25
 
26
+ # Get the duration of the audio file in milliseconds
27
+ duration_ms = int(acr.get_duration_ms_by_file(file_path))
28
+
29
+ results = []
30
+
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
+ # Recognize using file buffer
40
+ with open(file_path, 'rb') as f:
41
+ buf = f.read()
42
+ buffer_result = acr.recognize_by_filebuffer(buf, 0)
43
 
44
+ return {
45
+ "Partial Results": results,
46
+ "Full Result": full_result,
47
+ "Buffer Result": buffer_result
48
+ }
49
 
50
  # Create Gradio interface
51
  iface = gr.Interface(
 
56
  description="Upload an audio file to identify it using ACRCloud."
57
  )
58
 
59
+ # Launch the Gradio interface
60
  iface.launch()