Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@
|
|
| 20 |
# - FAISS ๋์คํฌ ์บ์
|
| 21 |
# - GPT๋ ๊ฒ์๋ ์๋ฌธ ์์์๋ง ํ์
|
| 22 |
# - ์์ฝ์ฒ ์ฌ์ดํธ ์ค์๊ฐ ์กฐํ ์ต์
|
| 23 |
-
# -
|
| 24 |
# - Formular Ingredients Statement ์๋ณธ ์์ ์ถ๋ ฅ
|
| 25 |
# - ์๋ณธ TOTAL ํ ์ ๊ฑฐ ํ ์ TOTAL ํ 1๊ฐ๋ง ์์ฑ
|
| 26 |
# ============================================================
|
|
@@ -30,6 +30,8 @@ import re
|
|
| 30 |
import json
|
| 31 |
import hashlib
|
| 32 |
import traceback
|
|
|
|
|
|
|
| 33 |
from pathlib import Path
|
| 34 |
from collections import defaultdict
|
| 35 |
|
|
@@ -72,8 +74,6 @@ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
|
| 72 |
FAISS_CACHE_DIR = ".faiss_cache"
|
| 73 |
|
| 74 |
MFDS_LIVE_URL = "https://nedrug.mfds.go.kr/pbp/CCBDF01"
|
| 75 |
-
COOS_LIVE_URL = "https://coos.kr/"
|
| 76 |
-
|
| 77 |
NO_EVIDENCE_TEXT = "ํด๋น ์๋ฃ ๋ด ์ ํ ๊ทผ๊ฑฐ ์์"
|
| 78 |
|
| 79 |
VECTOR_DB_CACHE = {}
|
|
@@ -296,42 +296,12 @@ FUNCTIONAL_KOREA_RULES = {
|
|
| 296 |
}
|
| 297 |
|
| 298 |
FRAGRANCE_ALLERGEN_RULES = {
|
| 299 |
-
"limonene": {
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
},
|
| 305 |
-
"linalool": {
|
| 306 |
-
"label": "Linalool",
|
| 307 |
-
"aliases": ["linalool", "๋ฆฌ๋ ๋ฃฐ"],
|
| 308 |
-
"leave_on": 0.001,
|
| 309 |
-
"wash_off": 0.01,
|
| 310 |
-
},
|
| 311 |
-
"citral": {
|
| 312 |
-
"label": "Citral",
|
| 313 |
-
"aliases": ["citral", "์ํธ๋"],
|
| 314 |
-
"leave_on": 0.001,
|
| 315 |
-
"wash_off": 0.01,
|
| 316 |
-
},
|
| 317 |
-
"geraniol": {
|
| 318 |
-
"label": "Geraniol",
|
| 319 |
-
"aliases": ["geraniol", "์ ๋ผ๋์ฌ"],
|
| 320 |
-
"leave_on": 0.001,
|
| 321 |
-
"wash_off": 0.01,
|
| 322 |
-
},
|
| 323 |
-
"eugenol": {
|
| 324 |
-
"label": "Eugenol",
|
| 325 |
-
"aliases": ["eugenol", "์ ์ ๋"],
|
| 326 |
-
"leave_on": 0.001,
|
| 327 |
-
"wash_off": 0.01,
|
| 328 |
-
},
|
| 329 |
-
"benzyl alcohol": {
|
| 330 |
-
"label": "Benzyl Alcohol",
|
| 331 |
-
"aliases": ["benzyl alcohol", "๋ฒค์ง์์ฝ์ฌ"],
|
| 332 |
-
"leave_on": 0.001,
|
| 333 |
-
"wash_off": 0.01,
|
| 334 |
-
},
|
| 335 |
}
|
| 336 |
|
| 337 |
|
|
@@ -527,18 +497,8 @@ def is_country_result_issue(text):
|
|
| 527 |
return False
|
| 528 |
|
| 529 |
keywords = [
|
| 530 |
-
"๋ฐฐํฉ๊ธ์ง",
|
| 531 |
-
"
|
| 532 |
-
"์ด๊ณผ",
|
| 533 |
-
"๋ถ์ ํฉ",
|
| 534 |
-
"์ฌ๊ฒํ ",
|
| 535 |
-
"ํ์ธ ํ์",
|
| 536 |
-
"์๋ฃ ํ์ธ",
|
| 537 |
-
"ํ์ ํ์",
|
| 538 |
-
"๋ผ๋ฒจ๋ง",
|
| 539 |
-
"๋ถ๊ฐ",
|
| 540 |
-
"์ฃผ์",
|
| 541 |
-
"๊ฒฝ๊ณ ",
|
| 542 |
]
|
| 543 |
|
| 544 |
return any(k in text for k in keywords)
|
|
@@ -628,10 +588,7 @@ def resolve_country_folder(country):
|
|
| 628 |
alias = COUNTRY_FOLDER_ALIASES.get(raw, COUNTRY_FOLDER_ALIASES.get(raw.upper(), raw))
|
| 629 |
|
| 630 |
candidates = [
|
| 631 |
-
raw,
|
| 632 |
-
alias,
|
| 633 |
-
raw.upper(),
|
| 634 |
-
alias.upper(),
|
| 635 |
raw.replace("_", " "),
|
| 636 |
raw.replace("-", " "),
|
| 637 |
raw.replace("EU FUTURE", "EU (FUTURE)"),
|
|
@@ -667,15 +624,8 @@ def resolve_country_folder(country):
|
|
| 667 |
|
| 668 |
def get_country_folders():
|
| 669 |
exclude = [
|
| 670 |
-
".git",
|
| 671 |
-
"
|
| 672 |
-
"__pycache__",
|
| 673 |
-
".ipynb_checkpoints",
|
| 674 |
-
".venv",
|
| 675 |
-
"venv",
|
| 676 |
-
"node_modules",
|
| 677 |
-
FAISS_CACHE_DIR,
|
| 678 |
-
"outputs",
|
| 679 |
]
|
| 680 |
|
| 681 |
folders = [
|
|
@@ -801,11 +751,7 @@ def build_database(country):
|
|
| 801 |
if not files:
|
| 802 |
return None
|
| 803 |
|
| 804 |
-
signature = tuple(
|
| 805 |
-
(f["relative_path"], f["size"], f["mtime_ns"])
|
| 806 |
-
for f in files
|
| 807 |
-
)
|
| 808 |
-
|
| 809 |
sig_hash = stable_hash(signature)
|
| 810 |
cached = VECTOR_DB_CACHE.get(folder)
|
| 811 |
|
|
@@ -815,10 +761,7 @@ def build_database(country):
|
|
| 815 |
embeddings = get_embeddings()
|
| 816 |
os.makedirs(FAISS_CACHE_DIR, exist_ok=True)
|
| 817 |
|
| 818 |
-
cache_path = os.path.join(
|
| 819 |
-
FAISS_CACHE_DIR,
|
| 820 |
-
f"{safe_filename(folder)}_{sig_hash[:16]}",
|
| 821 |
-
)
|
| 822 |
meta_path = os.path.join(cache_path, "metadata.json")
|
| 823 |
|
| 824 |
if os.path.isdir(cache_path) and os.path.exists(meta_path):
|
|
@@ -828,15 +771,8 @@ def build_database(country):
|
|
| 828 |
|
| 829 |
if meta.get("signature_hash") == sig_hash:
|
| 830 |
print(f"โ
FAISS ๋์คํฌ ์บ์ ๋ก๋: {folder}")
|
| 831 |
-
db = FAISS.load_local(
|
| 832 |
-
|
| 833 |
-
embeddings,
|
| 834 |
-
allow_dangerous_deserialization=True,
|
| 835 |
-
)
|
| 836 |
-
VECTOR_DB_CACHE[folder] = {
|
| 837 |
-
"signature_hash": sig_hash,
|
| 838 |
-
"db": db,
|
| 839 |
-
}
|
| 840 |
return db
|
| 841 |
|
| 842 |
except Exception as e:
|
|
@@ -845,11 +781,7 @@ def build_database(country):
|
|
| 845 |
print(f"๐ ๊ท์ DB ์ฌ์์ธ: {folder} / ํ์ผ {len(files)}๊ฐ")
|
| 846 |
EVAL_CACHE.clear()
|
| 847 |
|
| 848 |
-
splitter = RecursiveCharacterTextSplitter(
|
| 849 |
-
chunk_size=900,
|
| 850 |
-
chunk_overlap=120,
|
| 851 |
-
)
|
| 852 |
-
|
| 853 |
chunks = []
|
| 854 |
|
| 855 |
for item in files:
|
|
@@ -888,11 +820,7 @@ def build_database(country):
|
|
| 888 |
except Exception as e:
|
| 889 |
print(f"FAISS ์บ์ ์ ์ฅ ์คํจ: {folder} / {e}")
|
| 890 |
|
| 891 |
-
VECTOR_DB_CACHE[folder] = {
|
| 892 |
-
"signature_hash": sig_hash,
|
| 893 |
-
"db": db,
|
| 894 |
-
}
|
| 895 |
-
|
| 896 |
return db
|
| 897 |
|
| 898 |
|
|
@@ -931,7 +859,6 @@ def find_best_col(df, keywords, default_index=None, exclude_cols=None):
|
|
| 931 |
header = str(col).lower()
|
| 932 |
values = df[col].dropna().astype(str).str.strip()
|
| 933 |
sample = " ".join(values.head(80).tolist()).lower()
|
| 934 |
-
|
| 935 |
score = 0
|
| 936 |
|
| 937 |
for kw in keywords:
|
|
@@ -1260,9 +1187,32 @@ def make_grouped_materials(df):
|
|
| 1260 |
|
| 1261 |
|
| 1262 |
# ============================================================
|
| 1263 |
-
# 6. ์ค์๊ฐ ์กฐํ
|
| 1264 |
# ============================================================
|
| 1265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1266 |
def extract_terms_for_live_lookup(row_items):
|
| 1267 |
terms = []
|
| 1268 |
|
|
@@ -1285,7 +1235,7 @@ def extract_terms_for_live_lookup(row_items):
|
|
| 1285 |
return unique
|
| 1286 |
|
| 1287 |
|
| 1288 |
-
def
|
| 1289 |
try:
|
| 1290 |
html = page.content()
|
| 1291 |
|
|
@@ -1304,7 +1254,7 @@ def live_visible_text(page):
|
|
| 1304 |
return ""
|
| 1305 |
|
| 1306 |
|
| 1307 |
-
def
|
| 1308 |
selectors = [
|
| 1309 |
"input[type='search']",
|
| 1310 |
"input[type='text']",
|
|
@@ -1335,7 +1285,7 @@ def live_fill_search_input(page, term):
|
|
| 1335 |
return False
|
| 1336 |
|
| 1337 |
|
| 1338 |
-
def
|
| 1339 |
for txt in ["๊ฒ์", "์กฐํ", "Search"]:
|
| 1340 |
try:
|
| 1341 |
loc = page.get_by_text(txt, exact=True)
|
|
@@ -1386,7 +1336,7 @@ def live_click_search(page):
|
|
| 1386 |
return False
|
| 1387 |
|
| 1388 |
|
| 1389 |
-
def
|
| 1390 |
term_norm = normalize_compact(term)
|
| 1391 |
|
| 1392 |
selectors = [
|
|
@@ -1433,7 +1383,7 @@ def live_click_result_row(page, term):
|
|
| 1433 |
return False
|
| 1434 |
|
| 1435 |
|
| 1436 |
-
def
|
| 1437 |
clean = " ".join(safe_str(text).split())
|
| 1438 |
|
| 1439 |
if not clean:
|
|
@@ -1473,20 +1423,9 @@ def parse_live_text(term, text, source_name, source_url):
|
|
| 1473 |
has_reg_keyword = any(
|
| 1474 |
k in snippet_lower
|
| 1475 |
for k in [
|
| 1476 |
-
"๊ธ์ง",
|
| 1477 |
-
"
|
| 1478 |
-
"
|
| 1479 |
-
"์ ํ",
|
| 1480 |
-
"ํ๋",
|
| 1481 |
-
"๋ฐฐํฉํ๋",
|
| 1482 |
-
"์ต๋",
|
| 1483 |
-
"max",
|
| 1484 |
-
"maximum",
|
| 1485 |
-
"limit",
|
| 1486 |
-
"restricted",
|
| 1487 |
-
"prohibited",
|
| 1488 |
-
"banned",
|
| 1489 |
-
"%",
|
| 1490 |
]
|
| 1491 |
)
|
| 1492 |
|
|
@@ -1513,24 +1452,24 @@ def parse_live_text(term, text, source_name, source_url):
|
|
| 1513 |
"๊ท์ ๋ด์ฉ": snippet[:800],
|
| 1514 |
"ํ๋": limit,
|
| 1515 |
"์กฐ๊ฑด": "",
|
| 1516 |
-
"์ถ์ฒ":
|
| 1517 |
"์์ธ์๋ฌธ": clean[:6000],
|
| 1518 |
})
|
| 1519 |
|
| 1520 |
return rows
|
| 1521 |
|
| 1522 |
|
| 1523 |
-
def
|
| 1524 |
if not PLAYWRIGHT_AVAILABLE or sync_playwright is None:
|
| 1525 |
-
return pd.DataFrame(),
|
| 1526 |
|
| 1527 |
terms = [safe_str(t) for t in terms if safe_str(t)]
|
| 1528 |
terms = terms[:int(max_terms)] if max_terms else terms[:30]
|
| 1529 |
|
| 1530 |
-
|
| 1531 |
-
|
|
|
|
| 1532 |
|
| 1533 |
-
try:
|
| 1534 |
with sync_playwright() as p:
|
| 1535 |
browser = p.chromium.launch(headless=bool(headless))
|
| 1536 |
context = browser.new_context(
|
|
@@ -1541,44 +1480,63 @@ def live_lookup_terms(site_name, site_url, terms, max_terms=30, headless=True):
|
|
| 1541 |
|
| 1542 |
for idx, term in enumerate(terms, start=1):
|
| 1543 |
try:
|
| 1544 |
-
print(f"
|
| 1545 |
|
| 1546 |
-
page.goto(
|
| 1547 |
page.wait_for_timeout(2500)
|
| 1548 |
|
| 1549 |
-
if not
|
| 1550 |
-
|
| 1551 |
continue
|
| 1552 |
|
| 1553 |
-
if not
|
| 1554 |
try:
|
| 1555 |
page.keyboard.press("Enter")
|
| 1556 |
except Exception:
|
| 1557 |
pass
|
| 1558 |
|
| 1559 |
page.wait_for_timeout(3000)
|
| 1560 |
-
|
| 1561 |
page.wait_for_timeout(2000)
|
| 1562 |
|
| 1563 |
-
text =
|
| 1564 |
-
rows =
|
| 1565 |
|
| 1566 |
if rows:
|
| 1567 |
-
|
| 1568 |
else:
|
| 1569 |
-
|
| 1570 |
|
| 1571 |
except Exception as e:
|
| 1572 |
-
|
| 1573 |
|
| 1574 |
context.close()
|
| 1575 |
browser.close()
|
| 1576 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1577 |
except Exception as e:
|
| 1578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1579 |
|
| 1580 |
df = pd.DataFrame(all_rows)
|
| 1581 |
-
msg = f"
|
| 1582 |
|
| 1583 |
if errors:
|
| 1584 |
msg += " / ์ผ๋ถ ์คํจ: " + " ; ".join(errors[:5])
|
|
@@ -1589,26 +1547,6 @@ def live_lookup_terms(site_name, site_url, terms, max_terms=30, headless=True):
|
|
| 1589 |
return df, msg
|
| 1590 |
|
| 1591 |
|
| 1592 |
-
def mfds_live_lookup_terms(terms, max_terms=30, headless=True):
|
| 1593 |
-
return live_lookup_terms(
|
| 1594 |
-
site_name="MFDS",
|
| 1595 |
-
site_url=MFDS_LIVE_URL,
|
| 1596 |
-
terms=terms,
|
| 1597 |
-
max_terms=max_terms,
|
| 1598 |
-
headless=headless,
|
| 1599 |
-
)
|
| 1600 |
-
|
| 1601 |
-
|
| 1602 |
-
def coos_live_lookup_terms(terms, max_terms=30, headless=True):
|
| 1603 |
-
return live_lookup_terms(
|
| 1604 |
-
site_name="COOS",
|
| 1605 |
-
site_url=COOS_LIVE_URL,
|
| 1606 |
-
terms=terms,
|
| 1607 |
-
max_terms=max_terms,
|
| 1608 |
-
headless=headless,
|
| 1609 |
-
)
|
| 1610 |
-
|
| 1611 |
-
|
| 1612 |
def ingredient_matches_live_row(material, component, row):
|
| 1613 |
identity_values = [
|
| 1614 |
component.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
|
@@ -1642,7 +1580,6 @@ def live_row_country_matches(country, row_country):
|
|
| 1642 |
aliases += ["EU", "์ ๋ฝ", "์ ๋ฝ์ฐํฉ", "European Union", "Europe"]
|
| 1643 |
|
| 1644 |
aliases_norm = [normalize_compact(x) for x in aliases if safe_str(x)]
|
| 1645 |
-
|
| 1646 |
return any(alias and alias in row_country_norm for alias in aliases_norm)
|
| 1647 |
|
| 1648 |
|
|
@@ -1663,7 +1600,7 @@ def evaluate_live_detail_rule(country, material, component, live_df):
|
|
| 1663 |
category = safe_str(row.get("๊ตฌ๋ถ", "")) or "์ ํ"
|
| 1664 |
regulation = safe_str(row.get("๊ท์ ๋ด์ฉ", ""))
|
| 1665 |
limit = safe_str(row.get("ํ๋", ""))
|
| 1666 |
-
source = safe_str(row.get("์ถ์ฒ", "
|
| 1667 |
|
| 1668 |
parts = [category]
|
| 1669 |
|
|
@@ -1742,12 +1679,10 @@ def evaluate_local_rules(country, material, component, ingredient_totals, ingred
|
|
| 1742 |
amount = component.get("amount_float")
|
| 1743 |
|
| 1744 |
uv_result = evaluate_limit_rule(country, material, component, UV_FILTER_RULES, "UV ํํฐ")
|
| 1745 |
-
|
| 1746 |
if uv_result is not None:
|
| 1747 |
return uv_result
|
| 1748 |
|
| 1749 |
preservative_result = evaluate_limit_rule(country, material, component, PRESERVATIVE_RULES, "๋ณด์กด์ /์ ํ์ฑ๋ถ")
|
| 1750 |
-
|
| 1751 |
if preservative_result is not None:
|
| 1752 |
return preservative_result
|
| 1753 |
|
|
@@ -1872,16 +1807,8 @@ def sanitize_country_result(parsed, country_col="", component=None):
|
|
| 1872 |
)
|
| 1873 |
|
| 1874 |
bad_tokens = [
|
| 1875 |
-
"๊ท์ ๋ช
",
|
| 1876 |
-
"
|
| 1877 |
-
"๊ท์ / ์ด๋",
|
| 1878 |
-
"{๊ท์ ๊ตฐ๋ช
}",
|
| 1879 |
-
"{์ฑ๋ถ๋ช
",
|
| 1880 |
-
"{ํจ๋}",
|
| 1881 |
-
"{๊ธฐ์ค์น}",
|
| 1882 |
-
"placeholder",
|
| 1883 |
-
"N/A",
|
| 1884 |
-
"n/a",
|
| 1885 |
]
|
| 1886 |
|
| 1887 |
if any(token in result_text for token in bad_tokens):
|
|
@@ -1944,12 +1871,7 @@ def evaluate_component_with_llm(country, material, component, llm, target_id="")
|
|
| 1944 |
amount_text = format_percent(amount) if amount is not None else "ํจ๋ ํ์ธ ํ์"
|
| 1945 |
|
| 1946 |
if not inci_name and not korean_name:
|
| 1947 |
-
return {
|
| 1948 |
-
"๋ํ INCI": "-",
|
| 1949 |
-
"๊ฒฐ๊ณผ": "",
|
| 1950 |
-
"ํ์๋ฌธ๊ตฌ": "",
|
| 1951 |
-
"๋น๊ณ ": "-",
|
| 1952 |
-
}
|
| 1953 |
|
| 1954 |
if not db:
|
| 1955 |
result = {
|
|
@@ -2038,10 +1960,8 @@ INCI/CAS: {inci_name}
|
|
| 2038 |
response = llm.invoke(prompt).content
|
| 2039 |
parsed = parse_country_response(response)
|
| 2040 |
parsed = sanitize_country_result(parsed, country_col=country_col, component=component)
|
| 2041 |
-
|
| 2042 |
EVAL_CACHE[cache_key] = parsed
|
| 2043 |
log_search_context(target_id, country, component, parsed["๊ฒฐ๊ณผ"], "GPT_SINGLE", context)
|
| 2044 |
-
|
| 2045 |
return parsed
|
| 2046 |
|
| 2047 |
except Exception as e:
|
|
@@ -2076,14 +1996,7 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2076 |
"๋น๊ณ ": "-",
|
| 2077 |
}
|
| 2078 |
|
| 2079 |
-
log_search_context(
|
| 2080 |
-
item["target_id"],
|
| 2081 |
-
country,
|
| 2082 |
-
comp,
|
| 2083 |
-
NO_EVIDENCE_TEXT,
|
| 2084 |
-
"NO_DB",
|
| 2085 |
-
"ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์",
|
| 2086 |
-
)
|
| 2087 |
|
| 2088 |
return results
|
| 2089 |
|
|
@@ -2136,14 +2049,7 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2136 |
}
|
| 2137 |
results[item["target_id"]] = result
|
| 2138 |
EVAL_CACHE[make_eval_cache_key(country, comp)] = result
|
| 2139 |
-
log_search_context(
|
| 2140 |
-
item["target_id"],
|
| 2141 |
-
country,
|
| 2142 |
-
comp,
|
| 2143 |
-
result["๊ฒฐ๊ณผ"],
|
| 2144 |
-
"NO_MEANINGFUL_HIT",
|
| 2145 |
-
context,
|
| 2146 |
-
)
|
| 2147 |
|
| 2148 |
uncached_targets = filtered_targets
|
| 2149 |
|
|
@@ -2156,7 +2062,6 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2156 |
|
| 2157 |
for item in batch:
|
| 2158 |
comp = item["component"]
|
| 2159 |
-
|
| 2160 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2161 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 2162 |
amount = comp.get("amount_float")
|
|
@@ -2230,17 +2135,12 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 2230 |
continue
|
| 2231 |
|
| 2232 |
item_comp = None
|
| 2233 |
-
|
| 2234 |
for item in batch:
|
| 2235 |
if item["target_id"] == target_id:
|
| 2236 |
item_comp = item["component"]
|
| 2237 |
break
|
| 2238 |
|
| 2239 |
-
parsed_by_id[target_id] = sanitize_country_result(
|
| 2240 |
-
obj,
|
| 2241 |
-
country_col=country_col,
|
| 2242 |
-
component=item_comp or {},
|
| 2243 |
-
)
|
| 2244 |
|
| 2245 |
for item in batch:
|
| 2246 |
target_id = item["target_id"]
|
|
@@ -2302,11 +2202,7 @@ def make_formula_statement_rows(row_items, country_cols):
|
|
| 2302 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 2303 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2304 |
|
| 2305 |
-
if (
|
| 2306 |
-
is_total_like_text(no_value)
|
| 2307 |
-
or is_total_like_text(korean_name)
|
| 2308 |
-
or is_total_like_text(inci_name)
|
| 2309 |
-
):
|
| 2310 |
continue
|
| 2311 |
|
| 2312 |
row = {
|
|
@@ -2403,11 +2299,7 @@ def sum_formula_percent(formula_df):
|
|
| 2403 |
korean_name = safe_str(row.get("ํ๊ธ ํ์คํ ๋ช
์นญ", ""))
|
| 2404 |
inci_name = safe_str(row.get("INGREDIENT NAME (INCI)", ""))
|
| 2405 |
|
| 2406 |
-
if (
|
| 2407 |
-
is_total_like_text(no_value)
|
| 2408 |
-
or is_total_like_text(korean_name)
|
| 2409 |
-
or is_total_like_text(inci_name)
|
| 2410 |
-
):
|
| 2411 |
continue
|
| 2412 |
|
| 2413 |
num = to_number(row.get("%(w/w)", ""))
|
|
@@ -2523,15 +2415,7 @@ def style_output_workbook(path):
|
|
| 2523 |
except Exception:
|
| 2524 |
pass
|
| 2525 |
|
| 2526 |
-
widths = {
|
| 2527 |
-
1: 6,
|
| 2528 |
-
2: 34,
|
| 2529 |
-
3: 34,
|
| 2530 |
-
4: 26,
|
| 2531 |
-
5: 16,
|
| 2532 |
-
6: 10,
|
| 2533 |
-
7: 12,
|
| 2534 |
-
}
|
| 2535 |
|
| 2536 |
for col_idx in range(1, ws.max_column + 1):
|
| 2537 |
col_letter = get_column_letter(col_idx)
|
|
@@ -2599,7 +2483,6 @@ def process(
|
|
| 2599 |
file,
|
| 2600 |
product_name="",
|
| 2601 |
use_mfds_live=False,
|
| 2602 |
-
use_coos_live=False,
|
| 2603 |
live_max_terms=30,
|
| 2604 |
live_headless=True,
|
| 2605 |
):
|
|
@@ -2649,9 +2532,7 @@ def process(
|
|
| 2649 |
})
|
| 2650 |
|
| 2651 |
mfds_df = pd.DataFrame()
|
| 2652 |
-
coos_df = pd.DataFrame()
|
| 2653 |
mfds_live_msg = "์ฌ์ฉ ์ ํจ"
|
| 2654 |
-
coos_live_msg = "์ฌ์ฉ ์ ํจ"
|
| 2655 |
|
| 2656 |
terms = extract_terms_for_live_lookup(row_items)
|
| 2657 |
|
|
@@ -2666,17 +2547,6 @@ def process(
|
|
| 2666 |
mfds_df = pd.DataFrame()
|
| 2667 |
mfds_live_msg = f"MFDS ์ค์๊ฐ ์กฐํ ์คํจ: {e}"
|
| 2668 |
|
| 2669 |
-
if use_coos_live:
|
| 2670 |
-
try:
|
| 2671 |
-
coos_df, coos_live_msg = coos_live_lookup_terms(
|
| 2672 |
-
terms=terms,
|
| 2673 |
-
max_terms=live_max_terms,
|
| 2674 |
-
headless=live_headless,
|
| 2675 |
-
)
|
| 2676 |
-
except Exception as e:
|
| 2677 |
-
coos_df = pd.DataFrame()
|
| 2678 |
-
coos_live_msg = f"COOS ์ค์๊ฐ ์กฐํ ์คํจ: {e}"
|
| 2679 |
-
|
| 2680 |
for country in countries:
|
| 2681 |
col_name = display_country(country)
|
| 2682 |
gpt_targets = []
|
|
@@ -2726,42 +2596,7 @@ def process(
|
|
| 2726 |
if note and note != "-":
|
| 2727 |
item["notes"].append(f"{col_name}: {note}")
|
| 2728 |
|
| 2729 |
-
log_search_context(
|
| 2730 |
-
item["target_id"],
|
| 2731 |
-
country,
|
| 2732 |
-
item["component"],
|
| 2733 |
-
mfds_result.get("๊ฒฐ๊ณผ", ""),
|
| 2734 |
-
"MFDS_LIVE",
|
| 2735 |
-
note,
|
| 2736 |
-
)
|
| 2737 |
-
continue
|
| 2738 |
-
|
| 2739 |
-
coos_result = None
|
| 2740 |
-
|
| 2741 |
-
if use_coos_live and coos_df is not None and not coos_df.empty:
|
| 2742 |
-
coos_result = evaluate_live_detail_rule(
|
| 2743 |
-
country=country,
|
| 2744 |
-
material=item["material"],
|
| 2745 |
-
component=item["component"],
|
| 2746 |
-
live_df=coos_df,
|
| 2747 |
-
)
|
| 2748 |
-
|
| 2749 |
-
if coos_result is not None:
|
| 2750 |
-
item["row_data"][col_name] = coos_result.get("๊ฒฐ๊ณผ", "")
|
| 2751 |
-
item["row_data"][col_name + "_display"] = coos_result.get("ํ์๋ฌธ๊ตฌ", "ํ์ธ ํ์")
|
| 2752 |
-
|
| 2753 |
-
note = safe_str(coos_result.get("๋น๊ณ ", "-"))
|
| 2754 |
-
if note and note != "-":
|
| 2755 |
-
item["notes"].append(f"{col_name}: {note}")
|
| 2756 |
-
|
| 2757 |
-
log_search_context(
|
| 2758 |
-
item["target_id"],
|
| 2759 |
-
country,
|
| 2760 |
-
item["component"],
|
| 2761 |
-
coos_result.get("๊ฒฐ๊ณผ", ""),
|
| 2762 |
-
"COOS_LIVE",
|
| 2763 |
-
note,
|
| 2764 |
-
)
|
| 2765 |
continue
|
| 2766 |
|
| 2767 |
gpt_targets.append({
|
|
@@ -2770,11 +2605,7 @@ def process(
|
|
| 2770 |
"component": item["component"],
|
| 2771 |
})
|
| 2772 |
|
| 2773 |
-
batch_results = evaluate_components_with_llm_batch(
|
| 2774 |
-
country=country,
|
| 2775 |
-
targets=gpt_targets,
|
| 2776 |
-
llm=llm,
|
| 2777 |
-
)
|
| 2778 |
|
| 2779 |
for item in row_items:
|
| 2780 |
if col_name in item["row_data"]:
|
|
@@ -2819,11 +2650,7 @@ def process(
|
|
| 2819 |
|
| 2820 |
formula_total = sum_formula_percent(formula_df)
|
| 2821 |
|
| 2822 |
-
safe_country_name = "_".join([
|
| 2823 |
-
re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ_-]", "_", str(c))
|
| 2824 |
-
for c in countries
|
| 2825 |
-
])
|
| 2826 |
-
|
| 2827 |
product_part = safe_filename(product_name) if safe_str(product_name) else "product"
|
| 2828 |
out_path = str(OUTPUT_DIR / f"screening_result_{product_part}_{safe_country_name}.xlsx")
|
| 2829 |
|
|
@@ -2842,7 +2669,6 @@ def process(
|
|
| 2842 |
f"- In formula ํฉ๊ณ: {format_amount(formula_total)}%\n"
|
| 2843 |
f"- ์ฌ๊ฒํ ํ์ ์ฑ๋ถ: {len(regulatory_df)}๊ฐ\n"
|
| 2844 |
f"- MFDS ์ํ: {mfds_live_msg}\n"
|
| 2845 |
-
f"- COOS ์ํ: {coos_live_msg}\n"
|
| 2846 |
"- ๊ฒฐ๊ณผ ํ์ผ ์ํธ:\n"
|
| 2847 |
" 1) Formula Ingredients Statement\n"
|
| 2848 |
" 2) Regulatory Screening Summary\n"
|
|
@@ -2863,14 +2689,10 @@ def process(
|
|
| 2863 |
|
| 2864 |
with gr.Blocks(css=custom_css) as demo:
|
| 2865 |
gr.Markdown("# ๐ reBom Regulatory Screening AI")
|
| 2866 |
-
gr.Markdown("### ๊ตญ๊ฐ๋ณ ๊ท์ ํด๋ ๊ฒ์ + ์์ฝ์ฒ
|
| 2867 |
|
| 2868 |
with gr.Row():
|
| 2869 |
-
gr.Button(
|
| 2870 |
-
"๐ช ๋ก๊ทธ์์",
|
| 2871 |
-
link="/logout?all_session=false",
|
| 2872 |
-
variant="secondary",
|
| 2873 |
-
)
|
| 2874 |
|
| 2875 |
with gr.Tab("๐ ์คํฌ๋ฆฌ๋"):
|
| 2876 |
country = gr.CheckboxGroup(
|
|
@@ -2897,19 +2719,14 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 2897 |
value=False,
|
| 2898 |
)
|
| 2899 |
|
| 2900 |
-
use_coos_live = gr.Checkbox(
|
| 2901 |
-
label="COOS ์ฌ์ดํธ ์ค์๊ฐ ์กฐํ ์ฌ์ฉ",
|
| 2902 |
-
value=False,
|
| 2903 |
-
)
|
| 2904 |
-
|
| 2905 |
live_max_terms = gr.Number(
|
| 2906 |
-
label="์ค์๊ฐ ์กฐํ ์ต๋ ๊ฒ์์ด ์",
|
| 2907 |
value=5,
|
| 2908 |
precision=0,
|
| 2909 |
)
|
| 2910 |
|
| 2911 |
live_headless = gr.Checkbox(
|
| 2912 |
-
label="
|
| 2913 |
value=True,
|
| 2914 |
)
|
| 2915 |
|
|
@@ -2958,7 +2775,6 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 2958 |
file,
|
| 2959 |
product_name,
|
| 2960 |
use_mfds_live,
|
| 2961 |
-
use_coos_live,
|
| 2962 |
live_max_terms,
|
| 2963 |
live_headless,
|
| 2964 |
],
|
|
|
|
| 20 |
# - FAISS ๋์คํฌ ์บ์
|
| 21 |
# - GPT๋ ๊ฒ์๋ ์๋ฌธ ์์์๋ง ํ์
|
| 22 |
# - ์์ฝ์ฒ ์ฌ์ดํธ ์ค์๊ฐ ์กฐํ ์ต์
|
| 23 |
+
# - Playwright Chromium ์๋ ์ค์น ์ฌ์๋
|
| 24 |
# - Formular Ingredients Statement ์๋ณธ ์์ ์ถ๋ ฅ
|
| 25 |
# - ์๋ณธ TOTAL ํ ์ ๊ฑฐ ํ ์ TOTAL ํ 1๊ฐ๋ง ์์ฑ
|
| 26 |
# ============================================================
|
|
|
|
| 30 |
import json
|
| 31 |
import hashlib
|
| 32 |
import traceback
|
| 33 |
+
import sys
|
| 34 |
+
import subprocess
|
| 35 |
from pathlib import Path
|
| 36 |
from collections import defaultdict
|
| 37 |
|
|
|
|
| 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 = {}
|
|
|
|
| 296 |
}
|
| 297 |
|
| 298 |
FRAGRANCE_ALLERGEN_RULES = {
|
| 299 |
+
"limonene": {"label": "Limonene", "aliases": ["limonene", "๋ฆฌ๋ชจ๋จ"], "leave_on": 0.001, "wash_off": 0.01},
|
| 300 |
+
"linalool": {"label": "Linalool", "aliases": ["linalool", "๋ฆฌ๋ ๋ฃฐ"], "leave_on": 0.001, "wash_off": 0.01},
|
| 301 |
+
"citral": {"label": "Citral", "aliases": ["citral", "์ํธ๋"], "leave_on": 0.001, "wash_off": 0.01},
|
| 302 |
+
"geraniol": {"label": "Geraniol", "aliases": ["geraniol", "์ ๋ผ๋์ฌ"], "leave_on": 0.001, "wash_off": 0.01},
|
| 303 |
+
"eugenol": {"label": "Eugenol", "aliases": ["eugenol", "์ ์ ๋"], "leave_on": 0.001, "wash_off": 0.01},
|
| 304 |
+
"benzyl alcohol": {"label": "Benzyl Alcohol", "aliases": ["benzyl alcohol", "๋ฒค์ง์์ฝ์ฌ"], "leave_on": 0.001, "wash_off": 0.01},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
}
|
| 306 |
|
| 307 |
|
|
|
|
| 497 |
return False
|
| 498 |
|
| 499 |
keywords = [
|
| 500 |
+
"๋ฐฐํฉ๊ธ์ง", "๊ธ์ง", "์ด๊ณผ", "๋ถ์ ํฉ", "์ฌ๊ฒํ ", "ํ์ธ ํ์",
|
| 501 |
+
"์๋ฃ ํ์ธ", "ํ์ ํ์", "๋ผ๋ฒจ๋ง", "๋ถ๊ฐ", "์ฃผ์", "๊ฒฝ๊ณ ",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
]
|
| 503 |
|
| 504 |
return any(k in text for k in keywords)
|
|
|
|
| 588 |
alias = COUNTRY_FOLDER_ALIASES.get(raw, COUNTRY_FOLDER_ALIASES.get(raw.upper(), raw))
|
| 589 |
|
| 590 |
candidates = [
|
| 591 |
+
raw, alias, raw.upper(), alias.upper(),
|
|
|
|
|
|
|
|
|
|
| 592 |
raw.replace("_", " "),
|
| 593 |
raw.replace("-", " "),
|
| 594 |
raw.replace("EU FUTURE", "EU (FUTURE)"),
|
|
|
|
| 624 |
|
| 625 |
def get_country_folders():
|
| 626 |
exclude = [
|
| 627 |
+
".git", ".cache", "__pycache__", ".ipynb_checkpoints", ".venv",
|
| 628 |
+
"venv", "node_modules", FAISS_CACHE_DIR, "outputs",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
]
|
| 630 |
|
| 631 |
folders = [
|
|
|
|
| 751 |
if not files:
|
| 752 |
return None
|
| 753 |
|
| 754 |
+
signature = tuple((f["relative_path"], f["size"], f["mtime_ns"]) for f in files)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
sig_hash = stable_hash(signature)
|
| 756 |
cached = VECTOR_DB_CACHE.get(folder)
|
| 757 |
|
|
|
|
| 761 |
embeddings = get_embeddings()
|
| 762 |
os.makedirs(FAISS_CACHE_DIR, exist_ok=True)
|
| 763 |
|
| 764 |
+
cache_path = os.path.join(FAISS_CACHE_DIR, f"{safe_filename(folder)}_{sig_hash[:16]}")
|
|
|
|
|
|
|
|
|
|
| 765 |
meta_path = os.path.join(cache_path, "metadata.json")
|
| 766 |
|
| 767 |
if os.path.isdir(cache_path) and os.path.exists(meta_path):
|
|
|
|
| 771 |
|
| 772 |
if meta.get("signature_hash") == sig_hash:
|
| 773 |
print(f"โ
FAISS ๋์คํฌ ์บ์ ๋ก๋: {folder}")
|
| 774 |
+
db = FAISS.load_local(cache_path, embeddings, allow_dangerous_deserialization=True)
|
| 775 |
+
VECTOR_DB_CACHE[folder] = {"signature_hash": sig_hash, "db": db}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
return db
|
| 777 |
|
| 778 |
except Exception as e:
|
|
|
|
| 781 |
print(f"๐ ๊ท์ DB ์ฌ์์ธ: {folder} / ํ์ผ {len(files)}๊ฐ")
|
| 782 |
EVAL_CACHE.clear()
|
| 783 |
|
| 784 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=900, chunk_overlap=120)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
chunks = []
|
| 786 |
|
| 787 |
for item in files:
|
|
|
|
| 820 |
except Exception as e:
|
| 821 |
print(f"FAISS ์บ์ ์ ์ฅ ์คํจ: {folder} / {e}")
|
| 822 |
|
| 823 |
+
VECTOR_DB_CACHE[folder] = {"signature_hash": sig_hash, "db": db}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
return db
|
| 825 |
|
| 826 |
|
|
|
|
| 859 |
header = str(col).lower()
|
| 860 |
values = df[col].dropna().astype(str).str.strip()
|
| 861 |
sample = " ".join(values.head(80).tolist()).lower()
|
|
|
|
| 862 |
score = 0
|
| 863 |
|
| 864 |
for kw in keywords:
|
|
|
|
| 1187 |
|
| 1188 |
|
| 1189 |
# ============================================================
|
| 1190 |
+
# 6. ์์ฝ์ฒ ์ค์๊ฐ ์กฐํ
|
| 1191 |
# ============================================================
|
| 1192 |
|
| 1193 |
+
def install_playwright_chromium_once():
|
| 1194 |
+
try:
|
| 1195 |
+
print("๐ง Playwright Chromium ์ค์น ์๋ ์ค...")
|
| 1196 |
+
result = subprocess.run(
|
| 1197 |
+
[sys.executable, "-m", "playwright", "install", "chromium"],
|
| 1198 |
+
capture_output=True,
|
| 1199 |
+
text=True,
|
| 1200 |
+
timeout=300,
|
| 1201 |
+
)
|
| 1202 |
+
|
| 1203 |
+
if result.returncode == 0:
|
| 1204 |
+
print("โ
Playwright Chromium ์ค์น ์๋ฃ")
|
| 1205 |
+
return True, "Chromium ์ค์น ์๋ฃ"
|
| 1206 |
+
|
| 1207 |
+
msg = (result.stdout or "") + "\n" + (result.stderr or "")
|
| 1208 |
+
print("โ Playwright Chromium ์ค์น ์คํจ:", msg[-1000:])
|
| 1209 |
+
return False, msg[-1000:]
|
| 1210 |
+
|
| 1211 |
+
except Exception as e:
|
| 1212 |
+
print("โ Playwright Chromium ์ค์น ์์ธ:", e)
|
| 1213 |
+
return False, str(e)
|
| 1214 |
+
|
| 1215 |
+
|
| 1216 |
def extract_terms_for_live_lookup(row_items):
|
| 1217 |
terms = []
|
| 1218 |
|
|
|
|
| 1235 |
return unique
|
| 1236 |
|
| 1237 |
|
| 1238 |
+
def mfds_visible_text(page):
|
| 1239 |
try:
|
| 1240 |
html = page.content()
|
| 1241 |
|
|
|
|
| 1254 |
return ""
|
| 1255 |
|
| 1256 |
|
| 1257 |
+
def mfds_fill_search_input(page, term):
|
| 1258 |
selectors = [
|
| 1259 |
"input[type='search']",
|
| 1260 |
"input[type='text']",
|
|
|
|
| 1285 |
return False
|
| 1286 |
|
| 1287 |
|
| 1288 |
+
def mfds_click_search(page):
|
| 1289 |
for txt in ["๊ฒ์", "์กฐํ", "Search"]:
|
| 1290 |
try:
|
| 1291 |
loc = page.get_by_text(txt, exact=True)
|
|
|
|
| 1336 |
return False
|
| 1337 |
|
| 1338 |
|
| 1339 |
+
def mfds_click_result_row(page, term):
|
| 1340 |
term_norm = normalize_compact(term)
|
| 1341 |
|
| 1342 |
selectors = [
|
|
|
|
| 1383 |
return False
|
| 1384 |
|
| 1385 |
|
| 1386 |
+
def parse_mfds_text(term, text):
|
| 1387 |
clean = " ".join(safe_str(text).split())
|
| 1388 |
|
| 1389 |
if not clean:
|
|
|
|
| 1423 |
has_reg_keyword = any(
|
| 1424 |
k in snippet_lower
|
| 1425 |
for k in [
|
| 1426 |
+
"๊ธ์ง", "์ฌ์ฉ๊ธ์ง", "๋ฐฐํฉ๊ธ์ง", "์ ํ", "ํ๋", "๋ฐฐํฉํ๋",
|
| 1427 |
+
"์ต๋", "max", "maximum", "limit", "restricted",
|
| 1428 |
+
"prohibited", "banned", "%",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1429 |
]
|
| 1430 |
)
|
| 1431 |
|
|
|
|
| 1452 |
"๊ท์ ๋ด์ฉ": snippet[:800],
|
| 1453 |
"ํ๋": limit,
|
| 1454 |
"์กฐ๊ฑด": "",
|
| 1455 |
+
"์ถ์ฒ": "MFDS live: " + MFDS_LIVE_URL,
|
| 1456 |
"์์ธ์๋ฌธ": clean[:6000],
|
| 1457 |
})
|
| 1458 |
|
| 1459 |
return rows
|
| 1460 |
|
| 1461 |
|
| 1462 |
+
def mfds_live_lookup_terms(terms, max_terms=30, headless=True):
|
| 1463 |
if not PLAYWRIGHT_AVAILABLE or sync_playwright is None:
|
| 1464 |
+
return pd.DataFrame(), "MFDS ์กฐํ ์คํจ: playwright ๋ฏธ์ค์น ๋๋ ๋ธ๋ผ์ฐ์ ๋ฏธ์ค์น"
|
| 1465 |
|
| 1466 |
terms = [safe_str(t) for t in terms if safe_str(t)]
|
| 1467 |
terms = terms[:int(max_terms)] if max_terms else terms[:30]
|
| 1468 |
|
| 1469 |
+
def run_browser_lookup():
|
| 1470 |
+
local_rows = []
|
| 1471 |
+
local_errors = []
|
| 1472 |
|
|
|
|
| 1473 |
with sync_playwright() as p:
|
| 1474 |
browser = p.chromium.launch(headless=bool(headless))
|
| 1475 |
context = browser.new_context(
|
|
|
|
| 1480 |
|
| 1481 |
for idx, term in enumerate(terms, start=1):
|
| 1482 |
try:
|
| 1483 |
+
print(f"MFDS ์ค์๊ฐ ์กฐํ {idx}/{len(terms)}: {term}")
|
| 1484 |
|
| 1485 |
+
page.goto(MFDS_LIVE_URL, wait_until="domcontentloaded", timeout=30000)
|
| 1486 |
page.wait_for_timeout(2500)
|
| 1487 |
|
| 1488 |
+
if not mfds_fill_search_input(page, term):
|
| 1489 |
+
local_errors.append(f"{term}: ๊ฒ์ ์
๋ ฅ์ฐฝ ์ฐพ๊ธฐ ์คํจ")
|
| 1490 |
continue
|
| 1491 |
|
| 1492 |
+
if not mfds_click_search(page):
|
| 1493 |
try:
|
| 1494 |
page.keyboard.press("Enter")
|
| 1495 |
except Exception:
|
| 1496 |
pass
|
| 1497 |
|
| 1498 |
page.wait_for_timeout(3000)
|
| 1499 |
+
mfds_click_result_row(page, term)
|
| 1500 |
page.wait_for_timeout(2000)
|
| 1501 |
|
| 1502 |
+
text = mfds_visible_text(page)
|
| 1503 |
+
rows = parse_mfds_text(term, text)
|
| 1504 |
|
| 1505 |
if rows:
|
| 1506 |
+
local_rows.extend(rows)
|
| 1507 |
else:
|
| 1508 |
+
local_errors.append(f"{term}: ๊ตญ๊ฐ๋ณ ๋ฐฐํฉํ๋/์ ํ ์ ๋ณด ๋ฏธ๊ฒ์ถ")
|
| 1509 |
|
| 1510 |
except Exception as e:
|
| 1511 |
+
local_errors.append(f"{term}: {e}")
|
| 1512 |
|
| 1513 |
context.close()
|
| 1514 |
browser.close()
|
| 1515 |
|
| 1516 |
+
return local_rows, local_errors
|
| 1517 |
+
|
| 1518 |
+
try:
|
| 1519 |
+
all_rows, errors = run_browser_lookup()
|
| 1520 |
+
|
| 1521 |
except Exception as e:
|
| 1522 |
+
err_text = str(e)
|
| 1523 |
+
|
| 1524 |
+
if "Executable doesn't exist" in err_text or "playwright install" in err_text.lower():
|
| 1525 |
+
ok, install_msg = install_playwright_chromium_once()
|
| 1526 |
+
|
| 1527 |
+
if not ok:
|
| 1528 |
+
return pd.DataFrame(), f"MFDS ๋ธ๋ผ์ฐ์ ์ค์น ์คํจ: {install_msg}"
|
| 1529 |
+
|
| 1530 |
+
try:
|
| 1531 |
+
all_rows, errors = run_browser_lookup()
|
| 1532 |
+
except Exception as e2:
|
| 1533 |
+
return pd.DataFrame(), f"MFDS ๋ธ๋ผ์ฐ์ ์คํ ์คํจ: {e2}"
|
| 1534 |
+
|
| 1535 |
+
else:
|
| 1536 |
+
return pd.DataFrame(), f"MFDS ๋ธ๋ผ์ฐ์ ์คํ ์คํจ: {e}"
|
| 1537 |
|
| 1538 |
df = pd.DataFrame(all_rows)
|
| 1539 |
+
msg = f"MFDS ์ค์๊ฐ ์กฐํ ์๋ฃ: ๊ฒ์์ด {len(terms)}๊ฐ, ๊ฒฐ๊ณผ {len(df)}ํ"
|
| 1540 |
|
| 1541 |
if errors:
|
| 1542 |
msg += " / ์ผ๋ถ ์คํจ: " + " ; ".join(errors[:5])
|
|
|
|
| 1547 |
return df, msg
|
| 1548 |
|
| 1549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1550 |
def ingredient_matches_live_row(material, component, row):
|
| 1551 |
identity_values = [
|
| 1552 |
component.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
|
|
|
| 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)
|
| 1584 |
|
| 1585 |
|
|
|
|
| 1600 |
category = safe_str(row.get("๊ตฌ๋ถ", "")) or "์ ํ"
|
| 1601 |
regulation = safe_str(row.get("๊ท์ ๋ด์ฉ", ""))
|
| 1602 |
limit = safe_str(row.get("ํ๋", ""))
|
| 1603 |
+
source = safe_str(row.get("์ถ์ฒ", "MFDS live"))
|
| 1604 |
|
| 1605 |
parts = [category]
|
| 1606 |
|
|
|
|
| 1679 |
amount = component.get("amount_float")
|
| 1680 |
|
| 1681 |
uv_result = evaluate_limit_rule(country, material, component, UV_FILTER_RULES, "UV ํํฐ")
|
|
|
|
| 1682 |
if uv_result is not None:
|
| 1683 |
return uv_result
|
| 1684 |
|
| 1685 |
preservative_result = evaluate_limit_rule(country, material, component, PRESERVATIVE_RULES, "๋ณด์กด์ /์ ํ์ฑ๋ถ")
|
|
|
|
| 1686 |
if preservative_result is not None:
|
| 1687 |
return preservative_result
|
| 1688 |
|
|
|
|
| 1807 |
)
|
| 1808 |
|
| 1809 |
bad_tokens = [
|
| 1810 |
+
"๊ท์ ๋ช
", "์ด๋ ๊ณ์ฐ", "๊ท์ / ์ด๋", "{๊ท์ ๊ตฐ๋ช
}",
|
| 1811 |
+
"{์ฑ๋ถ๋ช
", "{ํจ๋}", "{๊ธฐ์ค์น}", "placeholder", "N/A", "n/a",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1812 |
]
|
| 1813 |
|
| 1814 |
if any(token in result_text for token in bad_tokens):
|
|
|
|
| 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 |
result = {
|
|
|
|
| 1960 |
response = llm.invoke(prompt).content
|
| 1961 |
parsed = parse_country_response(response)
|
| 1962 |
parsed = sanitize_country_result(parsed, country_col=country_col, component=component)
|
|
|
|
| 1963 |
EVAL_CACHE[cache_key] = parsed
|
| 1964 |
log_search_context(target_id, country, component, parsed["๊ฒฐ๊ณผ"], "GPT_SINGLE", context)
|
|
|
|
| 1965 |
return parsed
|
| 1966 |
|
| 1967 |
except Exception as e:
|
|
|
|
| 1996 |
"๋น๊ณ ": "-",
|
| 1997 |
}
|
| 1998 |
|
| 1999 |
+
log_search_context(item["target_id"], country, comp, NO_EVIDENCE_TEXT, "NO_DB", "ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์ DB ์์")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2000 |
|
| 2001 |
return results
|
| 2002 |
|
|
|
|
| 2049 |
}
|
| 2050 |
results[item["target_id"]] = result
|
| 2051 |
EVAL_CACHE[make_eval_cache_key(country, comp)] = result
|
| 2052 |
+
log_search_context(item["target_id"], country, comp, result["๊ฒฐ๊ณผ"], "NO_MEANINGFUL_HIT", context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2053 |
|
| 2054 |
uncached_targets = filtered_targets
|
| 2055 |
|
|
|
|
| 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")
|
|
|
|
| 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 |
+
parsed_by_id[target_id] = sanitize_country_result(obj, country_col=country_col, component=item_comp or {})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2144 |
|
| 2145 |
for item in batch:
|
| 2146 |
target_id = item["target_id"]
|
|
|
|
| 2202 |
korean_name = safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""))
|
| 2203 |
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2204 |
|
| 2205 |
+
if is_total_like_text(no_value) or is_total_like_text(korean_name) or is_total_like_text(inci_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2206 |
continue
|
| 2207 |
|
| 2208 |
row = {
|
|
|
|
| 2299 |
korean_name = safe_str(row.get("ํ๊ธ ํ์คํ ๋ช
์นญ", ""))
|
| 2300 |
inci_name = safe_str(row.get("INGREDIENT NAME (INCI)", ""))
|
| 2301 |
|
| 2302 |
+
if is_total_like_text(no_value) or is_total_like_text(korean_name) or is_total_like_text(inci_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2303 |
continue
|
| 2304 |
|
| 2305 |
num = to_number(row.get("%(w/w)", ""))
|
|
|
|
| 2415 |
except Exception:
|
| 2416 |
pass
|
| 2417 |
|
| 2418 |
+
widths = {1: 6, 2: 34, 3: 34, 4: 26, 5: 16, 6: 10, 7: 12}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2419 |
|
| 2420 |
for col_idx in range(1, ws.max_column + 1):
|
| 2421 |
col_letter = get_column_letter(col_idx)
|
|
|
|
| 2483 |
file,
|
| 2484 |
product_name="",
|
| 2485 |
use_mfds_live=False,
|
|
|
|
| 2486 |
live_max_terms=30,
|
| 2487 |
live_headless=True,
|
| 2488 |
):
|
|
|
|
| 2532 |
})
|
| 2533 |
|
| 2534 |
mfds_df = pd.DataFrame()
|
|
|
|
| 2535 |
mfds_live_msg = "์ฌ์ฉ ์ ํจ"
|
|
|
|
| 2536 |
|
| 2537 |
terms = extract_terms_for_live_lookup(row_items)
|
| 2538 |
|
|
|
|
| 2547 |
mfds_df = pd.DataFrame()
|
| 2548 |
mfds_live_msg = f"MFDS ์ค์๊ฐ ์กฐํ ์คํจ: {e}"
|
| 2549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2550 |
for country in countries:
|
| 2551 |
col_name = display_country(country)
|
| 2552 |
gpt_targets = []
|
|
|
|
| 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({
|
|
|
|
| 2605 |
"component": item["component"],
|
| 2606 |
})
|
| 2607 |
|
| 2608 |
+
batch_results = evaluate_components_with_llm_batch(country=country, targets=gpt_targets, llm=llm)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2609 |
|
| 2610 |
for item in row_items:
|
| 2611 |
if col_name in item["row_data"]:
|
|
|
|
| 2650 |
|
| 2651 |
formula_total = sum_formula_percent(formula_df)
|
| 2652 |
|
| 2653 |
+
safe_country_name = "_".join([re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ_-]", "_", str(c)) for c in countries])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2654 |
product_part = safe_filename(product_name) if safe_str(product_name) else "product"
|
| 2655 |
out_path = str(OUTPUT_DIR / f"screening_result_{product_part}_{safe_country_name}.xlsx")
|
| 2656 |
|
|
|
|
| 2669 |
f"- In formula ํฉ๊ณ: {format_amount(formula_total)}%\n"
|
| 2670 |
f"- ์ฌ๊ฒํ ํ์ ์ฑ๋ถ: {len(regulatory_df)}๊ฐ\n"
|
| 2671 |
f"- MFDS ์ํ: {mfds_live_msg}\n"
|
|
|
|
| 2672 |
"- ๊ฒฐ๊ณผ ํ์ผ ์ํธ:\n"
|
| 2673 |
" 1) Formula Ingredients Statement\n"
|
| 2674 |
" 2) Regulatory Screening Summary\n"
|
|
|
|
| 2689 |
|
| 2690 |
with gr.Blocks(css=custom_css) as demo:
|
| 2691 |
gr.Markdown("# ๐ reBom Regulatory Screening AI")
|
| 2692 |
+
gr.Markdown("### ๊ตญ๊ฐ๋ณ ๊ท์ ํด๋ ๊ฒ์ + ์์ฝ์ฒ ์ค์๊ฐ ์กฐํ + Formular Ingredients Statement ์ถ๋ ฅ")
|
| 2693 |
|
| 2694 |
with gr.Row():
|
| 2695 |
+
gr.Button("๐ช ๋ก๊ทธ์์", link="/logout?all_session=false", variant="secondary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2696 |
|
| 2697 |
with gr.Tab("๐ ์คํฌ๋ฆฌ๋"):
|
| 2698 |
country = gr.CheckboxGroup(
|
|
|
|
| 2719 |
value=False,
|
| 2720 |
)
|
| 2721 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2722 |
live_max_terms = gr.Number(
|
| 2723 |
+
label="์์ฝ์ฒ ์ค์๊ฐ ์กฐํ ์ต๋ ๊ฒ์์ด ์",
|
| 2724 |
value=5,
|
| 2725 |
precision=0,
|
| 2726 |
)
|
| 2727 |
|
| 2728 |
live_headless = gr.Checkbox(
|
| 2729 |
+
label="์์ฝ์ฒ ์กฐํ ๋ธ๋ผ์ฐ์ ์จ๊น ๋ชจ๋",
|
| 2730 |
value=True,
|
| 2731 |
)
|
| 2732 |
|
|
|
|
| 2775 |
file,
|
| 2776 |
product_name,
|
| 2777 |
use_mfds_live,
|
|
|
|
| 2778 |
live_max_terms,
|
| 2779 |
live_headless,
|
| 2780 |
],
|