Spaces:
Sleeping
Sleeping
prianshujha commited on
Commit Β·
83c1aed
1
Parent(s): 3e3df89
made changes to allow transcribe
Browse files- .gitattributes +1 -0
- app.py +5 -0
- config.py +3 -5
- data/calls.db +0 -0
- pipeline/evaluater.py +48 -32
- pipeline/intent_parser.py +7 -24
- pipeline/transcriber.py +8 -0
- requirements.txt +3 -3
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
models/* filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -48,6 +48,11 @@ logging.basicConfig(
|
|
| 48 |
)
|
| 49 |
logger = logging.getLogger(__name__)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Initialize database on startup
|
| 52 |
init_db()
|
| 53 |
|
|
|
|
| 48 |
)
|
| 49 |
logger = logging.getLogger(__name__)
|
| 50 |
|
| 51 |
+
# Suppress verbose logs from HuggingFace hub
|
| 52 |
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
| 53 |
+
logging.getLogger("huggingface_hub").setLevel(logging.WARNING)
|
| 54 |
+
logging.getLogger("transformers.modeling_utils").setLevel(logging.WARNING)
|
| 55 |
+
|
| 56 |
# Initialize database on startup
|
| 57 |
init_db()
|
| 58 |
|
config.py
CHANGED
|
@@ -26,11 +26,9 @@ TRANSCRIBE_LANGUAGE = "en" # ISO 639-1 β English only for now
|
|
| 26 |
TRANSCRIBE_DEVICE = "cuda:0" # falls back to "cpu" automatically in code
|
| 27 |
|
| 28 |
# ββ Intent Parser: Qwen2.5-7B-Instruct (GGUF via llama-cpp-python) ββββββββββββ
|
| 29 |
-
#
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
# --local-dir ./models
|
| 33 |
-
QWEN_GGUF_PATH = MODELS_DIR / "qwen2.5-7b-instruct-q4_k_m.gguf"
|
| 34 |
QWEN_N_GPU_LAYERS = 20 # offload 20 transformer layers to GPU (~0.8 GB VRAM)
|
| 35 |
# remaining ~15 layers run on CPU RAM
|
| 36 |
QWEN_N_CTX = 4096 # context window β sufficient for a call transcript
|
|
|
|
| 26 |
TRANSCRIBE_DEVICE = "cuda:0" # falls back to "cpu" automatically in code
|
| 27 |
|
| 28 |
# ββ Intent Parser: Qwen2.5-7B-Instruct (GGUF via llama-cpp-python) ββββββββββββ
|
| 29 |
+
# Using q3_k_m quantization (3.55 GB, good quality/size tradeoff).
|
| 30 |
+
# Already downloaded and available in ./models/
|
| 31 |
+
QWEN_GGUF_PATH = MODELS_DIR / "qwen2.5-7b-instruct-q3_k_m.gguf"
|
|
|
|
|
|
|
| 32 |
QWEN_N_GPU_LAYERS = 20 # offload 20 transformer layers to GPU (~0.8 GB VRAM)
|
| 33 |
# remaining ~15 layers run on CPU RAM
|
| 34 |
QWEN_N_CTX = 4096 # context window β sufficient for a call transcript
|
data/calls.db
CHANGED
|
Binary files a/data/calls.db and b/data/calls.db differ
|
|
|
pipeline/evaluater.py
CHANGED
|
@@ -142,6 +142,11 @@ class Evaluator:
|
|
| 142 |
return fast
|
| 143 |
|
| 144 |
self._ensure_loaded()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
prompt = self._build_prompt(intent, utterances, slot_available, slot_conflicts or [])
|
| 147 |
|
|
@@ -212,44 +217,55 @@ class Evaluator:
|
|
| 212 |
logger.info(f"Loading MiniCPM3-4B ({MINICPM_MODEL_ID}) on CPUβ¦")
|
| 213 |
t0 = time.perf_counter()
|
| 214 |
|
| 215 |
-
self._tokenizer = AutoTokenizer.from_pretrained(
|
| 216 |
-
MINICPM_MODEL_ID,
|
| 217 |
-
trust_remote_code=True,
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
# Try INT4 via bitsandbytes first; fall back to float32
|
| 221 |
try:
|
| 222 |
-
|
| 223 |
-
bnb_config = BitsAndBytesConfig(
|
| 224 |
-
load_in_4bit = True,
|
| 225 |
-
bnb_4bit_compute_dtype = torch.float32,
|
| 226 |
-
bnb_4bit_use_double_quant = True,
|
| 227 |
-
bnb_4bit_quant_type = "nf4",
|
| 228 |
-
)
|
| 229 |
-
self._model = AutoModelForCausalLM.from_pretrained(
|
| 230 |
MINICPM_MODEL_ID,
|
| 231 |
-
|
| 232 |
-
trust_remote_code = True,
|
| 233 |
-
device_map = "cpu",
|
| 234 |
-
low_cpu_mem_usage = True,
|
| 235 |
)
|
| 236 |
-
logger.info("MiniCPM3 loaded in INT4 (bitsandbytes NF4)")
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
-
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
def _build_prompt(
|
| 255 |
self,
|
|
|
|
| 142 |
return fast
|
| 143 |
|
| 144 |
self._ensure_loaded()
|
| 145 |
+
|
| 146 |
+
# If model failed to load, use fallback result
|
| 147 |
+
if self._model is None:
|
| 148 |
+
logger.debug("MiniCPM3 disabled β using fallback result")
|
| 149 |
+
return self._fallback_result(intent)
|
| 150 |
|
| 151 |
prompt = self._build_prompt(intent, utterances, slot_available, slot_conflicts or [])
|
| 152 |
|
|
|
|
| 217 |
logger.info(f"Loading MiniCPM3-4B ({MINICPM_MODEL_ID}) on CPUβ¦")
|
| 218 |
t0 = time.perf_counter()
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
try:
|
| 221 |
+
self._tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
MINICPM_MODEL_ID,
|
| 223 |
+
trust_remote_code=True,
|
|
|
|
|
|
|
|
|
|
| 224 |
)
|
|
|
|
| 225 |
|
| 226 |
+
# Try INT4 via bitsandbytes first; fall back to float32
|
| 227 |
+
try:
|
| 228 |
+
from transformers import BitsAndBytesConfig
|
| 229 |
+
bnb_config = BitsAndBytesConfig(
|
| 230 |
+
load_in_4bit = True,
|
| 231 |
+
bnb_4bit_compute_dtype = torch.float32,
|
| 232 |
+
bnb_4bit_use_double_quant = True,
|
| 233 |
+
bnb_4bit_quant_type = "nf4",
|
| 234 |
+
)
|
| 235 |
+
self._model = AutoModelForCausalLM.from_pretrained(
|
| 236 |
+
MINICPM_MODEL_ID,
|
| 237 |
+
quantization_config = bnb_config,
|
| 238 |
+
trust_remote_code = True,
|
| 239 |
+
device_map = "cpu",
|
| 240 |
+
low_cpu_mem_usage = True,
|
| 241 |
+
)
|
| 242 |
+
logger.info("MiniCPM3 loaded in INT4 (bitsandbytes NF4)")
|
| 243 |
+
|
| 244 |
+
except (ImportError, Exception) as e:
|
| 245 |
+
logger.warning(f"bitsandbytes INT4 failed ({e}) β loading float32 on CPU")
|
| 246 |
+
self._model = AutoModelForCausalLM.from_pretrained(
|
| 247 |
+
MINICPM_MODEL_ID,
|
| 248 |
+
torch_dtype = torch.float32,
|
| 249 |
+
trust_remote_code = True,
|
| 250 |
+
low_cpu_mem_usage = True,
|
| 251 |
+
)
|
| 252 |
+
self._model.to("cpu")
|
| 253 |
|
| 254 |
+
self._model.eval()
|
| 255 |
|
| 256 |
+
elapsed = time.perf_counter() - t0
|
| 257 |
+
logger.info(f"MiniCPM3 ready in {elapsed:.1f}s")
|
| 258 |
+
self._loaded = True
|
| 259 |
+
|
| 260 |
+
except Exception as e:
|
| 261 |
+
logger.error(
|
| 262 |
+
f"MiniCPM3 failed to load: {e}\n"
|
| 263 |
+
f"MiniCPM3 will be DISABLED. The pipeline will still work for transcription and intent parsing."
|
| 264 |
+
)
|
| 265 |
+
# Mark as loaded but with model=None so evaluate() knows to skip
|
| 266 |
+
self._loaded = True
|
| 267 |
+
self._model = None
|
| 268 |
+
self._tokenizer = None
|
| 269 |
|
| 270 |
def _build_prompt(
|
| 271 |
self,
|
pipeline/intent_parser.py
CHANGED
|
@@ -128,42 +128,25 @@ class SchedulingIntent(BaseModel):
|
|
| 128 |
# llama.cpp samples tokens that satisfy this grammar at every step.
|
| 129 |
|
| 130 |
_SCHEDULING_GRAMMAR = r"""
|
| 131 |
-
root ::=
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
object ::=
|
| 135 |
-
"{" ws
|
| 136 |
-
"\"intent\"" ws ":" ws intent-val ws "," ws
|
| 137 |
-
"\"caller_name\"" ws ":" ws str-or-null ws "," ws
|
| 138 |
-
"\"preferred_date\"" ws ":" ws str-or-null ws "," ws
|
| 139 |
-
"\"preferred_time\"" ws ":" ws str-or-null ws "," ws
|
| 140 |
-
"\"duration_minutes\"" ws ":" ws int-or-null ws "," ws
|
| 141 |
-
"\"participants\"" ws ":" ws str-array ws "," ws
|
| 142 |
-
"\"meeting_type\"" ws ":" ws meeting-type-val ws "," ws
|
| 143 |
-
"\"notes\"" ws ":" ws str-or-null ws "," ws
|
| 144 |
-
"\"confidence\"" ws ":" ws confidence-val ws "," ws
|
| 145 |
-
"\"missing_fields\"" ws ":" ws str-array
|
| 146 |
-
ws "}"
|
| 147 |
-
|
| 148 |
-
intent-val ::= "\"book_meeting\"" | "\"reschedule\"" | "\"cancel\"" |
|
| 149 |
-
"\"check_availability\"" | "\"provide_info\"" |
|
| 150 |
-
"\"end_call\"" | "\"unclear\""
|
| 151 |
|
| 152 |
meeting-type-val ::= "\"phone\"" | "\"video\"" | "\"in_person\"" | "null"
|
| 153 |
|
| 154 |
str-or-null ::= string | "null"
|
| 155 |
int-or-null ::= integer | "null"
|
| 156 |
|
| 157 |
-
confidence-val ::= "0" | "1" | ("0" "." [0-9]+) | ("1" "."
|
| 158 |
|
| 159 |
str-array ::= "[" ws "]" | "[" ws string (ws "," ws string)* ws "]"
|
| 160 |
|
| 161 |
-
string ::= "\"" ([^"\\] | "\\"
|
| 162 |
-
integer ::= "-"? [0-9]+
|
| 163 |
number ::= integer ("." [0-9]+)?
|
| 164 |
bool ::= "true" | "false"
|
| 165 |
null ::= "null"
|
| 166 |
-
array ::= "[" ws (
|
| 167 |
ws ::= [ \t\n\r]*
|
| 168 |
"""
|
| 169 |
|
|
|
|
| 128 |
# llama.cpp samples tokens that satisfy this grammar at every step.
|
| 129 |
|
| 130 |
_SCHEDULING_GRAMMAR = r"""
|
| 131 |
+
root ::= ws "{" ws "\"intent\"" ws ":" ws intent-val ws "," ws "\"caller_name\"" ws ":" ws str-or-null ws "," ws "\"preferred_date\"" ws ":" ws str-or-null ws "," ws "\"preferred_time\"" ws ":" ws str-or-null ws "," ws "\"duration_minutes\"" ws ":" ws int-or-null ws "," ws "\"participants\"" ws ":" ws str-array ws "," ws "\"meeting_type\"" ws ":" ws meeting-type-val ws "," ws "\"notes\"" ws ":" ws str-or-null ws "," ws "\"confidence\"" ws ":" ws confidence-val ws "," ws "\"missing_fields\"" ws ":" ws str-array ws "}" ws
|
| 132 |
+
|
| 133 |
+
intent-val ::= "\"book_meeting\"" | "\"reschedule\"" | "\"cancel\"" | "\"check_availability\"" | "\"provide_info\"" | "\"end_call\"" | "\"unclear\""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
meeting-type-val ::= "\"phone\"" | "\"video\"" | "\"in_person\"" | "null"
|
| 136 |
|
| 137 |
str-or-null ::= string | "null"
|
| 138 |
int-or-null ::= integer | "null"
|
| 139 |
|
| 140 |
+
confidence-val ::= ("0" | "1") | ("0" "." [0-9]+) | ("1" "." [0]*)
|
| 141 |
|
| 142 |
str-array ::= "[" ws "]" | "[" ws string (ws "," ws string)* ws "]"
|
| 143 |
|
| 144 |
+
string ::= "\"" ([^"\\] | "\\\\" ["\\\\/bfnrt] | "\\u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])* "\""
|
| 145 |
+
integer ::= ("-"?) [0-9]+
|
| 146 |
number ::= integer ("." [0-9]+)?
|
| 147 |
bool ::= "true" | "false"
|
| 148 |
null ::= "null"
|
| 149 |
+
array ::= "[" ws (number (ws "," ws number)*)? ws "]"
|
| 150 |
ws ::= [ \t\n\r]*
|
| 151 |
"""
|
| 152 |
|
pipeline/transcriber.py
CHANGED
|
@@ -216,17 +216,24 @@ class Transcriber:
|
|
| 216 |
- float16 on CUDA: ~2.2 GB VRAM β preferred
|
| 217 |
- float32 on CPU: ~8.0 GB RAM β fallback
|
| 218 |
"""
|
|
|
|
| 219 |
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 220 |
|
| 221 |
logger.info(f"Loading Cohere Transcribe ({TRANSCRIBE_MODEL_ID})β¦")
|
| 222 |
t0 = time.perf_counter()
|
| 223 |
|
| 224 |
token_kwargs = {"token": HF_TOKEN} if HF_TOKEN else {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# Processor (tokenizer + feature extractor) β tiny, load first
|
| 227 |
self._processor = AutoProcessor.from_pretrained(
|
| 228 |
TRANSCRIBE_MODEL_ID,
|
| 229 |
trust_remote_code=True,
|
|
|
|
| 230 |
**token_kwargs,
|
| 231 |
)
|
| 232 |
|
|
@@ -251,6 +258,7 @@ class Transcriber:
|
|
| 251 |
trust_remote_code = True,
|
| 252 |
torch_dtype = dtype,
|
| 253 |
low_cpu_mem_usage = True, # stream weights instead of double-buffering
|
|
|
|
| 254 |
**token_kwargs,
|
| 255 |
).to(self._device)
|
| 256 |
|
|
|
|
| 216 |
- float16 on CUDA: ~2.2 GB VRAM β preferred
|
| 217 |
- float32 on CPU: ~8.0 GB RAM β fallback
|
| 218 |
"""
|
| 219 |
+
import os
|
| 220 |
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 221 |
|
| 222 |
logger.info(f"Loading Cohere Transcribe ({TRANSCRIBE_MODEL_ID})β¦")
|
| 223 |
t0 = time.perf_counter()
|
| 224 |
|
| 225 |
token_kwargs = {"token": HF_TOKEN} if HF_TOKEN else {}
|
| 226 |
+
|
| 227 |
+
# After first download, use local cache only to avoid repeated HF hub calls
|
| 228 |
+
local_only = os.path.exists(
|
| 229 |
+
os.path.expanduser(f"~/.cache/huggingface/hub/models--{TRANSCRIBE_MODEL_ID.replace('/', '--')}")
|
| 230 |
+
)
|
| 231 |
|
| 232 |
# Processor (tokenizer + feature extractor) β tiny, load first
|
| 233 |
self._processor = AutoProcessor.from_pretrained(
|
| 234 |
TRANSCRIBE_MODEL_ID,
|
| 235 |
trust_remote_code=True,
|
| 236 |
+
local_files_only=local_only,
|
| 237 |
**token_kwargs,
|
| 238 |
)
|
| 239 |
|
|
|
|
| 258 |
trust_remote_code = True,
|
| 259 |
torch_dtype = dtype,
|
| 260 |
low_cpu_mem_usage = True, # stream weights instead of double-buffering
|
| 261 |
+
local_files_only = local_only,
|
| 262 |
**token_kwargs,
|
| 263 |
).to(self._device)
|
| 264 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# ββ Core ML / inference βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
-
torch=
|
| 3 |
-
torchaudio=
|
| 4 |
transformers>=4.56,<5.3,!=5.0.*,!=5.1.* # Cohere Transcribe requirement (exact)
|
| 5 |
accelerate>=0.30.0
|
| 6 |
bitsandbytes>=0.43.0 # 4-bit quant for MiniCPM on CPU
|
|
@@ -21,10 +21,10 @@ huggingface_hub
|
|
| 21 |
llama-cpp-python>=0.2.85 # plain CPU fallback β override with CUDA build above
|
| 22 |
|
| 23 |
# ββ VAD βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 24 |
silero-vad # pulls torch; also needs torchaudio for audio I/O
|
| 25 |
onnxruntime>=1.16.0 # ONNX runtime for Silero VAD
|
| 26 |
pyaudio # microphone access (Linux: apt install python3-pyaudio)
|
| 27 |
-
numpy>=1.24.0
|
| 28 |
|
| 29 |
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
gradio>=4.40.0
|
|
|
|
| 1 |
# ββ Core ML / inference βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
+
torch>=2.4.0
|
| 3 |
+
torchaudio>=2.4.0
|
| 4 |
transformers>=4.56,<5.3,!=5.0.*,!=5.1.* # Cohere Transcribe requirement (exact)
|
| 5 |
accelerate>=0.30.0
|
| 6 |
bitsandbytes>=0.43.0 # 4-bit quant for MiniCPM on CPU
|
|
|
|
| 21 |
llama-cpp-python>=0.2.85 # plain CPU fallback β override with CUDA build above
|
| 22 |
|
| 23 |
# ββ VAD βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
+
numpy>=1.24.0 # Install early to avoid dependency conflicts
|
| 25 |
silero-vad # pulls torch; also needs torchaudio for audio I/O
|
| 26 |
onnxruntime>=1.16.0 # ONNX runtime for Silero VAD
|
| 27 |
pyaudio # microphone access (Linux: apt install python3-pyaudio)
|
|
|
|
| 28 |
|
| 29 |
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
gradio>=4.40.0
|