jin2li commited on
Commit
b7b4501
ยท
verified ยท
1 Parent(s): 3515003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +888 -76
app.py CHANGED
@@ -35,11 +35,7 @@
35
  # reBom Regulatory Screening AI - compact full app.py
36
  # Excel/PDF/Image input + country folder RAG + local rules + single ingredient search
37
 
38
- import os, re, json, base64, hashlib, traceback, sqlite3, secrets, datetime, time, sys, subprocess, inspect
39
- # Hugging Face Spaces + Gradio 6 SSR can hang on "Building". Force client-side rendering before importing gradio.
40
- os.environ.setdefault("GRADIO_SSR_MODE", "False")
41
- os.environ.setdefault("GRADIO_SERVER_NAME", "0.0.0.0")
42
- os.environ.setdefault("GRADIO_SERVER_PORT", "7860")
43
  from pathlib import Path
44
  from collections import defaultdict
45
  from functools import lru_cache
@@ -97,10 +93,10 @@ RAW_SEARCH_LOGS = []
97
  REG_TEXT_CACHE = {}
98
  REG_SECTION_INDEX_CACHE = {}
99
 
100
- COUNTRY_ORDER = ["ASEAN", "CALIFORNIA", "CHINA", "EU (FUTURE)", "EU", "US", "us", "JAPAN", "KOREA", "MEXICO", "SEPHORA", "TARGET", "UK", "ULTA"]
101
  COUNTRY_DISPLAY = {"EU":"EU","์œ ๋Ÿฝ":"EU","FDA":"US","๋ฏธ๊ตญ":"US","USA":"US","US":"US","KOREA":"Korea","ํ•œ๊ตญ":"Korea","KR":"Korea","JAPAN":"์ผ๋ณธ","์ผ๋ณธ":"์ผ๋ณธ","JP":"์ผ๋ณธ","CHINA":"์ค‘๊ตญ","์ค‘๊ตญ":"์ค‘๊ตญ","CN":"์ค‘๊ตญ","ASEAN":"ASEAN","์•„์„ธ์•ˆ":"ASEAN","UK":"UK","์˜๊ตญ":"UK","EU (FUTURE)":"EU (FUTURE)","EU_FUTURE":"EU (FUTURE)","CALIFORNIA":"CALIFORNIA","MEXICO":"MEXICO","SEPHORA":"SEPHORA","TARGET":"TARGET","ULTA":"ULTA"}
102
  COUNTRY_FOLDER_ALIASES = {"EU":"EU","์œ ๋Ÿฝ":"EU","FDA":"US","๋ฏธ๊ตญ":"US","USA":"US","US":"US","us":"us","KOREA":"KOREA","ํ•œ๊ตญ":"KOREA","KR":"KOREA","JAPAN":"JAPAN","์ผ๋ณธ":"JAPAN","JP":"JAPAN","CHINA":"CHINA","์ค‘๊ตญ":"CHINA","CN":"CHINA","ASEAN":"ASEAN","์•„์„ธ์•ˆ":"ASEAN","UK":"UK","์˜๊ตญ":"UK","EU (FUTURE)":"EU (FUTURE)","EU_FUTURE":"EU (FUTURE)","EU FUTURE":"EU (FUTURE)","CALIFORNIA":"CALIFORNIA","MEXICO":"MEXICO","SEPHORA":"SEPHORA","TARGET":"TARGET","ULTA":"ULTA"}
103
- BLOCKED_PATHS = [str(BASE_DIR / c) for c in COUNTRY_ORDER] + [str(BASE_DIR/FAISS_CACHE_DIR), str(BASE_DIR / '.env'), str(BASE_DIR / '__pycache__')]
104
  ALLOWED_PATHS = [str(OUTPUT_DIR), str(BASE_DIR)]
105
  SUPPORTED_INPUT_EXTENSIONS = {".xlsx",".xls",".pdf",".png",".jpg",".jpeg",".webp"}
106
  SUPPORTED_REG_FILE_EXTENSIONS = {".pdf",".txt",".csv",".xlsx",".xls"}
