Deaconhead commited on
Commit
ec2e8a0
·
verified ·
1 Parent(s): 2436063

Initial public CPU Whisper small STT space

Browse files
Files changed (4) hide show
  1. README.md +11 -5
  2. __pycache__/app.cpython-314.pyc +0 -0
  3. app.py +86 -0
  4. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1,18 @@
1
  ---
2
  title: Whisper Small STT CPU
3
- emoji: 🐠
4
- colorFrom: purple
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 6.12.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
1
  ---
2
  title: Whisper Small STT CPU
3
+ emoji: 🎙️
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.25.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ # Whisper Small STT (Free CPU)
13
+
14
+ Public speech-to-text Space powered by `openai/whisper-small` on free CPU hardware.
15
+
16
+ - Upload audio or record with your microphone
17
+ - Choose `transcribe` (same language) or `translate` (to English)
18
+ - Includes a public API endpoint: `/transcribe`
__pycache__/app.cpython-314.pyc ADDED
Binary file (4.2 kB). View file
 
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import gradio as gr
4
+ from transformers import WhisperForConditionalGeneration, WhisperProcessor
5
+
6
+ MODEL_ID = "openai/whisper-small"
7
+
8
+ processor = WhisperProcessor.from_pretrained(MODEL_ID)
9
+ model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
10
+ model.eval()
11
+
12
+
13
+ def _to_float32_audio(audio: tuple[int, np.ndarray]) -> tuple[int, np.ndarray]:
14
+ sample_rate, data = audio
15
+ if data.ndim > 1:
16
+ data = data.mean(axis=1)
17
+
18
+ if np.issubdtype(data.dtype, np.integer):
19
+ max_int = np.iinfo(data.dtype).max
20
+ data = data.astype(np.float32) / float(max_int)
21
+ else:
22
+ data = data.astype(np.float32)
23
+ peak = np.max(np.abs(data)) if data.size else 0.0
24
+ if peak > 1.0:
25
+ data = data / peak
26
+
27
+ return sample_rate, data
28
+
29
+
30
+ def transcribe_audio(audio, task):
31
+ if audio is None:
32
+ return "Please upload or record audio first."
33
+
34
+ sample_rate, data = _to_float32_audio(audio)
35
+
36
+ inputs = processor(
37
+ data,
38
+ sampling_rate=sample_rate,
39
+ return_tensors="pt",
40
+ )
41
+
42
+ forced_decoder_ids = processor.get_decoder_prompt_ids(task=task)
43
+
44
+ with torch.inference_mode():
45
+ predicted_ids = model.generate(
46
+ inputs.input_features,
47
+ forced_decoder_ids=forced_decoder_ids,
48
+ )
49
+
50
+ text = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0].strip()
51
+ return text or "(No speech detected)"
52
+
53
+
54
+ with gr.Blocks(title="Whisper Small STT CPU") as demo:
55
+ gr.Markdown(
56
+ """
57
+ # Whisper Small STT (Free CPU)
58
+ Public speech-to-text using `openai/whisper-small`.
59
+
60
+ - `transcribe`: keep original language
61
+ - `translate`: translate speech to English
62
+ """
63
+ )
64
+
65
+ audio_input = gr.Audio(
66
+ label="Audio Input",
67
+ type="numpy",
68
+ sources=["upload", "microphone"],
69
+ )
70
+ task_input = gr.Dropdown(
71
+ choices=["transcribe", "translate"],
72
+ value="transcribe",
73
+ label="Task",
74
+ )
75
+ run_btn = gr.Button("Convert Speech to Text", variant="primary")
76
+ text_output = gr.Textbox(label="Transcript", lines=12)
77
+
78
+ run_btn.click(
79
+ fn=transcribe_audio,
80
+ inputs=[audio_input, task_input],
81
+ outputs=[text_output],
82
+ api_name="transcribe",
83
+ )
84
+
85
+ if __name__ == "__main__":
86
+ demo.queue(default_concurrency_limit=1).launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=5.25.0
2
+ transformers>=4.46.0
3
+ torch
4
+ numpy