Spaces:
Sleeping
Sleeping
File size: 8,302 Bytes
8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 99c2dd3 c1e3042 8c6664d c1e3042 8c6664d c1e3042 8c6664d c1e3042 9d49df0 8c6664d c1e3042 8c6664d 9d49df0 d059e20 9d49df0 | 1 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | import gradio as gr
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import numpy as np
import os
os.environ["OMP_NUM_THREADS"] = "2"
os.environ["MKL_NUM_THREADS"] = "2"
torch.set_num_threads(2)
device = "cpu"
torch_dtype = torch.float32
model_id = "openai/whisper-tiny"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id,
dtype=torch_dtype,
low_cpu_mem_usage=True,
use_safetensors=True,
attn_implementation="sdpa"
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
dtype=torch_dtype,
device=device,
ignore_warning=True
)
def transcribe_audio(audio_file, task="transcribe", language="auto", return_timestamps=False):
if audio_file is None:
return "No audio file provided."
try:
with torch.inference_mode():
generate_kwargs = {
"task": task,
"language": None if language == "auto" else language,
"num_beams": 1,
"do_sample": False,
"temperature": 0.0,
"max_new_tokens": 220,
"compression_ratio_threshold": 1.35,
"logprob_threshold": -1.0,
"no_speech_threshold": 0.6,
}
if task == "translate":
generate_kwargs["task"] = "translate"
result = pipe(
audio_file,
return_timestamps=return_timestamps,
generate_kwargs=generate_kwargs
)
if return_timestamps and "chunks" in result:
formatted_result = []
for chunk in result["chunks"]:
timestamp = f"[{chunk['timestamp'][0]:.2f}s - {chunk['timestamp'][1]:.2f}s]"
formatted_result.append(f"{timestamp} {chunk['text']}")
return "\n".join(formatted_result)
else:
return result["text"]
except Exception as e:
return f"Error processing audio: {str(e)}"
def transcribe_microphone(audio_data, task="transcribe", language="auto", return_timestamps=False):
if audio_data is None:
return "No audio recorded."
try:
sample_rate, audio_array = audio_data
audio_array = audio_array.astype(np.float32)
audio_array = audio_array / np.max(np.abs(audio_array))
with torch.inference_mode():
generate_kwargs = {
"task": task,
"language": None if language == "auto" else language,
"num_beams": 1,
"do_sample": False,
"temperature": 0.0,
"max_new_tokens": 448,
"compression_ratio_threshold": 1.35,
"logprob_threshold": -1.0,
"no_speech_threshold": 0.6,
}
if task == "translate":
generate_kwargs["task"] = "translate"
result = pipe(
{"array": audio_array, "sampling_rate": sample_rate},
return_timestamps=return_timestamps,
generate_kwargs=generate_kwargs
)
if return_timestamps and "chunks" in result:
formatted_result = []
for chunk in result["chunks"]:
timestamp = f"[{chunk['timestamp'][0]:.2f}s - {chunk['timestamp'][1]:.2f}s]"
formatted_result.append(f"{timestamp} {chunk['text']}")
return "\n".join(formatted_result)
else:
return result["text"]
except Exception as e:
return f"Error processing audio: {str(e)}"
languages = [
("Auto Detect", "auto"),
("English", "en"),
("Chinese", "zh"),
("German", "de"),
("Spanish", "es"),
("Russian", "ru"),
("Korean", "ko"),
("French", "fr"),
("Japanese", "ja"),
("Portuguese", "pt"),
("Turkish", "tr"),
("Polish", "pl"),
("Catalan", "ca"),
("Dutch", "nl"),
("Arabic", "ar"),
("Swedish", "sv"),
("Italian", "it"),
("Indonesian", "id"),
("Hindi", "hi"),
("Finnish", "fi"),
("Vietnamese", "vi"),
("Hebrew", "he"),
("Ukrainian", "uk"),
("Greek", "el"),
("Malay", "ms"),
("Czech", "cs"),
("Romanian", "ro"),
("Danish", "da"),
("Hungarian", "hu"),
("Tamil", "ta"),
("Norwegian", "no"),
("Thai", "th"),
("Urdu", "ur"),
("Croatian", "hr"),
("Bulgarian", "bg"),
("Lithuanian", "lt"),
("Latin", "la"),
]
with gr.Blocks(title="Whisper Tiny - Speech to Text") as demo:
gr.Markdown("# 🎤 Whisper Tiny - Speech to Text")
gr.Markdown("Upload an audio file or record directly to get fast transcription using OpenAI's Whisper Tiny model (39M parameters).")
with gr.Tab("Upload Audio File"):
with gr.Row():
with gr.Column():
audio_file = gr.Audio(
label="Upload Audio File",
type="filepath"
)
task_file = gr.Radio(
choices=[("Transcribe", "transcribe"), ("Translate to English", "translate")],
value="transcribe",
label="Task"
)
language_file = gr.Dropdown(
choices=languages,
value="auto",
label="Source Language"
)
timestamps_file = gr.Checkbox(
label="Return Timestamps",
value=False
)
submit_file = gr.Button("Transcribe Audio File", variant="primary")
with gr.Column():
output_file = gr.Textbox(
label="Transcription Result",
lines=10,
max_lines=20
)
with gr.Tab("Record Audio"):
with gr.Row():
with gr.Column():
audio_mic = gr.Audio(
label="Record Audio",
sources=["microphone"]
)
task_mic = gr.Radio(
choices=[("Transcribe", "transcribe"), ("Translate to English", "translate")],
value="transcribe",
label="Task"
)
language_mic = gr.Dropdown(
choices=languages,
value="auto",
label="Source Language"
)
timestamps_mic = gr.Checkbox(
label="Return Timestamps",
value=False
)
submit_mic = gr.Button("Transcribe Recording", variant="primary")
with gr.Column():
output_mic = gr.Textbox(
label="Transcription Result",
lines=10,
max_lines=20
)
submit_file.click(
transcribe_audio,
inputs=[audio_file, task_file, language_file, timestamps_file],
outputs=output_file
)
submit_mic.click(
transcribe_microphone,
inputs=[audio_mic, task_mic, language_mic, timestamps_mic],
outputs=output_mic
)
gr.Markdown("### Features:")
gr.Markdown("- **Lightweight**: Powered by Whisper Tiny model (39M parameters)")
gr.Markdown("- **CPU Optimized**: Optimized for 2-core CPU with 16GB RAM")
gr.Markdown("- **Multi-language**: Supports 99+ languages")
gr.Markdown("- **Translation**: Can translate speech to English")
gr.Markdown("- **Timestamps**: Optional word-level or sentence-level timestamps")
gr.Markdown("- **Fast Processing**: Smallest Whisper model for maximum speed")
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
) |