@@ -168,7 +164,12 @@ def safe_filename(text): return re.sub(r"[^a-zA-Z0-9๊ฐ€-ํžฃ._() -]","_",safe_str
168
  def chunk_list(items,size):
169
  for i in range(0,len(items),size): yield items[i:i+size]
170
 
 
 
 
 
171
 
 
172
  def get_embeddings():
173
  global EMBEDDINGS_CACHE
174
  if EMBEDDINGS_CACHE is None: EMBEDDINGS_CACHE=OpenAIEmbeddings(model="text-embedding-3-small")
@@ -256,6 +257,9 @@ def is_plain_fragrance_component(material,component):
256
  identity=normalize_compact(" ".join([component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…",""),component.get("INCI Name",""),component.get("FUNCTION",""),component.get("CAS.No",""),material.get("Trade name(๊ตญ๋ฌธ)",""),material.get("Trade name(์˜๋ฌธ)","")]))
257
  return any(normalize_compact(w) in identity for w in ["ํ–ฅ๋ฃŒ","fragrance","parfum","perfume","aroma"])
258
 
 
 
 
259
 
260
  def get_restricted_candidate_reason(material,component):
261
  if is_always_pass_component(material,component): return ""
@@ -272,6 +276,10 @@ def get_restricted_candidate_reason(material,component):
272
  if normalize_compact(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…",""))==normalize_compact("์‚ด๋ฆฌ์‹ค๋ฆญ์• ์”จ๋“œ"): return "์ œํ•œ ๊ฐ€๋Šฅ์„ฑ ์„ฑ๋ถ„"
273
  return ""
274
 
 
 
 
 
275
 
276
  def log_search_context(target_id,country,component,result_text,source,context):
277
  RAW_SEARCH_LOGS.append({"target_id":target_id,"๊ตญ๊ฐ€/์ฑ„๋„":display_country(country),"์„ฑ๋ถ„๋ช…":safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…","")),"INCI Name":safe_str(component.get("INCI Name","")),"ํŒ์ •":safe_str(result_text),"์ถœ์ฒ˜":safe_str(source),"๊ฒ€์ƒ‰ ์›๋ฌธ":safe_str(context)[:3000]})
@@ -310,6 +318,32 @@ def validate_limit_result_against_evidence(result_obj,evidence_text,component):
310
  result_obj["๋น„๊ณ "]=(note+" / "+ev) if note and note!="-" else ev
311
  return result_obj
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
 
315
  def standardize_formula_dataframe(df):
@@ -560,6 +594,8 @@ def is_broad_lookup_term(term):
560
  return False
561
 
562
 
 
 
563
  # ============================================================
564
  # ๊ทœ์ œ ๋™์˜์–ด / CAS / ์ด์„ฑ์งˆ์ฒด ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ
565
  # - ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ํ‘œ๊ธฐ๊ฐ€ INCI์™€ ๋‹ค๋ฅผ ๋•Œ PASS ์˜คํŒ์„ ๋ง‰๊ธฐ ์œ„ํ•œ ๋ณด์ •์ธต
@@ -1148,6 +1184,8 @@ def validate_country_file_match_context(context, component, lookup_terms):
1148
  return False
1149
 
1150
 
 
 
1151
  # country db
1152
 
1153
  def resolve_country_folder(country):
@@ -1157,16 +1195,18 @@ def resolve_country_folder(country):
1157
  fmap={f.lower():f for f in os.listdir(".") if os.path.isdir(f)}
1158
  return fmap.get(alias.lower(),alias)
1159
 
1160
- def _base_get_country_folders():
1161
  exclude={".git",".cache","__pycache__",".ipynb_checkpoints",".venv","venv","node_modules",FAISS_CACHE_DIR,"outputs"}
1162
  folders=[f for f in os.listdir(".") if os.path.isdir(f) and f not in exclude]
1163
  existing=set(folders); ordered=[c for c in COUNTRY_ORDER if c in existing]; extras=sorted([f for f in folders if f not in COUNTRY_ORDER])
1164
  return ordered+extras if ordered or extras else COUNTRY_ORDER.copy()
1165
 
1166
- def _base_refresh_country_choices_keep_selection(current_values=None):
1167
  choices=get_country_folders(); kept=[v for v in (current_values or []) if v in choices]
1168
  return gr.update(choices=choices,value=kept)
1169
 
 
 
1170
 
1171
  def get_regulatory_file_list(folder):
1172
  files=[]
@@ -1465,6 +1505,8 @@ def mfds_click_probable_result(page, term):
1465
  return False
1466
 
1467
 
 
 
1468
  def mfds_click_first_detail_result(page):
1469
  """
1470
  ์‹์•ฝ์ฒ˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์—์„œ ๊ฒ€์ƒ‰์–ด์™€ ๊ฒฐ๊ณผ๋ช…์นญ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
@@ -1753,6 +1795,8 @@ def mfds_lookup_one_term_with_retry(page, term):
1753
  return [], last_error[:300]
1754
 
1755
 
 
 
1756
  def mfds_launch_chromium(playwright_obj):
1757
  """
1758
  Playwright Chromium ๋ธŒ๋ผ์šฐ์ € ์‹คํ–‰ ํ•จ์ˆ˜.
@@ -1947,6 +1991,15 @@ def evaluate_live_detail_rule(country, material, component, live_df):
1947
  }
1948
 
1949
 
 
 
 
 
 
 
 
 
 
1950
 
1951
  def evaluate_limit_rule(country,material,component,group,name):
1952
  key=match_rule_key(material,component,group)
@@ -1960,6 +2013,24 @@ def evaluate_limit_rule(country,material,component,group,name):
1960
  return {"๋Œ€ํ‘œ INCI":label,"๊ฒฐ๊ณผ":f"{name} / {label} ํ˜„์žฌ ํ•จ๋Ÿ‰ {at}%, ํ•œ๋„ {format_percent(lim)}% ์ด๋‚ด","ํ‘œ์‹œ๋ฌธ๊ตฌ":cr.get("formula_text") or f"๋ฐฐํ•ฉํ•œ๋„: {format_percent(lim)}%","๋น„๊ณ ":"-"}
1961
  return {"๋Œ€ํ‘œ INCI":label,"๊ฒฐ๊ณผ":f"{name} / {label} / ํ•œ๋„๊ฐ’ ํ™•์ธ ํ•„์š”","ํ‘œ์‹œ๋ฌธ๊ตฌ":LIMIT_CHECK_TEXT,"๋น„๊ณ ":"๋กœ์ปฌ ๊ทœ์น™์— ์„ฑ๋ถ„์€ ์žˆ์œผ๋‚˜ ํ•œ๋„๊ฐ’์ด ๋น„์–ด ์žˆ์Œ."}
1962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1963
 
1964
  def parse_country_response(text):
1965
  res={"๋Œ€ํ‘œ INCI":"-","๊ฒฐ๊ณผ":NO_EVIDENCE_TEXT,"ํ‘œ์‹œ๋ฌธ๊ตฌ":"Pass","๋น„๊ณ ":"-"}
@@ -2018,6 +2089,16 @@ def make_formula_statement_rows(row_items,country_cols):
2018
  for cc in country_cols: total_row[cc]=""
2019
  rows.append(total_row); return pd.DataFrame(rows)
2020
 
 
 
 
 
 
 
 
 
 
 
2021
 
2022
  def sum_formula_percent(df): return sum(to_number(r.get("%(w/w)","")) or 0 for _,r in df.iterrows() if not is_total_like_text(r.get("INGREDIENT NAME (INCI)","")))
2023
 
@@ -2040,6 +2121,9 @@ def style_output_workbook(path,product_name=""):
2040
  for col in range(1,sheet.max_column+1): sheet.column_dimensions[get_column_letter(col)].width=18
2041
  wb.save(path)
2042
 
 
 
 
2043
 
2044
  def process(countries,file,product_name="",use_mfds_live=False,live_max_terms=5,live_headless=True):
2045
  """
@@ -2315,6 +2399,9 @@ def build_single_component_from_query(query, amount="", function="", single_amou
2315
  return mat, comp, at
2316
 
2317
 
 
 
 
2318
  def get_country_regulatory_text_cached(country):
2319
  """
2320
  ๊ตญ๊ฐ€๋ณ„ ๊ทœ์ œ ํŒŒ์ผ ์›๋ฌธ์„ ์ง์ ‘ ์ฝ์–ด์„œ ์บ์‹œํ•œ๋‹ค.
@@ -2548,7 +2635,7 @@ def split_regulatory_text_into_sections(raw_text):
2548
  return sections
2549
 
2550
 
2551
- def _base_get_country_section_index_cached(country):
2552
  """
2553
  ๋‹จ์ผ ์„ฑ๋ถ„ ๊ฒ€์ƒ‰ ๊ณ ์†ํ™”์šฉ ์„น์…˜ ์ธ๋ฑ์Šค.
2554
  ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ์„ ๋งค๋ฒˆ PDF/Excel ์ „์ฒด ์ฝ๊ธฐํ•˜์ง€ ์•Š๊ณ ,
@@ -2813,6 +2900,44 @@ def warmup_country_indexes(countries):
2813
  return "\n".join(messages) if messages else "์„ ํƒ๋œ ๊ตญ๊ฐ€ ์—†์Œ"
2814
 
2815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2816
  def make_restricted_or_positive_result(country, component, match, reg_type):
2817
  context = safe_str(match.get("context", "")) if isinstance(match, dict) else safe_str(match)
2818
  label = safe_str(reg_type.get("label", "๊ทœ์ œ ๋ชฉ๋ก"))
@@ -3025,6 +3150,177 @@ def build_country_source_record_index(country):
3025
  return records, "BUILT_SOURCE_RECORD_INDEX"
3026
 
3027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3028
  def shorten_for_table(value, max_len=80):
3029
  value = safe_str(value)
3030
 
@@ -3325,10 +3621,54 @@ DEFAULT_ADMIN_EMAIL = os.getenv("ADMIN_EMAIL", "jin2li@rebom.kr").strip().lower(
3325
  DEFAULT_ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "qwer0515!")
3326
 
3327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3328
  # ============================================================
3329
  # Gradio ๊ธฐ๋ณธ auth ์‚ฌ์šฉ ์‹œ ๋‚ด๋ถ€ ๋กœ๊ทธ์ธ ์ฒดํฌ ์šฐํšŒ
3330
  # ============================================================
3331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3332
 
3333
  # ============================================================
3334
  # ๋กœ๊ทธ์ธ ์ธ์ฆ
@@ -3336,6 +3676,35 @@ DEFAULT_ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "qwer0515!")
3336
  # - USER_DB์— ์—ฌ๋Ÿฌ ์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3337
  # ============================================================
3338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3339
 
3340
  # ============================================================
3341
  # ๋กœ๊ทธ์ธ ์ธ์ฆ
@@ -3347,28 +3716,34 @@ DEFAULT_ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "qwer0515!")
3347
  # "๋ฉ”์ผ์ฃผ์†Œ": "๋น„๋ฐ€๋ฒˆํ˜ธ",
3348
  # ============================================================
3349
 
 
 
3350
 
3351
- # Gradio ๊ธฐ๋ณธ auth๋ฅผ ํ†ต๊ณผํ•œ ์‚ฌ์šฉ์ž๋Š” ์ด๋ฏธ ์ธ์ฆ๋œ ์ƒํƒœ์ด๋ฏ€๋กœ ๋‚ด๋ถ€ ๋กœ๊ทธ์ธ ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
 
 
 
3352
 
3353
 
3354
- # ============================================================
3355
- # ๋กœ๊ทธ์ธ ์ธ์ฆ
3356
- # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3357
- # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3358
- # ============================================================
3359
 
3360
 
3361
- # ============================================================
3362
- # ๋กœ๊ทธ์ธ ์ธ์ฆ
3363
- # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3364
- # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3365
- # ============================================================
3366
 
3367
 
3368
- # ============================================================
3369
- # ๋กœ๊ทธ์ธ ์ธ์ฆ
3370
- # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3371
- # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
 
 
 
 
3372
 
3373
 
3374
 
@@ -3377,18 +3752,13 @@ DEFAULT_ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "qwer0515!")
3377
  # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3378
  # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3379
  # ============================================================
3380
- # ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ๋ฐฉ๋ฒ•:
3381
- # USER_DB ์•ˆ์— "์•„์ด๋””": "๋น„๋ฐ€๋ฒˆํ˜ธ", ํ˜•์‹์œผ๋กœ ํ•œ ์ค„์”ฉ ์ถ”๊ฐ€ํ•˜์„ธ์š”.
3382
- # ๊ฐ ์ค„ ๋์—๋Š” ์ฝค๋งˆ(,)๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
3383
- # ============================================================
3384
 
3385
  USER_DB = {
3386
  "jin2li@rebom.kr": "qwer0515!",
3387
- "jkh@rebom.kr": "rebom123!",
3388
- "admin": "rebom123!",
3389
 
3390
  # ์‚ฌ์šฉ์ž ๋“ฑ๋ก ์˜ˆ์‹œ:
3391
  # "yubin.p@rebom.kr": "123456!",
 
3392
  }
3393
 
3394
 
@@ -3411,13 +3781,97 @@ def process_single_ingredient_with_auth(user, countries, single_query, single_am
3411
  single_use_mfds_live,
3412
  )
3413
 
3414
- # ============================================================
3415
- # STRUCTURED FOLDER SCREENING PATCH V5.1
3416
- # ๊ธฐ์กด UI/๋กœ๊ทธ์ธ/์—‘์…€ ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” ์œ ์ง€ํ•˜๊ณ , ํŒ์ • ์—”์ง„๋งŒ ํด๋” ๊ตฌ์กฐ ๊ธฐ์ค€์œผ๋กœ ์žฌ์ •์˜
3417
- # ============================================================
3418
 
3419
- SOURCE_ONLY_MODE = True
3420
- TREAT_POSITIVE_LIST_AS_PASS = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3421
 
3422
  V51_PROHIBITED_TERMS = [
3423
  "๋ฐฐํ•ฉ๊ธˆ์ง€", "์‚ฌ์šฉ๊ธˆ์ง€", "์‚ฌ์šฉ ๊ธˆ์ง€", "๊ธˆ์ง€์›๋ฃŒ", "๊ธˆ์ง€ ์›๋ฃŒ", "๊ธˆ์ง€์„ฑ๋ถ„", "๊ธˆ์ง€ ์„ฑ๋ถ„",
@@ -3567,6 +4021,38 @@ def _v51_is_glycerin_deg_impurity_record(country, record, component):
3567
 
3568
  return has_glycerin and has_deg and has_impurity_limit
3569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3570
 
3571
 
3572
  def _v51_component_match_in_record(record_text, terms):
@@ -3658,6 +4144,24 @@ def normalize_formula_country_text(text, country_col="", inci_name="", amount_te
3658
  return "Pass"
3659
 
3660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3661
  def _v51_nonlegal_result(kind, country, component, best, target_id=""):
3662
  country_col = display_country(country)
3663
  representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
@@ -3732,11 +4236,141 @@ def _v51_nonlegal_result(kind, country, component, best, target_id=""):
3732
  return result
3733
 
3734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3735
  # ============================================================
3736
  # END STRUCTURED FOLDER SCREENING PATCH V5.1
3737
  # ============================================================
3738
 
3739
 
 
 
3740
  # ============================================================
3741
  # STRUCTURED FOLDER SCREENING PATCH V5.2
3742
  # 2026-06: US ํด๋”๋ช… ๋ฐ˜์˜ + ์ฃผ์š” ์˜คํŒ์ • ์ฐจ๋‹จ
@@ -3746,8 +4380,35 @@ def _v51_nonlegal_result(kind, country, component, best, target_id=""):
3746
  # - Phenoxyethanol: EU/ASEAN/KOREA/JAPAN ๋ณด์กด์ œ ์ตœ๋Œ€ 1.0% ํ•œ๋„๋กœ ๊ณ ์ • ๋ณด์ •
3747
  # ============================================================
3748
 
 
3749
  COUNTRY_DISPLAY.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "US"})
3750
  COUNTRY_FOLDER_ALIASES.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "us"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3751
 
3752
 
3753
  def _v52_component_blob(material, component):
@@ -3877,6 +4538,35 @@ def _v51_record_type(country, record, component=None):
3877
  return "unknown"
3878
 
3879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3880
 
3881
  # ============================================================
3882
  # END STRUCTURED FOLDER SCREENING PATCH V5.2
@@ -3893,8 +4583,12 @@ def _v51_record_type(country, record, component=None):
3893
  # - ํŒŒ์ผ๋ช…/๋ฌธ์„œ ์ „์ฒด์˜ prohibited/limit ํ‚ค์›Œ๋“œ๋งŒ์œผ๋กœ ์„ฑ๋ถ„ ํŒ์ • ๊ธˆ์ง€
3894
  # ============================================================
3895
 
 
 
 
3896
  COUNTRY_DISPLAY.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "US"})
3897
  COUNTRY_FOLDER_ALIASES.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "us", "Us": "US"})
 
3898
 
3899
 
3900
  def country_key(country):
@@ -3915,6 +4609,19 @@ def country_key(country):
3915
  return m.get(t, t)
3916
 
3917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3918
  def _v6_path_text(record):
3919
  return f"{safe_str(record.get('source_file',''))}\n{safe_str(record.get('sheet',''))}".replace('\\', '/')
3920
 
@@ -4161,6 +4868,125 @@ def _v6_nonlegal_result(country, material, component, record, rtype, target_id="
4161
  return _v6_result(representative, NO_EVIDENCE_TEXT, "Pass", src, target_id, country, component, "V6_REFERENCE_ONLY_NO_LEGAL_CLASSIFICATION")
4162
 
4163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4164
  def evaluate_critical_country_rule(country, material, component):
4165
  if SOURCE_ONLY_MODE:
4166
  return None
@@ -4179,6 +5005,16 @@ def evaluate_local_rules(country, material, component, ingredient_totals, ingred
4179
  return None
4180
 
4181
 
 
 
 
 
 
 
 
 
 
 
4182
 
4183
  # ============================================================
4184
  # END SOURCE TABLE ROW ENGINE PATCH V6
@@ -4597,6 +5433,8 @@ def is_country_result_issue(text):
4597
  # ============================================================
4598
 
4599
 
 
 
4600
  # ============================================================
4601
  # USER PATCH 2026-06-15
4602
  # 1) Output country/channel display names forced to English
@@ -4657,7 +5495,7 @@ def display_country(country):
4657
  return COUNTRY_DISPLAY.get(str(country).upper(), COUNTRY_DISPLAY.get(str(country), c))
4658
 
4659
 
4660
- _ORIGINAL_get_country_folders = _base_get_country_folders
4661
 
4662
 
4663
  def get_country_folders():
@@ -4665,7 +5503,7 @@ def get_country_folders():
4665
  return [c for c in _ORIGINAL_get_country_folders() if not _is_hidden_always_folder(c)]
4666
 
4667
 
4668
- _ORIGINAL_refresh_country_choices_keep_selection = _base_refresh_country_choices_keep_selection
4669
 
4670
 
4671
  def refresh_country_choices_keep_selection(current_values=None):
@@ -4682,7 +5520,7 @@ def refresh_country_choices_and_clear_cache(current_values=None):
4682
  return refresh_country_choices_keep_selection(current_values)
4683
 
4684
 
4685
- _ORIGINAL_get_country_section_index_cached = _base_get_country_section_index_cached
4686
 
4687
 
4688
  def get_country_section_index_cached(country):
@@ -4718,7 +5556,7 @@ def get_country_section_index_cached(country):
4718
  return list(sections or []) + extra_sections, status
4719
 
4720
 
4721
- with gr.Blocks() as demo:
4722
  current_user = gr.State({})
4723
  gr.Markdown("# ๐ŸŒ reBom")
4724
  gr.HTML("""
@@ -4797,6 +5635,8 @@ with gr.Blocks() as demo:
4797
  </script>
4798
 
4799
 
 
 
4800
  """)
4801
  gr.Markdown("### Screening")
4802
 
@@ -4848,39 +5688,11 @@ with gr.Blocks() as demo:
4848
  inputs=[current_user,single_country,single_query,single_amount,single_function],
4849
  outputs=[single_df_out,single_msg_out],
4850
  )
4851
-
4852
- # ============================================================
4853
- # Hugging Face Spaces launch - simple stable mode
4854
- # ============================================================
4855
-
4856
- # ์ค‘์š”:
4857
- # Hugging Face Spaces์—์„œ๋Š” share=True๊ฐ€ ํ•„์š” ์—†๊ณ , ์ผ๋ถ€ Gradio 6 SSR/iframe ์กฐํ•ฉ์—์„œ
4858
- # App ํ™”๋ฉด์ด ๋นˆ ํ™”๋ฉด์œผ๋กœ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ์–ด launch ์˜ต์…˜์„ ์ตœ์†Œํ™”ํ•œ๋‹ค.
4859
- try:
4860
- demo.queue()
4861
- except Exception:
4862
- pass
4863
-
4864
- _launch_kwargs = {
4865
- "server_name": "0.0.0.0",
4866
- "server_port": 7860,
4867
- "auth": auth_fn,
4868
- "share": False,
4869
- }
4870
-
4871
- # ์„ค์น˜๋œ Gradio ๋ฒ„์ „์— ์žˆ๋Š” ์ธ์ž๋งŒ ์ „๋‹ฌํ•œ๋‹ค. Gradio 5.x์—์„œ๋Š” ssr_mode๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ๋‹ค.
4872
  try:
4873
- _launch_params = set(inspect.signature(demo.launch).parameters.keys())
4874
- except Exception:
4875
- _launch_params = set()
4876
-
4877
- if not _launch_params or "ssr_mode" in _launch_params:
4878
- _launch_kwargs["ssr_mode"] = False
4879
-
4880
- # Gradio 6.x๋Š” css๋ฅผ launch()์—์„œ ๋ฐ›๋Š”๋‹ค. Gradio 5.x๋Š” ์—†์œผ๋ฉด ์ž๋™ ์ œ์™ธ๋œ๋‹ค.
4881
- if not _launch_params or "css" in _launch_params:
4882
- _launch_kwargs["css"] = custom_css
4883
-
4884
- _launch_kwargs = {k: v for k, v in _launch_kwargs.items() if (not _launch_params or k in _launch_params)}
4885
-
4886
- demo.launch(**_launch_kwargs)
 
35
  # reBom Regulatory Screening AI - compact full app.py
36
  # Excel/PDF/Image input + country folder RAG + local rules + single ingredient search
37
 
38
+ import os, re, json, base64, hashlib, traceback, sqlite3, secrets, datetime, time, sys, subprocess, time
 
 
 
 
39
  from pathlib import Path
40
  from collections import defaultdict
41
  from functools import lru_cache
 
93
  REG_TEXT_CACHE = {}
94
  REG_SECTION_INDEX_CACHE = {}
95
 
96
+ COUNTRY_ORDER = ["ASEAN","CALIFORNIA","CHINA","EU (FUTURE)","EU","US","JAPAN","KOREA","MEXICO","SEPHORA","TARGET","UK","ULTA"]
97
  COUNTRY_DISPLAY = {"EU":"EU","์œ ๋Ÿฝ":"EU","FDA":"US","๋ฏธ๊ตญ":"US","USA":"US","US":"US","KOREA":"Korea","ํ•œ๊ตญ":"Korea","KR":"Korea","JAPAN":"์ผ๋ณธ","์ผ๋ณธ":"์ผ๋ณธ","JP":"์ผ๋ณธ","CHINA":"์ค‘๊ตญ","์ค‘๊ตญ":"์ค‘๊ตญ","CN":"์ค‘๊ตญ","ASEAN":"ASEAN","์•„์„ธ์•ˆ":"ASEAN","UK":"UK","์˜๊ตญ":"UK","EU (FUTURE)":"EU (FUTURE)","EU_FUTURE":"EU (FUTURE)","CALIFORNIA":"CALIFORNIA","MEXICO":"MEXICO","SEPHORA":"SEPHORA","TARGET":"TARGET","ULTA":"ULTA"}
98
  COUNTRY_FOLDER_ALIASES = {"EU":"EU","์œ ๋Ÿฝ":"EU","FDA":"US","๋ฏธ๊ตญ":"US","USA":"US","US":"US","us":"us","KOREA":"KOREA","ํ•œ๊ตญ":"KOREA","KR":"KOREA","JAPAN":"JAPAN","์ผ๋ณธ":"JAPAN","JP":"JAPAN","CHINA":"CHINA","์ค‘๊ตญ":"CHINA","CN":"CHINA","ASEAN":"ASEAN","์•„์„ธ์•ˆ":"ASEAN","UK":"UK","์˜๊ตญ":"UK","EU (FUTURE)":"EU (FUTURE)","EU_FUTURE":"EU (FUTURE)","EU FUTURE":"EU (FUTURE)","CALIFORNIA":"CALIFORNIA","MEXICO":"MEXICO","SEPHORA":"SEPHORA","TARGET":"TARGET","ULTA":"ULTA"}
99
+ BLOCKED_PATHS = [str(BASE_DIR / c) for c in COUNTRY_ORDER] + [str(BASE_DIR/FAISS_CACHE_DIR), str(BASE_DIR/'.env'), str(BASE_DIR/'__pycache__')]
100
  ALLOWED_PATHS = [str(OUTPUT_DIR), str(BASE_DIR)]
101
  SUPPORTED_INPUT_EXTENSIONS = {".xlsx",".xls",".pdf",".png",".jpg",".jpeg",".webp"}
102
  SUPPORTED_REG_FILE_EXTENSIONS = {".pdf",".txt",".csv",".xlsx",".xls"}
 
164
  def chunk_list(items,size):
165
  for i in range(0,len(items),size): yield items[i:i+size]
166
 
167
+ def country_key(country):
168
+ t=safe_str(country).upper().replace("_"," ").replace("-"," "); t=re.sub(r"\s+"," ",t).strip()
169
+ m={"ํ•œ๊ตญ":"KOREA","KOREA":"KOREA","KR":"KOREA","EU":"EU","EUROPE":"EU","์œ ๋Ÿฝ":"EU","EU FUTURE":"EU_FUTURE","EU (FUTURE)":"EU_FUTURE","JAPAN":"JAPAN","JP":"JAPAN","์ผ๋ณธ":"JAPAN","CHINA":"CHINA","CN":"CHINA","์ค‘๊ตญ":"CHINA","USA":"US","US":"US","๋ฏธ๊ตญ":"US","FDA":"US","ASEAN":"ASEAN","์•„์„ธ์•ˆ":"ASEAN","UK":"UK","์˜๊ตญ":"UK","SEPHORA":"SEPHORA","TARGET":"TARGET","ULTA":"ULTA"}
170
+ return m.get(t,t)
171
 
172
+ def display_country(country): return COUNTRY_DISPLAY.get(str(country).upper(),COUNTRY_DISPLAY.get(str(country),str(country)))
173
  def get_embeddings():
174
  global EMBEDDINGS_CACHE
175
  if EMBEDDINGS_CACHE is None: EMBEDDINGS_CACHE=OpenAIEmbeddings(model="text-embedding-3-small")
 
257
  identity=normalize_compact(" ".join([component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…",""),component.get("INCI Name",""),component.get("FUNCTION",""),component.get("CAS.No",""),material.get("Trade name(๊ตญ๋ฌธ)",""),material.get("Trade name(์˜๋ฌธ)","")]))
258
  return any(normalize_compact(w) in identity for w in ["ํ–ฅ๋ฃŒ","fragrance","parfum","perfume","aroma"])
259
 
260
+ def evaluate_plain_fragrance_rule(country,material,component):
261
+ if not is_plain_fragrance_component(material,component): return None
262
+ return {"๋Œ€ํ‘œ INCI":safe_str(component.get("INCI Name","")) or "Fragrance","๊ฒฐ๊ณผ":f"ํ–ฅ๋ฃŒ / {display_country(country)} ์•Œ๋ ˆ๋ฅด๊ฒ ํ‘œ์‹œ์„ฑ๋ถ„ ๋ฐ ์„ธ๋ถ€ ์กฐ์„ฑ ํ™•์ธ ํ•„์š”","ํ‘œ์‹œ๋ฌธ๊ตฌ":NEED_CHECK_TEXT,"๋น„๊ณ ":"ํ–ฅ๋ฃŒ๋Š” ์„ธ๋ถ€ ๊ตฌ์„ฑ์„ฑ๋ถ„ ๋ฐ ์•Œ๋ ˆ๋ฅด๊ฒ ํ‘œ์‹œ๋Œ€์ƒ ์—ฌ๋ถ€ ํ™•์ธ ํ•„์š”. ์ž๋™ Pass ์ฒ˜๋ฆฌ ๊ธˆ์ง€."}
263
 
264
  def get_restricted_candidate_reason(material,component):
265
  if is_always_pass_component(material,component): return ""
 
276
  if normalize_compact(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…",""))==normalize_compact("์‚ด๋ฆฌ์‹ค๋ฆญ์• ์”จ๋“œ"): return "์ œํ•œ ๊ฐ€๋Šฅ์„ฑ ์„ฑ๋ถ„"
277
  return ""
278
 
279
+ def is_country_result_issue(text):
280
+ text=safe_str(text)
281
+ if not text or text in ["-",NO_EVIDENCE_TEXT,"Pass","PASS","pass"]: return False
282
+ return any(k in text for k in ["๋ฐฐํ•ฉ๊ธˆ์ง€","๊ธˆ์ง€","์ดˆ๊ณผ","๋ถ€์ ํ•ฉ","์žฌ๊ฒ€ํ† ","ํ™•์ธ ํ•„์š”","์ž๋ฃŒ ํ™•์ธ","ํ‘œ์‹œ ํ•„์š”","๏ฟฝ๏ฟฝ๋ฒจ๋ง","๋ถˆ๊ฐ€","์ฃผ์˜","๊ฒฝ๊ณ ","๋ฐฐํ•ฉํ•œ๋„ ํ™•์ธ ํ•„์š”","๊ธฐ๋Šฅ์„ฑ ํ™•์ธ","ํ•จ๋Ÿ‰ ํ™•์ธ"])
283
 
284
  def log_search_context(target_id,country,component,result_text,source,context):
285
  RAW_SEARCH_LOGS.append({"target_id":target_id,"๊ตญ๊ฐ€/์ฑ„๋„":display_country(country),"์„ฑ๋ถ„๋ช…":safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…","")),"INCI Name":safe_str(component.get("INCI Name","")),"ํŒ์ •":safe_str(result_text),"์ถœ์ฒ˜":safe_str(source),"๊ฒ€์ƒ‰ ์›๋ฌธ":safe_str(context)[:3000]})
 
318
  result_obj["๋น„๊ณ "]=(note+" / "+ev) if note and note!="-" else ev
319
  return result_obj
320
 
321
+ def normalize_formula_country_text(text,country_col="",inci_name="",amount_text=""):
322
+ text=safe_str(text); lower=text.lower(); inci_norm=normalize_compact(inci_name)
323
+ if not text or text=="-" or text==NO_EVIDENCE_TEXT: return "Pass"
324
+ if any(k in text for k in ["๋ฐฐํ•ฉ๊ธˆ์ง€","์‚ฌ์šฉ๊ธˆ์ง€","๊ธˆ์ง€"]) or any(k in lower for k in ["prohibited","banned","not allowed"]): return "๋ฐฐํ•ฉ๊ธˆ์ง€"
325
+ if LIMIT_CHECK_TEXT in text: return LIMIT_CHECK_TEXT
326
+ if NEED_CHECK_TEXT in text: return NEED_CHECK_TEXT
327
+ if "ํ–ฅ๋ฃŒ" in text or "fragrance" in lower or "parfum" in lower: return NEED_CHECK_TEXT
328
+ if "๊ธฐ๋Šฅ์„ฑ" in text: return "๊ธฐ๋Šฅ์„ฑ ํ™•์ธ"
329
+ if "ํ•จ๋Ÿ‰ ์ •๋ณด ์—†์Œ" in text or "ํ•จ๋Ÿ‰ ํ™•์ธ" in text: return "ํ•จ๋Ÿ‰ ํ™•์ธ"
330
+ if "octocrylene" in inci_norm or "์˜ฅํ† ํฌ๋ฆด๋ Œ" in inci_norm:
331
+ if country_col=="EU": return "๋ฐฐํ•ฉ ํ•œ๋„:\n์Šคํ”„๋ ˆ์ด: 9% /\n๊ธฐํƒ€์ œํ’ˆ: 10%"
332
+ if country_col=="FDA": return "๋ผ๋ฒจ๋ง ํ•„์ˆ˜"
333
+ if country_col in ["Korea","์ผ๋ณธ"]: return "๋ฐฐํ•ฉํ•œ๋„: 10%"
334
+ if "homosalate" in inci_norm or "ํ˜ธ๋ชจ์‚ด๋ ˆ์ดํŠธ" in inci_norm:
335
+ if country_col=="ASEAN": return "๋ฐฐํ•ฉํ•œ๋„: 10%"
336
+ if country_col=="EU": return "๋ฐฐํ•ฉํ•œ๋„: 7.34%"
337
+ if country_col=="FDA": return "๋ฐฐํ•ฉํ•œ๋„: 15%"
338
+ if country_col in ["Korea","์ผ๋ณธ"]: return "๋ฐฐํ•ฉํ•œ๋„: 10%"
339
+ if "octisalate" in inci_norm or "ethylhexylsalicylate" in inci_norm or "์˜ฅํ‹ฐ์‚ด๋ ˆ์ดํŠธ" in inci_norm: return "๋ฐฐํ•ฉํ•œ๋„: 5%"
340
+ if "avobenzone" in inci_norm or "์•„๋ณด๋ฒค์กด" in inci_norm: return "๋ฐฐํ•ฉํ•œ๋„: 3%" if country_col=="FDA" else "๋ฐฐํ•ฉํ•œ๋„: 5%"
341
+ if any(k in text for k in ["์ดˆ๊ณผ","๋ถ€์ ํ•ฉ","์‚ฌ์šฉ ๋ถˆ๊ฐ€","์‚ฌ์šฉ๋ถˆ๊ฐ€"]): return "ํ•œ๋„ ์ดˆ๊ณผ"
342
+ if "ํ‘œ์‹œ ํ•„์š”" in text or "๋ผ๋ฒจ" in text or "label" in lower: return "๋ผ๋ฒจ๋ง ํ•„์ˆ˜"
343
+ m=re.search(r"(?:๋ฐฐํ•ฉ\s*ํ•œ๋„|๋ฐฐํ•ฉํ•œ๋„|ํ•œ๋„|max|maximum|limit|์ตœ๋Œ€|์ดํ•˜|์ด๋‚ด)\s*[:๏ผš]?\s*([0-9]+(?:\.[0-9]+)?\s*%)",text,flags=re.I)
344
+ if m: return "๋ฐฐํ•ฉํ•œ๋„: "+m.group(1).replace(" ","")
345
+ if any(k in text for k in ["๋ฐฐํ•ฉํ•œ๋„","๋ฐฐํ•ฉ ํ•œ๋„","ํ•œ๋„","์ œํ•œ"]) or any(k in lower for k in ["maximum","limit","restricted"]): return LIMIT_CHECK_TEXT
346
+ return text[:120]
347
 
348
 
349
  def standardize_formula_dataframe(df):
 
594
  return False
595
 
596
 
597
+
598
+
599
  # ============================================================
600
  # ๊ทœ์ œ ๋™์˜์–ด / CAS / ์ด์„ฑ์งˆ์ฒด ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ
601
  # - ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ํ‘œ๊ธฐ๊ฐ€ INCI์™€ ๋‹ค๋ฅผ ๋•Œ PASS ์˜คํŒ์„ ๋ง‰๊ธฐ ์œ„ํ•œ ๋ณด์ •์ธต
 
1184
  return False
1185
 
1186
 
1187
+
1188
+
1189
  # country db
1190
 
1191
  def resolve_country_folder(country):
 
1195
  fmap={f.lower():f for f in os.listdir(".") if os.path.isdir(f)}
1196
  return fmap.get(alias.lower(),alias)
1197
 
1198
+ def get_country_folders():
1199
  exclude={".git",".cache","__pycache__",".ipynb_checkpoints",".venv","venv","node_modules",FAISS_CACHE_DIR,"outputs"}
1200
  folders=[f for f in os.listdir(".") if os.path.isdir(f) and f not in exclude]
1201
  existing=set(folders); ordered=[c for c in COUNTRY_ORDER if c in existing]; extras=sorted([f for f in folders if f not in COUNTRY_ORDER])
1202
  return ordered+extras if ordered or extras else COUNTRY_ORDER.copy()
1203
 
1204
+ def refresh_country_choices_keep_selection(current_values=None):
1205
  choices=get_country_folders(); kept=[v for v in (current_values or []) if v in choices]
1206
  return gr.update(choices=choices,value=kept)
1207
 
1208
+ def refresh_country_choices_and_clear_cache(current_values=None):
1209
+ VECTOR_DB_CACHE.clear(); EVAL_CACHE.clear(); REG_TEXT_CACHE.clear(); REG_SECTION_INDEX_CACHE.clear(); return refresh_country_choices_keep_selection(current_values)
1210
 
1211
  def get_regulatory_file_list(folder):
1212
  files=[]
 
1505
  return False
1506
 
1507
 
1508
+
1509
+
1510
  def mfds_click_first_detail_result(page):
1511
  """
1512
  ์‹์•ฝ์ฒ˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์—์„œ ๊ฒ€์ƒ‰์–ด์™€ ๊ฒฐ๊ณผ๋ช…์นญ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
 
1795
  return [], last_error[:300]
1796
 
1797
 
1798
+
1799
+
1800
  def mfds_launch_chromium(playwright_obj):
1801
  """
1802
  Playwright Chromium ๋ธŒ๋ผ์šฐ์ € ์‹คํ–‰ ํ•จ์ˆ˜.
 
1991
  }
1992
 
1993
 
1994
+ def evaluate_critical_country_rule(country,material,component):
1995
+ ckey=country_key(country); identity=get_identity_raw(material,component)
1996
+ for r in CRITICAL_COUNTRY_RULES.values():
1997
+ if any_exact_alias_match(r.get("aliases",[]), identity):
1998
+ cr=r.get(ckey)
1999
+ if not cr: return None
2000
+ if cr.get("status")=="prohibited": return {"๋Œ€ํ‘œ INCI":r.get("label","-"),"๊ฒฐ๊ณผ":cr.get("result","๋ฐฐํ•ฉ๊ธˆ์ง€"),"ํ‘œ์‹œ๋ฌธ๊ตฌ":cr.get("formula_text","๋ฐฐํ•ฉ๊ธˆ์ง€"),"๋น„๊ณ ":cr.get("note","-")}
2001
+ if cr.get("status")=="restricted": return {"๋Œ€ํ‘œ INCI":r.get("label","-"),"๊ฒฐ๊ณผ":cr.get("result","์ œํ•œ ์„ฑ๋ถ„"),"ํ‘œ์‹œ๋ฌธ๊ตฌ":cr.get("formula_text",LIMIT_CHECK_TEXT),"๋น„๊ณ ":cr.get("note","-")}
2002
+ return None
2003
 
2004
  def evaluate_limit_rule(country,material,component,group,name):
2005
  key=match_rule_key(material,component,group)
 
2013
  return {"๋Œ€ํ‘œ INCI":label,"๊ฒฐ๊ณผ":f"{name} / {label} ํ˜„์žฌ ํ•จ๋Ÿ‰ {at}%, ํ•œ๋„ {format_percent(lim)}% ์ด๋‚ด","ํ‘œ์‹œ๋ฌธ๊ตฌ":cr.get("formula_text") or f"๋ฐฐํ•ฉํ•œ๋„: {format_percent(lim)}%","๋น„๊ณ ":"-"}
2014
  return {"๋Œ€ํ‘œ INCI":label,"๊ฒฐ๊ณผ":f"{name} / {label} / ํ•œ๋„๊ฐ’ ํ™•์ธ ํ•„์š”","ํ‘œ์‹œ๋ฌธ๊ตฌ":LIMIT_CHECK_TEXT,"๋น„๊ณ ":"๋กœ์ปฌ ๊ทœ์น™์— ์„ฑ๋ถ„์€ ์žˆ์œผ๋‚˜ ํ•œ๋„๊ฐ’์ด ๋น„์–ด ์žˆ์Œ."}
2015
 
2016
+ def evaluate_local_rules(country,material,component,ingredient_totals,ingredient_amount_parts):
2017
+ crit=evaluate_critical_country_rule(country,material,component)
2018
+ if crit: return crit
2019
+ if is_always_pass_component(material,component): return {"๋Œ€ํ‘œ INCI":safe_str(component.get("INCI Name","")) or "Water","๊ฒฐ๊ณผ":NO_EVIDENCE_TEXT,"ํ‘œ์‹œ๋ฌธ๊ตฌ":"Pass","๋น„๊ณ ":"์ •์ œ์ˆ˜/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ˜๋ฆฌ"}
2020
+ fr=evaluate_plain_fragrance_rule(country,material,component)
2021
+ if fr: return fr
2022
+ for group,name in [(UV_FILTER_RULES,"UV ํ•„ํ„ฐ"),(PRESERVATIVE_RULES,"๋ณด์กด์ œ/์ œํ•œ์„ฑ๋ถ„")]:
2023
+ res=evaluate_limit_rule(country,material,component,group,name)
2024
+ if res: return res
2025
+ fkey=match_rule_key(material,component,FUNCTIONAL_KOREA_RULES)
2026
+ if country_key(country)=="KOREA" and fkey:
2027
+ r=FUNCTIONAL_KOREA_RULES[fkey]; amount=format_percent(component.get("amount_float")) if component.get("amount_float") is not None else "ํ•จ๋Ÿ‰ ํ™•์ธ ํ•„์š”"
2028
+ return {"๋Œ€ํ‘œ INCI":r["label"],"๊ฒฐ๊ณผ":r["result"].format(amount=amount),"ํ‘œ์‹œ๋ฌธ๊ตฌ":r.get("formula_text","๊ธฐ๋Šฅ์„ฑ ํ™•์ธ"),"๋น„๊ณ ":"๊ธฐ๋Šฅ์„ฑ ํ‘œ์‹œยท๊ด‘๊ณ  ์—ฌ๋ถ€ ํ™•์ธ"}
2029
+ identity=get_identity_raw(material,component)
2030
+ for r in FRAGRANCE_ALLERGEN_RULES.values():
2031
+ if any_exact_alias_match(r.get("aliases",[]), identity): return {"๋Œ€ํ‘œ INCI":r["label"],"๊ฒฐ๊ณผ":f"ํ–ฅ๋ฃŒ ์•Œ๋ ˆ๋ฅด๊ฒ / {r['label']} / {display_country(country)} ํ‘œ์‹œ๊ธฐ์ค€ ํ™•์ธ ํ•„์š”","ํ‘œ์‹œ๋ฌธ๊ตฌ":NEED_CHECK_TEXT,"๋น„๊ณ ":"ํ–ฅ๋ฃŒ ์•Œ๋ ˆ๋ฅด๊ฒ ํ›„๋ณด. ์„ธ๋ถ€ ํ‘œ์‹œ๊ธฐ์ค€ ๋ฐ ์ œํ’ˆ ์œ ํ˜• ํ™•์ธ ํ•„์š”."}
2032
+ if component.get("amount_float") is None: return {"๋Œ€ํ‘œ INCI":safe_str(component.get("INCI Name","")) or "-","๊ฒฐ๊ณผ":"ํ•จ๋Ÿ‰ ์ •๋ณด ์—†์Œ / In formula(w/w %) ํ™•์ธ ํ•„์š”","ํ‘œ์‹œ๋ฌธ๊ตฌ":"ํ•จ๋Ÿ‰ ํ™•์ธ","๋น„๊ณ ":"-"}
2033
+ return None
2034
 
2035
  def parse_country_response(text):
2036
  res={"๋Œ€ํ‘œ INCI":"-","๊ฒฐ๊ณผ":NO_EVIDENCE_TEXT,"ํ‘œ์‹œ๋ฌธ๊ตฌ":"Pass","๋น„๊ณ ":"-"}
 
2089
  for cc in country_cols: total_row[cc]=""
2090
  rows.append(total_row); return pd.DataFrame(rows)
2091
 
2092
+ def make_regulatory_summary_rows(row_items,country_cols):
2093
+ rows=[]
2094
+ for item in row_items:
2095
+ rd=item["row_data"]
2096
+ if not is_country_result_issue(rd.get("Result","")): continue
2097
+ row={"ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…":rd.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…",""),"INCI Name":rd.get("INCI Name",""),"In formula(w/w %)":rd.get("In formula\n(w/w %)",""),"Result":rd.get("Result","")}
2098
+ for cc in country_cols: row[cc]=rd.get(cc,"") if is_country_result_issue(rd.get(cc,"")) else "-"
2099
+ row["๋น„๊ณ "]=rd.get("๋น„๊ณ ","-"); rows.append(row)
2100
+ cols=["ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…","INCI Name","In formula(w/w %)","Result"]+country_cols+["๋น„๊ณ "]
2101
+ return pd.DataFrame(rows,columns=cols)
2102
 
2103
  def sum_formula_percent(df): return sum(to_number(r.get("%(w/w)","")) or 0 for _,r in df.iterrows() if not is_total_like_text(r.get("INGREDIENT NAME (INCI)","")))
2104
 
 
2121
  for col in range(1,sheet.max_column+1): sheet.column_dimensions[get_column_letter(col)].width=18
2122
  wb.save(path)
2123
 
2124
+ def build_integrated_result(rd,country_cols):
2125
+ issues=[cc for cc in country_cols if is_country_result_issue(rd.get(cc,""))]
2126
+ return "PASS" if not issues else "์žฌ๊ฒ€ํ†  ํ•„์š”: "+", ".join(issues)
2127
 
2128
  def process(countries,file,product_name="",use_mfds_live=False,live_max_terms=5,live_headless=True):
2129
  """
 
2399
  return mat, comp, at
2400
 
2401
 
2402
+
2403
+
2404
+
2405
  def get_country_regulatory_text_cached(country):
2406
  """
2407
  ๊ตญ๊ฐ€๋ณ„ ๊ทœ์ œ ํŒŒ์ผ ์›๋ฌธ์„ ์ง์ ‘ ์ฝ์–ด์„œ ์บ์‹œํ•œ๋‹ค.
 
2635
  return sections
2636
 
2637
 
2638
+ def get_country_section_index_cached(country):
2639
  """
2640
  ๋‹จ์ผ ์„ฑ๋ถ„ ๊ฒ€์ƒ‰ ๊ณ ์†ํ™”์šฉ ์„น์…˜ ์ธ๋ฑ์Šค.
2641
  ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ์„ ๋งค๋ฒˆ PDF/Excel ์ „์ฒด ์ฝ๊ธฐํ•˜์ง€ ์•Š๊ณ ,
 
2900
  return "\n".join(messages) if messages else "์„ ํƒ๋œ ๊ตญ๊ฐ€ ์—†์Œ"
2901
 
2902
 
2903
+
2904
+
2905
+ def extract_limit_condition_from_context(context):
2906
+ text = safe_str(context)
2907
+ compact = re.sub(r"\s+", " ", text).strip()
2908
+ if not compact:
2909
+ return "", ""
2910
+
2911
+ patterns = [
2912
+ r"([0-9]+(?:\.[0-9]+)?\s*%)",
2913
+ r"([0-9]+(?:\.[0-9]+)?\s*ppm)",
2914
+ r"([0-9]+(?:\.[0-9]+)?\s*g\s*/\s*100\s*g)",
2915
+ r"([0-9]+(?:\.[0-9]+)?\s*g\s+per\s+100\s*g)",
2916
+ r"([0-9]+(?:\.[0-9]+)?\s*mg\s*/\s*kg)",
2917
+ ]
2918
+ limits = []
2919
+ for pat in patterns:
2920
+ for m in re.finditer(pat, compact, flags=re.IGNORECASE):
2921
+ v = m.group(1).replace(" ", "")
2922
+ if v not in limits:
2923
+ limits.append(v)
2924
+
2925
+ condition_keywords = [
2926
+ "leave-on", "rinse-off", "oral", "mucosa", "washed away",
2927
+ "๋ณด์กด์ œ", "๋ฐฉ๋ถ€์ œ", "์ž์™ธ์„ ", "์ƒ‰์†Œ", "์ ๋ง‰", "๊ตฌ๊ฐ•",
2928
+ "์ œํ•œ์‚ฌํ•ญ", "๊ฒฝ๊ณ ", "warning", "condition", "ํ•œ๋„", "์ œํ•œ",
2929
+ ]
2930
+ condition_lines = []
2931
+ for line in [safe_str(x) for x in text.splitlines() if safe_str(x)]:
2932
+ l = line.lower()
2933
+ if any(k.lower() in l for k in condition_keywords):
2934
+ cleaned = re.sub(r"\s+", " ", line).strip()
2935
+ if cleaned not in condition_lines:
2936
+ condition_lines.append(cleaned)
2937
+
2938
+ return ", ".join(limits[:5]), " / ".join(condition_lines[:5])[:700]
2939
+
2940
+
2941
  def make_restricted_or_positive_result(country, component, match, reg_type):
2942
  context = safe_str(match.get("context", "")) if isinstance(match, dict) else safe_str(match)
2943
  label = safe_str(reg_type.get("label", "๊ทœ์ œ ๋ชฉ๋ก"))
 
3150
  return records, "BUILT_SOURCE_RECORD_INDEX"
3151
 
3152
 
3153
+ def pick_best_source_record(records):
3154
+ priority = {
3155
+ "prohibited": 100,
3156
+ "restricted": 80,
3157
+ "positive_uv": 50,
3158
+ "positive_preservative": 50,
3159
+ "positive_colorant": 50,
3160
+ "unknown": 10,
3161
+ }
3162
+
3163
+ return sorted(
3164
+ records,
3165
+ key=lambda r: priority.get(r.get("regulation_type", {}).get("type", "unknown"), 0),
3166
+ reverse=True,
3167
+ )[0] if records else None
3168
+
3169
+
3170
+ def evaluate_component_from_uploaded_source_records(country, material, component, target_id=""):
3171
+ """
3172
+ ๊ตญ๊ฐ€ ํด๋” ํŒŒ์ผ๋งŒ ๋ณด๊ณ  ํŒ์ •.
3173
+ ํŒŒ์ผ record ์•ˆ์— ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/๊ตญ๋ฌธ/CAS๊ฐ€ ์ง์ ‘ ์žˆ์„ ๋•Œ๋งŒ ํŒ์ •ํ•œ๋‹ค.
3174
+ """
3175
+ terms = source_match_terms_for_component(material, component)
3176
+ term_norms = [normalize_compact(t) for t in terms if len(normalize_compact(t)) >= 3]
3177
+
3178
+ representative = (
3179
+ safe_str(component.get("INCI Name", ""))
3180
+ or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", ""))
3181
+ or safe_str(component.get("CAS.No", ""))
3182
+ or "-"
3183
+ )
3184
+
3185
+ if not term_norms:
3186
+ return None
3187
+
3188
+ records, status = build_country_source_record_index(country)
3189
+ hits = []
3190
+
3191
+ for rec in records:
3192
+ rec_norm = rec.get("text_norm", "")
3193
+
3194
+ if any(tn in rec_norm for tn in term_norms):
3195
+ hits.append(rec)
3196
+
3197
+ if not hits:
3198
+ return None
3199
+
3200
+ best = pick_best_source_record(hits)
3201
+ reg_type = best.get("regulation_type", {})
3202
+ reg_kind = reg_type.get("type", "unknown")
3203
+ reg_label = reg_type.get("label", "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜")
3204
+ context = best.get("text", "")
3205
+ country_col = display_country(country)
3206
+
3207
+ if reg_kind == "prohibited":
3208
+ result = {
3209
+ "๋Œ€ํ‘œ INCI": representative,
3210
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ",
3211
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "๋ฐฐํ•ฉ๊ธˆ์ง€",
3212
+ "๋น„๊ณ ": f"[์ถœ์ฒ˜ํŒŒ์ผ: {best.get('source_file', '')}] [row: {best.get('row_index', '')}] {context[:900]}",
3213
+ }
3214
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "UPLOADED_SOURCE_RECORD_PROHIBITED", result["๋น„๊ณ "])
3215
+ return result
3216
+
3217
+ if reg_kind == "restricted":
3218
+ limit_text, condition_text = extract_limit_condition_from_context(context)
3219
+ result = {
3220
+ "๋Œ€ํ‘œ INCI": representative,
3221
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ" + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else ""),
3222
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}" if limit_text else NEED_CHECK_TEXT,
3223
+ "๋น„๊ณ ": f"[์ถœ์ฒ˜ํŒŒ์ผ: {best.get('source_file', '')}] [row: {best.get('row_index', '')}] {context[:900]}",
3224
+ }
3225
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "UPLOADED_SOURCE_RECORD_RESTRICTED", result["๋น„๊ณ "])
3226
+ return result
3227
+
3228
+ if reg_kind.startswith("positive_"):
3229
+ limit_text, condition_text = extract_limit_condition_from_context(context)
3230
+ result = {
3231
+ "๋Œ€ํ‘œ INCI": representative,
3232
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ" + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else ""),
3233
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}" if limit_text else "ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ / ํ•œ๋„ ํ™•์ธ ํ•„์š”",
3234
+ "๋น„๊ณ ": f"[์ถœ์ฒ˜ํŒŒ์ผ: {best.get('source_file', '')}] [row: {best.get('row_index', '')}] {context[:900]}",
3235
+ }
3236
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "UPLOADED_SOURCE_RECORD_POSITIVE", result["๋น„๊ณ "])
3237
+ return result
3238
+
3239
+ # ํŒŒ์ผ์—์„œ๋Š” ์„ฑ๋ถ„์„ ์ฐพ์•˜์ง€๋งŒ ํŒŒ์ผ ์œ ํ˜•์ด ๋ถˆ๋ช…ํ™•ํ•œ ๊ฒฝ์šฐ:
3240
+ # PASS๋กœ ๋‹จ์ •ํ•˜์ง€ ์•Š๊ณ  ํ™•์ธ ํ•„์š”๋กœ ํ‘œ์‹œ. ๋‹จ, ์ด ๊ฒฝ์šฐ๋„ "ํŒŒ์ผ ์ง์ ‘ ํ™•์ธ"์€ ๋‚จ๊ธด๋‹ค.
3241
+ result = {
3242
+ "๋Œ€ํ‘œ INCI": representative,
3243
+ "๊ฒฐ๊ณผ": f"{country_col} ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ / ํŒŒ์ผ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜",
3244
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": NEED_CHECK_TEXT,
3245
+ "๋น„๊ณ ": f"[์ถœ์ฒ˜ํŒŒ์ผ: {best.get('source_file', '')}] [row: {best.get('row_index', '')}] {context[:900]}",
3246
+ }
3247
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "UPLOADED_SOURCE_RECORD_UNKNOWN", result["๋น„๊ณ "])
3248
+ return result
3249
+
3250
+
3251
+ def evaluate_component_from_country_files_only(country, material, component, target_id=""):
3252
+ """
3253
+ ๊ตญ๊ฐ€๋ณ„ ์—…๋กœ๋“œ ํŒŒ์ผ ๊ธฐ๋ฐ˜ ํŒ์ •.
3254
+ ์„ฑ๋ถ„๋ณ„ ํ•˜๋“œ์ฝ”๋”ฉ registry๋ณด๋‹ค ์—…๋กœ๋“œ๋œ ๊ตญ๊ฐ€ ํŒŒ์ผ row/line record๋ฅผ ์šฐ์„  ์‚ฌ์šฉํ•œ๋‹ค.
3255
+
3256
+ ์›์น™:
3257
+ - ์—…๋กœ๋“œ ํŒŒ์ผ record์— ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/๊ตญ๋ฌธ/CAS๊ฐ€ ์ง์ ‘ ์žˆ์œผ๋ฉด ๊ทธ ํŒŒ์ผ ์œ ํ˜•์— ๋”ฐ๋ผ ํŒ์ •
3258
+ - ๊ธˆ์ง€/์ œํ•œ/Positive List ํŒŒ์ผ๋ช… ๋˜๋Š” row ๋ฌธ๋งฅ์œผ๋กœ ์œ ํ˜• ํŒ๋‹จ
3259
+ - ํŒŒ์ผ์—์„œ ์ง์ ‘ ๊ทผ๊ฑฐ๊ฐ€ ์—†์œผ๋ฉด Pass
3260
+ - broad alias/glycol/extract ๊ฐ™์€ ๋„“์€ ๊ฒ€์ƒ‰์–ด๋กœ ๊ธˆ์ง€/์ œํ•œ ๋งŒ๋“ค์ง€ ์•Š์Œ
3261
+ """
3262
+ country_col = display_country(country)
3263
+
3264
+ inci_name = safe_str(component.get("INCI Name", ""))
3265
+ korean_name = safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", ""))
3266
+ cas_no = safe_str(component.get("CAS.No", ""))
3267
+ representative = inci_name or korean_name or cas_no or "-"
3268
+
3269
+ critical_result = evaluate_critical_country_rule(
3270
+ country=country,
3271
+ material=material,
3272
+ component=component,
3273
+ )
3274
+
3275
+ if critical_result is not None:
3276
+ log_search_context(
3277
+ target_id,
3278
+ country,
3279
+ component,
3280
+ critical_result.get("๊ฒฐ๊ณผ", ""),
3281
+ "CRITICAL_COUNTRY_RULE",
3282
+ critical_result.get("๋น„๊ณ ", "-"),
3283
+ )
3284
+ return critical_result
3285
+
3286
+ if is_always_pass_component(material, component):
3287
+ result = {
3288
+ "๋Œ€ํ‘œ INCI": representative if representative != "-" else "Water",
3289
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
3290
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
3291
+ "๋น„๊ณ ": "์ •์ œ์ˆ˜/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ˜๋ฆฌ",
3292
+ }
3293
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "ALWAYS_PASS_RULE", result["๋น„๊ณ "])
3294
+ return result
3295
+
3296
+ source_result = evaluate_component_from_uploaded_source_records(
3297
+ country=country,
3298
+ material=material,
3299
+ component=component,
3300
+ target_id=target_id,
3301
+ )
3302
+
3303
+ if source_result is not None:
3304
+ return source_result
3305
+
3306
+ result = {
3307
+ "๋Œ€ํ‘œ INCI": representative,
3308
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
3309
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
3310
+ "๋น„๊ณ ": "์—…๋กœ๋“œ๋œ ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ๋‚ด ํ˜„์žฌ ์„ฑ๋ถ„ ์ง์ ‘ ๊ทผ๊ฑฐ ์—†์Œ",
3311
+ }
3312
+ log_search_context(
3313
+ target_id,
3314
+ country,
3315
+ component,
3316
+ result["๊ฒฐ๊ณผ"],
3317
+ "UPLOADED_SOURCE_RECORD_NO_HIT",
3318
+ result["๋น„๊ณ "],
3319
+ )
3320
+ return result
3321
+
3322
+
3323
+
3324
  def shorten_for_table(value, max_len=80):
