Hemg commited on
Commit
1f204d5
·
verified ·
1 Parent(s): 37a0ab4

Upload 5 files

Browse files
Files changed (4) hide show
  1. app.py +79 -0
  2. examples/example.flac +0 -0
  3. packages.txt +1 -0
  4. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+
5
+ MODEL_NAME = "JackismyShephard/whisper-tiny-finetuned-minds14"
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(
20
+ seconds: float, always_include_hours: bool = False, decimal_marker: str = "."
21
+ ):
22
+ if seconds is not None:
23
+ milliseconds = round(seconds * 1000.0)
24
+
25
+ hours = milliseconds // 3_600_000
26
+ milliseconds -= hours * 3_600_000
27
+
28
+ minutes = milliseconds // 60_000
29
+ milliseconds -= minutes * 60_000
30
+
31
+ seconds = milliseconds // 1_000
32
+ milliseconds -= seconds * 1_000
33
+
34
+ hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
35
+ return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
36
+ else:
37
+ # we have a malformed timestamp so just return it as is
38
+ return seconds
39
+
40
+
41
+ def transcribe(file, return_timestamps):
42
+ outputs = pipe(
43
+ file,
44
+ batch_size=BATCH_SIZE,
45
+ return_timestamps=return_timestamps,
46
+ )
47
+ text = outputs["text"]
48
+ if return_timestamps:
49
+ timestamps = outputs["chunks"]
50
+ timestamps = [
51
+ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
52
+ for chunk in timestamps
53
+ ]
54
+ text = "\n".join(str(feature) for feature in timestamps)
55
+ return text
56
+
57
+
58
+ demo = gr.Interface(
59
+ fn=transcribe,
60
+ inputs=[
61
+ gr.Audio(label="Audio", type="filepath"),
62
+ gr.Checkbox(label="Return timestamps"),
63
+ ],
64
+ outputs=gr.Textbox(show_copy_button=True, label="Text"),
65
+ title="Automatic Speech Recognition",
66
+ description=(
67
+ "Transcribe or translate long-form audio file or microphone inputs with the click of a button! Demo uses the"
68
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe or translate audio"
69
+ " of arbitrary length."
70
+ ),
71
+ examples=[
72
+ ["examples/example.flac", False],
73
+ ["examples/example.flac", True],
74
+ ],
75
+ cache_examples=True,
76
+ allow_flagging="never",
77
+ )
78
+
79
+ demo.launch()
examples/example.flac ADDED
Binary file (282 kB). View file
 
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ transformers