Corin1998 commited on
Commit
da82e08
·
verified ·
1 Parent(s): 45e6e77

Create compliance.py

Browse files
Files changed (1) hide show
  1. app/compliance.py +50 -0
app/compliance.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ import re
3
+ from typing import List, Tuple
4
+ from .openai_client import openai_judge
5
+
6
+ # 代表的なNGワード・薬機法NG表現(例)
7
+ NG_PATTERNS = [
8
+ r"100%",
9
+ r"完全(に)?治(す|る)",
10
+ r"副作用(が)?ない",
11
+ r"医師(も)?推薦",
12
+ r"奇跡",
13
+ r"即日(で)?効果",
14
+ r"永久(に)?",
15
+ r"保証(します)?",
16
+ ]
17
+
18
+ def rule_based_check(text: str, extra_ng: List[str] | None = None) -> Tuple[bool, List[str]]:
19
+ bads = []
20
+ pats = list(NG_PATTERNS)
21
+ if extra_ng:
22
+ pats += [re.escape(w) for w in extra_ng]
23
+ for p in pats:
24
+ if re.search(p, text, flags=re.IGNORECASE):
25
+ bads.append(p)
26
+ return (len(bads) == 0), bads
27
+
28
+
29
+ JUDGE_SYSTEM_PROMPT = """
30
+ あなたは日本の広告表現審査の専門家です。薬機法・景表法・健康増進法の観点から、次の広告文の合否を判定し、問題点を箇条書きで示し、安全な修正案を1つ提示してください。
31
+ 基準:
32
+ - 機能性表示/医薬的効能効果を断定する表現は禁止(例:治る、効く、100% 等)
33
+ - 比較優良・有名人/医師推奨の示唆は禁止
34
+ - 根拠のない数値誇張や永久/即効などの絶対表現は禁止
35
+ - 体験談は個人差注記を付記
36
+ 出力JSON形式:{"pass": true/false, "reasons": ["..."], "fixed": "..."}
37
+ """
38
+
39
+ def llm_check_and_fix(text: str) -> tuple[bool, List[str], str | None]:
40
+ # 改行込みの f-string を正しくクォート
41
+ resp = openai_judge(system=JUDGE_SYSTEM_PROMPT, user=f"広告文:\n{text}")
42
+ try:
43
+ data = resp
44
+ ok = bool(data.get("pass", False))
45
+ reasons = [str(r) for r in data.get("reasons", [])]
46
+ fixed = str(data.get("fixed")) if (not ok and data.get("fixed")) else None
47
+ return ok, reasons, fixed
48
+ except Exception:
49
+ # LLM失敗時は通す(要監視)
50
+ return True, [], None