3325
  value = safe_str(value)
3326
 
 
3621
  DEFAULT_ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "qwer0515!")
3622
 
3623
 
3624
+ def process_with_auth(user, countries, file, product_name="", use_mfds_live=False, live_max_terms=5, live_headless=True):
3625
+ if not is_approved_user(user):
3626
+ return None, None, require_login_message()
3627
+
3628
+ return process(countries, file, product_name, use_mfds_live, live_max_terms, live_headless)
3629
+
3630
+
3631
+ def process_single_ingredient_with_auth(user, countries, single_query, single_amount, single_function, single_use_mfds_live=False):
3632
+ output_columns = ["๊ตญ๊ฐ€", "๊ฒ€์ƒ‰์–ด", "๋ฐฐํ•ฉํ•œ๋„/๊ธˆ์ง€", "์ƒ์„ธ ํŒ์ •", "ํŒ์ • ์ถœ์ฒ˜", "๋น„๊ณ "]
3633
+
3634
+ if not is_approved_user(user):
3635
+ return pd.DataFrame(columns=output_columns), require_login_message()
3636
+
3637
+ return process_single_ingredient(
3638
+ countries,
3639
+ single_query,
3640
+ single_amount,
3641
+ single_function,
3642
+ single_use_mfds_live,
3643
+ )
3644
+
3645
+
3646
+ def auth_fn(username, password):
3647
+ username = str(username).strip().lower()
3648
+ password = str(password).strip()
3649
+
3650
+ return USER_DB.get(username) == password
3651
+
3652
+
3653
+
3654
  # ============================================================
