Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
demo.launch()
|
|
|
1 |
+
Hugging Face's logo
|
2 |
+
Hugging Face
|
3 |
+
Search models, datasets, users...
|
4 |
+
Models
|
5 |
+
Datasets
|
6 |
+
Spaces
|
7 |
+
Posts
|
8 |
+
Docs
|
9 |
+
Solutions
|
10 |
+
Pricing
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
Spaces:
|
15 |
+
|
16 |
+
KingNish
|
17 |
+
/
|
18 |
+
Realtime-whisper-large-v3-turbo
|
19 |
+
|
20 |
+
|
21 |
+
like
|
22 |
+
254
|
23 |
+
App
|
24 |
+
Files
|
25 |
+
Community
|
26 |
+
5
|
27 |
+
Realtime-whisper-large-v3-turbo
|
28 |
+
/
|
29 |
+
app.py
|
30 |
+
|
31 |
+
KingNish's picture
|
32 |
+
KingNish
|
33 |
+
Update app.py
|
34 |
+
fc21d85
|
35 |
+
verified
|
36 |
+
about 1 month ago
|
37 |
+
raw
|
38 |
+
|
39 |
+
Copy download link
|
40 |
+
history
|
41 |
+
blame
|
42 |
+
contribute
|
43 |
+
delete
|
44 |
+
|
45 |
+
5.6 kB
|
46 |
+
import spaces
|
47 |
+
import torch
|
48 |
import gradio as gr
|
49 |
+
import tempfile
|
50 |
+
import os
|
51 |
+
import uuid
|
52 |
+
import scipy.io.wavfile
|
53 |
+
import time
|
54 |
+
import numpy as np
|
55 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, WhisperTokenizer, pipeline
|
56 |
+
import subprocess
|
57 |
+
subprocess.run(
|
58 |
+
"pip install flash-attn --no-build-isolation",
|
59 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
60 |
+
shell=True,
|
61 |
+
)
|
62 |
+
|
63 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
64 |
+
torch_dtype = torch.float16
|
65 |
+
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
66 |
+
|
67 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
68 |
+
MODEL_NAME, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, attn_implementation="flash_attention_2"
|
69 |
+
)
|
70 |
+
model.to(device)
|
71 |
+
|
72 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
73 |
+
tokenizer = WhisperTokenizer.from_pretrained(MODEL_NAME)
|
74 |
+
|
75 |
+
pipe = pipeline(
|
76 |
+
task="automatic-speech-recognition",
|
77 |
+
model=model,
|
78 |
+
tokenizer=tokenizer,
|
79 |
+
feature_extractor=processor.feature_extractor,
|
80 |
+
chunk_length_s=10,
|
81 |
+
torch_dtype=torch_dtype,
|
82 |
+
device=device,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
|
85 |
+
@spaces.GPU
|
86 |
+
def transcribe(inputs, previous_transcription):
|
87 |
+
start_time = time.time()
|
88 |
+
try:
|
89 |
+
filename = f"{uuid.uuid4().hex}.wav"
|
90 |
+
sample_rate, audio_data = inputs
|
91 |
+
scipy.io.wavfile.write(filename, sample_rate, audio_data)
|
92 |
+
|
93 |
+
transcription = pipe(filename)["text"]
|
94 |
+
previous_transcription += transcription
|
95 |
+
|
96 |
+
end_time = time.time()
|
97 |
+
latency = end_time - start_time
|
98 |
+
return previous_transcription, f"{latency:.2f}"
|
99 |
+
except Exception as e:
|
100 |
+
print(f"Error during Transcription: {e}")
|
101 |
+
return previous_transcription, "Error"
|
102 |
+
|
103 |
+
@spaces.GPU
|
104 |
+
def translate_and_transcribe(inputs, previous_transcription, target_language):
|
105 |
+
start_time = time.time()
|
106 |
+
try:
|
107 |
+
filename = f"{uuid.uuid4().hex}.wav"
|
108 |
+
sample_rate, audio_data = inputs
|
109 |
+
scipy.io.wavfile.write(filename, sample_rate, audio_data)
|
110 |
+
|
111 |
+
translation = pipe(filename, generate_kwargs={"task": "translate", "language": target_language} )["text"]
|
112 |
+
|
113 |
+
previous_transcription += translation
|
114 |
+
|
115 |
+
end_time = time.time()
|
116 |
+
latency = end_time - start_time
|
117 |
+
return previous_transcription, f"{latency:.2f}"
|
118 |
+
except Exception as e:
|
119 |
+
print(f"Error during Translation and Transcription: {e}")
|
120 |
+
return previous_transcription, "Error"
|
121 |
+
|
122 |
+
def clear():
|
123 |
+
return ""
|
124 |
+
|
125 |
+
with gr.Blocks() as microphone:
|
126 |
+
with gr.Column():
|
127 |
+
gr.Markdown(f"# Realtime Whisper Large V3 Turbo: \n Transcribe Audio in Realtime. This Demo uses the Checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers.\n Note: The first token takes about 5 seconds. After that, it works flawlessly.")
|
128 |
+
with gr.Row():
|
129 |
+
input_audio_microphone = gr.Audio(streaming=True)
|
130 |
+
output = gr.Textbox(label="Transcription", value="")
|
131 |
+
latency_textbox = gr.Textbox(label="Latency (seconds)", value="0.0", scale=0)
|
132 |
+
with gr.Row():
|
133 |
+
clear_button = gr.Button("Clear Output")
|
134 |
+
|
135 |
+
input_audio_microphone.stream(transcribe, [input_audio_microphone, output], [output, latency_textbox], time_limit=45, stream_every=2, concurrency_limit=None)
|
136 |
+
clear_button.click(clear, outputs=[output])
|
137 |
+
|
138 |
+
|
139 |
|
140 |
+
demo.launch()
|
|