Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import os
|
| 3 |
import time
|
| 4 |
import traceback
|
| 5 |
-
import math
|
| 6 |
-
import time as _time
|
| 7 |
|
| 8 |
import torch
|
| 9 |
import gradio as gr
|
| 10 |
from transformers import pipeline
|
| 11 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 12 |
|
| 13 |
-
# Use
|
| 14 |
MODEL_NAME = "openai/whisper-small"
|
| 15 |
-
BATCH_SIZE = 2
|
| 16 |
|
| 17 |
# device for transformers pipeline: 0 for GPU, -1 for CPU
|
| 18 |
device = 0 if torch.cuda.is_available() else -1
|
|
@@ -21,14 +18,14 @@ device = 0 if torch.cuda.is_available() else -1
|
|
| 21 |
pipe = pipeline(
|
| 22 |
task="automatic-speech-recognition",
|
| 23 |
model=MODEL_NAME,
|
| 24 |
-
chunk_length_s=60, # process <=59s clips in one chunk when used as whole;
|
| 25 |
device=device,
|
| 26 |
ignore_warning=True,
|
| 27 |
)
|
| 28 |
|
| 29 |
# Chunking settings (tweakable)
|
| 30 |
CHUNK_SEC = 8.0 # seconds per chunk (increase to reduce boundaries)
|
| 31 |
-
OVERLAP_SEC = 0.01
|
| 32 |
SLEEP_BETWEEN_CHUNKS = 0.01
|
| 33 |
|
| 34 |
def _merge_with_overlap(accumulated: str, chunk_text: str, max_overlap_words: int = 10) -> str:
|
|
@@ -40,21 +37,18 @@ def _merge_with_overlap(accumulated: str, chunk_text: str, max_overlap_words: in
|
|
| 40 |
if not accumulated:
|
| 41 |
return chunk_text
|
| 42 |
|
| 43 |
-
# simple whitespace tokenization
|
| 44 |
acc_words = accumulated.strip().split()
|
| 45 |
chunk_words = chunk_text.strip().split()
|
| 46 |
if not chunk_words:
|
| 47 |
return accumulated
|
| 48 |
|
| 49 |
max_k = min(max_overlap_words, len(acc_words), len(chunk_words))
|
| 50 |
-
# find largest k where last k words of acc == first k words of chunk
|
| 51 |
overlap_k = 0
|
| 52 |
for k in range(max_k, 0, -1):
|
| 53 |
if acc_words[-k:] == chunk_words[:k]:
|
| 54 |
overlap_k = k
|
| 55 |
break
|
| 56 |
|
| 57 |
-
# join: accumulated + remaining chunk words after overlap
|
| 58 |
if overlap_k > 0:
|
| 59 |
merged_words = acc_words + chunk_words[overlap_k:]
|
| 60 |
else:
|
|
@@ -66,10 +60,6 @@ def transcribe_file(filepath: str, task: str):
|
|
| 66 |
"""
|
| 67 |
Incremental transcription generator with overlap + dedupe merge.
|
| 68 |
Yields the accumulated transcript string after each processed chunk.
|
| 69 |
-
Uses globals:
|
| 70 |
-
- pipe (transformers/faster-whisper pipeline)
|
| 71 |
-
- CHUNK_SEC, OVERLAP_SEC, SLEEP_BETWEEN_CHUNKS
|
| 72 |
-
- _merge_with_overlap(accumulated, chunk_text)
|
| 73 |
"""
|
| 74 |
if not filepath:
|
| 75 |
raise gr.Error("No audio file submitted! Please upload or record audio (max 59s recommended).")
|
|
@@ -93,17 +83,15 @@ def transcribe_file(filepath: str, task: str):
|
|
| 93 |
chunk_samples = max(1, int(CHUNK_SEC * sr))
|
| 94 |
overlap_samples = int(OVERLAP_SEC * sr)
|
| 95 |
|
| 96 |
-
# Number of
|
| 97 |
num_chunks = max(1, (total_samples + chunk_samples - 1) // chunk_samples)
|
| 98 |
|
| 99 |
accumulated_text = ""
|
| 100 |
|
| 101 |
for i in range(num_chunks):
|
| 102 |
-
# base start/end for this logical chunk
|
| 103 |
start = i * chunk_samples
|
| 104 |
end = min((i + 1) * chunk_samples, total_samples)
|
| 105 |
|
| 106 |
-
# apply overlap (extend start backward except first chunk; extend end forward except last chunk)
|
| 107 |
if i > 0:
|
| 108 |
start = max(0, start - overlap_samples)
|
| 109 |
if i < num_chunks - 1:
|
|
@@ -113,18 +101,15 @@ def transcribe_file(filepath: str, task: str):
|
|
| 113 |
if chunk_array.size == 0:
|
| 114 |
continue
|
| 115 |
|
| 116 |
-
# Run ASR on this chunk (fast/greedy)
|
| 117 |
inputs = {"array": chunk_array, "sampling_rate": sr}
|
| 118 |
result = pipe(inputs, batch_size=1, generate_kwargs={"task": task})
|
| 119 |
chunk_text = result.get("text", "").strip()
|
| 120 |
|
| 121 |
-
# Merge with overlap deduplication
|
| 122 |
accumulated_text = _merge_with_overlap(accumulated_text, chunk_text)
|
| 123 |
|
| 124 |
# yield partial accumulated transcript so UI updates progressively
|
| 125 |
yield accumulated_text
|
| 126 |
|
| 127 |
-
# short pause to let the frontend update smoothly
|
| 128 |
time.sleep(SLEEP_BETWEEN_CHUNKS)
|
| 129 |
|
| 130 |
# Ensure final result is emitted
|
|
@@ -134,61 +119,17 @@ def transcribe_file(filepath: str, task: str):
|
|
| 134 |
tb = traceback.format_exc()
|
| 135 |
raise gr.Error(f"Transcription failed: {e}\n{tb}")
|
| 136 |
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
chat_state = chat_state + [{"role": "user", "content": user_message}]
|
| 143 |
-
assistant_reply = (
|
| 144 |
-
"🟡 [UI only] This is a local Chat UI placeholder. "
|
| 145 |
-
"To enable real ChatGPT-style replies, integrate an API (OpenAI/HF) and replace this function."
|
| 146 |
-
)
|
| 147 |
-
chat_state = chat_state + [{"role": "assistant", "content": assistant_reply}]
|
| 148 |
-
return "", chat_state, chat_state
|
| 149 |
-
|
| 150 |
-
# --- Build Gradio app using Blocks ---
|
| 151 |
-
with gr.Blocks(title="Health Voice-Based Assistant — STT + Chat UI") as demo:
|
| 152 |
-
gr.Markdown("# Health Voice-Based Assistant\nUpload audio or record to transcribe speech to text using Whisper-tiny (incremental).")
|
| 153 |
-
|
| 154 |
-
with gr.Row():
|
| 155 |
-
with gr.Column(scale=2):
|
| 156 |
-
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload / Record audio")
|
| 157 |
-
task_radio = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 158 |
-
transcribe_btn = gr.Button("Transcribe uploaded audio")
|
| 159 |
-
output_text = gr.Textbox(label="Transcription", lines=10)
|
| 160 |
-
with gr.Column(scale=1):
|
| 161 |
-
# Collapsible disclaimer for medical assistant
|
| 162 |
-
with gr.Accordion("Disclaimer & Safety (click to open)", open=False):
|
| 163 |
-
gr.Markdown(
|
| 164 |
-
"""
|
| 165 |
-
**Important — Medical disclaimer**
|
| 166 |
-
|
| 167 |
-
- This assistant is **not** a medical professional and **must not** be used as a substitute for professional medical advice, diagnosis, or treatment.
|
| 168 |
-
- It **cannot** prescribe medications, create personalized treatment plans, or make emergency decisions.
|
| 169 |
-
- Always consult a licensed healthcare provider for medical concerns. If you or someone is experiencing a medical emergency, call local emergency services immediately.
|
| 170 |
-
- Transcriptions may contain errors — verify important clinical details from the original audio and consult professionals before acting.
|
| 171 |
-
"""
|
| 172 |
-
)
|
| 173 |
-
gr.Markdown("**Quick tips**\n- For best results, use short clips (<= 59 seconds) and speak clearly.\n- Incremental mode streams partial transcripts as they become available.")
|
| 174 |
-
gr.Markdown(f"**Model checkpoint:** [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME})")
|
| 175 |
|
| 176 |
# Wire up button (transcribe_file is a generator that yields intermediate strings)
|
| 177 |
transcribe_btn.click(fn=transcribe_file, inputs=[audio_input, task_radio], outputs=output_text)
|
| 178 |
|
| 179 |
-
gr.Markdown("---\n## Chat (UI-only, no model integrated)")
|
| 180 |
-
chatbot = gr.Chatbot(label="Assistant chat (UI-only)", height=300, type="messages")
|
| 181 |
-
chat_input = gr.Textbox(placeholder="Type your message here...", lines=2)
|
| 182 |
-
send_btn = gr.Button("Send")
|
| 183 |
-
chat_state = gr.State([])
|
| 184 |
-
|
| 185 |
-
send_btn.click(fn=local_chat_send, inputs=[chat_input, chat_state], outputs=[chat_input, chatbot, chat_state])
|
| 186 |
-
|
| 187 |
-
gr.Markdown("**Notes for integration**: Replace `local_chat_send` with a function that calls your chat model or external API (OpenAI / HF Inference). Store API keys securely (do not hardcode).")
|
| 188 |
-
|
| 189 |
if __name__ == "__main__":
|
| 190 |
-
demo.launch(
|
| 191 |
-
server_name="0.0.0.0",
|
| 192 |
-
server_port=7860,
|
| 193 |
-
ssr_mode=False
|
| 194 |
-
)
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import time
|
| 3 |
import traceback
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import torch
|
| 6 |
import gradio as gr
|
| 7 |
from transformers import pipeline
|
| 8 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 9 |
|
| 10 |
+
# Use Whisper-small for inference (faster than full models).
|
| 11 |
MODEL_NAME = "openai/whisper-small"
|
| 12 |
+
BATCH_SIZE = 2
|
| 13 |
|
| 14 |
# device for transformers pipeline: 0 for GPU, -1 for CPU
|
| 15 |
device = 0 if torch.cuda.is_available() else -1
|
|
|
|
| 18 |
pipe = pipeline(
|
| 19 |
task="automatic-speech-recognition",
|
| 20 |
model=MODEL_NAME,
|
| 21 |
+
chunk_length_s=60, # process <=59s clips in one chunk when used as whole; we still do our own chunking
|
| 22 |
device=device,
|
| 23 |
ignore_warning=True,
|
| 24 |
)
|
| 25 |
|
| 26 |
# Chunking settings (tweakable)
|
| 27 |
CHUNK_SEC = 8.0 # seconds per chunk (increase to reduce boundaries)
|
| 28 |
+
OVERLAP_SEC = 0.01 # seconds to overlap between chunks (captures cut words)
|
| 29 |
SLEEP_BETWEEN_CHUNKS = 0.01
|
| 30 |
|
| 31 |
def _merge_with_overlap(accumulated: str, chunk_text: str, max_overlap_words: int = 10) -> str:
|
|
|
|
| 37 |
if not accumulated:
|
| 38 |
return chunk_text
|
| 39 |
|
|
|
|
| 40 |
acc_words = accumulated.strip().split()
|
| 41 |
chunk_words = chunk_text.strip().split()
|
| 42 |
if not chunk_words:
|
| 43 |
return accumulated
|
| 44 |
|
| 45 |
max_k = min(max_overlap_words, len(acc_words), len(chunk_words))
|
|
|
|
| 46 |
overlap_k = 0
|
| 47 |
for k in range(max_k, 0, -1):
|
| 48 |
if acc_words[-k:] == chunk_words[:k]:
|
| 49 |
overlap_k = k
|
| 50 |
break
|
| 51 |
|
|
|
|
| 52 |
if overlap_k > 0:
|
| 53 |
merged_words = acc_words + chunk_words[overlap_k:]
|
| 54 |
else:
|
|
|
|
| 60 |
"""
|
| 61 |
Incremental transcription generator with overlap + dedupe merge.
|
| 62 |
Yields the accumulated transcript string after each processed chunk.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
"""
|
| 64 |
if not filepath:
|
| 65 |
raise gr.Error("No audio file submitted! Please upload or record audio (max 59s recommended).")
|
|
|
|
| 83 |
chunk_samples = max(1, int(CHUNK_SEC * sr))
|
| 84 |
overlap_samples = int(OVERLAP_SEC * sr)
|
| 85 |
|
| 86 |
+
# Number of logical chunks
|
| 87 |
num_chunks = max(1, (total_samples + chunk_samples - 1) // chunk_samples)
|
| 88 |
|
| 89 |
accumulated_text = ""
|
| 90 |
|
| 91 |
for i in range(num_chunks):
|
|
|
|
| 92 |
start = i * chunk_samples
|
| 93 |
end = min((i + 1) * chunk_samples, total_samples)
|
| 94 |
|
|
|
|
| 95 |
if i > 0:
|
| 96 |
start = max(0, start - overlap_samples)
|
| 97 |
if i < num_chunks - 1:
|
|
|
|
| 101 |
if chunk_array.size == 0:
|
| 102 |
continue
|
| 103 |
|
|
|
|
| 104 |
inputs = {"array": chunk_array, "sampling_rate": sr}
|
| 105 |
result = pipe(inputs, batch_size=1, generate_kwargs={"task": task})
|
| 106 |
chunk_text = result.get("text", "").strip()
|
| 107 |
|
|
|
|
| 108 |
accumulated_text = _merge_with_overlap(accumulated_text, chunk_text)
|
| 109 |
|
| 110 |
# yield partial accumulated transcript so UI updates progressively
|
| 111 |
yield accumulated_text
|
| 112 |
|
|
|
|
| 113 |
time.sleep(SLEEP_BETWEEN_CHUNKS)
|
| 114 |
|
| 115 |
# Ensure final result is emitted
|
|
|
|
| 119 |
tb = traceback.format_exc()
|
| 120 |
raise gr.Error(f"Transcription failed: {e}\n{tb}")
|
| 121 |
|
| 122 |
+
# --- Minimal Gradio app: only record + transcribe ---
|
| 123 |
+
with gr.Blocks(title="Record Option") as demo:
|
| 124 |
+
gr.Markdown("# Record Option\nRecord or upload audio and transcribe incrementally using Whisper.")
|
| 125 |
|
| 126 |
+
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload / Record audio")
|
| 127 |
+
task_radio = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 128 |
+
transcribe_btn = gr.Button("Transcribe")
|
| 129 |
+
output_text = gr.Textbox(label="Transcription", lines=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# Wire up button (transcribe_file is a generator that yields intermediate strings)
|
| 132 |
transcribe_btn.click(fn=transcribe_file, inputs=[audio_input, task_radio], outputs=output_text)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
|
|
|
|
|
|
|
|
|
|
|
|