KingNish commited on
Commit
1e8d252
1 Parent(s): 3b1b278

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -4
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  from transformers import pipeline
5
  import tempfile
6
  import os
 
7
 
8
  MODEL_NAME = "ylacombe/whisper-large-v3-turbo"
9
  BATCH_SIZE = 8
@@ -18,14 +19,30 @@ pipe = pipeline(
18
 
19
  @spaces.GPU
20
  def transcribe(inputs, previous_transcription):
21
- previous_transcription += pipe(inputs[1], batch_size=BATCH_SIZE, generate_kwargs={"task": "transcribe"}, return_timestamps=True)["text"]
22
- return previous_transcription
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  with gr.Blocks() as demo:
25
  with gr.Column():
26
  input_audio_microphone = gr.Audio(streaming=True)
27
  output = gr.Textbox(label="Transcription", value="")
28
-
29
- input_audio_microphone.stream(transcribe, [input_audio_microphone, output], [output], time_limit=45, stream_every=3, concurrency_limit=None)
30
 
31
  demo.queue().launch()
 
4
  from transformers import pipeline
5
  import tempfile
6
  import os
7
+ import uuid
8
 
9
  MODEL_NAME = "ylacombe/whisper-large-v3-turbo"
10
  BATCH_SIZE = 8
 
19
 
20
  @spaces.GPU
21
  def transcribe(inputs, previous_transcription):
22
+ try:
23
+ # Generate a unique filename using UUID
24
+ filename = f"{uuid.uuid4().hex}.wav"
25
+ filepath = os.path.join(tempfile.gettempdir(), filename)
26
+
27
+ # Save the audio data to the temporary file
28
+ with open(filepath, "wb") as f:
29
+ f.write(inputs[1])
30
+
31
+ previous_transcription += pipe(filepath, batch_size=BATCH_SIZE, generate_kwargs={"task": "transcribe"}, return_timestamps=True)["text"]
32
+
33
+ # Remove the temporary file after transcription
34
+ os.remove(filepath)
35
+
36
+ return previous_transcription
37
+ except Exception as e:
38
+ print(f"Error during transcription: {e}")
39
+ return previous_transcription # Return the current transcription if an error occurs
40
 
41
  with gr.Blocks() as demo:
42
  with gr.Column():
43
  input_audio_microphone = gr.Audio(streaming=True)
44
  output = gr.Textbox(label="Transcription", value="")
45
+
46
+ input_audio_microphone.stream(transcribe, [input_audio_microphone, output], [output], time_limit=45, stream_every=2, concurrency_limit=None)
47
 
48
  demo.queue().launch()