voidash commited on
Commit
c12a0a7
·
verified ·
1 Parent(s): ed8fed2

revert format=wav (broke preprocess); convert mic webm via ffmpeg in handler; show_error

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -20,10 +20,28 @@ def get_model():
20
  return MODEL
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def transcribe(audio_path):
24
  if not audio_path:
25
  return "(no audio)"
26
  import soundfile as sf
 
27
  info = sf.info(audio_path)
28
  if info.duration > 60:
29
  return "Please keep clips under 60 seconds for this CPU demo."
@@ -38,7 +56,7 @@ examples = [[f"examples/{f}"] for f in sorted(os.listdir("examples"))] \
38
 
39
  demo = gr.Interface(
40
  fn=transcribe,
41
- inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", format="wav",
42
  label="Nepali speech (mic or file, ≤60 s)"),
43
  outputs=gr.Textbox(label="Transcript (Devanagari)", rtl=False),
44
  title="NepaliConformer — Nepali ASR for real telephone calls",
@@ -57,4 +75,4 @@ demo = gr.Interface(
57
  )
58
 
59
  if __name__ == "__main__":
60
- demo.launch(ssr_mode=False)
 
20
  return MODEL
21
 
22
 
23
+ def _ensure_wav(path):
24
+ """Mic recordings arrive as webm/mp4; libsndfile can't read those — convert."""
25
+ import soundfile as sf
26
+ try:
27
+ sf.info(path)
28
+ return path
29
+ except Exception:
30
+ import subprocess
31
+ import tempfile
32
+ out = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
33
+ subprocess.run(
34
+ ["ffmpeg", "-y", "-loglevel", "error", "-i", path, "-ar", "16000", "-ac", "1", out],
35
+ check=True,
36
+ )
37
+ return out
38
+
39
+
40
  def transcribe(audio_path):
41
  if not audio_path:
42
  return "(no audio)"
43
  import soundfile as sf
44
+ audio_path = _ensure_wav(audio_path)
45
  info = sf.info(audio_path)
46
  if info.duration > 60:
47
  return "Please keep clips under 60 seconds for this CPU demo."
 
56
 
57
  demo = gr.Interface(
58
  fn=transcribe,
59
+ inputs=gr.Audio(sources=["microphone", "upload"], type="filepath",
60
  label="Nepali speech (mic or file, ≤60 s)"),
61
  outputs=gr.Textbox(label="Transcript (Devanagari)", rtl=False),
62
  title="NepaliConformer — Nepali ASR for real telephone calls",
 
75
  )
76
 
77
  if __name__ == "__main__":
78
+ demo.launch(ssr_mode=False, show_error=True)