Spaces:
Sleeping
Sleeping
Commit ·
c8fa660
1
Parent(s): 256ade0
Refactored to fix gradio block error
Browse files19 unmodified lines2021222324252627282930313233344 unmodified lines3940414243444546474849505152535455565758596061626340 unmodified lines1041051061071081091101111121131141151161171185 unmodified lines12412512612712812913016 unmodified lines14714814915015115215315415546 unmodified lines2022032042052062072082092102112 unmodified lines2142152162172182192202212222232242252262272282292302312322332342354 unmodified lines24024124224324419 unmodified lines# Local WhisperProcessor language hint (keep consistent with how you evaluated)LOCAL_LANGUAGE = "shona"# OpenAI language hint (ISO-639-1). Chichewa/Nyanja often "ny". Set to None to auto-detect.OPENAI_LANGUAGE = "ny"# Audio constraintsTARGET_SR = 16000MAX_SECONDS = 30.0# OpenAI transcription model (commercial)OPENAI_MODEL = "whisper-1" # simple + stable# -----------------------------# UI Text / Styling4 unmodified linesstyle="max-width: 100%; height: auto; border-radius: 12px;"></div>"""TITLE_HTML = """<h1 style="text-align:center; font-size:34px; margin-bottom:10px;">Chichewa Speech2Text: How Custom Data Improves Performance</h1>"""HIGHLIGHT_TEXT = """<p style="text-align:center; font-size:20px; font-weight:600; color:#1F3A5F; margin-bottom:20px;">Observe how the fine-tuned model provides better transcription quality.</p>"""DESCRIPTION_HTML = """<p style="text-align:center; font-size:18px; margin-bottom: 18px;">Upload or record a short Chichewa voice note (≤30 seconds). The same audio will be transcribed by three systems.</p>"""ARTICLE_HTML = """<p style="text-align:center; margin-top: 10px;">Read more about the <a href="https://dmatekenya.github.io/Chichewa-Speech2Text/README.html" target="_blank">ChichewaSpeech2Text</a> project40 unmodified lines@torch.inference_mode()def transcribe_local(model: WhisperForConditionalGeneration, audio_16k: np.ndarray) -> str:inputs = PROCESSOR(audio_16k, sampling_rate=TARGET_SR, return_tensors="pt")feats = inputs.input_features.to(DEVICE)if DEVICE == "cuda":feats = feats.to(dtype=DTYPE)ids = model.generate(inputs=feats)text = PROCESSOR.batch_decode(ids, skip_special_tokens=True)[0]return text.strip()def transcribe_openai(audio_path: str) -> str:if not os.getenv("OPENAI_API_KEY"):5 unmodified linesresp = OPENAI_CLIENT.audio.transcriptions.create(file=f,model=OPENAI_MODEL,language=OPENAI_LANGUAGE, # set to None for auto-detectprompt=prompt,temperature=0.0,response_format="json",16 unmodified linesexcept Exception as e:return f"❌ Failed to load audio: {e}", "", "", ""if dur > MAX_SECONDS:return f"⚠️ Audio is {dur:.1f}s. Please keep clips ≤ {MAX_SECONDS:.0f}s.", "", "", ""status = []# Base (local)46 unmodified lines# UI# -----------------------------with gr.Blocks(theme="grass", title="Chichewa Speech2Text") as demo:gr.Markdown(LOGO_HTML)gr.Markdown(TITLE_HTML)gr.Markdown(HIGHLIGHT_TEXT)gr.Markdown(DESCRIPTION_HTML)audio_in = gr.Audio(sources=["microphone", "upload"],2 unmodified lines)run_btn = gr.Button("Transcribe & Compare", variant="primary")status_out = gr.Textbox(label="Status / timing", lines=3)with gr.Row():base_out = gr.Textbox(label="Open Source ASR Model",lines=12,)ft_out = gr.Textbox(label="Open Source Model Fine-Tuned with Custom Chichewa Speech",lines=12,)commercial_out = gr.Textbox(label="Frontier Commercial ASR Model (OpenAI)",lines=12,)run_btn.click(fn=transcribe_all,4 unmodified linesgr.Markdown(ARTICLE_HTML)if __name__ == "__main__":# Queue helps when multiple people test at once during your seminardemo.queue(default_concurrency_limit=2).launch()19 unmodified lines2021222324252627282930314 unmodified lines36373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828340 unmodified lines1241251261271281291301311321331341351361371381391401411421431441451465 unmodified lines15215315415515615716 unmodified lines17417517617717817946 unmodified lines2262272282292302312322332342 unmodified lines2372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672684 unmodified lines27327427527619 unmodified lines# Local WhisperProcessor language hint (keep consistent with how you evaluated)LOCAL_LANGUAGE = "shona"# Audio constraintsTARGET_SR = 16000MAX_SECONDS = 30.0# OpenAI transcription model (commercial)OPENAI_MODEL = "gpt-4o-transcribe" # simple + stable# -----------------------------# UI Text / Styling4 unmodified linesstyle="max-width: 100%; height: auto; border-radius: 12px;"></div>"""HEADER_HTML = """<div style="text-align:center; max-width:900px; margin:0 auto;"><h1 style="font-size:36px; margin-bottom:12px;">Chichewa Speech2Text: How Custom Data Improves Performance</h1><p style="font-size:22px; font-weight:700; color:#1F3A5F; margin-bottom:12px;">Observe how the fine-tuned model provides better transcription quality.</p><p style="font-size:18px; color:#444; margin-bottom:25px;">Upload or record a short Chichewa voice note (≤30 seconds).The same audio will be transcribed by three systems.</p></div>"""DIVIDER = """<div style="max-width:900px; margin:20px auto;"><hr style="border:0; border-top:1px solid #ddd;"></div>"""# TITLE_HTML = """# <h1 style="text-align:center; font-size:34px; margin-bottom:10px;"># Chichewa Speech2Text: How Custom Data Improves Performance# </h1># """# HIGHLIGHT_TEXT = """# <p style="text-align:center; font-size:20px; font-weight:600; color:#1F3A5F; margin-bottom:20px;"># Observe how the fine-tuned model provides better transcription quality.# </p># """# DESCRIPTION_HTML = """# <p style="text-align:center; font-size:18px; margin-bottom: 18px;"># Upload or record a short Chichewa voice note (≤30 seconds). The same audio will be transcribed by three systems.# </p># """ARTICLE_HTML = """<p style="text-align:center; margin-top: 10px;">Read more about the <a href="https://dmatekenya.github.io/Chichewa-Speech2Text/README.html" target="_blank">ChichewaSpeech2Text</a> project40 unmodified lines@torch.inference_mode()def transcribe_local(model: WhisperForConditionalGeneration, audio_16k: np.ndarray) -> str:# Prepare input features for the modelinput_features = PROCESSOR(audio_16k,return_tensors="pt",sampling_rate=TARGET_SR).input_featuresinput_features = input_features.to(DEVICE)if DEVICE == "cuda":input_features = input_features.to(dtype=DTYPE)# Generate transcription (NOTE: input_features= ... not inputs= ...)generated_ids = model.generate(input_features=input_features)# Decodetranscription = PROCESSOR.batch_decode(generated_ids, skip_special_tokens=True)[0]return transcription.strip()def transcribe_openai(audio_path: str) -> str:if not os.getenv("OPENAI_API_KEY"):5 unmodified linesresp = OPENAI_CLIENT.audio.transcriptions.create(file=f,model=OPENAI_MODEL,prompt=prompt,temperature=0.0,response_format="json",16 unmodified linesexcept Exception as e:return f"❌ Failed to load audio: {e}", "", "", ""status = []# Base (local)46 unmodified lines# UI# -----------------------------with gr.Blocks(theme="grass", title="Chichewa Speech2Text") as demo:gr.HTML(LOGO_HTML)gr.HTML(DIVIDER)gr.HTML(HEADER_HTML)audio_in = gr.Audio(sources=["microphone", "upload"],2 unmodified lines)run_btn = gr.Button("Transcribe & Compare", variant="primary")status_out = gr.Textbox(label="Status / timing", lines=3)with gr.Row():with gr.Column():gr.HTML("""<div style="font-size:20px; font-weight:700; color:#444; margin-bottom:6px;">Open Source ASR Model</div>""")base_out = gr.Textbox(lines=12, show_label=False)with gr.Column():gr.HTML("""<div style="font-size:20px; font-weight:700; color:#1F3A5F; margin-bottom:6px;">Open Source Model Fine-Tuned with Custom Chichewa Speech</div>""")ft_out = gr.Textbox(lines=12, show_label=False)with gr.Column():gr.HTML("""<div style="font-size:20px; font-weight:700; color:#8A4B00; margin-bottom:6px;">Frontier Commercial ASR Model (OpenAI)</div>""")commercial_out = gr.Textbox(lines=12, show_label=False)run_btn.click(fn=transcribe_all,4 unmodified linesgr.Markdown(ARTICLE_HTML)if __name__ == "__main__":demo.queue(default_concurrency_limit=2).launch()
app.py+74-42