jpdiazpardo commited on
Commit
e559d03
1 Parent(s): c2110e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -11
app.py CHANGED
@@ -1,15 +1,70 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
6
 
7
- pipeline = pipeline("automatic-speech-recognition",
8
- model="openai/whisper-medium",
9
- chunk_length_s=30,
10
- batch_size=8,
11
- device = device
12
- )
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- iface = gr.Interface.from_pipeline(pipeline, inputs= gr.inputs.Audio(source="upload", type="filepath"), title="Audio Transcription App")
15
- iface.launch(debug=True)
 
 
 
1
  import torch
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+
5
+ MODEL_NAME = "openai/whisper-large"
6
+ BATCH_SIZE = 8
7
+
8
+ device = 0 if torch.cuda.is_available() else "cpu"
9
+
10
+ pipe = pipeline(
11
+ task="automatic-speech-recognition",
12
+ model=MODEL_NAME,
13
+ chunk_length_s=30,
14
+ device=device,
15
+ )
16
+
17
+
18
+ # Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
19
+ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
20
+ if seconds is not None:
21
+ milliseconds = round(seconds * 1000.0)
22
+
23
+ hours = milliseconds // 3_600_000
24
+ milliseconds -= hours * 3_600_000
25
+
26
+ minutes = milliseconds // 60_000
27
+ milliseconds -= minutes * 60_000
28
+
29
+ seconds = milliseconds // 1_000
30
+ milliseconds -= seconds * 1_000
31
+
32
+ hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
33
+ return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
34
+ else:
35
+ # we have a malformed timestamp so just return it as is
36
+ return seconds
37
+
38
+
39
+ def transcribe(file, task, return_timestamps):
40
+ outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
41
+ text = outputs["text"]
42
+ if return_timestamps:
43
+ timestamps = outputs["chunks"]
44
+ timestamps = [
45
+ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
46
+ for chunk in timestamps
47
+ ]
48
+ text = "\n".join(str(feature) for feature in timestamps)
49
+ return text
50
 
 
51
 
52
+ file_transcribe = gr.Interface(
53
+ fn=transcribe,
54
+ inputs=[
55
+ gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
56
+ gr.inputs.Checkbox(default=False, label="Return timestamps"),
57
+ ],
58
+ outputs="text",
59
+ layout="horizontal",
60
+ theme="huggingface",
61
+ title="Whisper Demo: Transcribe Audio",
62
+ description=(
63
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
64
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
65
+ " of arbitrary length."
66
+ ),
67
+ allow_flagging="never",
68
+ )
69
 
70
+ file_transcribe.launch(enable_queue=True)