hrant1/transcriptsmith-qwen-demo
0
1from __future__ import annotations2 3import json4import os5from functools import lru_cache6from pathlib import Path7 8import gradio as gr9 10from src.ml.demo_runtime import DemoConfigError, TASK_DESCRIPTIONS, TASK_ORDER, build_demo_runner_from_env11 12 13EXAMPLES_PATH = Path(__file__).resolve().parent / "demo" / "examples.json"14 15 16def load_examples() -> list[list[str]]:17 if not EXAMPLES_PATH.exists():18 return []19 with EXAMPLES_PATH.open("r", encoding="utf-8") as input_file:20 payload = json.load(input_file)21 return [[str(row["text"])] for row in payload if isinstance(row, dict) and row.get("text")]22 23 24@lru_cache(maxsize=1)25def get_runner():26 return build_demo_runner_from_env()27 28 29def describe_runtime() -> str:30 try:31 runner = get_runner()32 except Exception as exc:33 return (34 "Demo configuration is incomplete. Set `DEMO_MODEL_SPEC_PATH` or "35 "`DEMO_BASE_MODEL_ID`/`DEMO_TOKENIZER_PATH`, or provide "36 "`DEMO_BACKEND_URL` for external inference.\n\n"37 f"Current error: {exc}"38 )39 40 return (41 f"Mode: `{runner.metadata.mode}` \n"42 f"Model: `{runner.metadata.model_name}` \n"43 f"Source: `{runner.metadata.source}` \n"44 f"Max new tokens: `{runner.metadata.max_new_tokens}`"45 )46 47 48def run_demo(text: str) -> tuple[str, str, str, str, str]:49 if not str(text).strip():50 message = "Enter an ASR-style sentence to generate outputs for all four tasks."51 return "", "", "", "", message52 53 try:54 runner = get_runner()55 outputs = runner.predict_all(text)56 except DemoConfigError as exc:57 return "", "", "", "", f"Configuration error: {exc}"58 except Exception as exc: # pragma: no cover - keeps app failures visible to the user59 return "", "", "", "", f"Inference failed: {exc}"60 61 return (62 outputs["NORM"].output_text,63 outputs["PUNCT"].output_text,64 outputs["BOTH"].output_text,65 outputs["NO_TOKEN"].output_text,66 describe_runtime(),67 )68 69 70with gr.Blocks(title="Transcriptsmith Demo") as demo:71 gr.Markdown(72 """73 # Transcriptsmith Demo74 75 Enter one ASR-style sentence and inspect how the multitask model handles all four tasks:76 `NORM`, `PUNCT`, `BOTH`, and `NO_TOKEN`.77 """78 )79 runtime_info = gr.Markdown(describe_runtime())80 81 with gr.Row():82 input_text = gr.Textbox(83 label="Input text",84 placeholder="e.g. i was born on april fifth twenty ten and it cost ten dollars",85 lines=4,86 )87 88 gr.Examples(89 examples=load_examples(),90 inputs=[input_text],91 label="Curated demo examples",92 )93 94 with gr.Row():95 run_button = gr.Button("Run", variant="primary")96 clear_button = gr.Button("Clear")97 98 with gr.Row():99 with gr.Column():100 gr.Markdown(f"### NORM\n{TASK_DESCRIPTIONS['NORM']}")101 norm_output = gr.Textbox(label="Output", lines=4)102 with gr.Column():103 gr.Markdown(f"### PUNCT\n{TASK_DESCRIPTIONS['PUNCT']}")104 punct_output = gr.Textbox(label="Output", lines=4)105 106 with gr.Row():107 with gr.Column():108 gr.Markdown(f"### BOTH\n{TASK_DESCRIPTIONS['BOTH']}")109 both_output = gr.Textbox(label="Output", lines=4)110 with gr.Column():111 gr.Markdown(f"### NO_TOKEN\n{TASK_DESCRIPTIONS['NO_TOKEN']}")112 no_token_output = gr.Textbox(label="Output", lines=4)113 114 status_output = gr.Markdown()115 116 run_button.click(117 fn=run_demo,118 inputs=[input_text],119 outputs=[norm_output, punct_output, both_output, no_token_output, status_output],120 )121 input_text.submit(122 fn=run_demo,123 inputs=[input_text],124 outputs=[norm_output, punct_output, both_output, no_token_output, status_output],125 )126 clear_button.click(127 fn=lambda: ("", "", "", "", "", describe_runtime()),128 outputs=[input_text, norm_output, punct_output, both_output, no_token_output, status_output],129 )130 131 132if __name__ == "__main__":133 demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))134 