Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,15 +13,15 @@
|
|
| 13 |
# - Gradio ๋ก๊ทธ์ธ ์ธ์ฆ / public share link ์ ์ฉ
|
| 14 |
# - MergedCell column_letter ์ค๋ฅ ์์ ์๋ฃ
|
| 15 |
# ============================================================
|
| 16 |
-
|
| 17 |
# ============================================================
|
| 18 |
# reBom Regulatory Screening AI
|
| 19 |
# - ๊ตญ๊ฐ๋ณ ๊ท์ ํด๋ ์์์๋ง ๊ฒ์
|
| 20 |
# - FAISS ๋์คํฌ ์บ์
|
| 21 |
# - GPT๋ ๊ฒ์๋ ์๋ฌธ ์์์๋ง ํ์
|
| 22 |
# - ์์ฝ์ฒ ์ฌ์ดํธ ์ค์๊ฐ ์กฐํ ์ต์
|
| 23 |
-
# -
|
| 24 |
-
# -
|
|
|
|
| 25 |
# - ์๋ณธ TOTAL ํ ์ ๊ฑฐ ํ ์ TOTAL ํ 1๊ฐ๋ง ์์ฑ
|
| 26 |
# ============================================================
|
| 27 |
|
|
@@ -72,9 +72,11 @@ OUTPUT_DIR = (BASE_DIR / "outputs").resolve()
|
|
| 72 |
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 73 |
|
| 74 |
FAISS_CACHE_DIR = ".faiss_cache"
|
| 75 |
-
|
| 76 |
MFDS_LIVE_URL = "https://nedrug.mfds.go.kr/pbp/CCBDF01"
|
|
|
|
| 77 |
NO_EVIDENCE_TEXT = "ํด๋น ์๋ฃ ๋ด ์ ํ ๊ทผ๊ฑฐ ์์"
|
|
|
|
|
|
|
| 78 |
|
| 79 |
VECTOR_DB_CACHE = {}
|
| 80 |
EVAL_CACHE = {}
|
|
@@ -192,6 +194,11 @@ UV_FILTER_RULES = {
|
|
| 192 |
"homosalate": {
|
| 193 |
"label": "Homosalate",
|
| 194 |
"aliases": ["homosalate", "ํธ๋ชจ์ด๋ ์ดํธ", "118-56-9"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
"EU": {
|
| 196 |
"status": "allowed",
|
| 197 |
"limit": 7.34,
|
|
@@ -473,6 +480,7 @@ def get_identity_text(material, component):
|
|
| 473 |
component.get("INCI Name", ""),
|
| 474 |
material.get("Trade name(๊ตญ๋ฌธ)", ""),
|
| 475 |
material.get("Trade name(์๋ฌธ)", ""),
|
|
|
|
| 476 |
]
|
| 477 |
|
| 478 |
return normalize_text(" ".join([safe_str(x) for x in texts if safe_str(x)]))
|
|
@@ -490,6 +498,40 @@ def match_rule_key(material, component, rule_dict):
|
|
| 490 |
return None
|
| 491 |
|
| 492 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
def is_country_result_issue(text):
|
| 494 |
text = safe_str(text)
|
| 495 |
|
|
@@ -499,6 +541,7 @@ def is_country_result_issue(text):
|
|
| 499 |
keywords = [
|
| 500 |
"๋ฐฐํฉ๊ธ์ง", "๊ธ์ง", "์ด๊ณผ", "๋ถ์ ํฉ", "์ฌ๊ฒํ ", "ํ์ธ ํ์",
|
| 501 |
"์๋ฃ ํ์ธ", "ํ์ ํ์", "๋ผ๋ฒจ๋ง", "๋ถ๊ฐ", "์ฃผ์", "๊ฒฝ๊ณ ",
|
|
|
|
| 502 |
]
|
| 503 |
|
| 504 |
return any(k in text for k in keywords)
|
|
@@ -526,6 +569,12 @@ def normalize_formula_country_text(text, country_col="", inci_name="", amount_te
|
|
| 526 |
|
| 527 |
lower = text.lower()
|
| 528 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
if "octocrylene" in inci_norm or "์ฅํ ํฌ๋ฆด๋ " in inci_norm:
|
| 530 |
if country_col == "EU":
|
| 531 |
return "๋ฐฐํฉ ํ๋:\n์คํ๋ ์ด: 9% /\n๊ธฐํ์ ํ: 10%"
|
|
@@ -535,6 +584,8 @@ def normalize_formula_country_text(text, country_col="", inci_name="", amount_te
|
|
| 535 |
return "๋ฐฐํฉํ๋: 10%"
|
| 536 |
|
| 537 |
if "homosalate" in inci_norm or "ํธ๋ชจ์ด๋ ์ดํธ" in inci_norm:
|
|
|
|
|
|
|
| 538 |
if country_col == "EU":
|
| 539 |
return "๋ฐฐํฉํ๋: 7.34%"
|
| 540 |
if country_col == "FDA":
|
|
@@ -568,7 +619,7 @@ def normalize_formula_country_text(text, country_col="", inci_name="", amount_te
|
|
| 568 |
return "๋ฐฐํฉํ๋: " + percent_match.group(1).replace(" ", "")
|
| 569 |
|
| 570 |
if "ํ์ธ ํ์" in text or "์๋ฃ ํ์ธ" in text or "์ฌ๊ฒํ " in text or "๊ฒํ ํ์" in text:
|
| 571 |
-
return
|
| 572 |
|
| 573 |
if "์ด๋ด" in text or "ํต๊ณผ" in text or "ํ๋ ์ด๋ด" in text:
|
| 574 |
return "Pass"
|
|
@@ -1551,6 +1602,7 @@ def ingredient_matches_live_row(material, component, row):
|
|
| 1551 |
identity_values = [
|
| 1552 |
component.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
| 1553 |
component.get("INCI Name", ""),
|
|
|
|
| 1554 |
material.get("Trade name(๊ตญ๋ฌธ)", ""),
|
| 1555 |
material.get("Trade name(์๋ฌธ)", ""),
|
| 1556 |
]
|
|
@@ -1578,6 +1630,8 @@ def live_row_country_matches(country, row_country):
|
|
| 1578 |
aliases += ["๋ฏธ๊ตญ", "USA", "US", "United States", "FDA"]
|
| 1579 |
elif col == "EU":
|
| 1580 |
aliases += ["EU", "์ ๋ฝ", "์ ๋ฝ์ฐํฉ", "European Union", "Europe"]
|
|
|
|
|
|
|
| 1581 |
|
| 1582 |
aliases_norm = [normalize_compact(x) for x in aliases if safe_str(x)]
|
| 1583 |
return any(alias and alias in row_country_norm for alias in aliases_norm)
|
|
@@ -1633,13 +1687,18 @@ def evaluate_limit_rule(country, material, component, rule_group, rule_type_name
|
|
| 1633 |
|
| 1634 |
rule = rule_group[key]
|
| 1635 |
country_rule = rule.get(ckey)
|
| 1636 |
-
|
| 1637 |
-
if not country_rule:
|
| 1638 |
-
return None
|
| 1639 |
-
|
| 1640 |
label = rule.get("label", key)
|
| 1641 |
amount = component.get("amount_float")
|
| 1642 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1643 |
status = country_rule.get("status", "allowed")
|
| 1644 |
formula_text = country_rule.get("formula_text", "")
|
| 1645 |
|
|
@@ -1656,21 +1715,23 @@ def evaluate_limit_rule(country, material, component, rule_group, rule_type_name
|
|
| 1656 |
if limit is not None:
|
| 1657 |
if amount is not None and amount > limit:
|
| 1658 |
result_text = f"{rule_type_name} / {label} ํ์ฌ ํจ๋ {amount_text}%, ํ๋ {format_percent(limit)}% ์ด๊ณผ(์ฌ๊ฒํ ํ์)"
|
|
|
|
| 1659 |
else:
|
| 1660 |
-
result_text = f"{rule_type_name} / {label} ํ์ฌ ํจ๋ {amount_text}%, ํ๋ {format_percent(limit)}% ์ด๋ด
|
|
|
|
| 1661 |
|
| 1662 |
return {
|
| 1663 |
"๋ํ INCI": label,
|
| 1664 |
"๊ฒฐ๊ณผ": result_text,
|
| 1665 |
-
"ํ์๋ฌธ๊ตฌ":
|
| 1666 |
"๋น๊ณ ": "-",
|
| 1667 |
}
|
| 1668 |
|
| 1669 |
return {
|
| 1670 |
"๋ํ INCI": label,
|
| 1671 |
-
"๊ฒฐ๊ณผ":
|
| 1672 |
-
"ํ์๋ฌธ๊ตฌ":
|
| 1673 |
-
"๋น๊ณ ": "
|
| 1674 |
}
|
| 1675 |
|
| 1676 |
|
|
@@ -1713,7 +1774,12 @@ def evaluate_local_rules(country, material, component, ingredient_totals, ingred
|
|
| 1713 |
continue
|
| 1714 |
|
| 1715 |
if ckey not in ["EU", "EU_FUTURE", "UK", "KOREA"]:
|
| 1716 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1717 |
|
| 1718 |
total_amount = ingredient_totals.get(fragrance_key, amount)
|
| 1719 |
total_text = format_percent(total_amount) if total_amount is not None else "ํจ๋ ํ์ธ ํ์"
|
|
@@ -1786,7 +1852,7 @@ def clean_json_text(text):
|
|
| 1786 |
return text
|
| 1787 |
|
| 1788 |
|
| 1789 |
-
def sanitize_country_result(parsed, country_col="", component=None):
|
| 1790 |
if not isinstance(parsed, dict):
|
| 1791 |
parsed = {}
|
| 1792 |
|
|
@@ -1798,6 +1864,11 @@ def sanitize_country_result(parsed, country_col="", component=None):
|
|
| 1798 |
|
| 1799 |
display_text = safe_str(parsed.get("ํ์๋ฌธ๊ตฌ", ""))
|
| 1800 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1801 |
if not display_text:
|
| 1802 |
display_text = normalize_formula_country_text(
|
| 1803 |
result_text,
|
|
@@ -1813,7 +1884,7 @@ def sanitize_country_result(parsed, country_col="", component=None):
|
|
| 1813 |
|
| 1814 |
if any(token in result_text for token in bad_tokens):
|
| 1815 |
result_text = "ํ์ธ ํ์ / ๊ท์ ์ธ๋ถ ๊ณ์ฐ ์ฌํ์ธ ํ์"
|
| 1816 |
-
display_text =
|
| 1817 |
|
| 1818 |
if note == "-":
|
| 1819 |
note = "๊ตฌ์ฒด ํ๋ ๋๋ ์ด๋ ๊ณ์ฐ๊ฐ ํ์ธ ํ์"
|
|
@@ -1836,6 +1907,7 @@ def make_eval_cache_key(country, component):
|
|
| 1836 |
str(db_signature),
|
| 1837 |
safe_str(component.get("INCI Name", "")).lower(),
|
| 1838 |
safe_str(component.get("ํ๊ธ ์ฑ๋ถ๋ช
", "")).lower(),
|
|
|
|
| 1839 |
amount_text,
|
| 1840 |
)
|
| 1841 |
|
|
@@ -1867,19 +1939,30 @@ def evaluate_component_with_llm(country, material, component, llm, target_id="")
|
|
| 1867 |
|
| 1868 |
inci_name = safe_str(component.get("INCI Name", ""))
|
| 1869 |
korean_name = safe_str(component.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
|
|
|
| 1870 |
amount = component.get("amount_float")
|
| 1871 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
|
|
|
| 1872 |
|
| 1873 |
if not inci_name and not korean_name:
|
| 1874 |
return {"๋ํ INCI": "-", "๊ฒฐ๊ณผ": "", "ํ์๋ฌธ๊ตฌ": "", "๋น๊ณ ": "-"}
|
| 1875 |
|
| 1876 |
if not db:
|
| 1877 |
-
|
| 1878 |
-
|
| 1879 |
-
|
| 1880 |
-
|
| 1881 |
-
|
| 1882 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1883 |
log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "NO_DB", "ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์")
|
| 1884 |
return result
|
| 1885 |
|
|
@@ -1896,7 +1979,8 @@ def evaluate_component_with_llm(country, material, component, llm, target_id="")
|
|
| 1896 |
์๋ฃ๋ช
๊ตญ๋ฌธ: {trade_ko}
|
| 1897 |
์๋ฃ๋ช
์๋ฌธ: {trade_en}
|
| 1898 |
๊ฒํ ์ฑ๋ถ ๊ตญ๋ฌธ: {korean_name}
|
| 1899 |
-
๊ฒํ ์ฑ๋ถ INCI
|
|
|
|
| 1900 |
์ต์ข
์ ํ ๋ด ํจ๋: {amount_text}%
|
| 1901 |
์ ํ์ ํ: Leave-on ๋ฐ Wash-off ๊ธฐ์ค ๋ชจ๋ ํ์ธ
|
| 1902 |
"""
|
|
@@ -1904,17 +1988,26 @@ def evaluate_component_with_llm(country, material, component, llm, target_id="")
|
|
| 1904 |
has_hit, pre_context = has_meaningful_search_hit(
|
| 1905 |
db=db,
|
| 1906 |
search_text=search_text,
|
| 1907 |
-
ingredient_names=[inci_name, korean_name],
|
| 1908 |
k=MEANINGFUL_HIT_K,
|
| 1909 |
)
|
| 1910 |
|
| 1911 |
if not has_hit:
|
| 1912 |
-
|
| 1913 |
-
|
| 1914 |
-
|
| 1915 |
-
|
| 1916 |
-
|
| 1917 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1918 |
EVAL_CACHE[cache_key] = result
|
| 1919 |
log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "NO_MEANINGFUL_HIT", pre_context)
|
| 1920 |
return result
|
|
@@ -1933,11 +2026,14 @@ def evaluate_component_with_llm(country, material, component, llm, target_id="")
|
|
| 1933 |
- ๋ค๋ฅธ ๊ตญ๊ฐ ๊ท์ ์ ํ์ฌ ๊ตญ๊ฐ์ ์์ ์ ์ฉํ์ง ๋ง.
|
| 1934 |
- ๊ฒ์ ์๋ฌธ์ ๋ช
์๋์ง ์์ ์ ํ, ๋ฐฐํฉ๊ธ์ง, ๋ฐฐํฉํ๋๋ ๋ง๋ค์ง ๋ง.
|
| 1935 |
- ๊ฒ์ ์๋ฌธ์ ๋ช
์๋ ์ ํ/๊ธ์ง/ํ๋ ๊ทผ๊ฑฐ๊ฐ ์์ผ๋ฉด ๊ฒฐ๊ณผ๋ ์ ํํ "{NO_EVIDENCE_TEXT}"์ผ๋ก ์์ฑํด.
|
|
|
|
| 1936 |
|
| 1937 |
[๊ฒํ ์ฑ๋ถ]
|
| 1938 |
๊ตญ๋ฌธ๋ช
: {korean_name}
|
| 1939 |
-
INCI
|
|
|
|
| 1940 |
์ต์ข
์ ํ ๋ด ํจ๋: {amount_text}%
|
|
|
|
| 1941 |
|
| 1942 |
[๊ฒ์๋ {country} ๊ท์ ์๋ฌธ]
|
| 1943 |
{context}
|
|
@@ -1959,7 +2055,13 @@ INCI/CAS: {inci_name}
|
|
| 1959 |
try:
|
| 1960 |
response = llm.invoke(prompt).content
|
| 1961 |
parsed = parse_country_response(response)
|
| 1962 |
-
parsed = sanitize_country_result(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1963 |
EVAL_CACHE[cache_key] = parsed
|
| 1964 |
log_search_context(target_id, country, component, parsed["๊ฒฐ๊ณผ"], "GPT_SINGLE", context)
|
| 1965 |
return parsed
|
|
@@ -1968,7 +2070,7 @@ INCI/CAS: {inci_name}
|
|
| 1968 |
result = {
|
| 1969 |
"๋ํ INCI": inci_name or "-",
|
| 1970 |
"๊ฒฐ๊ณผ": "ํ์ธ ํ์",
|
| 1971 |
-
"ํ
|
| 1972 |
"๋น๊ณ ": str(e),
|
| 1973 |
}
|
| 1974 |
log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "GPT_SINGLE_ERROR", str(e))
|
|
@@ -1987,16 +2089,35 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 1987 |
if not db:
|
| 1988 |
for item in targets:
|
| 1989 |
comp = item["component"]
|
|
|
|
| 1990 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1991 |
|
| 1992 |
-
results[item["target_id"]] =
|
| 1993 |
-
|
| 1994 |
-
"
|
| 1995 |
-
|
| 1996 |
-
|
| 1997 |
-
|
| 1998 |
-
|
| 1999 |
-
|
|
|
|
| 2000 |
|
| 2001 |
return results
|
| 2002 |
|
|
@@ -2019,37 +2140,56 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2019 |
|
| 2020 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2021 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
|
|
|
| 2022 |
amount = comp.get("amount_float")
|
| 2023 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
|
|
|
| 2024 |
|
| 2025 |
search_text = f"""
|
| 2026 |
๊ตญ๊ฐ/์ฑ๋: {country}
|
| 2027 |
์๋ฃ๋ช
๊ตญ๋ฌธ: {material.get("Trade name(๊ตญ๋ฌธ)", "")}
|
| 2028 |
์๋ฃ๋ช
์๋ฌธ: {material.get("Trade name(์๋ฌธ)", "")}
|
| 2029 |
๊ฒํ ์ฑ๋ถ ๊ตญ๋ฌธ: {korean_name}
|
| 2030 |
-
๊ฒํ ์ฑ๋ถ INCI
|
|
|
|
| 2031 |
์ต์ข
์ ํ ๋ด ํจ๋: {amount_text}%
|
| 2032 |
"""
|
| 2033 |
|
| 2034 |
has_hit, context = has_meaningful_search_hit(
|
| 2035 |
db=db,
|
| 2036 |
search_text=search_text,
|
| 2037 |
-
ingredient_names=[inci_name, korean_name],
|
| 2038 |
k=MEANINGFUL_HIT_K,
|
| 2039 |
)
|
| 2040 |
|
| 2041 |
if has_hit:
|
| 2042 |
filtered_targets.append(item)
|
| 2043 |
else:
|
| 2044 |
-
|
| 2045 |
-
|
| 2046 |
-
|
| 2047 |
-
|
| 2048 |
-
|
| 2049 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2050 |
results[item["target_id"]] = result
|
| 2051 |
EVAL_CACHE[make_eval_cache_key(country, comp)] = result
|
| 2052 |
-
log_search_context(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2053 |
|
| 2054 |
uncached_targets = filtered_targets
|
| 2055 |
|
|
@@ -2062,19 +2202,24 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2062 |
|
| 2063 |
for item in batch:
|
| 2064 |
comp = item["component"]
|
|
|
|
| 2065 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2066 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
|
|
|
| 2067 |
amount = comp.get("amount_float")
|
| 2068 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
|
|
|
| 2069 |
|
| 2070 |
target_payload.append({
|
| 2071 |
"target_id": item["target_id"],
|
| 2072 |
"์ฑ๋ถ ๊ตญ๋ฌธ๋ช
": korean_name,
|
| 2073 |
-
"INCI
|
|
|
|
| 2074 |
"์ต์ข
์ ํ ๋ด ํจ๋(%)": amount_text,
|
|
|
|
| 2075 |
})
|
| 2076 |
|
| 2077 |
-
search_blocks.append(f"{inci_name} {korean_name} {amount_text}%")
|
| 2078 |
|
| 2079 |
search_text = f"""
|
| 2080 |
๊ตญ๊ฐ/์ฑ๋: {country}
|
|
@@ -2095,6 +2240,7 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2095 |
- ์๋ ๊ฒ์ ์๋ฌธ์ "{country}" ํด๋์์๋ง ๊ฒ์๋ ๊ทผ๊ฑฐ๋ค.
|
| 2096 |
- ์ธ๋ถ ์ง์, ํ์ต ์ง์, ์ผ๋ฐ ๊ท์ ์ง์์ ์ฌ์ฉํ์ง ๋ง.
|
| 2097 |
- ๊ฒ์ ์๋ฌธ์ ๊ทผ๊ฑฐ๊ฐ ์์ผ๋ฉด ๊ฒฐ๊ณผ๋ ์ ํํ "{NO_EVIDENCE_TEXT}"์ผ๋ก ์์ฑํด.
|
|
|
|
| 2098 |
|
| 2099 |
[๊ฒํ ๋์ JSON]
|
| 2100 |
{json.dumps(target_payload, ensure_ascii=False, indent=2)}
|
|
@@ -2135,12 +2281,24 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2135 |
continue
|
| 2136 |
|
| 2137 |
item_comp = None
|
|
|
|
|
|
|
| 2138 |
for item in batch:
|
| 2139 |
if item["target_id"] == target_id:
|
| 2140 |
item_comp = item["component"]
|
|
|
|
| 2141 |
break
|
| 2142 |
|
| 2143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2144 |
|
| 2145 |
for item in batch:
|
| 2146 |
target_id = item["target_id"]
|
|
@@ -2552,6 +2710,34 @@ def process(
|
|
| 2552 |
gpt_targets = []
|
| 2553 |
|
| 2554 |
for item in row_items:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2555 |
local_result = evaluate_local_rules(
|
| 2556 |
country=country,
|
| 2557 |
material=item["material"],
|
|
@@ -2574,31 +2760,10 @@ def process(
|
|
| 2574 |
item["component"],
|
| 2575 |
local_result.get("๊ฒฐ๊ณผ", ""),
|
| 2576 |
"LOCAL_RULE",
|
| 2577 |
-
note if note and note != "-" else "๋ก์ปฌ
|
| 2578 |
)
|
| 2579 |
continue
|
| 2580 |
|
| 2581 |
-
mfds_result = None
|
| 2582 |
-
|
| 2583 |
-
if use_mfds_live and mfds_df is not None and not mfds_df.empty:
|
| 2584 |
-
mfds_result = evaluate_live_detail_rule(
|
| 2585 |
-
country=country,
|
| 2586 |
-
material=item["material"],
|
| 2587 |
-
component=item["component"],
|
| 2588 |
-
live_df=mfds_df,
|
| 2589 |
-
)
|
| 2590 |
-
|
| 2591 |
-
if mfds_result is not None:
|
| 2592 |
-
item["row_data"][col_name] = mfds_result.get("๊ฒฐ๊ณผ", "")
|
| 2593 |
-
item["row_data"][col_name + "_display"] = mfds_result.get("ํ์๋ฌธ๊ตฌ", "ํ์ธ ํ์")
|
| 2594 |
-
|
| 2595 |
-
note = safe_str(mfds_result.get("๋น๊ณ ", "-"))
|
| 2596 |
-
if note and note != "-":
|
| 2597 |
-
item["notes"].append(f"{col_name}: {note}")
|
| 2598 |
-
|
| 2599 |
-
log_search_context(item["target_id"], country, item["component"], mfds_result.get("๊ฒฐ๊ณผ", ""), "MFDS_LIVE", note)
|
| 2600 |
-
continue
|
| 2601 |
-
|
| 2602 |
gpt_targets.append({
|
| 2603 |
"target_id": item["target_id"],
|
| 2604 |
"material": item["material"],
|
|
@@ -2617,7 +2782,7 @@ def process(
|
|
| 2617 |
result = {
|
| 2618 |
"๋ํ INCI": safe_str(item["component"].get("INCI Name", "")) or "-",
|
| 2619 |
"๊ฒฐ๊ณผ": "ํ์ธ ํ์",
|
| 2620 |
-
"ํ์๋ฌธ๊ตฌ":
|
| 2621 |
"๋น๊ณ ": "batch ๊ฒฐ๊ณผ ๋๋ฝ",
|
| 2622 |
}
|
| 2623 |
|
|
@@ -2832,4 +2997,4 @@ except TypeError:
|
|
| 2832 |
auth=auth_fn,
|
| 2833 |
allowed_paths=ALLOWED_PATHS,
|
| 2834 |
blocked_paths=BLOCKED_PATHS,
|
| 2835 |
-
)
|
|
|
|
| 13 |
# - Gradio ๋ก๊ทธ์ธ ์ธ์ฆ / public share link ์ ์ฉ
|
| 14 |
# - MergedCell column_letter ์ค๋ฅ ์์ ์๋ฃ
|
| 15 |
# ============================================================
|
|
|
|
| 16 |
# ============================================================
|
| 17 |
# reBom Regulatory Screening AI
|
| 18 |
# - ๊ตญ๊ฐ๋ณ ๊ท์ ํด๋ ์์์๋ง ๊ฒ์
|
| 19 |
# - FAISS ๋์คํฌ ์บ์
|
| 20 |
# - GPT๋ ๊ฒ์๋ ์๋ฌธ ์์์๋ง ํ์
|
| 21 |
# - ์์ฝ์ฒ ์ฌ์ดํธ ์ค์๊ฐ ์กฐํ ์ต์
|
| 22 |
+
# - COOS ์ ๊ฑฐ
|
| 23 |
+
# - ๊ณ ์ํ ์ฑ๋ถ Pass ์๋ ๋ฐฉ์ง
|
| 24 |
+
# - Formular Ingredients Statement ์์ ์ถ๋ ฅ
|
| 25 |
# - ์๋ณธ TOTAL ํ ์ ๊ฑฐ ํ ์ TOTAL ํ 1๊ฐ๋ง ์์ฑ
|
| 26 |
# ============================================================
|
| 27 |
|
|
|
|
| 72 |
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 73 |
|
| 74 |
FAISS_CACHE_DIR = ".faiss_cache"
|
|
|
|
| 75 |
MFDS_LIVE_URL = "https://nedrug.mfds.go.kr/pbp/CCBDF01"
|
| 76 |
+
|
| 77 |
NO_EVIDENCE_TEXT = "ํด๋น ์๋ฃ ๋ด ์ ํ ๊ทผ๊ฑฐ ์์"
|
| 78 |
+
NEED_CHECK_TEXT = "ํ์ธ ํ์"
|
| 79 |
+
LIMIT_CHECK_TEXT = "๋ฐฐํฉํ๋ ํ์ธ ํ์"
|
| 80 |
|
| 81 |
VECTOR_DB_CACHE = {}
|
| 82 |
EVAL_CACHE = {}
|
|
|
|
| 194 |
"homosalate": {
|
| 195 |
"label": "Homosalate",
|
| 196 |
"aliases": ["homosalate", "ํธ๋ชจ์ด๋ ์ดํธ", "118-56-9"],
|
| 197 |
+
"ASEAN": {
|
| 198 |
+
"status": "allowed",
|
| 199 |
+
"limit": 10,
|
| 200 |
+
"formula_text": "๋ฐฐํฉํ๋: 10%",
|
| 201 |
+
},
|
| 202 |
"EU": {
|
| 203 |
"status": "allowed",
|
| 204 |
"limit": 7.34,
|
|
|
|
| 480 |
component.get("INCI Name", ""),
|
| 481 |
material.get("Trade name(๊ตญ๋ฌธ)", ""),
|
| 482 |
material.get("Trade name(์๋ฌธ)", ""),
|
| 483 |
+
component.get("CAS.No", ""),
|
| 484 |
]
|
| 485 |
|
| 486 |
return normalize_text(" ".join([safe_str(x) for x in texts if safe_str(x)]))
|
|
|
|
| 498 |
return None
|
| 499 |
|
| 500 |
|
| 501 |
+
def get_restricted_candidate_reason(material, component):
|
| 502 |
+
uv_key = match_rule_key(material, component, UV_FILTER_RULES)
|
| 503 |
+
if uv_key:
|
| 504 |
+
return "UV ํํฐ"
|
| 505 |
+
|
| 506 |
+
preservative_key = match_rule_key(material, component, PRESERVATIVE_RULES)
|
| 507 |
+
if preservative_key:
|
| 508 |
+
return "๋ณด์กด์ /์ ํ์ฑ๋ถ"
|
| 509 |
+
|
| 510 |
+
functional_key = match_rule_key(material, component, FUNCTIONAL_KOREA_RULES)
|
| 511 |
+
if functional_key:
|
| 512 |
+
return "๊ธฐ๋ฅ์ฑ ์ฑ๋ถ"
|
| 513 |
+
|
| 514 |
+
identity = get_identity_text(material, component)
|
| 515 |
+
for _, rule in FRAGRANCE_ALLERGEN_RULES.items():
|
| 516 |
+
for alias in rule.get("aliases", []):
|
| 517 |
+
alias_norm = normalize_text(alias)
|
| 518 |
+
if alias_norm and alias_norm in identity:
|
| 519 |
+
return "ํฅ๋ฃ ์๋ ๋ฅด๊ฒ"
|
| 520 |
+
|
| 521 |
+
function_text = normalize_text(component.get("FUNCTION", ""))
|
| 522 |
+
inci_text = normalize_text(component.get("INCI Name", ""))
|
| 523 |
+
|
| 524 |
+
high_risk_words = [
|
| 525 |
+
"sunscreen", "uv filter", "preservative", "colorant", "hair dye",
|
| 526 |
+
"retinol", "retinal", "retinyl", "salicylic acid",
|
| 527 |
+
]
|
| 528 |
+
|
| 529 |
+
if any(w in function_text for w in high_risk_words) or any(w in inci_text for w in high_risk_words):
|
| 530 |
+
return "์ ํ ๊ฐ๋ฅ์ฑ ์ฑ๋ถ"
|
| 531 |
+
|
| 532 |
+
return ""
|
| 533 |
+
|
| 534 |
+
|
| 535 |
def is_country_result_issue(text):
|
| 536 |
text = safe_str(text)
|
| 537 |
|
|
|
|
| 541 |
keywords = [
|
| 542 |
"๋ฐฐํฉ๊ธ์ง", "๊ธ์ง", "์ด๊ณผ", "๋ถ์ ํฉ", "์ฌ๊ฒํ ", "ํ์ธ ํ์",
|
| 543 |
"์๋ฃ ํ์ธ", "ํ์ ํ์", "๋ผ๋ฒจ๋ง", "๋ถ๊ฐ", "์ฃผ์", "๊ฒฝ๊ณ ",
|
| 544 |
+
"๋ฐฐํฉํ๋ ํ์ธ ํ์",
|
| 545 |
]
|
| 546 |
|
| 547 |
return any(k in text for k in keywords)
|
|
|
|
| 569 |
|
| 570 |
lower = text.lower()
|
| 571 |
|
| 572 |
+
if LIMIT_CHECK_TEXT in text:
|
| 573 |
+
return LIMIT_CHECK_TEXT
|
| 574 |
+
|
| 575 |
+
if NEED_CHECK_TEXT in text:
|
| 576 |
+
return NEED_CHECK_TEXT
|
| 577 |
+
|
| 578 |
if "octocrylene" in inci_norm or "์ฅํ ํฌ๋ฆด๋ " in inci_norm:
|
| 579 |
if country_col == "EU":
|
| 580 |
return "๋ฐฐํฉ ํ๋:\n์คํ๋ ์ด: 9% /\n๊ธฐํ์ ํ: 10%"
|
|
|
|
| 584 |
return "๋ฐฐํฉํ๋: 10%"
|
| 585 |
|
| 586 |
if "homosalate" in inci_norm or "ํธ๋ชจ์ด๋ ์ดํธ" in inci_norm:
|
| 587 |
+
if country_col == "ASEAN":
|
| 588 |
+
return "๋ฐฐํฉํ๋: 10%"
|
| 589 |
if country_col == "EU":
|
| 590 |
return "๋ฐฐํฉํ๋: 7.34%"
|
| 591 |
if country_col == "FDA":
|
|
|
|
| 619 |
return "๋ฐฐํฉํ๋: " + percent_match.group(1).replace(" ", "")
|
| 620 |
|
| 621 |
if "ํ์ธ ํ์" in text or "์๋ฃ ํ์ธ" in text or "์ฌ๊ฒํ " in text or "๊ฒํ ํ์" in text:
|
| 622 |
+
return NEED_CHECK_TEXT
|
| 623 |
|
| 624 |
if "์ด๋ด" in text or "ํต๊ณผ" in text or "ํ๋ ์ด๋ด" in text:
|
| 625 |
return "Pass"
|
|
|
|
| 1602 |
identity_values = [
|
| 1603 |
component.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
| 1604 |
component.get("INCI Name", ""),
|
| 1605 |
+
component.get("CAS.No", ""),
|
| 1606 |
material.get("Trade name(๊ตญ๋ฌธ)", ""),
|
| 1607 |
material.get("Trade name(์๋ฌธ)", ""),
|
| 1608 |
]
|
|
|
|
| 1630 |
aliases += ["๋ฏธ๊ตญ", "USA", "US", "United States", "FDA"]
|
| 1631 |
elif col == "EU":
|
| 1632 |
aliases += ["EU", "์ ๋ฝ", "์ ๋ฝ์ฐํฉ", "European Union", "Europe"]
|
| 1633 |
+
elif col == "ASEAN":
|
| 1634 |
+
aliases += ["ASEAN", "์์ธ์"]
|
| 1635 |
|
| 1636 |
aliases_norm = [normalize_compact(x) for x in aliases if safe_str(x)]
|
| 1637 |
return any(alias and alias in row_country_norm for alias in aliases_norm)
|
|
|
|
| 1687 |
|
| 1688 |
rule = rule_group[key]
|
| 1689 |
country_rule = rule.get(ckey)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1690 |
label = rule.get("label", key)
|
| 1691 |
amount = component.get("amount_float")
|
| 1692 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
| 1693 |
+
|
| 1694 |
+
if not country_rule:
|
| 1695 |
+
return {
|
| 1696 |
+
"๋ํ INCI": label,
|
| 1697 |
+
"๊ฒฐ๊ณผ": f"{rule_type_name} / {label} / {display_country(country)} ๋ฐฐํฉํ๋ ์๋ฃ ํ์ธ ํ์",
|
| 1698 |
+
"ํ์๋ฌธ๊ตฌ": LIMIT_CHECK_TEXT,
|
| 1699 |
+
"๋น๊ณ ": "๋ก์ปฌ ์ ํ์ฑ๋ถ ๋ชฉ๋ก์๋ ์์ผ๋ ํด๋น ๊ตญ๊ฐ ํ๋๊ฐ ๋ฏธ๋ฑ๋ก. Pass ์ฒ๋ฆฌ ๊ธ์ง.",
|
| 1700 |
+
}
|
| 1701 |
+
|
| 1702 |
status = country_rule.get("status", "allowed")
|
| 1703 |
formula_text = country_rule.get("formula_text", "")
|
| 1704 |
|
|
|
|
| 1715 |
if limit is not None:
|
| 1716 |
if amount is not None and amount > limit:
|
| 1717 |
result_text = f"{rule_type_name} / {label} ํ์ฌ ํจ๋ {amount_text}%, ํ๋ {format_percent(limit)}% ์ด๊ณผ(์ฌ๊ฒํ ํ์)"
|
| 1718 |
+
display_text = "ํ๋ ์ด๊ณผ"
|
| 1719 |
else:
|
| 1720 |
+
result_text = f"{rule_type_name} / {label} ํ์ฌ ํจ๋ {amount_text}%, ํ๋ {format_percent(limit)}% ์ด๋ด"
|
| 1721 |
+
display_text = formula_text or f"๋ฐฐํฉํ๋: {format_percent(limit)}%"
|
| 1722 |
|
| 1723 |
return {
|
| 1724 |
"๋ํ INCI": label,
|
| 1725 |
"๊ฒฐ๊ณผ": result_text,
|
| 1726 |
+
"ํ์๋ฌธ๊ตฌ": display_text,
|
| 1727 |
"๋น๊ณ ": "-",
|
| 1728 |
}
|
| 1729 |
|
| 1730 |
return {
|
| 1731 |
"๋ํ INCI": label,
|
| 1732 |
+
"๊ฒฐ๊ณผ": f"{rule_type_name} / {label} / ํ๋๊ฐ ํ์ธ ํ์",
|
| 1733 |
+
"ํ์๋ฌธ๊ตฌ": LIMIT_CHECK_TEXT,
|
| 1734 |
+
"๋น๊ณ ": "๋ก์ปฌ ๊ท์น์ ์ฑ๋ถ์ ์์ผ๋ ํ๋๊ฐ์ด ๋น์ด ์์. Pass ์ฒ๋ฆฌ ๊ธ์ง.",
|
| 1735 |
}
|
| 1736 |
|
| 1737 |
|
|
|
|
| 1774 |
continue
|
| 1775 |
|
| 1776 |
if ckey not in ["EU", "EU_FUTURE", "UK", "KOREA"]:
|
| 1777 |
+
return {
|
| 1778 |
+
"๋ํ INCI": rule["label"],
|
| 1779 |
+
"๊ฒฐ๊ณผ": f"ํฅ๋ฃ ์๋ ๋ฅด๊ฒ / {rule['label']} / {display_country(country)} ํ์๊ธฐ์ค ์๋ฃ ํ์ธ ํ์",
|
| 1780 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 1781 |
+
"๋น๊ณ ": "ํฅ๋ฃ ์๋ ๋ฅด๊ฒ ํ๋ณด์ด๋ ํด๋น ๊ตญ๊ฐ ๊ธฐ์ค ๋ฏธ๋ฑ๋ก. Pass ์ฒ๋ฆฌ ๊ธ์ง.",
|
| 1782 |
+
}
|
| 1783 |
|
| 1784 |
total_amount = ingredient_totals.get(fragrance_key, amount)
|
| 1785 |
total_text = format_percent(total_amount) if total_amount is not None else "ํจ๋ ํ์ธ ํ์"
|
|
|
|
| 1852 |
return text
|
| 1853 |
|
| 1854 |
|
| 1855 |
+
def sanitize_country_result(parsed, country_col="", component=None, high_risk_reason=""):
|
| 1856 |
if not isinstance(parsed, dict):
|
| 1857 |
parsed = {}
|
| 1858 |
|
|
|
|
| 1864 |
|
| 1865 |
display_text = safe_str(parsed.get("ํ์๋ฌธ๊ตฌ", ""))
|
| 1866 |
|
| 1867 |
+
if high_risk_reason and result_text == NO_EVIDENCE_TEXT:
|
| 1868 |
+
result_text = f"{high_risk_reason} / {representative} / {country_col} ์ ํ ์ฌ๋ถ ํ์ธ ํ์"
|
| 1869 |
+
display_text = NEED_CHECK_TEXT
|
| 1870 |
+
note = "๊ณ ์ํ๊ตฐ ํ๋ณด ์ฑ๋ถ์ด๋ฏ๋ก ๊ทผ๊ฑฐ ๋ฏธ๊ฒ์ถ๋ง์ผ๋ก Pass ์ฒ๋ฆฌํ์ง ์์"
|
| 1871 |
+
|
| 1872 |
if not display_text:
|
| 1873 |
display_text = normalize_formula_country_text(
|
| 1874 |
result_text,
|
|
|
|
| 1884 |
|
| 1885 |
if any(token in result_text for token in bad_tokens):
|
| 1886 |
result_text = "ํ์ธ ํ์ / ๊ท์ ์ธ๋ถ ๊ณ์ฐ ์ฌํ์ธ ํ์"
|
| 1887 |
+
display_text = NEED_CHECK_TEXT
|
| 1888 |
|
| 1889 |
if note == "-":
|
| 1890 |
note = "๊ตฌ์ฒด ํ๋ ๋๋ ์ด๋ ๊ณ์ฐ๊ฐ ํ์ธ ํ์"
|
|
|
|
| 1907 |
str(db_signature),
|
| 1908 |
safe_str(component.get("INCI Name", "")).lower(),
|
| 1909 |
safe_str(component.get("ํ๊ธ ์ฑ๋ถ๋ช
", "")).lower(),
|
| 1910 |
+
safe_str(component.get("CAS.No", "")).lower(),
|
| 1911 |
amount_text,
|
| 1912 |
)
|
| 1913 |
|
|
|
|
| 1939 |
|
| 1940 |
inci_name = safe_str(component.get("INCI Name", ""))
|
| 1941 |
korean_name = safe_str(component.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 1942 |
+
cas_no = safe_str(component.get("CAS.No", ""))
|
| 1943 |
amount = component.get("amount_float")
|
| 1944 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
| 1945 |
+
high_risk_reason = get_restricted_candidate_reason(material, component)
|
| 1946 |
|
| 1947 |
if not inci_name and not korean_name:
|
| 1948 |
return {"๋ํ INCI": "-", "๊ฒฐ๊ณผ": "", "ํ์๋ฌธ๊ตฌ": "", "๋น๊ณ ": "-"}
|
| 1949 |
|
| 1950 |
if not db:
|
| 1951 |
+
if high_risk_reason:
|
| 1952 |
+
result = {
|
| 1953 |
+
"๋ํ INCI": inci_name or korean_name or "-",
|
| 1954 |
+
"๊ฒฐ๊ณผ": f"{high_risk_reason} / {inci_name or korean_name} / {country_col} ์ ํ ์ฌ๋ถ ํ์ธ ํ์",
|
| 1955 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 1956 |
+
"๋น๊ณ ": "ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์. ๊ณ ์ํ๊ตฐ ํ๋ณด์ด๋ฏ๋ก Pass ์ฒ๋ฆฌํ์ง ์์.",
|
| 1957 |
+
}
|
| 1958 |
+
else:
|
| 1959 |
+
result = {
|
| 1960 |
+
"๋ํ INCI": inci_name or "-",
|
| 1961 |
+
"๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
|
| 1962 |
+
"ํ์๋ฌธ๊ตฌ": "Pass",
|
| 1963 |
+
"๋น๊ณ ": "-",
|
| 1964 |
+
}
|
| 1965 |
+
|
| 1966 |
log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "NO_DB", "ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์")
|
| 1967 |
return result
|
| 1968 |
|
|
|
|
| 1979 |
์๋ฃ๋ช
๊ตญ๋ฌธ: {trade_ko}
|
| 1980 |
์๋ฃ๋ช
์๋ฌธ: {trade_en}
|
| 1981 |
๊ฒํ ์ฑ๋ถ ๊ตญ๋ฌธ: {korean_name}
|
| 1982 |
+
๊ฒํ ์ฑ๋ถ INCI: {inci_name}
|
| 1983 |
+
CAS.No: {cas_no}
|
| 1984 |
์ต์ข
์ ํ ๋ด ํจ๋: {amount_text}%
|
| 1985 |
์ ํ์ ํ: Leave-on ๋ฐ Wash-off ๊ธฐ์ค ๋ชจ๋ ํ์ธ
|
| 1986 |
"""
|
|
|
|
| 1988 |
has_hit, pre_context = has_meaningful_search_hit(
|
| 1989 |
db=db,
|
| 1990 |
search_text=search_text,
|
| 1991 |
+
ingredient_names=[inci_name, korean_name, cas_no],
|
| 1992 |
k=MEANINGFUL_HIT_K,
|
| 1993 |
)
|
| 1994 |
|
| 1995 |
if not has_hit:
|
| 1996 |
+
if high_risk_reason:
|
| 1997 |
+
result = {
|
| 1998 |
+
"๋ํ INCI": inci_name or korean_name or "-",
|
| 1999 |
+
"๊ฒฐ๊ณผ": f"{high_risk_reason} / {inci_name or korean_name} / {country_col} ์ ํ ์ฌ๋ถ ํ์ธ ํ์",
|
| 2000 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 2001 |
+
"๋น๊ณ ": "๊ณ ์ํ๊ตฐ ํ๋ณด์ด๋ ๊ตญ๊ฐ๋ณ ๊ท์ ํ์ผ์์ ์ง์ ๊ทผ๊ฑฐ ๋ฏธ๊ฒ์ถ. Pass ์ฒ๋ฆฌํ์ง ์์.",
|
| 2002 |
+
}
|
| 2003 |
+
else:
|
| 2004 |
+
result = {
|
| 2005 |
+
"๋ํ INCI": inci_name or "-",
|
| 2006 |
+
"๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
|
| 2007 |
+
"ํ์๋ฌธ๊ตฌ": "Pass",
|
| 2008 |
+
"๋น๊ณ ": "-",
|
| 2009 |
+
}
|
| 2010 |
+
|
| 2011 |
EVAL_CACHE[cache_key] = result
|
| 2012 |
log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "NO_MEANINGFUL_HIT", pre_context)
|
| 2013 |
return result
|
|
|
|
| 2026 |
- ๋ค๋ฅธ ๊ตญ๊ฐ ๊ท์ ์ ํ์ฌ ๊ตญ๊ฐ์ ์์ ์ ์ฉํ์ง ๋ง.
|
| 2027 |
- ๊ฒ์ ์๋ฌธ์ ๋ช
์๋์ง ์์ ์ ํ, ๋ฐฐํฉ๊ธ์ง, ๋ฐฐํฉํ๋๋ ๋ง๋ค์ง ๋ง.
|
| 2028 |
- ๊ฒ์ ์๋ฌธ์ ๋ช
์๋ ์ ํ/๊ธ์ง/ํ๋ ๊ทผ๊ฑฐ๊ฐ ์์ผ๋ฉด ๊ฒฐ๊ณผ๋ ์ ํํ "{NO_EVIDENCE_TEXT}"์ผ๋ก ์์ฑํด.
|
| 2029 |
+
- ๋จ, ๊ณ ์ํ๊ตฐ ํ๋ณด ์ฑ๋ถ์ด๋ฉด Pass๋ผ๊ณ ๋จ์ ํ์ง ๋ง๊ณ ํ์ธ ํ์๋ก ํ๋จํ ์ ์๋ค.
|
| 2030 |
|
| 2031 |
[๊ฒํ ์ฑ๋ถ]
|
| 2032 |
๊ตญ๋ฌธ๋ช
: {korean_name}
|
| 2033 |
+
INCI: {inci_name}
|
| 2034 |
+
CAS.No: {cas_no}
|
| 2035 |
์ต์ข
์ ํ ๋ด ํจ๋: {amount_text}%
|
| 2036 |
+
๊ณ ์ํ๊ตฐ ํ๋ณด ์ฌ๋ถ: {high_risk_reason or "์๋"}
|
| 2037 |
|
| 2038 |
[๊ฒ์๋ {country} ๊ท์ ์๋ฌธ]
|
| 2039 |
{context}
|
|
|
|
| 2055 |
try:
|
| 2056 |
response = llm.invoke(prompt).content
|
| 2057 |
parsed = parse_country_response(response)
|
| 2058 |
+
parsed = sanitize_country_result(
|
| 2059 |
+
parsed,
|
| 2060 |
+
country_col=country_col,
|
| 2061 |
+
component=component,
|
| 2062 |
+
high_risk_reason=high_risk_reason,
|
| 2063 |
+
)
|
| 2064 |
+
|
| 2065 |
EVAL_CACHE[cache_key] = parsed
|
| 2066 |
log_search_context(target_id, country, component, parsed["๊ฒฐ๊ณผ"], "GPT_SINGLE", context)
|
| 2067 |
return parsed
|
|
|
|
| 2070 |
result = {
|
| 2071 |
"๋ํ INCI": inci_name or "-",
|
| 2072 |
"๊ฒฐ๊ณผ": "ํ์ธ ํ์",
|
| 2073 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 2074 |
"๋น๊ณ ": str(e),
|
| 2075 |
}
|
| 2076 |
log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "GPT_SINGLE_ERROR", str(e))
|
|
|
|
| 2089 |
if not db:
|
| 2090 |
for item in targets:
|
| 2091 |
comp = item["component"]
|
| 2092 |
+
material = item["material"]
|
| 2093 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2094 |
+
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 2095 |
+
high_risk_reason = get_restricted_candidate_reason(material, comp)
|
| 2096 |
+
|
| 2097 |
+
if high_risk_reason:
|
| 2098 |
+
result = {
|
| 2099 |
+
"๋ํ INCI": inci_name or korean_name or "-",
|
| 2100 |
+
"๊ฒฐ๊ณผ": f"{high_risk_reason} / {inci_name or korean_name} / {country_col} ์ ํ ์ฌ๋ถ ํ์ธ ํ์",
|
| 2101 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 2102 |
+
"๋น๊ณ ": "ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์. ๊ณ ์ํ๊ตฐ ํ๋ณด์ด๋ฏ๋ก Pass ์ฒ๋ฆฌํ์ง ์์.",
|
| 2103 |
+
}
|
| 2104 |
+
else:
|
| 2105 |
+
result = {
|
| 2106 |
+
"๋ํ INCI": inci_name or "-",
|
| 2107 |
+
"๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
|
| 2108 |
+
"ํ์๋ฌธ๊ตฌ": "Pass",
|
| 2109 |
+
"๋น๊ณ ": "-",
|
| 2110 |
+
}
|
| 2111 |
|
| 2112 |
+
results[item["target_id"]] = result
|
| 2113 |
+
log_search_context(
|
| 2114 |
+
item["target_id"],
|
| 2115 |
+
country,
|
| 2116 |
+
comp,
|
| 2117 |
+
result["๊ฒฐ๊ณผ"],
|
| 2118 |
+
"NO_DB",
|
| 2119 |
+
"ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์",
|
| 2120 |
+
)
|
| 2121 |
|
| 2122 |
return results
|
| 2123 |
|
|
|
|
| 2140 |
|
| 2141 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2142 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 2143 |
+
cas_no = safe_str(comp.get("CAS.No", ""))
|
| 2144 |
amount = comp.get("amount_float")
|
| 2145 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
| 2146 |
+
high_risk_reason = get_restricted_candidate_reason(material, comp)
|
| 2147 |
|
| 2148 |
search_text = f"""
|
| 2149 |
๊ตญ๊ฐ/์ฑ๋: {country}
|
| 2150 |
์๋ฃ๋ช
๊ตญ๋ฌธ: {material.get("Trade name(๊ตญ๋ฌธ)", "")}
|
| 2151 |
์๋ฃ๋ช
์๋ฌธ: {material.get("Trade name(์๋ฌธ)", "")}
|
| 2152 |
๊ฒํ ์ฑ๋ถ ๊ตญ๋ฌธ: {korean_name}
|
| 2153 |
+
๊ฒํ ์ฑ๋ถ INCI: {inci_name}
|
| 2154 |
+
CAS.No: {cas_no}
|
| 2155 |
์ต์ข
์ ํ ๋ด ํจ๋: {amount_text}%
|
| 2156 |
"""
|
| 2157 |
|
| 2158 |
has_hit, context = has_meaningful_search_hit(
|
| 2159 |
db=db,
|
| 2160 |
search_text=search_text,
|
| 2161 |
+
ingredient_names=[inci_name, korean_name, cas_no],
|
| 2162 |
k=MEANINGFUL_HIT_K,
|
| 2163 |
)
|
| 2164 |
|
| 2165 |
if has_hit:
|
| 2166 |
filtered_targets.append(item)
|
| 2167 |
else:
|
| 2168 |
+
if high_risk_reason:
|
| 2169 |
+
result = {
|
| 2170 |
+
"๋ํ INCI": inci_name or korean_name or "-",
|
| 2171 |
+
"๊ฒฐ๊ณผ": f"{high_risk_reason} / {inci_name or korean_name} / {country_col} ์ ํ ์ฌ๋ถ ํ์ธ ํ์",
|
| 2172 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 2173 |
+
"๋น๊ณ ": "๊ณ ์ํ๊ตฐ ํ๋ณด์ด๋ ๊ตญ๊ฐ๋ณ ๊ท์ ํ์ผ์์ ์ง์ ๊ทผ๊ฑฐ ๋ฏธ๊ฒ์ถ. Pass ์ฒ๋ฆฌํ์ง ์์.",
|
| 2174 |
+
}
|
| 2175 |
+
else:
|
| 2176 |
+
result = {
|
| 2177 |
+
"๋ํ INCI": inci_name or "-",
|
| 2178 |
+
"๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
|
| 2179 |
+
"ํ์๋ฌธ๊ตฌ": "Pass",
|
| 2180 |
+
"๋น๊ณ ": "-",
|
| 2181 |
+
}
|
| 2182 |
+
|
| 2183 |
results[item["target_id"]] = result
|
| 2184 |
EVAL_CACHE[make_eval_cache_key(country, comp)] = result
|
| 2185 |
+
log_search_context(
|
| 2186 |
+
item["target_id"],
|
| 2187 |
+
country,
|
| 2188 |
+
comp,
|
| 2189 |
+
result["๊ฒฐ๊ณผ"],
|
| 2190 |
+
"NO_MEANINGFUL_HIT",
|
| 2191 |
+
context,
|
| 2192 |
+
)
|
| 2193 |
|
| 2194 |
uncached_targets = filtered_targets
|
| 2195 |
|
|
|
|
| 2202 |
|
| 2203 |
for item in batch:
|
| 2204 |
comp = item["component"]
|
| 2205 |
+
material = item["material"]
|
| 2206 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2207 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 2208 |
+
cas_no = safe_str(comp.get("CAS.No", ""))
|
| 2209 |
amount = comp.get("amount_float")
|
| 2210 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
| 2211 |
+
high_risk_reason = get_restricted_candidate_reason(material, comp)
|
| 2212 |
|
| 2213 |
target_payload.append({
|
| 2214 |
"target_id": item["target_id"],
|
| 2215 |
"์ฑ๋ถ ๊ตญ๋ฌธ๋ช
": korean_name,
|
| 2216 |
+
"INCI": inci_name,
|
| 2217 |
+
"CAS.No": cas_no,
|
| 2218 |
"์ต์ข
์ ํ ๋ด ํจ๋(%)": amount_text,
|
| 2219 |
+
"๊ณ ์ํ๊ตฐ ํ๋ณด": high_risk_reason or "์๋",
|
| 2220 |
})
|
| 2221 |
|
| 2222 |
+
search_blocks.append(f"{inci_name} {korean_name} {cas_no} {amount_text}%")
|
| 2223 |
|
| 2224 |
search_text = f"""
|
| 2225 |
๊ตญ๊ฐ/์ฑ๋: {country}
|
|
|
|
| 2240 |
- ์๋ ๊ฒ์ ์๋ฌธ์ "{country}" ํด๋์์๋ง ๊ฒ์๋ ๊ทผ๊ฑฐ๋ค.
|
| 2241 |
- ์ธ๋ถ ์ง์, ํ์ต ์ง์, ์ผ๋ฐ ๊ท์ ์ง์์ ์ฌ์ฉํ์ง ๋ง.
|
| 2242 |
- ๊ฒ์ ์๋ฌธ์ ๊ทผ๊ฑฐ๊ฐ ์์ผ๋ฉด ๊ฒฐ๊ณผ๋ ์ ํํ "{NO_EVIDENCE_TEXT}"์ผ๋ก ์์ฑํด.
|
| 2243 |
+
- ๋จ, ๊ณ ์ํ๊ตฐ ํ๋ณด ์ฑ๋ถ์ด๋ฉด Pass๋ผ๊ณ ๋จ์ ํ์ง ๋ง๊ณ ํ์ธ ํ์๋ก ํ๋จํ ์ ์๋ค.
|
| 2244 |
|
| 2245 |
[๊ฒํ ๋์ JSON]
|
| 2246 |
{json.dumps(target_payload, ensure_ascii=False, indent=2)}
|
|
|
|
| 2281 |
continue
|
| 2282 |
|
| 2283 |
item_comp = None
|
| 2284 |
+
item_material = None
|
| 2285 |
+
|
| 2286 |
for item in batch:
|
| 2287 |
if item["target_id"] == target_id:
|
| 2288 |
item_comp = item["component"]
|
| 2289 |
+
item_material = item["material"]
|
| 2290 |
break
|
| 2291 |
|
| 2292 |
+
high_risk_reason = ""
|
| 2293 |
+
if item_comp and item_material:
|
| 2294 |
+
high_risk_reason = get_restricted_candidate_reason(item_material, item_comp)
|
| 2295 |
+
|
| 2296 |
+
parsed_by_id[target_id] = sanitize_country_result(
|
| 2297 |
+
obj,
|
| 2298 |
+
country_col=country_col,
|
| 2299 |
+
component=item_comp or {},
|
| 2300 |
+
high_risk_reason=high_risk_reason,
|
| 2301 |
+
)
|
| 2302 |
|
| 2303 |
for item in batch:
|
| 2304 |
target_id = item["target_id"]
|
|
|
|
| 2710 |
gpt_targets = []
|
| 2711 |
|
| 2712 |
for item in row_items:
|
| 2713 |
+
mfds_result = None
|
| 2714 |
+
|
| 2715 |
+
if use_mfds_live and mfds_df is not None and not mfds_df.empty:
|
| 2716 |
+
mfds_result = evaluate_live_detail_rule(
|
| 2717 |
+
country=country,
|
| 2718 |
+
material=item["material"],
|
| 2719 |
+
component=item["component"],
|
| 2720 |
+
live_df=mfds_df,
|
| 2721 |
+
)
|
| 2722 |
+
|
| 2723 |
+
if mfds_result is not None:
|
| 2724 |
+
item["row_data"][col_name] = mfds_result.get("๊ฒฐ๊ณผ", "")
|
| 2725 |
+
item["row_data"][col_name + "_display"] = mfds_result.get("ํ์๋ฌธ๊ตฌ", NEED_CHECK_TEXT)
|
| 2726 |
+
|
| 2727 |
+
note = safe_str(mfds_result.get("๋น๊ณ ", "-"))
|
| 2728 |
+
if note and note != "-":
|
| 2729 |
+
item["notes"].append(f"{col_name}: {note}")
|
| 2730 |
+
|
| 2731 |
+
log_search_context(
|
| 2732 |
+
item["target_id"],
|
| 2733 |
+
country,
|
| 2734 |
+
item["component"],
|
| 2735 |
+
mfds_result.get("๊ฒฐ๊ณผ", ""),
|
| 2736 |
+
"MFDS_LIVE",
|
| 2737 |
+
note,
|
| 2738 |
+
)
|
| 2739 |
+
continue
|
| 2740 |
+
|
| 2741 |
local_result = evaluate_local_rules(
|
| 2742 |
country=country,
|
| 2743 |
material=item["material"],
|
|
|
|
| 2760 |
item["component"],
|
| 2761 |
local_result.get("๊ฒฐ๊ณผ", ""),
|
| 2762 |
"LOCAL_RULE",
|
| 2763 |
+
note if note and note != "-" else "๋ก์ปฌ ๊ท์น ํ์ ",
|
| 2764 |
)
|
| 2765 |
continue
|
| 2766 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2767 |
gpt_targets.append({
|
| 2768 |
"target_id": item["target_id"],
|
| 2769 |
"material": item["material"],
|
|
|
|
| 2782 |
result = {
|
| 2783 |
"๋ํ INCI": safe_str(item["component"].get("INCI Name", "")) or "-",
|
| 2784 |
"๊ฒฐ๊ณผ": "ํ์ธ ํ์",
|
| 2785 |
+
"ํ์๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
|
| 2786 |
"๋น๊ณ ": "batch ๊ฒฐ๊ณผ ๋๋ฝ",
|
| 2787 |
}
|
| 2788 |
|
|
|
|
| 2997 |
auth=auth_fn,
|
| 2998 |
allowed_paths=ALLOWED_PATHS,
|
| 2999 |
blocked_paths=BLOCKED_PATHS,
|
| 3000 |
+
)
|