njand commited on
Commit
ed370e7
·
1 Parent(s): 0789470

Update requirements.txt

Browse files
Files changed (2) hide show
  1. app.py +25 -15
  2. requirements.txt +3 -5
app.py CHANGED
@@ -1,22 +1,25 @@
1
- import transformers.utils
2
-
3
- # Patch missing helper in modern transformers for Optimum compatibility
4
- if not hasattr(transformers.utils, "is_tf_available"):
5
- transformers.utils.is_tf_available = lambda: False
6
-
7
  import gradio as gr
8
  import librosa
9
  import numpy as np
10
- import torch
11
- from optimum.onnxruntime import ORTModelForCTC
12
  from transformers import AutoProcessor
13
 
14
-
15
  MODEL_ID = "njand/wav2vec2-xls-r-latin"
16
  FILE_NAME = "model_quantized.onnx"
17
 
 
18
  processor = AutoProcessor.from_pretrained(MODEL_ID)
19
- model = ORTModelForCTC.from_pretrained(MODEL_ID, file_name=FILE_NAME)
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  def transcribe_audio(audio_tuple) -> str:
@@ -26,28 +29,34 @@ def transcribe_audio(audio_tuple) -> str:
26
  try:
27
  sample_rate, audio_data = audio_tuple
28
 
 
29
  if audio_data.ndim > 1:
30
  audio_data = audio_data.mean(axis=1)
31
 
 
32
  if np.issubdtype(audio_data.dtype, np.integer):
33
  max_val = np.iinfo(audio_data.dtype).max
34
  audio_data = audio_data.astype(np.float32) / max_val
35
  elif audio_data.dtype != np.float32:
36
  audio_data = audio_data.astype(np.float32)
37
 
 
38
  if sample_rate != 16000:
39
  audio_data = librosa.resample(
40
  audio_data, orig_sr=sample_rate, target_sr=16000
41
  )
42
 
43
- input_values = processor(
44
- audio_data, sampling_rate=16000, return_tensors="pt"
 
45
  ).input_values
46
 
47
- with torch.no_grad():
48
- logits = model(input_values).logits
 
49
 
50
- predicted_ids = torch.argmax(logits, dim=-1)
 
51
  transcription = processor.batch_decode(predicted_ids)[0]
52
 
53
  return (
@@ -60,6 +69,7 @@ def transcribe_audio(audio_tuple) -> str:
60
  return f"❌ An error occurred during transcription: {str(e)}"
61
 
62
 
 
63
  with gr.Blocks(theme=gr.themes.Soft(), title="Latin Speech-to-Text") as demo:
64
  gr.Markdown("# 🎙️ Latin Speech Recognition Demo")
65
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import librosa
3
  import numpy as np
4
+ import onnxruntime as ort
5
+ from huggingface_hub import hf_hub_download
6
  from transformers import AutoProcessor
7
 
 
8
  MODEL_ID = "njand/wav2vec2-xls-r-latin"
9
  FILE_NAME = "model_quantized.onnx"
10
 
11
+ print("Downloading processor and ONNX model...")
12
  processor = AutoProcessor.from_pretrained(MODEL_ID)
13
+
14
+ # Download ONNX quantized weights directly from HF Hub
15
+ model_path = hf_hub_download(repo_id=MODEL_ID, filename=FILE_NAME)
16
+
17
+ # Explicitly initialize ONNX Runtime on CPU to avoid ZeroGPU driver clashes
18
+ session = ort.InferenceSession(
19
+ model_path, providers=["CPUExecutionProvider"]
20
+ )
21
+ input_name = session.get_inputs()[0].name
22
+ print("ONNX Model loaded successfully!")
23
 
24
 
25
  def transcribe_audio(audio_tuple) -> str:
 
29
  try:
30
  sample_rate, audio_data = audio_tuple
31
 
32
+ # Convert stereo to mono if audio has multiple channels
33
  if audio_data.ndim > 1:
34
  audio_data = audio_data.mean(axis=1)
35
 
36
+ # Normalize integer PCM data to float32 (-1.0 to 1.0)
37
  if np.issubdtype(audio_data.dtype, np.integer):
38
  max_val = np.iinfo(audio_data.dtype).max
39
  audio_data = audio_data.astype(np.float32) / max_val
40
  elif audio_data.dtype != np.float32:
41
  audio_data = audio_data.astype(np.float32)
42
 
43
+ # Resample to 16 kHz (required by Wav2Vec2)
44
  if sample_rate != 16000:
45
  audio_data = librosa.resample(
46
  audio_data, orig_sr=sample_rate, target_sr=16000
47
  )
48
 
49
+ # Preprocess input audio into NumPy arrays
50
+ inputs = processor(
51
+ audio_data, sampling_rate=16000, return_tensors="np"
52
  ).input_values
53
 
54
+ # Run ONNX inference
55
+ onnx_outputs = session.run(None, {input_name: inputs})
56
+ logits = onnx_outputs[0]
57
 
58
+ # Decode token predictions
59
+ predicted_ids = np.argmax(logits, axis=-1)
60
  transcription = processor.batch_decode(predicted_ids)[0]
61
 
62
  return (
 
69
  return f"❌ An error occurred during transcription: {str(e)}"
70
 
71
 
72
+ # Build Gradio UI
73
  with gr.Blocks(theme=gr.themes.Soft(), title="Latin Speech-to-Text") as demo:
74
  gr.Markdown("# 🎙️ Latin Speech Recognition Demo")
75
 
requirements.txt CHANGED
@@ -1,6 +1,4 @@
1
- optimum[onnxruntime]
2
- transformers
3
  librosa
4
- torch==2.11.0
5
- numpy
6
- numba>=0.60.0
 
1
+ onnxruntime
 
2
  librosa
3
+ transformers
4
+ huggingface-hub