jin2li commited on
Commit
31ccc3a
ยท
verified ยท
1 Parent(s): c0170bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -291
app.py CHANGED
@@ -20,7 +20,7 @@
20
  # - FAISS ๋””์Šคํฌ ์บ์‹œ
21
  # - GPT๋Š” ๊ฒ€์ƒ‰๋œ ์›๋ฌธ ์•ˆ์—์„œ๋งŒ ํŒ์ •
22
  # - ์‹์•ฝ์ฒ˜ ์‚ฌ์ดํŠธ ์‹ค์‹œ๊ฐ„ ์กฐํšŒ ์˜ต์…˜
23
- # - COOS ์‚ฌ์ดํŠธ ์‹ค์‹œ๊ฐ„ ์กฐํšŒ ์˜ต์…˜
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
- "label": "Limonene",
301
- "aliases": ["limonene", "๋ฆฌ๋ชจ๋„จ"],
302
- "leave_on": 0.001,
303
- "wash_off": 0.01,
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
- ".cache",
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
- cache_path,
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 live_visible_text(page):
1289
  try:
1290
  html = page.content()
1291
 
@@ -1304,7 +1254,7 @@ def live_visible_text(page):
1304
  return ""
1305
 
1306
 
1307
- def live_fill_search_input(page, term):
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 live_click_search(page):
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 live_click_result_row(page, term):
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 parse_live_text(term, text, source_name, source_url):
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
- "์ถœ์ฒ˜": f"{source_name} live: {source_url}",
1517
  "์ƒ์„ธ์›๋ฌธ": clean[:6000],
1518
  })
1519
 
1520
  return rows
1521
 
1522
 
1523
- def live_lookup_terms(site_name, site_url, terms, max_terms=30, headless=True):
1524
  if not PLAYWRIGHT_AVAILABLE or sync_playwright is None:
1525
- return pd.DataFrame(), f"{site_name} ์กฐํšŒ ์‹คํŒจ: playwright ๋ฏธ์„ค์น˜ ๋˜๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ฏธ์„ค์น˜"
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
- all_rows = []
1531
- errors = []
 
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"{site_name} ์‹ค์‹œ๊ฐ„ ์กฐํšŒ {idx}/{len(terms)}: {term}")
1545
 
1546
- page.goto(site_url, wait_until="domcontentloaded", timeout=30000)
1547
  page.wait_for_timeout(2500)
1548
 
1549
- if not live_fill_search_input(page, term):
1550
- errors.append(f"{term}: ๊ฒ€์ƒ‰ ์ž…๋ ฅ์ฐฝ ์ฐพ๊ธฐ ์‹คํŒจ")
1551
  continue
1552
 
1553
- if not live_click_search(page):
1554
  try:
1555
  page.keyboard.press("Enter")
1556
  except Exception:
1557
  pass
1558
 
1559
  page.wait_for_timeout(3000)
1560
- live_click_result_row(page, term)
1561
  page.wait_for_timeout(2000)
1562
 
1563
- text = live_visible_text(page)
1564
- rows = parse_live_text(term, text, site_name, site_url)
1565
 
1566
  if rows:
1567
- all_rows.extend(rows)
1568
  else:
1569
- errors.append(f"{term}: ๊ตญ๊ฐ€๋ณ„ ๋ฐฐํ•ฉํ•œ๋„/์ œํ•œ ์ •๋ณด ๋ฏธ๊ฒ€์ถœ")
1570
 
1571
  except Exception as e:
1572
- errors.append(f"{term}: {e}")
1573
 
1574
  context.close()
1575
  browser.close()
1576
 
 
 
 
 
 
1577
  except Exception as e:
1578
- return pd.DataFrame(), f"{site_name} ๋ธŒ๋ผ์šฐ์ € ์‹คํ–‰ ์‹คํŒจ: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1579
 
1580
  df = pd.DataFrame(all_rows)
1581
- msg = f"{site_name} ์‹ค์‹œ๊ฐ„ ์กฐํšŒ ์™„๋ฃŒ: ๊ฒ€์ƒ‰์–ด {len(terms)}๊ฐœ, ๊ฒฐ๊ณผ {len(df)}ํ–‰"
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("์ถœ์ฒ˜", "LIVE"))
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("### ๊ตญ๊ฐ€๋ณ„ ๊ทœ์ œ ํด๋” ๊ฒ€์ƒ‰ + ์‹์•ฝ์ฒ˜/COOS ์‹ค์‹œ๊ฐ„ ์กฐํšŒ + Formular Ingredients Statement ์ถœ๋ ฅ")
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
  ],