3655
  # Gradio ๊ธฐ๋ณธ auth ์‚ฌ์šฉ ์‹œ ๋‚ด๋ถ€ ๋กœ๊ทธ์ธ ์ฒดํฌ ์šฐํšŒ
3656
  # ============================================================
3657
 
3658
+ def process_with_auth(user, countries, file, product_name="", use_mfds_live=False, live_max_terms=5, live_headless=True):
3659
+ return process(countries, file, product_name, use_mfds_live, live_max_terms, live_headless)
3660
+
3661
+
3662
+ def process_single_ingredient_with_auth(user, countries, single_query, single_amount, single_function, single_use_mfds_live=False):
3663
+ return process_single_ingredient(
3664
+ countries,
3665
+ single_query,
3666
+ single_amount,
3667
+ single_function,
3668
+ single_use_mfds_live,
3669
+ )
3670
+
3671
+
3672
 
3673
  # ============================================================
3674
  # ๋กœ๊ทธ์ธ ์ธ์ฆ
 
3676
  # - USER_DB์— ์—ฌ๋Ÿฌ ์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3677
  # ============================================================
3678
 
3679
+ USER_DB = {
3680
+ "jin2li@rebom.kr": "qwer0515!",
3681
+
3682
+ # ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ์˜ˆ์‹œ:
3683
+ # "yubin.p@rebom.kr": "์›ํ•˜๋Š”๋น„๋ฐ€๋ฒˆํ˜ธ",
3684
+ # "jkh@rebom.kr": "rebom123!",
3685
+ }
3686
+
3687
+
3688
+ def auth_fn(username, password):
3689
+ username = str(username).strip().lower()
3690
+ password = str(password).strip()
3691
+ return USER_DB.get(username) == password
3692
+
3693
+
3694
+ def process_with_auth(user, countries, file, product_name="", use_mfds_live=False, live_max_terms=5, live_headless=True):
3695
+ return process(countries, file, product_name, use_mfds_live, live_max_terms, live_headless)
3696
+
3697
+
3698
+ def process_single_ingredient_with_auth(user, countries, single_query, single_amount, single_function, single_use_mfds_live=False):
3699
+ return process_single_ingredient(
3700
+ countries,
3701
+ single_query,
3702
+ single_amount,
3703
+ single_function,
3704
+ single_use_mfds_live,
3705
+ )
3706
+
3707
+
3708
 
