Spaces:
Sleeping
Sleeping
Peter Michael Gits commited on
Commit Β·
cded70e
1
Parent(s): e0f3b91
Initial STT service with ZeroGPU support
Browse files- .gitignore +1 -0
- README.md +50 -7
- app.py +513 -0
- requirements.txt +12 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
#### 4. `.gitignore`
|
README.md
CHANGED
|
@@ -1,14 +1,57 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
-
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ZeroGPU STT Service
|
| 3 |
+
emoji: π€
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: orange
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.8.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
+
suggested_hardware: zerogpu
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# π€ ZeroGPU Speech-to-Text Service
|
| 15 |
+
|
| 16 |
+
High-performance speech recognition powered by **Hugging Face ZeroGPU** and **Nvidia H200** with Whisper models.
|
| 17 |
+
|
| 18 |
+
## π― Features
|
| 19 |
+
|
| 20 |
+
- π **ZeroGPU Acceleration**: Dynamic H200 GPU allocation
|
| 21 |
+
- π€ **Multi-language Support**: 100+ languages with auto-detection
|
| 22 |
+
- β‘ **Real-time Processing**: Often faster than audio duration
|
| 23 |
+
- π **Timestamp Precision**: Word-level timing information
|
| 24 |
+
- π¦ **Batch Processing**: Multiple files in parallel
|
| 25 |
+
- π΄ **Live Transcription**: Real-time microphone input
|
| 26 |
+
- π **WebRTC Ready**: Integration with live audio streams
|
| 27 |
+
- π° **Cost Efficient**: No idle costs with Pro subscription
|
| 28 |
+
|
| 29 |
+
## ποΈ Architecture
|
| 30 |
+
|
| 31 |
+
- **Backend**: Whisper (OpenAI) with PyTorch optimization
|
| 32 |
+
- **Frontend**: Gradio with enhanced multi-tab UI
|
| 33 |
+
- **GPU**: ZeroGPU with H200 dynamic scaling
|
| 34 |
+
- **Models**: Whisper tiny/base/small/medium/large-v2
|
| 35 |
+
|
| 36 |
+
## π Performance
|
| 37 |
+
|
| 38 |
+
- **Real-time Factor**: 0.1x - 0.5x (much faster than real-time)
|
| 39 |
+
- **Languages**: 100+ with auto-detection
|
| 40 |
+
- **Accuracy**: State-of-the-art with Whisper models
|
| 41 |
+
- **Batch processing**: Parallel execution on H200
|
| 42 |
+
|
| 43 |
+
## π» API Usage
|
| 44 |
+
|
| 45 |
+
### Python Client
|
| 46 |
+
```python
|
| 47 |
+
from gradio_client import Client
|
| 48 |
+
|
| 49 |
+
client = Client("YOUR_USERNAME/stt-gpu-service")
|
| 50 |
+
result = client.predict(
|
| 51 |
+
"audio.wav", # audio file
|
| 52 |
+
"auto", # language
|
| 53 |
+
"base", # model size
|
| 54 |
+
True, # timestamps
|
| 55 |
+
api_name="/predict"
|
| 56 |
+
)
|
| 57 |
+
status, transcription, timestamps = result
|
app.py
ADDED
|
@@ -0,0 +1,513 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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6 |
+
import io
|
| 7 |
+
import logging
|
| 8 |
+
import time
|
| 9 |
+
import os
|
| 10 |
+
from transformers import (
|
| 11 |
+
AutoModelForSpeechSeq2Seq,
|
| 12 |
+
AutoProcessor,
|
| 13 |
+
pipeline
|
| 14 |
+
)
|
| 15 |
+
import spaces # Required for ZeroGPU
|
| 16 |
+
import tempfile
|
| 17 |
+
import soundfile as sf
|
| 18 |
+
|
| 19 |
+
# Set up logging
|
| 20 |
+
logging.basicConfig(level=logging.INFO)
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
# Global variables for models
|
| 24 |
+
whisper_model = None
|
| 25 |
+
whisper_processor = None
|
| 26 |
+
transcription_pipeline = None
|
| 27 |
+
device = None
|
| 28 |
+
|
| 29 |
+
# Supported languages for Whisper
|
| 30 |
+
LANGUAGES = {
|
| 31 |
+
"auto": "Auto-detect",
|
| 32 |
+
"en": "English",
|
| 33 |
+
"es": "Spanish",
|
| 34 |
+
"fr": "French",
|
| 35 |
+
"de": "German",
|
| 36 |
+
"it": "Italian",
|
| 37 |
+
"pt": "Portuguese",
|
| 38 |
+
"ru": "Russian",
|
| 39 |
+
"ja": "Japanese",
|
| 40 |
+
"ko": "Korean",
|
| 41 |
+
"zh": "Chinese",
|
| 42 |
+
"ar": "Arabic",
|
| 43 |
+
"hi": "Hindi",
|
| 44 |
+
"tr": "Turkish",
|
| 45 |
+
"pl": "Polish",
|
| 46 |
+
"nl": "Dutch",
|
| 47 |
+
"sv": "Swedish",
|
| 48 |
+
"da": "Danish",
|
| 49 |
+
"no": "Norwegian",
|
| 50 |
+
"fi": "Finnish"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
def load_model(model_size="base"):
|
| 54 |
+
"""Load the Whisper STT model - optimized for ZeroGPU"""
|
| 55 |
+
global whisper_model, whisper_processor, transcription_pipeline, device
|
| 56 |
+
|
| 57 |
+
logger.info(f"Loading Whisper {model_size} model for ZeroGPU...")
|
| 58 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 59 |
+
logger.info(f"Using device: {device}")
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
model_id = f"openai/whisper-{model_size}"
|
| 63 |
+
|
| 64 |
+
# Load model with optimizations for H200
|
| 65 |
+
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 66 |
+
model_id,
|
| 67 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 68 |
+
low_cpu_mem_usage=True,
|
| 69 |
+
use_safetensors=True,
|
| 70 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
whisper_processor = AutoProcessor.from_pretrained(model_id)
|
| 74 |
+
|
| 75 |
+
# Create pipeline for easier inference
|
| 76 |
+
transcription_pipeline = pipeline(
|
| 77 |
+
"automatic-speech-recognition",
|
| 78 |
+
model=whisper_model,
|
| 79 |
+
tokenizer=whisper_processor.tokenizer,
|
| 80 |
+
feature_extractor=whisper_processor.feature_extractor,
|
| 81 |
+
max_new_tokens=128,
|
| 82 |
+
chunk_length_s=30,
|
| 83 |
+
batch_size=16,
|
| 84 |
+
return_timestamps=True,
|
| 85 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 86 |
+
device=device,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
logger.info(f"Whisper {model_size} model loaded successfully on {device}!")
|
| 90 |
+
return True
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.error(f"Error loading model: {e}")
|
| 93 |
+
return False
|
| 94 |
+
|
| 95 |
+
def preprocess_audio(audio_input):
|
| 96 |
+
"""Preprocess audio for Whisper"""
|
| 97 |
+
try:
|
| 98 |
+
# Handle different input types
|
| 99 |
+
if isinstance(audio_input, tuple):
|
| 100 |
+
sample_rate, audio_data = audio_input
|
| 101 |
+
audio_data = audio_data.astype(np.float32)
|
| 102 |
+
if audio_data.ndim > 1:
|
| 103 |
+
audio_data = np.mean(audio_data, axis=1) # Convert to mono
|
| 104 |
+
elif isinstance(audio_input, str):
|
| 105 |
+
# File path
|
| 106 |
+
audio_data, sample_rate = librosa.load(audio_input, sr=16000, mono=True)
|
| 107 |
+
else:
|
| 108 |
+
raise ValueError("Unsupported audio input type")
|
| 109 |
+
|
| 110 |
+
# Resample to 16kHz if needed (Whisper requirement)
|
| 111 |
+
if sample_rate != 16000:
|
| 112 |
+
audio_data = librosa.resample(audio_data, orig_sr=sample_rate, target_sr=16000)
|
| 113 |
+
|
| 114 |
+
# Normalize audio
|
| 115 |
+
audio_data = audio_data / np.max(np.abs(audio_data))
|
| 116 |
+
|
| 117 |
+
return audio_data, 16000
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logger.error(f"Error preprocessing audio: {e}")
|
| 120 |
+
return None, None
|
| 121 |
+
|
| 122 |
+
@spaces.GPU # ZeroGPU allocation for transcription
|
| 123 |
+
def transcribe_audio(audio_input, language="auto", model_size="base", return_timestamps=True):
|
| 124 |
+
"""Transcribe audio using Whisper with ZeroGPU acceleration"""
|
| 125 |
+
global transcription_pipeline
|
| 126 |
+
|
| 127 |
+
if audio_input is None:
|
| 128 |
+
return "β No audio provided", "", ""
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
# Load model if not already loaded or if model size changed
|
| 132 |
+
if transcription_pipeline is None:
|
| 133 |
+
success = load_model(model_size)
|
| 134 |
+
if not success:
|
| 135 |
+
return "β Error: Could not load Whisper model", "", ""
|
| 136 |
+
|
| 137 |
+
start_time = time.time()
|
| 138 |
+
logger.info("Starting transcription with ZeroGPU...")
|
| 139 |
+
|
| 140 |
+
# Preprocess audio
|
| 141 |
+
audio_data, sample_rate = preprocess_audio(audio_input)
|
| 142 |
+
if audio_data is None:
|
| 143 |
+
return "β Error: Could not process audio file", "", ""
|
| 144 |
+
|
| 145 |
+
# Set language for transcription
|
| 146 |
+
generate_kwargs = {}
|
| 147 |
+
if language != "auto":
|
| 148 |
+
generate_kwargs["language"] = language
|
| 149 |
+
|
| 150 |
+
# Transcribe with ZeroGPU acceleration
|
| 151 |
+
with torch.cuda.amp.autocast(enabled=torch.cuda.is_available()):
|
| 152 |
+
result = transcription_pipeline(
|
| 153 |
+
audio_data,
|
| 154 |
+
generate_kwargs=generate_kwargs,
|
| 155 |
+
return_timestamps=return_timestamps
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Extract results
|
| 159 |
+
transcription = result["text"]
|
| 160 |
+
|
| 161 |
+
# Format timestamps if available
|
| 162 |
+
timestamps_text = ""
|
| 163 |
+
if return_timestamps and "chunks" in result:
|
| 164 |
+
timestamps_text = "\n".join([
|
| 165 |
+
f"[{chunk['timestamp'][0]:.1f}s - {chunk['timestamp'][1]:.1f}s]: {chunk['text']}"
|
| 166 |
+
for chunk in result["chunks"]
|
| 167 |
+
])
|
| 168 |
+
|
| 169 |
+
# Calculate performance metrics
|
| 170 |
+
processing_time = time.time() - start_time
|
| 171 |
+
audio_duration = len(audio_data) / sample_rate
|
| 172 |
+
real_time_factor = processing_time / audio_duration if audio_duration > 0 else 0
|
| 173 |
+
|
| 174 |
+
gpu_name = torch.cuda.get_device_name(0) if torch.cuda.is_available() else "CPU"
|
| 175 |
+
status = f"β
Transcribed {audio_duration:.1f}s audio in {processing_time:.2f}s on {gpu_name} (RTF: {real_time_factor:.2f}x)"
|
| 176 |
+
|
| 177 |
+
return status, transcription, timestamps_text
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
error_msg = f"β Error during transcription: {str(e)}"
|
| 181 |
+
logger.error(error_msg)
|
| 182 |
+
return error_msg, "", ""
|
| 183 |
+
|
| 184 |
+
@spaces.GPU # ZeroGPU for batch processing
|
| 185 |
+
def batch_transcribe(audio_files, language="auto", model_size="base"):
|
| 186 |
+
"""Batch transcription with ZeroGPU optimization"""
|
| 187 |
+
if not audio_files:
|
| 188 |
+
return "β No audio files provided", []
|
| 189 |
+
|
| 190 |
+
results = []
|
| 191 |
+
start_time = time.time()
|
| 192 |
+
|
| 193 |
+
logger.info(f"Starting batch transcription of {len(audio_files)} files...")
|
| 194 |
+
|
| 195 |
+
for i, audio_file in enumerate(audio_files):
|
| 196 |
+
try:
|
| 197 |
+
status, transcription, timestamps = transcribe_audio(
|
| 198 |
+
audio_file, language, model_size, return_timestamps=True
|
| 199 |
+
)
|
| 200 |
+
results.append({
|
| 201 |
+
"file": f"Audio_{i+1}",
|
| 202 |
+
"transcription": transcription,
|
| 203 |
+
"timestamps": timestamps,
|
| 204 |
+
"status": status
|
| 205 |
+
})
|
| 206 |
+
except Exception as e:
|
| 207 |
+
results.append({
|
| 208 |
+
"file": f"Audio_{i+1}",
|
| 209 |
+
"transcription": "",
|
| 210 |
+
"timestamps": "",
|
| 211 |
+
"status": f"β Error: {str(e)}"
|
| 212 |
+
})
|
| 213 |
+
|
| 214 |
+
total_time = time.time() - start_time
|
| 215 |
+
batch_status = f"π Batch completed: {len(audio_files)} files in {total_time:.2f}s"
|
| 216 |
+
|
| 217 |
+
return batch_status, results
|
| 218 |
+
|
| 219 |
+
def get_system_info():
|
| 220 |
+
"""Get system information including ZeroGPU details"""
|
| 221 |
+
info = {
|
| 222 |
+
"π ZeroGPU": "Active" if torch.cuda.is_available() else "Not Available",
|
| 223 |
+
"π― GPU Name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "CPU Only",
|
| 224 |
+
"πΎ GPU Memory": f"{torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB" if torch.cuda.is_available() else "N/A",
|
| 225 |
+
"β‘ CUDA Version": torch.version.cuda if torch.cuda.is_available() else "N/A",
|
| 226 |
+
"π§ PyTorch": torch.__version__,
|
| 227 |
+
"π€ Model Status": "β
Loaded" if transcription_pipeline is not None else "β³ Loading on first use",
|
| 228 |
+
"ποΈ Mixed Precision": "β
Enabled" if torch.cuda.is_available() else "β CPU Mode",
|
| 229 |
+
"π Whisper Version": "openai/whisper-base (default)"
|
| 230 |
+
}
|
| 231 |
+
return "\n".join([f"{k}: {v}" for k, v in info.items()])
|
| 232 |
+
|
| 233 |
+
# Model size options
|
| 234 |
+
MODEL_SIZES = [
|
| 235 |
+
("tiny", "Tiny (39 MB) - Fastest"),
|
| 236 |
+
("base", "Base (74 MB) - Balanced"),
|
| 237 |
+
("small", "Small (244 MB) - Better accuracy"),
|
| 238 |
+
("medium", "Medium (769 MB) - High accuracy"),
|
| 239 |
+
("large-v2", "Large-v2 (1550 MB) - Best accuracy")
|
| 240 |
+
]
|
| 241 |
+
|
| 242 |
+
# Create enhanced Gradio interface for ZeroGPU STT
|
| 243 |
+
with gr.Blocks(
|
| 244 |
+
title="π€ ZeroGPU STT Service",
|
| 245 |
+
theme=gr.themes.Soft(),
|
| 246 |
+
css="""
|
| 247 |
+
.gradio-container {
|
| 248 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 249 |
+
}
|
| 250 |
+
.main-header {
|
| 251 |
+
text-align: center;
|
| 252 |
+
background: rgba(255,255,255,0.1);
|
| 253 |
+
padding: 20px;
|
| 254 |
+
border-radius: 10px;
|
| 255 |
+
margin-bottom: 20px;
|
| 256 |
+
}
|
| 257 |
+
"""
|
| 258 |
+
) as iface:
|
| 259 |
+
|
| 260 |
+
with gr.Row():
|
| 261 |
+
gr.Markdown("""
|
| 262 |
+
<div class="main-header">
|
| 263 |
+
|
| 264 |
+
# π€ ZeroGPU Speech-to-Text Service
|
| 265 |
+
## Powered by Hugging Face Pro + Nvidia H200
|
| 266 |
+
|
| 267 |
+
Ultra-fast speech recognition with dynamic GPU scaling
|
| 268 |
+
|
| 269 |
+
</div>
|
| 270 |
+
""")
|
| 271 |
+
|
| 272 |
+
with gr.Tabs():
|
| 273 |
+
# Single transcription tab
|
| 274 |
+
with gr.TabItem("π€ Single Transcription"):
|
| 275 |
+
with gr.Row():
|
| 276 |
+
with gr.Column(scale=2):
|
| 277 |
+
audio_input = gr.Audio(
|
| 278 |
+
label="π΅ Audio Input",
|
| 279 |
+
type="filepath",
|
| 280 |
+
sources=["microphone", "upload"]
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
with gr.Row():
|
| 284 |
+
language_dropdown = gr.Dropdown(
|
| 285 |
+
choices=[(desc, code) for code, desc in LANGUAGES.items()],
|
| 286 |
+
value="auto",
|
| 287 |
+
label="π Language",
|
| 288 |
+
info="Select language or auto-detect"
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
model_dropdown = gr.Dropdown(
|
| 292 |
+
choices=[(desc, code) for code, desc in MODEL_SIZES],
|
| 293 |
+
value="base",
|
| 294 |
+
label="π€ Model Size",
|
| 295 |
+
info="Larger models = better accuracy but slower"
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
timestamps_checkbox = gr.Checkbox(
|
| 299 |
+
label="π Include Timestamps",
|
| 300 |
+
value=True,
|
| 301 |
+
info="Show word-level timing information"
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
with gr.Row():
|
| 305 |
+
transcribe_btn = gr.Button("π― Transcribe", variant="primary", size="lg")
|
| 306 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
| 307 |
+
|
| 308 |
+
with gr.Column(scale=1):
|
| 309 |
+
system_info = gr.Textbox(
|
| 310 |
+
label="βοΈ ZeroGPU Status",
|
| 311 |
+
value=get_system_info(),
|
| 312 |
+
interactive=False,
|
| 313 |
+
lines=9
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
status_output = gr.Textbox(
|
| 317 |
+
label="π Transcription Status",
|
| 318 |
+
interactive=False,
|
| 319 |
+
lines=2
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
with gr.Row():
|
| 323 |
+
with gr.Column():
|
| 324 |
+
transcription_output = gr.Textbox(
|
| 325 |
+
label="π Transcription",
|
| 326 |
+
interactive=False,
|
| 327 |
+
lines=6,
|
| 328 |
+
placeholder="Transcribed text will appear here..."
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
with gr.Column():
|
| 332 |
+
timestamps_output = gr.Textbox(
|
| 333 |
+
label="β° Timestamps",
|
| 334 |
+
interactive=False,
|
| 335 |
+
lines=6,
|
| 336 |
+
placeholder="Timestamp information will appear here..."
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
# Batch transcription tab
|
| 340 |
+
with gr.TabItem("π¦ Batch Transcription"):
|
| 341 |
+
with gr.Row():
|
| 342 |
+
batch_audio = gr.File(
|
| 343 |
+
label="π΅ Audio Files",
|
| 344 |
+
file_count="multiple",
|
| 345 |
+
file_types=["audio"]
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
with gr.Column():
|
| 349 |
+
batch_language = gr.Dropdown(
|
| 350 |
+
choices=[(desc, code) for code, desc in LANGUAGES.items()],
|
| 351 |
+
value="auto",
|
| 352 |
+
label="π Language for All"
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
batch_model = gr.Dropdown(
|
| 356 |
+
choices=[(desc, code) for code, desc in MODEL_SIZES],
|
| 357 |
+
value="base",
|
| 358 |
+
label="π€ Model Size"
|
| 359 |
+
)
|
| 360 |
+
|
| 361 |
+
batch_btn = gr.Button("π Transcribe Batch", variant="primary", size="lg")
|
| 362 |
+
batch_status = gr.Textbox(label="π Batch Status", interactive=False)
|
| 363 |
+
batch_results = gr.JSON(label="π Batch Results", show_label=True)
|
| 364 |
+
|
| 365 |
+
# Live transcription tab
|
| 366 |
+
with gr.TabItem("π΄ Live Transcription"):
|
| 367 |
+
gr.Markdown("""
|
| 368 |
+
## π΄ Real-time Speech Recognition
|
| 369 |
+
|
| 370 |
+
Use your microphone for continuous speech recognition:
|
| 371 |
+
|
| 372 |
+
1. **Click Start Recording** below
|
| 373 |
+
2. **Speak clearly** into your microphone
|
| 374 |
+
3. **Click Stop** to see transcription
|
| 375 |
+
4. **Use with WebRTC** for integration
|
| 376 |
+
""")
|
| 377 |
+
|
| 378 |
+
with gr.Row():
|
| 379 |
+
live_audio = gr.Audio(
|
| 380 |
+
label="ποΈ Live Audio Input",
|
| 381 |
+
sources=["microphone"],
|
| 382 |
+
type="filepath",
|
| 383 |
+
streaming=False
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
live_language = gr.Dropdown(
|
| 387 |
+
choices=[(desc, code) for code, desc in LANGUAGES.items()],
|
| 388 |
+
value="auto",
|
| 389 |
+
label="π Language"
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
live_transcribe_btn = gr.Button("π― Transcribe Live Audio", variant="primary")
|
| 393 |
+
live_output = gr.Textbox(label="π Live Transcription", lines=4)
|
| 394 |
+
|
| 395 |
+
# API Documentation tab
|
| 396 |
+
with gr.TabItem("π§ API Usage"):
|
| 397 |
+
gr.Markdown("""
|
| 398 |
+
## οΏ½οΏ½ API Access
|
| 399 |
+
|
| 400 |
+
Use this STT service programmatically:
|
| 401 |
+
|
| 402 |
+
```python
|
| 403 |
+
from gradio_client import Client
|
| 404 |
+
|
| 405 |
+
# Connect to your ZeroGPU STT service
|
| 406 |
+
client = Client("YOUR_USERNAME/stt-gpu-service")
|
| 407 |
+
|
| 408 |
+
# Transcribe audio file
|
| 409 |
+
result = client.predict(
|
| 410 |
+
"path/to/audio.wav", # audio file
|
| 411 |
+
"auto", # language (auto-detect)
|
| 412 |
+
"base", # model size
|
| 413 |
+
True, # include timestamps
|
| 414 |
+
api_name="/predict"
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
status, transcription, timestamps = result
|
| 418 |
+
print(f"Transcription: {transcription}")
|
| 419 |
+
```
|
| 420 |
+
|
| 421 |
+
### π ZeroGPU STT Benefits:
|
| 422 |
+
- **Real-time Factor**: Often <0.5x (faster than real-time)
|
| 423 |
+
- **Multi-language**: 100+ languages supported
|
| 424 |
+
- **High Accuracy**: Whisper state-of-the-art models
|
| 425 |
+
- **Batch Processing**: Multiple files in parallel
|
| 426 |
+
- **WebRTC Ready**: Integration with live audio streams
|
| 427 |
+
|
| 428 |
+
### π Performance Metrics:
|
| 429 |
+
- **Short audio (<30s)**: ~1-3 seconds processing
|
| 430 |
+
- **Long audio (>5min)**: Chunked processing with H200
|
| 431 |
+
- **Batch files**: Parallel execution for efficiency
|
| 432 |
+
- **Memory optimized**: Automatic cleanup between requests
|
| 433 |
+
|
| 434 |
+
### π Integration with TTS:
|
| 435 |
+
```python
|
| 436 |
+
# Complete voice pipeline
|
| 437 |
+
stt_client = Client("YOUR_USERNAME/stt-gpu-service")
|
| 438 |
+
tts_client = Client("YOUR_USERNAME/tts-gpu-service")
|
| 439 |
+
|
| 440 |
+
# Speech-to-text
|
| 441 |
+
_, transcription, _ = stt_client.predict(audio_file, "auto", "base", False)
|
| 442 |
+
|
| 443 |
+
# Process text (your logic here)
|
| 444 |
+
response_text = process_with_llm(transcription)
|
| 445 |
+
|
| 446 |
+
# Text-to-speech
|
| 447 |
+
audio_response, _ = tts_client.predict(response_text, "v2/en_speaker_6")
|
| 448 |
+
```
|
| 449 |
+
""")
|
| 450 |
+
|
| 451 |
+
# Examples with various audio types
|
| 452 |
+
gr.Examples(
|
| 453 |
+
examples=[
|
| 454 |
+
# You would need to upload sample audio files to your space
|
| 455 |
+
# ["sample_audio_en.wav", "en", "base", True],
|
| 456 |
+
# ["sample_audio_es.wav", "es", "base", True],
|
| 457 |
+
# ["sample_audio_long.wav", "auto", "small", True]
|
| 458 |
+
],
|
| 459 |
+
inputs=[audio_input, language_dropdown, model_dropdown, timestamps_checkbox],
|
| 460 |
+
outputs=[status_output, transcription_output, timestamps_output],
|
| 461 |
+
fn=transcribe_audio,
|
| 462 |
+
cache_examples=False,
|
| 463 |
+
label="π― Audio Examples"
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
# Event handlers
|
| 467 |
+
transcribe_btn.click(
|
| 468 |
+
fn=transcribe_audio,
|
| 469 |
+
inputs=[audio_input, language_dropdown, model_dropdown, timestamps_checkbox],
|
| 470 |
+
outputs=[status_output, transcription_output, timestamps_output]
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
clear_btn.click(
|
| 474 |
+
fn=lambda: (None, "", "", ""),
|
| 475 |
+
outputs=[audio_input, transcription_output, timestamps_output, status_output]
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
+
live_transcribe_btn.click(
|
| 479 |
+
fn=lambda audio, lang: transcribe_audio(audio, lang, "base", False),
|
| 480 |
+
inputs=[live_audio, live_language],
|
| 481 |
+
outputs=[live_output]
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
# Batch processing
|
| 485 |
+
def process_batch_files(files, language, model):
|
| 486 |
+
"""Process uploaded batch files"""
|
| 487 |
+
if not files:
|
| 488 |
+
return "β No files uploaded", []
|
| 489 |
+
|
| 490 |
+
file_paths = [f.name for f in files]
|
| 491 |
+
return batch_transcribe(file_paths, language, model)
|
| 492 |
+
|
| 493 |
+
batch_btn.click(
|
| 494 |
+
fn=process_batch_files,
|
| 495 |
+
inputs=[batch_audio, batch_language, batch_model],
|
| 496 |
+
outputs=[batch_status, batch_results]
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
+
# Auto-refresh system info
|
| 500 |
+
iface.load(
|
| 501 |
+
fn=get_system_info,
|
| 502 |
+
outputs=[system_info],
|
| 503 |
+
every=30
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
# Launch the STT app optimized for ZeroGPU
|
| 507 |
+
if __name__ == "__main__":
|
| 508 |
+
iface.launch(
|
| 509 |
+
server_name="0.0.0.0",
|
| 510 |
+
server_port=7860,
|
| 511 |
+
share=False,
|
| 512 |
+
show_error=True
|
| 513 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.1.0
|
| 2 |
+
torchaudio>=2.1.0
|
| 3 |
+
transformers>=4.35.0
|
| 4 |
+
accelerate>=0.24.0
|
| 5 |
+
gradio>=4.8.0
|
| 6 |
+
spaces>=0.19.0
|
| 7 |
+
numpy>=1.21.0
|
| 8 |
+
soundfile>=0.12.0
|
| 9 |
+
librosa>=0.9.0
|
| 10 |
+
huggingface_hub>=0.19.0
|
| 11 |
+
datasets>=2.14.0
|
| 12 |
+
openai-whisper>=20231117
|