"""Accent Detective: how Taiwanese does a Mandarin recording sound? (Hugging Face Space) Loads the model, its lexicons and code shipped with the Space (model/), or DETECTIVE_PATH, or the model repo JacobLinCool/accent-detective, and scores an uploaded or recorded clip with every size of the one network. """ import os import sys import gradio as gr from huggingface_hub import snapshot_download MODEL_REPO = os.environ.get("DETECTIVE_REPO", "JacobLinCool/accent-detective") HERE = os.path.dirname(os.path.abspath(__file__)) path = (os.environ.get("DETECTIVE_PATH") or (os.path.join(HERE, "model") if os.path.exists(os.path.join(HERE, "model", "model.safetensors")) else None) or snapshot_download(MODEL_REPO, token=os.environ.get("HF_TOKEN"))) sys.path.insert(0, path) from detective import load # noqa: E402 det = load() SIZES = {s: det.parameters(s) for s in det.SIZES} LAYERS = dict(zip(det.SIZES, det.meta["config"]["exits"])) MAIN = det.SIZES[-1] # the largest size, the most accurate NAME = {s: s.capitalize() for s in det.SIZES} CSS = """ .ad-score {font-size:48px; font-weight:700; line-height:1.1; margin-top:6px} .ad-unit {font-size:13px; font-weight:400; opacity:.7; margin-left:4px} .ad-bar {height:8px; border-radius:4px; background:var(--border-color-primary); overflow:hidden; margin:6px 0 10px} .ad-bar > div {height:100%; background:linear-gradient(90deg,#d9534f,#e0a030 50%,#2f9e5b)} .ad-note {font-size:12px; opacity:.65; margin-top:2px} .ad-empty {opacity:.5; font-size:14px; padding:24px 0} .ad-sizes {width:100%; border-collapse:collapse; margin-top:14px; font-size:14px} .ad-sizes td, .ad-sizes th {padding:4px 6px; border-top:1px solid var(--border-color-primary); text-align:right} .ad-sizes td:first-child, .ad-sizes th:first-child {text-align:left} .ad-sizes tr.ad-on td {font-weight:700} """ ABOUT = """ **數字是什麼** 最大的數字是四種大小中最大的 Small,判斷這段語音是臺灣華語的機率。50% 代表無法判斷,越高越像臺灣華語,越低越不像。機率已經用開發資料校準:在沒看過的 Common Voice 語者上,判為 80% 的語音,大約八成真的是臺灣華語。 **怎麼用** - 適合比較同一句話的不同念法,例如同一個人換兩種腔調念,或不同的語音合成系統念同一句。 - 兩種念法都很接近 0% 或 100% 時,機率的差別會變得很小,所以這時會顯示到小數一位。 - 文字可以不填。填了文字,模型會多看它預期的音節。兩種方式的準確度幾乎相同。 - 超過 15 秒的錄音,會依停頓切成 4–10 秒的片段分別評分,再依長度加權。這時不使用文字。 **四種大小** 表格列出同一個網路四種深度的機率,由小到大是 Nano、Tiny、Mini、Small。較小的就是較大的前半段,一次計算就能得到四個結果。較大的通常較準,較小的較快,適合資源有限的環境。 **已知限制** - 大陸西南官話地區(四川、重慶、貴州、雲南)的語者念普通話時,約兩到三成會被判為臺灣華語,其他大陸地區約 2–8%。 - 明顯的背景噪音或房間殘響會降低準確度。MP3 壓縮幾乎沒有影響。 - 香港、新加坡、馬來西亞的華語沒有評估過。 模型與說明:[JacobLinCool/accent-detective](https://huggingface.co/JacobLinCool/accent-detective) """ # Common Voice test clips (CC0) of speakers the model never trained on EXAMPLES = [("taiwan_1.mp3", "看到這種狀況會有點擔心"), ("taiwan_2.mp3", "入住當天早餐就隨便你吃"), ("mainland_1.mp3", "勇敢是指具有勇气的行为。"), ("mainland_2.mp3", "他用的是经验论心理学的方法。")] EXAMPLE_LABELS = [(f"Common Voice {'zh-TW' if f.startswith('taiwan') else 'zh-CN'}:{t}", f) for f, t in EXAMPLES] def pct(p: float) -> str: """A probability as a percentage, one decimal near 0% or 100% so that close renditions still differ.""" v = 100 * p return f"{v:.0f}%" if 1 <= v <= 99 else f"{v:.1f}%" def score(audio, text): if audio is None: raise gr.Error("請提供一段語音") res = det.score_recording(audio, (text or "").strip() or None, sizes=det.SIZES) r = res[MAIN] out = [f'
| 同一個網路的四種大小 | 臺灣華語機率 |
|---|