3709
  # ============================================================
3710
  # ๋กœ๊ทธ์ธ ์ธ์ฆ
 
3716
  # "๋ฉ”์ผ์ฃผ์†Œ": "๋น„๋ฐ€๋ฒˆํ˜ธ",
3717
  # ============================================================
3718
 
3719
+ USER_DB = {
3720
+ "jin2li@rebom.kr": "qwer0515!",
3721
 
3722
+ # ์‚ฌ์šฉ์ž ๋“ฑ๋ก ์˜ˆ์‹œ:
3723
+ # "yubin.p@rebom.kr": "123456!",
3724
+ # "jkh@rebom.kr": "rebom123!",
3725
+ }
3726
 
3727
 
3728
+ def auth_fn(username, password):
3729
+ username = str(username).strip().lower()
3730
+ password = str(password).strip()
3731
+ return USER_DB.get(username) == password
 
3732
 
3733
 
3734
+ # Gradio ๊ธฐ๋ณธ auth๋ฅผ ํ†ต๊ณผํ•œ ์‚ฌ์šฉ์ž๋Š” ์ด๋ฏธ ์ธ์ฆ๋œ ์ƒํƒœ์ด๋ฏ€๋กœ ๋‚ด๋ถ€ ๋กœ๊ทธ์ธ ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
3735
+ def process_with_auth(user, countries, file, product_name="", use_mfds_live=False, live_max_terms=5, live_headless=True):
3736
+ return process(countries, file, product_name, use_mfds_live, live_max_terms, live_headless)
 
 
3737
 
