michaelodafe commited on
Commit
345b60e
·
verified ·
1 Parent(s): 61eba8f

Add punctuation + capitalization formatting layer

Browse files
Files changed (1) hide show
  1. app.py +3 -31
app.py CHANGED
@@ -44,36 +44,8 @@ BASE = "openai/whisper-large-v3-turbo"
44
  ADAPTER = "michaelodafe/whisper-pidgin-v1"
45
  MAX_SECONDS = 30
46
 
47
- INITIAL_PROMPT = (
48
- "dis na bbc news pidgin tori about buhari tinubu atiku saraki "
49
- "tony nwoye femi otedola akinwunmi ambode oseloka henry obaze "
50
- "zainab balogun jimoh moshood and aisha for nigeria politics "
51
- "for states like lagos anambra delta kogi niger abuja kano rivers "
52
- "edo ogun salford and offa with organizations like apc pdp nema "
53
- "jamb frsc brt jp morgan and wikipedia pipo dey tok say di dey na "
54
- "wey pikin tori sabi hapun sometin anytin becos redi alredi neva "
55
- "dem una abi oga chillax snakebite"
56
- )
57
-
58
- _DIGIT_PAIR = re.compile(r"(\d) (\d)")
59
- _PUNCT = re.compile(r"[.,!?;:\"]")
60
- _INTRA_NUM_COMMA = re.compile(r"(\d),(\d)")
61
-
62
-
63
- def postprocess(text: str) -> str:
64
- while True:
65
- new = _INTRA_NUM_COMMA.sub(r"\1\2", text)
66
- if new == text:
67
- break
68
- text = new
69
- text = _PUNCT.sub("", text)
70
- while True:
71
- new = _DIGIT_PAIR.sub(r"\1\2", text)
72
- if new == text:
73
- break
74
- text = new
75
- return re.sub(r" +", " ", text).strip()
76
-
77
 
78
  print("Loading processor + base model + adapter...")
79
  processor = WhisperProcessor.from_pretrained(BASE, language="english", task="transcribe")
@@ -111,7 +83,7 @@ def transcribe(audio):
111
  gen_kwargs["prompt_ids"] = prompt_ids
112
  output_ids = model.generate(inputs.input_features, **gen_kwargs)
113
  text = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
114
- return postprocess(text.strip().lower())
115
  except Exception as e:
116
  return f"⚠️ Error: {type(e).__name__}: {e}"
117
 
 
44
  ADAPTER = "michaelodafe/whisper-pidgin-v1"
45
  MAX_SECONDS = 30
46
 
47
+ # Shared decode helpers (hotword prompt + punctuation/casing formatter).
48
+ from decode import INITIAL_PROMPT, format_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  print("Loading processor + base model + adapter...")
51
  processor = WhisperProcessor.from_pretrained(BASE, language="english", task="transcribe")
 
83
  gen_kwargs["prompt_ids"] = prompt_ids
84
  output_ids = model.generate(inputs.input_features, **gen_kwargs)
85
  text = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
86
+ return format_output(text)
87
  except Exception as e:
88
  return f"⚠️ Error: {type(e).__name__}: {e}"
89