Spaces:
Running
Running
Download app.py from JacobLinCool/accent-detective: direct link, hf CLI and curl.
- Browser
- Download file 6.66 kB
-
https://huggingface.co/spaces/JacobLinCool/accent-detective/resolve/main/app.py
- Command line
-
hf download hf://spaces/JacobLinCool/accent-detective/app.py
-
curl -L -o app.py https://huggingface.co/spaces/JacobLinCool/accent-detective/resolve/main/app.py
6.66 kB
| """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'<div class="ad-score">{pct(r["probability"])}</div>' | |
| f'<div class="ad-note">臺灣華語機率({NAME[MAIN]},50%=無法判斷)</div>' | |
| f'<div class="ad-bar"><div style="width:{100 * r["probability"]:.1f}%"></div></div>'] | |
| notes = [] | |
| if len(r["pieces"]) > 1: | |
| notes.append(f"錄音較長,已依停頓切成 {len(r['pieces'])} 段分別評分,總分依長度加權,未使用文字") | |
| elif text and text.strip() and r["mode"] == "audio": | |
| notes.append("文字裡沒有可用的漢字音節,這次只用語音評分") | |
| else: | |
| notes.append("有用到文字" if r["mode"] == "text" else "只用語音,沒有文字") | |
| out.append(f'<div class="ad-note">{notes[0]}</div>') | |
| rows = "".join(f'<tr class="{"ad-on" if k == MAIN else ""}"><td>{NAME[k]}({SIZES[k] / 1e6:.1f} M 參數,{LAYERS[k]} 層)</td>' | |
| f'<td>{pct(v["probability"])}</td></tr>' for k, v in res.items()) | |
| out.append('<table class="ad-sizes"><tr><th>同一個網路的四種大小</th><th>臺灣華語機率</th></tr>' | |
| f'{rows}</table>') | |
| return "".join(out) | |
| with gr.Blocks(title="Accent Detective", css=CSS, theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## Accent Detective 這段華語聽起來有多像臺灣華語?\n" | |
| "上傳或錄一段華語,看它在發音與聲調上多像臺灣華語。文字可以不填。") | |
| with gr.Row(equal_height=False): | |
| with gr.Column(scale=2, min_width=300): | |
| audio = gr.Audio(type="filepath", sources=["upload", "microphone"], label="語音") | |
| text = gr.Textbox(label="這段語音念的文字(選填)", lines=2) | |
| go = gr.Button("評分", variant="primary", size="lg") | |
| with gr.Column(scale=3): | |
| out = gr.HTML('<div class="ad-empty">上傳或錄一段華語,文字可以不填</div>') | |
| with gr.Accordion("怎麼看這些分數", open=False): | |
| gr.Markdown(ABOUT) | |
| gr.Examples( | |
| examples=[[os.path.join(HERE, "examples", f), t] for f, t in EXAMPLES], | |
| example_labels=[label for label, _ in EXAMPLE_LABELS], | |
| inputs=[audio, text], outputs=out, fn=score, run_on_click=True, cache_examples=False, | |
| label="範例") | |
| go.click(score, [audio, text], out, api_name="score") | |
| demo.queue(default_concurrency_limit=2).launch() | |