3738
 
3739
+ def process_single_ingredient_with_auth(user, countries, single_query, single_amount, single_function, single_use_mfds_live=False):
3740
+ return process_single_ingredient(
3741
+ countries,
3742
+ single_query,
3743
+ single_amount,
3744
+ single_function,
3745
+ single_use_mfds_live,
3746
+ )
3747
 
3748
 
3749
 
 
3752
  # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3753
  # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3754
  # ============================================================
 
 
 
 
3755
 
3756
  USER_DB = {
3757
  "jin2li@rebom.kr": "qwer0515!",
 
 
3758
 
3759
  # ์‚ฌ์šฉ์ž ๋“ฑ๋ก ์˜ˆ์‹œ:
3760
  # "yubin.p@rebom.kr": "123456!",
3761
+ # "jkh@rebom.kr": "rebom123!",
3762
  }
3763
 
3764
 
 
3781
  single_use_mfds_live,
3782
  )
3783
 
 
 
 
 
3784
 
3785
+
3786
+ # ============================================================
3787
+ # ๋กœ๊ทธ์ธ ์ธ์ฆ
3788
+ # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3789
+ # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3790
+ # ============================================================
3791
+
3792
+ USER_DB = {
3793
+ "jin2li@rebom.kr": "qwer0515!",
3794
+
3795
+ # ์‚ฌ์šฉ์ž ๋“ฑ๋ก ์˜ˆ์‹œ:
3796
+ # "yubin.p@rebom.kr": "123456!",
3797
+ # "jkh@rebom.kr": "rebom123!",
3798
+ }
3799
+
3800
+
3801
+ def auth_fn(username, password):
3802
+ username = str(username).strip().lower()
3803
+ password = str(password).strip()
3804
+ return USER_DB.get(username) == password
3805
+
3806
+
3807
+ def process_with_auth(user, countries, file, product_name="", use_mfds_live=False, live_max_terms=5, live_headless=True):
3808
+ return process(countries, file, product_name, use_mfds_live, live_max_terms, live_headless)
3809
+
3810
+
3811
+ def process_single_ingredient_with_auth(user, countries, single_query, single_amount, single_function, single_use_mfds_live=False):
3812
+ return process_single_ingredient(
3813
+ countries,
3814
+ single_query,
3815
+ single_amount,
3816
+ single_function,
3817
+ single_use_mfds_live,
3818
+ )
3819
+
3820
+
3821
+
3822
+ # ============================================================
3823
+ # ๋กœ๊ทธ์ธ ์ธ์ฆ
3824
+ # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3825
+ # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3826
+
3827
+
3828
+
3829
+ # ============================================================
3830
+ # ๋กœ๊ทธ์ธ ์ธ์ฆ
3831
+ # - Gradio ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ
3832
+ # - ์•„๋ž˜ USER_DB์— ๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ง์ ‘ ๋“ฑ๋ก
3833
+ # ============================================================
3834
+ # ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ๋ฐฉ๋ฒ•:
3835
+ # USER_DB ์•ˆ์— "์•„์ด๋””": "๋น„๋ฐ€๋ฒˆํ˜ธ", ํ˜•์‹์œผ๋กœ ํ•œ ์ค„์”ฉ ์ถ”๊ฐ€ํ•˜์„ธ์š”.
3836
+ # ๊ฐ ์ค„ ๋์—๋Š” ์ฝค๋งˆ(,)๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
3837
+ # ============================================================
3838
+
3839
+ USER_DB = {
3840
+ "jin2li@rebom.kr": "qwer0515!",
3841
+ "jkh@rebom.kr": "rebom123!",
3842
+ "admin": "rebom123!",
3843
+
3844
+ # ์‚ฌ์šฉ์ž ๋“ฑ๋ก ์˜ˆ์‹œ:
3845
+ # "yubin.p@rebom.kr": "123456!",
3846
+ }
3847
+
3848
+
3849
+ def auth_fn(username, password):
3850
+ username = str(username).strip().lower()
3851
+ password = str(password).strip()
3852
+ return USER_DB.get(username) == password
3853
+
3854
+
3855
+ def process_with_auth(user, countries, file, product_name="", use_mfds_live=False, live_max_terms=5, live_headless=True):
3856
+ return process(countries, file, product_name, use_mfds_live, live_max_terms, live_headless)
3857
+
3858
+
3859
+ def process_single_ingredient_with_auth(user, countries, single_query, single_amount, single_function, single_use_mfds_live=False):
3860
+ return process_single_ingredient(
3861
+ countries,
3862
+ single_query,
3863
+ single_amount,
3864
+ single_function,
3865
+ single_use_mfds_live,
3866
+ )
3867
+
3868
+ # ============================================================
3869
+ # STRUCTURED FOLDER SCREENING PATCH V5.1
3870
+ # ๊ธฐ์กด UI/๋กœ๊ทธ์ธ/์—‘์…€ ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” ์œ ์ง€ํ•˜๊ณ , ํŒ์ • ์—”์ง„๋งŒ ํด๋” ๊ตฌ์กฐ ๊ธฐ์ค€์œผ๋กœ ์žฌ์ •์˜
3871
+ # ============================================================
3872
+
3873
+ SOURCE_ONLY_MODE = True
3874
+ TREAT_POSITIVE_LIST_AS_PASS = True
3875
 
