ovieyra21 commited on
Commit
a54c666
β€’
1 Parent(s): 75106f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -1
app.py CHANGED
@@ -1,3 +1,100 @@
 
 
 
1
  import gradio as gr
2
 
3
- gr.load("models/ovieyra21/whisper-small-curso").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ from transformers.pipelines.audio_utils import ffmpeg_read
4
  import gradio as gr
5
 
6
+ MODEL_NAME = "ovieyra21/whisper-small-curso"
7
+ BATCH_SIZE = 8
8
+
9
+ device = 0 if torch.cuda.is_available() else "cpu"
10
+
11
+ pipe = pipeline(
12
+ task="automatic-speech-recognition",
13
+ model=MODEL_NAME,
14
+ chunk_length_s=30,
15
+ device=device,
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
+ def transcribe(file, task, return_timestamps):
39
+ outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
40
+ text = outputs["text"]
41
+ if return_timestamps:
42
+ timestamps = outputs["chunks"]
43
+ timestamps = [
44
+ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
45
+ for chunk in timestamps
46
+ ]
47
+ text = "\n".join(str(feature) for feature in timestamps)
48
+ return text
49
+
50
+
51
+ demo = gr.Blocks()
52
+
53
+ mic_transcribe = gr.Interface(
54
+ fn=transcribe,
55
+ inputs=[
56
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
57
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
58
+ gr.inputs.Checkbox(default=False, label="Return timestamps"),
59
+ ],
60
+ outputs="text",
61
+ layout="horizontal",
62
+ theme="huggingface",
63
+ title="Whisper Demo: Transcribe Audio",
64
+ description=(
65
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
66
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
67
+ " of arbitrary length."
68
+ ),
69
+ allow_flagging="never",
70
+ )
71
+ file_transcribe = gr.Interface(
72
+ fn=transcribe,
73
+ inputs=[
74
+ gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
75
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
76
+ gr.inputs.Checkbox(default=False, label="Return timestamps"),
77
+ ],
78
+ outputs="text",
79
+ layout="horizontal",
80
+ theme="huggingface",
81
+ title="Whisper Demo: Transcribe Audio",
82
+ description=(
83
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
84
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
85
+ " of arbitrary length."
86
+ ),
87
+ examples=[
88
+ ["./example.flac", "transcribe", False],
89
+ ["./example.flac", "transcribe", True],
90
+ ],
91
+ cache_examples=True,
92
+ allow_flagging="never",
93
+ )
94
+
95
+ with demo:
96
+ gr.TabbedInterface([mic_transcribe, file_transcribe], ["Transcribe Microphone", "Transcribe Audio File"])
97
+
98
+
99
+
100
+ demo.launch(enable_queue=True)