Download app.py from sakitibi/AsakuraWikiEvilContentsChecker: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/sakitibi/AsakuraWikiEvilContentsChecker/resolve/main/app.py
- Command line
-
hf download hf://spaces/sakitibi/AsakuraWikiEvilContentsChecker/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sakitibi/AsakuraWikiEvilContentsChecker/resolve/main/app.py
2.83 kB
| import gradio as gr | |
| from transformers import pipeline | |
| import os | |
| # 無料CPU環境で動く、日本語に強い軽量な対話型AIモデル(Qwen2.5-1.5Bなど)を読み込む | |
| # 初回起動時に自動でダウンロードされます | |
| try: | |
| pipe = pipeline("text-generation", model="Qwen/Qwen2.5-1.5B-Instruct", device_map="cpu") | |
| except Exception as e: | |
| pipe = None | |
| # 💡 チェックしたいキャラクター名をここに設定 | |
| TARGET_CHARACTER = "名前は長い方が有利" | |
| def check_advocacy(text): | |
| if pipe is None: | |
| # 万が一モデルの読み込みに失敗した場合の簡易フォールバック | |
| if TARGET_CHARACTER in text and ("悪くない" in text or "絶対に" in text): | |
| return "Yes" | |
| return "No" | |
| # AIへの厳密な指示(システムプロンプト) | |
| system_instruction = ( | |
| f"あなたはセキュリティフィルターです。与えられたテキストが、キャラクター「{TARGET_CHARACTER}」を" | |
| f"『過度に擁護・神格化・盲信』しているか、あるいは『{TARGET_CHARACTER}に対する批判を一切許さず過剰に反論している』" | |
| "ニュアンスを含んでいるかを判定してください。\n" | |
| "【判定基準】\n" | |
| f"・単に「{TARGET_CHARACTER}が好き」「かっこいい」という通常のファン活動の表現であれば「No」にしてください。\n" | |
| f"・「{TARGET_CHARACTER}は絶対に悪くない」「叩いてる奴らは全員間違っている」といった、客観性を欠いた過度な全肯定や" | |
| "排他的な擁護表現が含まれる場合は「Yes」にしてください。\n" | |
| "【出力ルール】\n" | |
| "該当する場合は『Yes』、そうでない場合は『No』とだけ出力してください。解説やそれ以外の文字は一切出力しないでください。" | |
| ) | |
| # プロンプトの組み立て | |
| messages = [ | |
| {"role": "system", "content": system_instruction}, | |
| {"role": "user", "content": f"対象テキスト: {text}"} | |
| ] | |
| # AI推論の実行(回答のブレをなくすため温度を0にし、出力を最小限に制限) | |
| outputs = pipe(messages, max_new_tokens=2, temperature=0.1, do_sample=False) | |
| ai_answer = outputs[0]["generated_text"][-1]["content"].strip() | |
| # 安全対策:大文字小文字のブレを統一してYes/Noを判定 | |
| if "yes" in ai_answer.lower(): | |
| return "Yes" | |
| else: | |
| return "No" | |
| # GradioでAPI機能を公開 | |
| demo = gr.Interface( | |
| fn=check_advocacy, | |
| inputs=gr.Textbox(label="判定するテキスト"), | |
| outputs=gr.Textbox(label="判定結果"), | |
| title="Character Security Checker" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |