Adityadn commited on
Commit
e161859
·
verified ·
1 Parent(s): ae714b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -1,6 +1,8 @@
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')
@@ -20,9 +22,10 @@ acr = ACRCloudRecognizer(config)
20
 
21
  def identify_audio(file):
22
  # Create a temporary file path
23
- file_path = "temp_audio_file" + os.path.splitext(file.name)[-1] # Preserve the file extension
 
24
 
25
- # Open the uploaded file using the file object provided by Gradio
26
  with open(file_path, 'wb') as f:
27
  f.write(file.read()) # Write the content of the uploaded file to disk
28
 
@@ -44,6 +47,9 @@ def identify_audio(file):
44
  buf = f.read()
45
  buffer_result = acr.recognize_by_filebuffer(buf, 0)
46
 
 
 
 
47
  return {
48
  "Partial Results": results,
49
  "Full Result": full_result,
 
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')
 
22
 
23
  def identify_audio(file):
24
  # Create a temporary file path
25
+ temp_dir = tempfile.mkdtemp()
26
+ file_path = os.path.join(temp_dir, file.name)
27
 
28
+ # Open the uploaded file and save it to the temporary directory
29
  with open(file_path, 'wb') as f:
30
  f.write(file.read()) # Write the content of the uploaded file to disk
31
 
 
47
  buf = f.read()
48
  buffer_result = acr.recognize_by_filebuffer(buf, 0)
49
 
50
+ # Clean up the temporary file
51
+ shutil.rmtree(temp_dir)
52
+
53
  return {
54
  "Partial Results": results,
55
  "Full Result": full_result,