zerolat3ncy commited on
Commit
e709ef1
·
verified ·
1 Parent(s): 8dd40e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -74
app.py CHANGED
@@ -6,20 +6,14 @@ import torch
6
  import librosa
7
  import soundfile as sf
8
  import gradio as gr
9
- from transformers import AutoProcessor, AutoModelForCTC, AutoModelForSeq2SeqLM, AutoTokenizer
10
 
11
- CHUNK_SEC = 20
12
- OVERLAP_SEC = 1.0
13
- MT_BATCH_SIZE = 8
14
- MODEL_ID = os.environ.get("MODEL_ID")
15
- MT_MODEL_ID = os.environ.get("MT_MODEL_ID")
16
- SRC_LANG = "nya_Latn"
17
- TGT_LANG = "eng_Latn"
18
 
19
- processor = AutoProcessor.from_pretrained(MODEL_ID)
20
- model = AutoModelForCTC.from_pretrained(MODEL_ID).eval()
21
- mt_tok = AutoTokenizer.from_pretrained(MT_MODEL_ID, src_lang=SRC_LANG)
22
- mt_model = AutoModelForSeq2SeqLM.from_pretrained(MT_MODEL_ID).eval()
23
 
24
 
25
  def convert_to_wav(input_path):
@@ -30,9 +24,8 @@ def convert_to_wav(input_path):
30
 
31
 
32
  @spaces.GPU
33
- def transcribe_and_translate(audio_path, progress=gr.Progress()):
34
  model.to("cuda")
35
- mt_model.to("cuda")
36
 
37
  wav = convert_to_wav(audio_path)
38
  audio, sr = librosa.load(wav, sr=16000, mono=True)
@@ -66,71 +59,26 @@ def transcribe_and_translate(audio_path, progress=gr.Progress()):
66
 
67
  transcript = " ".join(parts).strip()
68
 
69
- # Batched translation
70
- progress(1.0, desc="Translating...")
71
- sentences = [s.strip() for s in transcript.replace("\n", ". ").split(".") if s.strip()]
72
- translated_parts = []
73
- for batch_start in range(0, len(sentences), MT_BATCH_SIZE):
74
- batch = sentences[batch_start : batch_start + MT_BATCH_SIZE]
75
- inp = mt_tok(batch, return_tensors="pt", padding=True, truncation=True, max_length=512)
76
- inp = {k: v.to("cuda") for k, v in inp.items()}
77
- with torch.no_grad():
78
- out = mt_model.generate(
79
- **inp,
80
- forced_bos_token_id=mt_tok.convert_tokens_to_ids(TGT_LANG))
81
- translated_parts.extend(mt_tok.batch_decode(out, skip_special_tokens=True))
82
-
83
- translation = " ".join(translated_parts)
84
-
85
  txt = tempfile.mktemp(suffix=".txt")
86
  with open(txt, "w", encoding="utf-8") as f:
87
- f.write("=== TRANSCRIPT ===\n")
88
- f.write(transcript + "\n\n")
89
- f.write("=== TRANSLATION ===\n")
90
- f.write(translation + "\n")
91
-
92
- return transcript, translation, gr.update(value=txt, visible=True)
93
-
94
-
95
- @spaces.GPU
96
- def translate_text(text):
97
- mt_model.to("cuda")
98
-
99
- sentences = [s.strip() for s in text.replace("\n", ". ").split(".") if s.strip()]
100
- translated_parts = []
101
- for batch_start in range(0, len(sentences), MT_BATCH_SIZE):
102
- batch = sentences[batch_start : batch_start + MT_BATCH_SIZE]
103
- inp = mt_tok(batch, return_tensors="pt", padding=True, truncation=True, max_length=512)
104
- inp = {k: v.to("cuda") for k, v in inp.items()}
105
- with torch.no_grad():
106
- out = mt_model.generate(
107
- **inp,
108
- forced_bos_token_id=mt_tok.convert_tokens_to_ids(TGT_LANG))
109
- translated_parts.extend(mt_tok.batch_decode(out, skip_special_tokens=True))
110
 
111
- return " ".join(translated_parts)
112
 
113
 
114
  with gr.Blocks(title="Chichewa ASR") as demo:
115
- gr.Markdown("## Chichewa Speech Transcription and Translation")
116
-
117
- with gr.Tabs():
118
- with gr.Tab("Transcribe"):
119
- audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath")
120
- transcribe_btn = gr.Button("Process")
121
- transcript = gr.Textbox(label="Speech Transcription", lines=3, interactive=False)
122
- transcript_trans = gr.Textbox(label="English Translation", lines=3, interactive=False)
123
-
124
- with gr.Tab("Translate"):
125
- text_input = gr.Textbox(label="Chichewa Text", lines=6)
126
- translate_btn = gr.Button("Translate")
127
- text_output = gr.Textbox(label="English Translation", lines=6, interactive=False)
128
-
129
- def run_transcribe(audio):
130
- t, tr, _ = transcribe_and_translate(audio)
131
- return t, tr
132
-
133
- transcribe_btn.click(fn=run_transcribe, inputs=audio_input, outputs=[transcript, transcript_trans], show_progress="full")
134
- translate_btn.click(fn=translate_text, inputs=text_input, outputs=text_output, show_progress="full")
135
 
136
  demo.launch(debug=True)
 
6
  import librosa
7
  import soundfile as sf
8
  import gradio as gr
9
+ from transformers import AutoProcessor, AutoModelForCTC
10
 
11
+ CHUNK_SEC = 20
12
+ OVERLAP_SEC = 1.0
13
+ MODEL_ID = os.environ.get("MODEL_ID")
 
 
 
 
14
 
15
+ processor = AutoProcessor.from_pretrained(MODEL_ID)
16
+ model = AutoModelForCTC.from_pretrained(MODEL_ID).eval()
 
 
17
 
18
 
19
  def convert_to_wav(input_path):
 
24
 
25
 
26
  @spaces.GPU
27
+ def transcribe(audio_path, progress=gr.Progress()):
28
  model.to("cuda")
 
29
 
30
  wav = convert_to_wav(audio_path)
31
  audio, sr = librosa.load(wav, sr=16000, mono=True)
 
59
 
60
  transcript = " ".join(parts).strip()
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  txt = tempfile.mktemp(suffix=".txt")
63
  with open(txt, "w", encoding="utf-8") as f:
64
+ f.write(transcript + "\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ return transcript, gr.update(value=txt, visible=True)
67
 
68
 
69
  with gr.Blocks(title="Chichewa ASR") as demo:
70
+ gr.Markdown("## Chichewa Speech Transcription")
71
+
72
+ audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath")
73
+ transcribe_btn = gr.Button("Transcribe")
74
+ transcript_out = gr.Textbox(label="Transcription", lines=6, interactive=False)
75
+ download_out = gr.File(label="Download transcript", visible=False)
76
+
77
+ transcribe_btn.click(
78
+ fn=transcribe,
79
+ inputs=audio_input,
80
+ outputs=[transcript_out, download_out],
81
+ show_progress="full",
82
+ )
 
 
 
 
 
 
 
83
 
84
  demo.launch(debug=True)