3876
  V51_PROHIBITED_TERMS = [
3877
  "๋ฐฐํ•ฉ๊ธˆ์ง€", "์‚ฌ์šฉ๊ธˆ์ง€", "์‚ฌ์šฉ ๊ธˆ์ง€", "๊ธˆ์ง€์›๋ฃŒ", "๊ธˆ์ง€ ์›๋ฃŒ", "๊ธˆ์ง€์„ฑ๋ถ„", "๊ธˆ์ง€ ์„ฑ๋ถ„",
 
4021
 
4022
  return has_glycerin and has_deg and has_impurity_limit
4023
 
4024
+ def _v51_record_type(country, record, component=None):
4025
+ if component is not None and _v51_is_glycerin_deg_impurity_record(country, record, component):
4026
+ return "nonlegal_impurity"
4027
+
4028
+ category = _v51_folder_category(country, record)
4029
+ text = safe_str(record.get("text", ""))
4030
+ path_text = _v51_path_text(record)
4031
+ combined = path_text + "\n" + text[:1500]
4032
+
4033
+ if category in ["nonlegal_prop65", "nonlegal_retailer", "nonlegal_impurity", "nonlegal_voluntary", "nonlegal_labeling", "reference"]:
4034
+ return category
4035
+ if category in ["legal_prohibited", "legal_restricted", "positive_list"]:
4036
+ return category
4037
+
4038
+ # mixed legal file ๋‚ด๋ถ€ ๋ฌธ๋งฅ ๋ณด์ •
4039
+ if category == "legal_mixed":
4040
+ if _v51_text_has_any(combined, ["appendix 1", "annex ii", "prohibited", "๊ธˆ์ง€", "์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์›๋ฃŒ"]):
4041
+ return "legal_prohibited"
4042
+ if _v51_text_has_any(combined, ["appendix 2", "annex iii", "restricted", "์ œํ•œ", "๋ฐฐํ•ฉํ•œ๋„", "maximum amount"]):
4043
+ return "legal_restricted"
4044
+ if _v51_text_has_any(combined, ["appendix 3", "appendix 4", "annex iv", "annex v", "annex vi", "preservative", "uv absorber", "uv filter", "tar colors", "์ƒ‰์†Œ", "๋ณด์กด์ œ", "์ž์™ธ์„ "]):
4045
+ return "positive_list"
4046
+ return "reference"
4047
+
4048
+ # ํด๋”๋ช…์ด ๋ถˆ๋ช…ํ™•ํ•œ ๊ฒฝ์šฐ ๋ฌธ๋งฅ๋งŒ์œผ๋กœ ๋งค์šฐ ๋ณด์ˆ˜์ ์œผ๋กœ ํŒ๋‹จ
4049
+ if _v51_text_has_any(combined, V51_PROHIBITED_TERMS):
4050
+ return "legal_prohibited"
4051
+ if _v51_text_has_any(combined, V51_RESTRICTED_TERMS):
4052
+ return "legal_restricted"
4053
+ if _v51_text_has_any(combined, ["positive list", "permitted", "ํ—ˆ์šฉ๋ชฉ๋ก", "ํ—ˆ์šฉ ๋ชฉ๋ก", "coloring agents", "preservatives", "uv filters"]):
4054
+ return "positive_list"
4055
+ return "unknown"
4056
 
4057
 
4058
  def _v51_component_match_in_record(record_text, terms):
 
4144
  return "Pass"
4145
 
4146
 
4147
+ def evaluate_critical_country_rule(country, material, component):
4148
+ if SOURCE_ONLY_MODE:
4149
+ return None
4150
+ return None
4151
+
4152
+
4153
+ def evaluate_plain_fragrance_rule(country, material, component):
4154
+ if SOURCE_ONLY_MODE:
4155
+ return None
4156
+ return None
4157
+
4158
+
4159
+ def evaluate_local_rules(country, material, component, ingredient_totals, ingredient_amount_parts):
4160
+ if SOURCE_ONLY_MODE:
4161
+ return None
4162
+ return None
4163
+
4164
+
4165
  def _v51_nonlegal_result(kind, country, component, best, target_id=""):
4166
  country_col = display_country(country)
4167
  representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
 
4236
  return result
4237
 
4238
 
4239
+ def evaluate_component_from_uploaded_source_records(country, material, component, target_id=""):
4240
+ terms = source_match_terms_for_component(material, component)
4241
+ representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
4242
+ if not terms:
4243
+ return None
4244
+
4245
+ records, status = build_country_source_record_index(country)
4246
+ hits = []
4247
+ for rec in records:
4248
+ rec_text = safe_str(rec.get("text", ""))
4249
+ if _v51_component_match_in_record(rec_text, terms):
4250
+ hits.append(rec)
4251
+
4252
+ if not hits:
4253
+ return None
4254
+
4255
+ priority = {
4256
+ "legal_prohibited": 100,
4257
+ "legal_restricted": 90,
4258
+ "positive_list": 70,
4259
+ "nonlegal_retailer": 60,
4260
+ "nonlegal_prop65": 58,
4261
+ "nonlegal_voluntary": 56,
4262
+ "nonlegal_impurity": 54,
4263
+ "nonlegal_labeling": 52,
4264
+ "reference": 20,
4265
+ "unknown": 10,
4266
+ }
4267
+ typed_hits = [(rec, _v51_record_type(country, rec, component)) for rec in hits]
4268
+ typed_hits.sort(key=lambda x: priority.get(x[1], 0), reverse=True)
4269
+ best, kind = typed_hits[0]
4270
+ context = safe_str(best.get("text", ""))
4271
+ src_note = f"[์ถœ์ฒ˜ํŒŒ์ผ: {best.get('source_file', '')}] [row: {best.get('row_index', '')}] {context[:900]}"
4272
+ country_col = display_country(country)
4273
+
4274
+ if kind == "legal_prohibited":
4275
+ result = {
4276
+ "๋Œ€ํ‘œ INCI": representative,
4277
+ "๊ฒฐ๊ณผ": f"{country_col}: ์—…๋กœ๋“œ ๋ฒ•์ • ๊ธˆ์ง€ ํด๋”/ํŒŒ์ผ์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ",
4278
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "๋ฐฐํ•ฉ๊ธˆ์ง€",
4279
+ "๋น„๊ณ ": src_note,
4280
+ }
4281
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V51_LEGAL_PROHIBITED", result["๋น„๊ณ "])
4282
+ return result
4283
+
4284
+ if kind == "legal_restricted":
4285
+ limit_text, condition_text = extract_limit_condition_from_context(context)
4286
+ result = {
4287
+ "๋Œ€ํ‘œ INCI": representative,
4288
+ "๊ฒฐ๊ณผ": f"{country_col}: ์—…๋กœ๋“œ ๋ฒ•์ • ์ œํ•œ ํด๋”/ํŒŒ์ผ์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ" + (f" / ํ•œ๋„ {limit_text}" if limit_text else " / ํ•œ๋„ยท์กฐ๊ฑด ํ™•์ธ ํ•„์š”") + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else ""),
4289
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}" if limit_text else NEED_CHECK_TEXT,
4290
+ "๋น„๊ณ ": src_note,
4291
+ }
4292
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V51_LEGAL_RESTRICTED", result["๋น„๊ณ "])
4293
+ return result
4294
+
4295
+ if kind == "positive_list":
4296
+ limit_text, condition_text = extract_limit_condition_from_context(context)
4297
+ if limit_text:
4298
+ result = {
4299
+ "๋Œ€ํ‘œ INCI": representative,
4300
+ "๊ฒฐ๊ณผ": f"{country_col}: ํ—ˆ์šฉ๋ชฉ๋ก์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ / ํ•œ๋„ {limit_text}" + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else ""),
4301
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}",
4302
+ "๋น„๊ณ ": src_note,
4303
+ }
4304
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V51_POSITIVE_LIST_LIMIT", result["๋น„๊ณ "])
4305
+ return result
4306
+ result = {
4307
+ "๋Œ€ํ‘œ INCI": representative,
4308
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
4309
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
4310
+ "๋น„๊ณ ": "ํ—ˆ์šฉ๋ชฉ๋ก์—์„œ ์„ฑ๋ถ„๋ช… ํ™•์ธ. ๋ช…ํ™•ํ•œ ๋ฐฐํ•ฉํ•œ๋„ ์ˆ˜์น˜ ๊ทผ๊ฑฐ ์—†์Œ. " + src_note,
4311
+ }
4312
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V51_POSITIVE_LIST_PASS", result["๋น„๊ณ "])
4313
+ return result
4314
+
4315
+ if kind.startswith("nonlegal_") or kind == "reference":
4316
+ return _v51_nonlegal_result(kind, country, component, best, target_id)
4317
+
4318
+ result = {
4319
+ "๋Œ€ํ‘œ INCI": representative,
4320
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
4321
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
4322
+ "๋น„๊ณ ": "์„ฑ๋ถ„๋ช…์€ ์—…๋กœ๋“œ ํŒŒ์ผ์—์„œ ๋ฐœ๊ฒฌ๋˜์—ˆ์œผ๋‚˜ ๊ธˆ์ง€/์ œํ•œ/ํ•œ๋„ ๊ทผ๊ฑฐ ์—†์Œ. " + src_note,
4323
+ }
4324
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V51_SOURCE_NAME_ONLY_PASS", result["๋น„๊ณ "])
4325
+ return result
4326
+
4327
+
4328
+ def evaluate_component_from_country_files_only(country, material, component, target_id=""):
4329
+ representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
4330
+
4331
+ if is_always_pass_component(material, component):
4332
+ result = {
4333
+ "๋Œ€ํ‘œ INCI": representative if representative != "-" else "Water",
4334
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
4335
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
4336
+ "๋น„๊ณ ": "์ •์ œ์ˆ˜/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ˜๋ฆฌ",
4337
+ }
4338
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "ALWAYS_PASS_RULE", result["๋น„๊ณ "])
4339
+ return result
4340
+
4341
+ source_result = evaluate_component_from_uploaded_source_records(country, material, component, target_id)
4342
+ if source_result is not None:
4343
+ return source_result
4344
+
4345
+ result = {
4346
+ "๋Œ€ํ‘œ INCI": representative,
4347
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
4348
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
4349
+ "๋น„๊ณ ": "์—…๋กœ๋“œ๋œ ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ๋‚ด ํ˜„์žฌ ์„ฑ๋ถ„ ์ง์ ‘ ๊ทผ๊ฑฐ ์—†์Œ",
4350
+ }
4351
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V51_SOURCE_NO_HIT_PASS", result["๋น„๊ณ "])
4352
+ return result
4353
+
4354
+
4355
+ def is_country_result_issue(text):
4356
+ text = safe_str(text)
4357
+ if not text or text in ["-", NO_EVIDENCE_TEXT, "Pass", "PASS", "pass"]:
4358
+ return False
4359
+ if "ํ—ˆ์šฉ๋ชฉ๋ก" in text and not _v51_text_has_any(text, V51_RESTRICTED_TERMS + V51_PROHIBITED_TERMS):
4360
+ return False
4361
+ issue_markers = [
4362
+ "๋ฐฐํ•ฉ๊ธˆ์ง€", "๊ธˆ์ง€", "๋ฐฐํ•ฉํ•œ๋„", "ํ•œ๋„", "ํ™•์ธ ํ•„์š”", "๊ฒฝ๊ณ ํ‘œ์‹œ", "Clean ๊ธฐ์ค€",
4363
+ "๋ถˆ์ˆœ๋ฌผ", "์˜ค์—ผ๋ฌผ์งˆ", "์ž์œจ๊ธฐ์ค€", "ํ‘œ์‹œ๊ธฐ์ค€", "๋ถ€์ ํ•ฉ", "์žฌ๊ฒ€ํ† ", "์‚ฌ์šฉ ๋ถˆ๊ฐ€", "์‚ฌ์šฉ๋ถˆ๊ฐ€",
4364
+ ]
4365
+ return _v51_text_has_any(text, issue_markers + V51_PROHIBITED_TERMS + V51_RESTRICTED_TERMS)
4366
+
4367
  # ============================================================
4368
  # END STRUCTURED FOLDER SCREENING PATCH V5.1
4369
  # ============================================================
4370
 
4371
 
4372
+
4373
+
4374
  # ============================================================
4375
  # STRUCTURED FOLDER SCREENING PATCH V5.2
4376
  # 2026-06: US ํด๋”๋ช… ๋ฐ˜์˜ + ์ฃผ์š” ์˜คํŒ์ • ์ฐจ๋‹จ
 
4380
  # - Phenoxyethanol: EU/ASEAN/KOREA/JAPAN ๋ณด์กด์ œ ์ตœ๋Œ€ 1.0% ํ•œ๋„๋กœ ๊ณ ์ • ๋ณด์ •
4381
  # ============================================================
4382
 
4383
+ COUNTRY_ORDER = ["ASEAN", "CALIFORNIA", "CHINA", "EU (FUTURE)", "EU", "US", "JAPAN", "KOREA", "MEXICO", "SEPHORA", "TARGET", "UK", "ULTA"]
4384
  COUNTRY_DISPLAY.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "US"})
4385
  COUNTRY_FOLDER_ALIASES.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "us"})
4386
+ BLOCKED_PATHS = [str(BASE_DIR / c) for c in COUNTRY_ORDER] + [str(BASE_DIR / "us")] + [str(BASE_DIR/FAISS_CACHE_DIR), str(BASE_DIR/'.env'), str(BASE_DIR/'__pycache__')]
4387
+
4388
+
4389
+ def country_key(country):
4390
+ t = safe_str(country).upper().replace("_", " ").replace("-", " ")
4391
+ t = re.sub(r"\s+", " ", t).strip()
4392
+ m = {
4393
+ "ํ•œ๊ตญ": "KOREA", "KOREA": "KOREA", "KR": "KOREA",
4394
+ "EU": "EU", "EUROPE": "EU", "์œ ๋Ÿฝ": "EU",
4395
+ "EU FUTURE": "EU_FUTURE", "EU (FUTURE)": "EU_FUTURE",
4396
+ "JAPAN": "JAPAN", "JP": "JAPAN", "์ผ๋ณธ": "JAPAN",
4397
+ "CHINA": "CHINA", "CN": "CHINA", "์ค‘๊ตญ": "CHINA",
4398
+ "USA": "US", "US": "US", "๋ฏธ๊ตญ": "US", "FDA": "US",
4399
+ "ASEAN": "ASEAN", "์•„์„ธ์•ˆ": "ASEAN",
4400
+ "UK": "UK", "์˜๊ตญ": "UK",
4401
+ "SEPHORA": "SEPHORA", "TARGET": "TARGET", "ULTA": "ULTA",
4402
+ "CALIFORNIA": "CALIFORNIA", "MEXICO": "MEXICO",
4403
+ }
4404
+ return m.get(t, t)
4405
+
4406
+
4407
+ def display_country(country):
4408
+ c = country_key(country)
4409
+ if c == "US":
4410
+ return "US"
4411
+ return COUNTRY_DISPLAY.get(str(country).upper(), COUNTRY_DISPLAY.get(str(country), c))
4412
 
4413
 
4414
  def _v52_component_blob(material, component):
 
4538
  return "unknown"
4539
 
4540
 
4541
+ def evaluate_component_from_country_files_only(country, material, component, target_id=""):
4542
+ representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
4543
+
4544
+ if is_always_pass_component(material, component):
4545
+ result = {
4546
+ "๋Œ€ํ‘œ INCI": representative if representative != "-" else "Water",
4547
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
4548
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
4549
+ "๋น„๊ณ ": "์ •์ œ์ˆ˜/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ˜๏ฟฝ๏ฟฝ๏ฟฝ",
4550
+ }
4551
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "ALWAYS_PASS_RULE", result["๋น„๊ณ "])
4552
+ return result
4553
+
4554
+ correction = _v52_known_correction(country, material, component, target_id)
4555
+ if correction is not None:
4556
+ return correction
4557
+
4558
+ source_result = evaluate_component_from_uploaded_source_records(country, material, component, target_id)
4559
+ if source_result is not None:
4560
+ return source_result
4561
+
4562
+ result = {
4563
+ "๋Œ€ํ‘œ INCI": representative,
4564
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
4565
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
4566
+ "๋น„๊ณ ": "์—…๋กœ๋“œ๋œ ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ๋‚ด ํ˜„์žฌ ์„ฑ๋ถ„ ์ง์ ‘ ๊ทผ๊ฑฐ ์—†์Œ",
4567
+ }
4568
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "V52_SOURCE_NO_HIT_PASS", result["๋น„๊ณ "])
4569
+ return result
4570
 
4571
  # ============================================================
4572
  # END STRUCTURED FOLDER SCREENING PATCH V5.2
 
4583
  # - ํŒŒ์ผ๋ช…/๋ฌธ์„œ ์ „์ฒด์˜ prohibited/limit ํ‚ค์›Œ๋“œ๋งŒ์œผ๋กœ ์„ฑ๋ถ„ ํŒ์ • ๊ธˆ์ง€
4584
  # ============================================================
4585
 
4586
+ SOURCE_ONLY_MODE = True
4587
+ TREAT_POSITIVE_LIST_AS_PASS = True
4588
+ COUNTRY_ORDER = ["ASEAN", "CALIFORNIA", "CHINA", "EU (FUTURE)", "EU", "US", "us", "JAPAN", "KOREA", "MEXICO", "SEPHORA", "TARGET", "UK", "ULTA"]
4589
  COUNTRY_DISPLAY.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "US"})
4590
  COUNTRY_FOLDER_ALIASES.update({"FDA": "US", "๋ฏธ๊ตญ": "US", "USA": "US", "US": "US", "us": "us", "Us": "US"})
4591
+ BLOCKED_PATHS = [str(BASE_DIR / c) for c in COUNTRY_ORDER] + [str(BASE_DIR/FAISS_CACHE_DIR), str(BASE_DIR/'.env'), str(BASE_DIR/'__pycache__')]
4592
 
4593
 
4594
  def country_key(country):
 
4609
  return m.get(t, t)
4610
 
4611
 
4612
+ def display_country(country):
4613
+ c = country_key(country)
4614
+ if c == "US":
4615
+ return "US"
4616
+ if c == "KOREA":
4617
+ return "Korea"
4618
+ if c == "JAPAN":
4619
+ return "์ผ๋ณธ"
4620
+ if c == "CHINA":
4621
+ return "์ค‘๊ตญ"
4622
+ return COUNTRY_DISPLAY.get(str(country).upper(), COUNTRY_DISPLAY.get(str(country), c))
4623
+
4624
+
4625
  def _v6_path_text(record):
4626
  return f"{safe_str(record.get('source_file',''))}\n{safe_str(record.get('sheet',''))}".replace('\\', '/')
4627
 
 
4868
  return _v6_result(representative, NO_EVIDENCE_TEXT, "Pass", src, target_id, country, component, "V6_REFERENCE_ONLY_NO_LEGAL_CLASSIFICATION")
4869
 
4870
 
4871
+ def pick_best_source_record(records):
4872
+ priority = {
4873
+ "legal_prohibited": 100,
4874
+ "legal_restricted": 80,
4875
+ "positive_list": 60,
4876
+ "nonlegal_prop65": 45,
4877
+ "nonlegal_retailer": 40,
4878
+ "nonlegal_impurity": 35,
4879
+ "nonlegal_voluntary": 30,
4880
+ "nonlegal_labeling": 30,
4881
+ "reference_microplastics": 20,
4882
+ "reference": 10,
4883
+ "unknown": 0,
4884
+ }
4885
+ return sorted(records, key=lambda r: priority.get(r.get("v6_type", "unknown"), 0), reverse=True)[0] if records else None
4886
+
4887
+
4888
+ def evaluate_component_from_uploaded_source_records(country, material, component, target_id=""):
4889
+ """
4890
+ V6 source-table-row engine.
4891
+ ์„ฑ๋ถ„๋ช…/CAS๊ฐ€ ๊ฐ™์€ row/window์— ์ง์ ‘ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ hit๋กœ ์ธ์ •ํ•˜๊ณ ,
4892
+ ํด๋”/Annex/Appendix ๊ตฌ์กฐ๊ฐ€ ๋ฒ•์ • ์œ ํ˜•์„ ๊ฒฐ์ •ํ•œ๋‹ค.
4893
+ """
4894
+ terms = _v6_component_terms(material, component)
4895
+ representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
4896
+ if not terms:
4897
+ return None
4898
+
4899
+ records, status = build_country_source_record_index(country)
4900
+ hits = []
4901
+ for rec in records:
4902
+ if not _v6_row_direct_match(rec.get("text", ""), terms):
4903
+ continue
4904
+ rtype = _v6_record_type(country, rec, material, component)
4905
+ rec2 = dict(rec)
4906
+ rec2["v6_type"] = rtype
4907
+ hits.append(rec2)
4908
+
4909
+ if not hits:
4910
+ return None
4911
+
4912
+ best = pick_best_source_record(hits)
4913
+ rtype = best.get("v6_type", "unknown")
4914
+ context = safe_str(best.get("text", ""))
4915
+ src_note = f"[์ถœ์ฒ˜ํŒŒ์ผ: {best.get('source_file','')}] [row: {best.get('row_index','')}] {context[:900]}"
4916
+ cdisp = display_country(country)
4917
+
4918
+ if rtype == "legal_prohibited":
4919
+ return _v6_result(
4920
+ representative,
4921
+ f"{cdisp}: ๋ฒ•์ • ๊ธˆ์ง€๋ชฉ๋ก์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ",
4922
+ "๋ฐฐํ•ฉ๊ธˆ์ง€",
4923
+ src_note,
4924
+ target_id, country, component, "V6_ROW_MATCH_PROHIBITED_FOLDER",
4925
+ )
4926
+
4927
+ if rtype == "legal_restricted":
4928
+ limit_text, condition_text = _v6_extract_limit_condition_from_row(context)
4929
+ formula = f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}" if limit_text else NEED_CHECK_TEXT
4930
+ result_text = f"{cdisp}: ๋ฒ•์ • ์ œํ•œ๋ชฉ๋ก์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ" + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else "")
4931
+ return _v6_result(
4932
+ representative,
4933
+ result_text,
4934
+ formula,
4935
+ src_note,
4936
+ target_id, country, component, "V6_ROW_MATCH_RESTRICTED_FOLDER",
4937
+ )
4938
+
4939
+ if rtype == "positive_list":
4940
+ limit_text, condition_text = _v6_extract_limit_condition_from_row(context)
4941
+ formula = f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}" if limit_text else "ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ / ํ•œ๋„ ํ™•์ธ ํ•„์š”"
4942
+ result_text = f"{cdisp}: ํ—ˆ์šฉ๋ชฉ๋ก์—์„œ ์„ฑ๋ถ„ ์ง์ ‘ ํ™•์ธ" + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else "")
4943
+ source_tag = "V6_ROW_MATCH_POSITIVE_LIST_LIMIT" if limit_text else "V6_ROW_MATCH_POSITIVE_LIST"
4944
+ return _v6_result(
4945
+ representative,
4946
+ result_text,
4947
+ formula,
4948
+ src_note,
4949
+ target_id, country, component, source_tag,
4950
+ )
4951
+
4952
+ if rtype in ["nonlegal_prop65", "nonlegal_retailer", "nonlegal_impurity", "nonlegal_voluntary", "nonlegal_labeling", "reference", "reference_microplastics"]:
4953
+ return _v6_nonlegal_result(country, material, component, best, rtype, target_id)
4954
+
4955
+ # unknown ํด๋”/ํŒŒ์ผ์€ ์„ฑ๋ถ„๋ช…์ด ์žˆ์–ด๋„ ๋ฒ•์ • ๊ธˆ์ง€ยทํ•œ๋„ ํŒ์ •์„ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค.
4956
+ return _v6_result(
4957
+ representative,
4958
+ NO_EVIDENCE_TEXT,
4959
+ "Pass",
4960
+ src_note + " / ํŒŒ์ผ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜: ๋ฒ•์ • ๊ธˆ์ง€ยทํ•œ๋„ ์ž๋™ํŒ์ • ์ œ์™ธ",
4961
+ target_id, country, component, "V6_UNKNOWN_SOURCE_NO_LEGAL_CLASSIFICATION",
4962
+ )
4963
+
4964
+
4965
+ def evaluate_component_from_country_files_only(country, material, component, target_id=""):
4966
+ representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or safe_str(component.get("CAS.No", "")) or "-"
4967
+
4968
+ if is_always_pass_component(material, component):
4969
+ return _v6_result(
4970
+ representative if representative != "-" else "Water",
4971
+ NO_EVIDENCE_TEXT,
4972
+ "Pass",
4973
+ "์ •์ œ์ˆ˜/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ˜๋ฆฌ",
4974
+ target_id, country, component, "ALWAYS_PASS_RULE",
4975
+ )
4976
+
4977
+ source_result = evaluate_component_from_uploaded_source_records(country, material, component, target_id)
4978
+ if source_result is not None:
4979
+ return source_result
4980
+
4981
+ return _v6_result(
4982
+ representative,
4983
+ NO_EVIDENCE_TEXT,
4984
+ "Pass",
4985
+ "์—…๋กœ๋“œ๋œ ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ๋‚ด ํ˜„์žฌ ์„ฑ๋ถ„์˜ ๊ฐ™์€ ํ–‰/๊ทผ์ ‘ ํ–‰ ์ง์ ‘ ๊ทผ๊ฑฐ ์—†์Œ",
4986
+ target_id, country, component, "V6_NO_ROW_MATCH_PASS",
4987
+ )
4988
+
4989
+
4990
  def evaluate_critical_country_rule(country, material, component):
4991
  if SOURCE_ONLY_MODE:
4992
  return None
 
5005
  return None
5006
 
5007
 
5008
+ def is_country_result_issue(text):
5009
+ text = safe_str(text)
5010
+ if not text or text in ["-", NO_EVIDENCE_TEXT, "Pass", "PASS", "pass"]:
5011
+ return False
5012
+ if "ํ—ˆ์šฉ๋ชฉ๋ก" in text and not _v6_has(text, ["๋ฐฐํ•ฉํ•œ๋„", "ํ•œ๋„", "์กฐ๊ฑด", "ํ™•์ธ ํ•„์š”"]):
5013
+ return False
5014
+ return _v6_has(text, [
5015
+ "๋ฐฐํ•ฉ๊ธˆ์ง€", "๋ฐฐํ•ฉํ•œ๋„", "ํ•œ๋„", "ํ™•์ธ ํ•„์š”", "๊ฒฝ๊ณ ํ‘œ์‹œ", "Clean ๊ธฐ์ค€", "๋ถˆ์ˆœ๋ฌผ", "์˜ค์—ผ๋ฌผ์งˆ",
5016
+ "์ž์œจ๊ธฐ์ค€", "ํ‘œ์‹œ๊ธฐ์ค€", "๋ถ€์ ํ•ฉ", "์žฌ๊ฒ€ํ† ", "์‚ฌ์šฉ ๋ถˆ๊ฐ€", "์‚ฌ์šฉ๋ถˆ๊ฐ€", "Microplastics",
5017
+ ])
5018
 
5019
  # ============================================================
5020
  # END SOURCE TABLE ROW ENGINE PATCH V6
 
5433
  # ============================================================
5434
 
5435
 
5436
+
5437
+
5438
  # ============================================================
5439
  # USER PATCH 2026-06-15
5440
  # 1) Output country/channel display names forced to English
 
5495
  return COUNTRY_DISPLAY.get(str(country).upper(), COUNTRY_DISPLAY.get(str(country), c))
5496
 
5497
 
5498
+ _ORIGINAL_get_country_folders = get_country_folders
5499
 
5500
 
5501
  def get_country_folders():
 
5503
  return [c for c in _ORIGINAL_get_country_folders() if not _is_hidden_always_folder(c)]
5504
 
5505
 
5506
+ _ORIGINAL_refresh_country_choices_keep_selection = refresh_country_choices_keep_selection
5507
 
5508
 
5509
  def refresh_country_choices_keep_selection(current_values=None):
 
5520
  return refresh_country_choices_keep_selection(current_values)
5521
 
5522
 
5523
+ _ORIGINAL_get_country_section_index_cached = get_country_section_index_cached
5524
 
5525
 
5526
  def get_country_section_index_cached(country):
 
5556
  return list(sections or []) + extra_sections, status
5557
 
5558
 
5559
+ with gr.Blocks(css=custom_css) as demo:
5560
  current_user = gr.State({})
5561
  gr.Markdown("# ๐ŸŒ reBom")
5562
  gr.HTML("""
 
5635
  </script>
5636
 
5637
 
5638
+
5639
+
5640
  """)
5641
  gr.Markdown("### Screening")
5642
 
 
5688
  inputs=[current_user,single_country,single_query,single_amount,single_function],
5689
  outputs=[single_df_out,single_msg_out],
5690
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5691
  try:
5692
+ demo.launch(
5693
+ auth=auth_fn,
5694
+ server_name="0.0.0.0",server_port=7860,share=True,ssr_mode=False,allowed_paths=ALLOWED_PATHS,blocked_paths=BLOCKED_PATHS,show_api=False,footer_links=[],app_kwargs={"docs_url":None,"redoc_url":None,"openapi_url":None})
5695
+ except TypeError:
5696
+ demo.launch(
5697
+ auth=auth_fn,
5698
+ server_name="0.0.0.0",server_port=7860,share=True,allowed_paths=ALLOWED_PATHS,blocked_paths=BLOCKED_PATHS)