PerryCheng614 commited on
Commit
ff9e518
1 Parent(s): 7bd2b9b

update delete audio file logic

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -9,6 +9,9 @@ def process_audio(audio_path, prompt=""):
9
  """
10
  Send audio file to FastAPI backend for processing
11
  """
 
 
 
12
  try:
13
  # Prepare the file for upload
14
  files = {
@@ -24,10 +27,31 @@ def process_audio(audio_path, prompt=""):
24
  return response.json()['response']
25
  except Exception as e:
26
  return f"Error processing audio: {str(e)}"
27
- finally:
28
- # Clean up the temporary file if it exists
29
- if audio_path and os.path.exists(audio_path):
30
- os.remove(audio_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Create Gradio interface
33
  demo = gr.Interface(
@@ -41,7 +65,7 @@ demo = gr.Interface(
41
  gr.Textbox(
42
  placeholder="Enter prompt (optional)",
43
  label="Prompt",
44
- value="transcribe this audio in English and return me the transcription:"
45
  )
46
  ],
47
  outputs=gr.Textbox(label="Response"),
 
9
  """
10
  Send audio file to FastAPI backend for processing
11
  """
12
+ # Clear any previous audio files before processing new one
13
+ clear_previous_audio_files(audio_path)
14
+
15
  try:
16
  # Prepare the file for upload
17
  files = {
 
27
  return response.json()['response']
28
  except Exception as e:
29
  return f"Error processing audio: {str(e)}"
30
+
31
+ def clear_previous_audio_files(current_file):
32
+ """
33
+ Clear previous audio files in the same directory as the current file,
34
+ except for the current file and example files
35
+ """
36
+ if not current_file:
37
+ return
38
+
39
+ directory = os.path.dirname(current_file)
40
+ if not directory:
41
+ directory = "."
42
+
43
+ for file in os.listdir(directory):
44
+ file_path = os.path.join(directory, file)
45
+ # Skip if it's the current file, example files, or not a file
46
+ if (file_path == current_file or
47
+ 'example' in file_path or
48
+ not os.path.isfile(file_path) or
49
+ not file.endswith(('.wav', '.mp3'))):
50
+ continue
51
+ try:
52
+ os.remove(file_path)
53
+ except:
54
+ pass # Silently ignore deletion errors
55
 
56
  # Create Gradio interface
57
  demo = gr.Interface(
 
65
  gr.Textbox(
66
  placeholder="Enter prompt (optional)",
67
  label="Prompt",
68
+ value="Summarize this audio in English."
69
  )
70
  ],
71
  outputs=gr.Textbox(label="Response"),