Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- README.md +3 -3
- app.py +292 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
---
|
| 2 |
-
title: Paramatch
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.11.0
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Paramatch — 声優推定デモ
|
| 3 |
+
emoji: 🕵
|
| 4 |
+
colorFrom: green
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.11.0
|
app.py
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Paramatch Classification Frontend — HF Spaces Gradio app.
|
| 2 |
+
|
| 3 |
+
Calls a remote backend (HF Space or URL) via gradio_client for predictions,
|
| 4 |
+
renders results as styled HTML cards.
|
| 5 |
+
|
| 6 |
+
Set the BACKEND_SPACE env var (or HF Spaces secret) to the backend
|
| 7 |
+
Space name or URL, e.g. "your-username/paramatch-backend" or "http://localhost:7860".
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import argparse
|
| 13 |
+
import os
|
| 14 |
+
import subprocess
|
| 15 |
+
import tempfile
|
| 16 |
+
|
| 17 |
+
import gradio as gr
|
| 18 |
+
import librosa
|
| 19 |
+
from gradio_client import Client, handle_file
|
| 20 |
+
|
| 21 |
+
MAX_DURATION_SEC = 30
|
| 22 |
+
|
| 23 |
+
# ---------------------------------------------------------------------------
|
| 24 |
+
# Configuration
|
| 25 |
+
# ---------------------------------------------------------------------------
|
| 26 |
+
|
| 27 |
+
DEFAULT_TOP_K = 3
|
| 28 |
+
|
| 29 |
+
# ---------------------------------------------------------------------------
|
| 30 |
+
# CSS
|
| 31 |
+
# ---------------------------------------------------------------------------
|
| 32 |
+
|
| 33 |
+
CARD_CSS = """\
|
| 34 |
+
.result-container {
|
| 35 |
+
display: flex;
|
| 36 |
+
flex-direction: column;
|
| 37 |
+
gap: 8px;
|
| 38 |
+
}
|
| 39 |
+
.result-card {
|
| 40 |
+
border: 1px solid var(--border-color-primary);
|
| 41 |
+
border-radius: 12px;
|
| 42 |
+
padding: 12px 16px;
|
| 43 |
+
background: var(--background-fill-primary);
|
| 44 |
+
transition: box-shadow 0.2s;
|
| 45 |
+
}
|
| 46 |
+
.result-card:first-child {
|
| 47 |
+
border: 2px solid var(--color-accent);
|
| 48 |
+
box-shadow: 0 2px 12px var(--color-accent-soft);
|
| 49 |
+
}
|
| 50 |
+
.result-card-header {
|
| 51 |
+
display: flex;
|
| 52 |
+
align-items: center;
|
| 53 |
+
gap: 10px;
|
| 54 |
+
margin-bottom: 4px;
|
| 55 |
+
}
|
| 56 |
+
.rank-badge {
|
| 57 |
+
font-size: 1.2em;
|
| 58 |
+
min-width: 28px;
|
| 59 |
+
text-align: center;
|
| 60 |
+
}
|
| 61 |
+
.prob-bar-container {
|
| 62 |
+
flex: 1;
|
| 63 |
+
height: 6px;
|
| 64 |
+
background: var(--background-fill-secondary);
|
| 65 |
+
border-radius: 3px;
|
| 66 |
+
overflow: hidden;
|
| 67 |
+
}
|
| 68 |
+
.prob-bar {
|
| 69 |
+
height: 100%;
|
| 70 |
+
border-radius: 3px;
|
| 71 |
+
transition: width 0.3s ease;
|
| 72 |
+
}
|
| 73 |
+
.prob-text {
|
| 74 |
+
font-size: 0.95em;
|
| 75 |
+
font-weight: 600;
|
| 76 |
+
min-width: 52px;
|
| 77 |
+
text-align: right;
|
| 78 |
+
}
|
| 79 |
+
.speaker-name {
|
| 80 |
+
font-size: 1.1em;
|
| 81 |
+
padding-left: 38px;
|
| 82 |
+
color: var(--body-text-color);
|
| 83 |
+
}"""
|
| 84 |
+
|
| 85 |
+
RANK_ICONS = ["\U0001f947", "\U0001f948", "\U0001f949"]
|
| 86 |
+
|
| 87 |
+
# ---------------------------------------------------------------------------
|
| 88 |
+
# HTML builder
|
| 89 |
+
# ---------------------------------------------------------------------------
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def build_result_html(data: dict) -> str:
|
| 93 |
+
"""Build styled HTML cards from backend response."""
|
| 94 |
+
results = data.get("results", [])
|
| 95 |
+
if not results:
|
| 96 |
+
return (
|
| 97 |
+
'<div style="text-align:center;padding:32px 16px;color:var(--body-text-color-subdued);">'
|
| 98 |
+
'<p style="font-size:1.3em;margin-bottom:8px;">音声をアップロードして「分析」を押してください</p>'
|
| 99 |
+
'<p style="font-size:0.9em;">対応形式: WAV, MP3, OGG, FLAC など</p>'
|
| 100 |
+
"</div>"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
show_badges = len(results) > 1
|
| 104 |
+
cards: list[str] = []
|
| 105 |
+
for i, r in enumerate(results):
|
| 106 |
+
prob_pct = r.get("probability", 0)
|
| 107 |
+
bar_color = "var(--color-accent)"
|
| 108 |
+
|
| 109 |
+
if show_badges:
|
| 110 |
+
rank_icon = (
|
| 111 |
+
RANK_ICONS[i]
|
| 112 |
+
if i < len(RANK_ICONS)
|
| 113 |
+
else f"<span style='font-weight:600;'>{i + 1}</span>"
|
| 114 |
+
)
|
| 115 |
+
badge_html = f'<span class="rank-badge">{rank_icon}</span>'
|
| 116 |
+
name_pad = "padding-left:38px;"
|
| 117 |
+
else:
|
| 118 |
+
badge_html = '<span class="rank-badge">\U0001f3af</span>'
|
| 119 |
+
name_pad = "padding-left:38px;"
|
| 120 |
+
|
| 121 |
+
card = f"""\
|
| 122 |
+
<div class="result-card">
|
| 123 |
+
<div class="result-card-header">
|
| 124 |
+
{badge_html}
|
| 125 |
+
<div class="prob-bar-container">
|
| 126 |
+
<div class="prob-bar" style="width:{prob_pct:.1f}%;background:{bar_color};"></div>
|
| 127 |
+
</div>
|
| 128 |
+
<span class="prob-text" style="color:{bar_color};">{prob_pct:.1f}%</span>
|
| 129 |
+
</div>
|
| 130 |
+
<div class="speaker-name" style="{name_pad}">{r.get("speaker_name", "Unknown")}</div>
|
| 131 |
+
</div>"""
|
| 132 |
+
cards.append(card)
|
| 133 |
+
|
| 134 |
+
return (
|
| 135 |
+
f'<style>{CARD_CSS}</style><div class="result-container">{"".join(cards)}</div>'
|
| 136 |
+
'<p style="text-align:right;font-size:0.95em;margin-top:8px;">※敬称略</p>'
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# ---------------------------------------------------------------------------
|
| 141 |
+
# Backend client
|
| 142 |
+
# ---------------------------------------------------------------------------
|
| 143 |
+
|
| 144 |
+
backend_space = os.environ.get("BACKEND_SPACE", "")
|
| 145 |
+
backend_client: Client | None = None
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def get_client() -> Client:
|
| 149 |
+
global backend_client
|
| 150 |
+
if backend_client is None:
|
| 151 |
+
if not backend_space:
|
| 152 |
+
raise gr.Error(
|
| 153 |
+
"BACKEND_SPACE env var is not set. "
|
| 154 |
+
"Set it to the backend Space name or URL."
|
| 155 |
+
)
|
| 156 |
+
backend_client = Client(backend_space)
|
| 157 |
+
return backend_client
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def compress_audio(audio_path: str) -> str:
|
| 161 |
+
"""Compress audio to 16kHz mono OGG Vorbis to reduce network transfer."""
|
| 162 |
+
stem = os.path.splitext(os.path.basename(audio_path))[0]
|
| 163 |
+
out_path = os.path.join(tempfile.gettempdir(), f"{stem}.ogg")
|
| 164 |
+
subprocess.run(
|
| 165 |
+
["ffmpeg", "-y", "-i", audio_path, "-ar", "16000", "-ac", "1",
|
| 166 |
+
"-c:a", "libvorbis", "-q:a", "4", out_path],
|
| 167 |
+
capture_output=True, check=True,
|
| 168 |
+
)
|
| 169 |
+
return out_path
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def process(audio_file: str | None) -> str:
|
| 173 |
+
if audio_file is None:
|
| 174 |
+
return build_result_html({})
|
| 175 |
+
duration = librosa.get_duration(path=audio_file)
|
| 176 |
+
if duration > MAX_DURATION_SEC:
|
| 177 |
+
raise gr.Error(
|
| 178 |
+
f"音声が長すぎます({duration:.1f}秒)。{MAX_DURATION_SEC}秒以内の音声をアップロードしてください。"
|
| 179 |
+
)
|
| 180 |
+
try:
|
| 181 |
+
compressed = compress_audio(audio_file)
|
| 182 |
+
except subprocess.CalledProcessError:
|
| 183 |
+
raise gr.Error("音声ファイルの読み込みに失敗しました。別のファイルをお試しください。")
|
| 184 |
+
try:
|
| 185 |
+
client = get_client()
|
| 186 |
+
data = client.predict(handle_file(compressed), DEFAULT_TOP_K, api_name="/classify")
|
| 187 |
+
finally:
|
| 188 |
+
os.unlink(compressed)
|
| 189 |
+
if not data.get("results"):
|
| 190 |
+
return (
|
| 191 |
+
'<div style="text-align:center;padding:32px 16px;color:var(--body-text-color-subdued);">'
|
| 192 |
+
'<p style="font-size:1.3em;margin-bottom:8px;">一致する話者が見つかりませんでした</p>'
|
| 193 |
+
'<p style="font-size:0.9em;">別の音声ファイルをお試しください</p>'
|
| 194 |
+
"</div>"
|
| 195 |
+
)
|
| 196 |
+
return build_result_html(data)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# ---------------------------------------------------------------------------
|
| 200 |
+
# App
|
| 201 |
+
# ---------------------------------------------------------------------------
|
| 202 |
+
|
| 203 |
+
TITLE_HTML = """\
|
| 204 |
+
<h1 style="text-align:center;margin-bottom:4px;">
|
| 205 |
+
Paramatch — 声優推定デモ
|
| 206 |
+
</h1>
|
| 207 |
+
"""
|
| 208 |
+
|
| 209 |
+
DESCRIPTION_MD = f"""\
|
| 210 |
+
音声ファイルをアップロードすると、話者識別モデル **Paramatch** が声優の方々の声との類似性を分析し、その結果をスコアとして算出します。
|
| 211 |
+
|
| 212 |
+
### 開発の目的
|
| 213 |
+
話者識別モデル **Paramatch** は、実演家の皆様や事務所・権利者の方々が、生成AI等による**無断学習やなりすまし**といった状況の事実確認を行う際の、客観的な判断をサポートするために開発されました。
|
| 214 |
+
このデモ版の公開は、表現者のアイデンティティである「声」の権利を守るための有効な対抗策を模索し、実際の不正事例に対する技術の有効性を検証することを主な目的としています。
|
| 215 |
+
"""
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
DISCLAIMER_ACCURACY_MD = f"""\
|
| 219 |
+
### 注意事項
|
| 220 |
+
|
| 221 |
+
- **{MAX_DURATION_SEC}秒以下**の、BGMやノイズ、複数人の会話が含まれない**声のみのクリアな音声**をご使用ください。
|
| 222 |
+
- 音声ファイルの**長さが長いほど精度が向上**します。数秒程度の短い音声では、正確な推定が難しい場合があります。
|
| 223 |
+
- 表示される声優名やスコアはあくまで参考値であり、**高スコアであっても、該当する声優本人を元とした音声であることを保証するものではありません。**
|
| 224 |
+
声質が似ている別の話者が検知されたり、逆に本人であっても状況によりスコアが低くなったり検知できなかったりする場合があります。
|
| 225 |
+
- 本モデルは現在研究開発途中であり、認識精度は完全ではありません。このデモの結果は**参考情報としてのみ**ご利用ください。法的判断の根拠等として使用することはできません。
|
| 226 |
+
"""
|
| 227 |
+
|
| 228 |
+
DISCLAIMER_DATA_MD = """\
|
| 229 |
+
### データの取り扱いについて
|
| 230 |
+
|
| 231 |
+
- 学習データは適切な公開ソースから収集したものであり、音声からの情報解析を行う**Paramatchモデルの学習にのみ**使用しています。音声合成等の生成モデルや、弊社の他製品等への転用は行っておりません。
|
| 232 |
+
- アップロードされた音声は、このデモでの分析処理のみに使用され、サーバーへの保存や二次利用は一切行われません。
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def ping() -> str:
|
| 237 |
+
print("Ping received")
|
| 238 |
+
return "pong"
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def build_app() -> gr.Blocks:
|
| 242 |
+
with gr.Blocks(
|
| 243 |
+
title="Paramatch — 声優推定デモ",
|
| 244 |
+
theme=gr.themes.Ocean(),
|
| 245 |
+
delete_cache=(3600, 600),
|
| 246 |
+
) as demo:
|
| 247 |
+
gr.HTML(TITLE_HTML)
|
| 248 |
+
gr.Markdown(DESCRIPTION_MD)
|
| 249 |
+
|
| 250 |
+
with gr.Row():
|
| 251 |
+
with gr.Column(scale=1):
|
| 252 |
+
audio_input = gr.Audio(label="入力音声", type="filepath")
|
| 253 |
+
submit_btn = gr.Button("分析", variant="primary")
|
| 254 |
+
|
| 255 |
+
with gr.Column(scale=1):
|
| 256 |
+
output_html = gr.HTML(
|
| 257 |
+
value=build_result_html({}),
|
| 258 |
+
label="推定結果",
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
gr.Markdown(DISCLAIMER_ACCURACY_MD)
|
| 262 |
+
gr.Markdown(DISCLAIMER_DATA_MD)
|
| 263 |
+
|
| 264 |
+
submit_btn.click(
|
| 265 |
+
fn=process,
|
| 266 |
+
inputs=[audio_input],
|
| 267 |
+
outputs=output_html,
|
| 268 |
+
api_visibility="private",
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
gr.Textbox(visible=False).change(fn=ping, inputs=[], outputs=[], api_name="ping")
|
| 272 |
+
|
| 273 |
+
return demo
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
if __name__ == "__main__":
|
| 277 |
+
parser = argparse.ArgumentParser(description="Paramatch Classification Frontend")
|
| 278 |
+
parser.add_argument(
|
| 279 |
+
"--share", action="store_true", help="Create a public Gradio link"
|
| 280 |
+
)
|
| 281 |
+
parser.add_argument(
|
| 282 |
+
"--top-k", type=int, default=DEFAULT_TOP_K, help="Default top-K results"
|
| 283 |
+
)
|
| 284 |
+
parser.add_argument("--port", type=int, default=None)
|
| 285 |
+
args = parser.parse_args()
|
| 286 |
+
|
| 287 |
+
DEFAULT_TOP_K = args.top_k
|
| 288 |
+
app = build_app()
|
| 289 |
+
app.queue(default_concurrency_limit=1, max_size=10, api_open=False).launch(
|
| 290 |
+
share=args.share,
|
| 291 |
+
server_port=args.port,
|
| 292 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
gradio_client
|
| 3 |
+
librosa
|
| 4 |
+
soundfile
|