riteshkr commited on
Commit
68dec23
β€’
1 Parent(s): bfc9e9d

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -102
app.py DELETED
@@ -1,102 +0,0 @@
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 = "riteshkr/whisper-large-v3-quantized"
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
-
19
- # Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
20
- def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
21
- if seconds is not None:
22
- milliseconds = round(seconds * 1000.0)
23
-
24
- hours = milliseconds // 3_600_000
25
- milliseconds -= hours * 3_600_000
26
-
27
- minutes = milliseconds // 60_000
28
- milliseconds -= minutes * 60_000
29
-
30
- seconds = milliseconds // 1_000
31
- milliseconds -= seconds * 1_000
32
-
33
- hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
34
- return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
35
- else:
36
- # we have a malformed timestamp so just return it as is
37
- return seconds
38
-
39
-
40
- def transcribe(file, task, return_timestamps):
41
- outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
42
- text = outputs["text"]
43
- if return_timestamps:
44
- timestamps = outputs["chunks"]
45
- timestamps = [
46
- f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
47
- for chunk in timestamps
48
- ]
49
- text = "\n".join(str(feature) for feature in timestamps)
50
- return text
51
-
52
-
53
-
54
- demo = gr.Blocks()
55
-
56
- mic_transcribe = gr.Interface(
57
- fn=transcribe,
58
- inputs=[
59
- gr.Audio(source="microphone", type="filepath", optional=True),
60
- gr.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
61
- gr.Checkbox(default=False, label="Return timestamps"),
62
- ],
63
- outputs="text",
64
- layout="horizontal",
65
- theme="huggingface",
66
- title="Whisper Demo: Transcribe Audio",
67
- description=(
68
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
69
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
70
- " of arbitrary length."
71
- ),
72
- allow_flagging="never",
73
- )
74
-
75
- file_transcribe = gr.Interface(
76
- fn=transcribe,
77
- inputs=[
78
- gr.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
79
- gr.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
80
- gr.Checkbox(default=False, label="Return timestamps"),
81
- ],
82
- outputs="text",
83
- layout="horizontal",
84
- theme="huggingface",
85
- title="Whisper Demo: Transcribe Audio",
86
- description=(
87
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
88
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
89
- " of arbitrary length."
90
- ),
91
- examples=[
92
- ["./example.flac", "transcribe", False],
93
- ["./example.flac", "transcribe", True],
94
- ],
95
- cache_examples=True,
96
- allow_flagging="never",
97
- )
98
-
99
- with demo:
100
- gr.TabbedInterface([mic_transcribe, file_transcribe], ["Transcribe Microphone", "Transcribe Audio File"])
101
-
102
- demo.launch(enable_queue=True)