siddqamar commited on
Commit
d09d492
·
verified ·
1 Parent(s): de4c5c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -15,20 +15,28 @@ theme = gr.themes.Base(
15
  neutral_hue="gray",
16
  )
17
 
18
- def validate_file(file):
 
 
 
 
19
  # Check file size (25 MB limit)
20
- file_size_mb = os.path.getsize(file) / (1024 * 1024)
21
  if file_size_mb > 25:
22
  return False, f"File size is {file_size_mb:.2f} MB. Please upload a file smaller than 25 MB."
23
 
24
  # Check file extension
25
- file_extension = os.path.splitext(file)[1].lower()
26
  if file_extension not in ['.mp3', '.wav']:
27
  return False, "Only .mp3 and .wav formats are supported."
28
 
29
  return True, "File is valid."
30
 
31
  def transcribe_audio(audio_file):
 
 
 
 
32
  # Validate the file first
33
  is_valid, message = validate_file(audio_file)
34
  if not is_valid:
@@ -48,6 +56,7 @@ def transcribe_audio(audio_file):
48
  transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
49
 
50
  return transcription
 
51
  except Exception as e:
52
  return f"An error occurred during transcription: {str(e)}"
53
 
@@ -58,7 +67,8 @@ with gr.Blocks(theme=theme) as demo:
58
 
59
  with gr.Row():
60
  with gr.Column():
61
- audio_input = gr.Audio(type="filepath", label="Upload Audio File")
 
62
  submit_btn = gr.Button("Transcribe", variant="primary")
63
 
64
  with gr.Column():
@@ -72,4 +82,5 @@ with gr.Blocks(theme=theme) as demo:
72
  gr.Markdown("- Uses the Whisper base model which works best with clear audio")
73
 
74
  # Launch the app
75
- demo.launch()
 
 
15
  neutral_hue="gray",
16
  )
17
 
18
+ def validate_file(file_path):
19
+ # Check if file exists
20
+ if not file_path or not os.path.exists(file_path):
21
+ return False, "No file uploaded or file not found."
22
+
23
  # Check file size (25 MB limit)
24
+ file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
25
  if file_size_mb > 25:
26
  return False, f"File size is {file_size_mb:.2f} MB. Please upload a file smaller than 25 MB."
27
 
28
  # Check file extension
29
+ file_extension = os.path.splitext(file_path)[1].lower()
30
  if file_extension not in ['.mp3', '.wav']:
31
  return False, "Only .mp3 and .wav formats are supported."
32
 
33
  return True, "File is valid."
34
 
35
  def transcribe_audio(audio_file):
36
+ # Check if audio_file is None
37
+ if audio_file is None:
38
+ return "Please upload an audio file."
39
+
40
  # Validate the file first
41
  is_valid, message = validate_file(audio_file)
42
  if not is_valid:
 
56
  transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
57
 
58
  return transcription
59
+
60
  except Exception as e:
61
  return f"An error occurred during transcription: {str(e)}"
62
 
 
67
 
68
  with gr.Row():
69
  with gr.Column():
70
+ # Fixed: Use sources parameter instead of type
71
+ audio_input = gr.Audio(sources=["upload"], label="Upload Audio File")
72
  submit_btn = gr.Button("Transcribe", variant="primary")
73
 
74
  with gr.Column():
 
82
  gr.Markdown("- Uses the Whisper base model which works best with clear audio")
83
 
84
  # Launch the app
85
+ if __name__ == "__main__":
86
+ demo.launch()