jin2li commited on
Commit
19a8ef5
ยท
verified ยท
1 Parent(s): 531499f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +340 -48
app.py CHANGED
@@ -768,11 +768,11 @@ def build_single_component_from_query(query, amount="", function="", single_amou
768
 
769
 
770
 
 
771
  def get_country_regulatory_text_cached(country):
772
  """
773
- ๋‹จ์ผ ์„ฑ๋ถ„/ํŒŒ์ผ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰ ์†๋„ ๊ฐœ์„ ์šฉ.
774
- FAISS/์ž„๋ฒ ๋”ฉ์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์›๋ฌธ์„ ์ง์ ‘ ์ฝ์–ด์„œ ์บ์‹œํ•œ๋‹ค.
775
- ์ฒซ ๊ฒ€์ƒ‰์€ ํŒŒ์ผ์„ ์ฝ๋А๋ผ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐ™์€ ์•ฑ ์„ธ์…˜์˜ ๋‹ค์Œ ๊ฒ€์ƒ‰๋ถ€ํ„ฐ๋Š” ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
776
  """
777
  global REG_TEXT_CACHE
778
 
@@ -816,15 +816,166 @@ def get_country_regulatory_text_cached(country):
816
  return combined, "READ_FILES"
817
 
818
 
819
- def extract_relevant_context_direct(raw_text, ingredient_names, window=2, max_blocks=12):
820
  """
821
- ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์›๋ฌธ์—์„œ ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/INCI/CAS๊ฐ€ ์ง์ ‘ ๋“ค์–ด๊ฐ„ ๋ฌธ๋‹จ๋งŒ ๋น ๋ฅด๊ฒŒ ์ถ”์ถœํ•œ๋‹ค.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
822
  """
823
  raw_text = safe_str(raw_text)
824
 
825
  if not raw_text:
826
- return "", False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
827
 
 
 
 
 
 
828
  names_norm = [
829
  normalize_compact(x)
830
  for x in ingredient_names
@@ -832,41 +983,96 @@ def extract_relevant_context_direct(raw_text, ingredient_names, window=2, max_bl
832
  ]
833
 
834
  if not names_norm:
835
- return "", False
 
 
 
 
 
 
 
 
 
 
 
836
 
837
- # ํŒŒ์ผ/์‹œํŠธ/ํŽ˜์ด์ง€ ๊ตฌ๋ถ„๊ณผ ์ค„ ๋‹จ์œ„ ๊ฒ€์ƒ‰์„ ๊ฐ™์ด ์“ฐ๊ธฐ ์œ„ํ•ด ๋„ˆ๋ฌด ์งง์€ ์ค„์€ ์ฃผ๋ณ€ ๋ฌธ๋งฅ๊ณผ ํ•ฉ์นœ๋‹ค.
838
- rough_blocks = re.split(r"[\n\r]+", raw_text)
839
- rough_blocks = [safe_str(x) for x in rough_blocks if safe_str(x)]
840
 
841
- hit_indices = []
 
842
 
843
- for idx, block in enumerate(rough_blocks):
844
- block_norm = normalize_compact(block)
845
 
846
- if any(name in block_norm for name in names_norm):
847
- hit_indices.append(idx)
 
848
 
849
- if len(hit_indices) >= max_blocks:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
850
  break
851
 
852
- if not hit_indices:
853
- return "", False
854
 
855
- selected = []
856
- seen = set()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857
 
858
- for idx in hit_indices:
859
- start = max(0, idx - window)
860
- end = min(len(rough_blocks), idx + window + 1)
 
 
861
 
862
- for j in range(start, end):
863
- if j in seen:
864
- continue
 
 
 
 
 
 
865
 
866
- seen.add(j)
867
- selected.append(rough_blocks[j])
868
 
869
- context = "\n".join(selected)
 
 
 
 
 
870
 
871
  return context[:8000], True
872
 
@@ -875,14 +1081,13 @@ def extract_relevant_context_direct(raw_text, ingredient_names, window=2, max_bl
875
  def evaluate_component_from_country_files_only(country, material, component, target_id=""):
876
  """
877
  GPT ํŒ์ • ์—†์ด ์‚ฌ์šฉ์ž๊ฐ€ ์—…๋ฐ์ดํŠธํ•œ ๊ตญ๊ฐ€๋ณ„ ๊ทœ์ œ ํŒŒ์ผ๋งŒ ์ง์ ‘ ๊ฒ€์ƒ‰ํ•ด์„œ ํŒ์ •ํ•œ๋‹ค.
878
- ์†๋„ ๊ฐœ์„ :
879
- - ๋‹จ์ผ ์„ฑ๋ถ„์—์„œ FAISS/์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ ์—†์ด ์›๋ฌธ ์ง์ ‘ ๊ฒ€์ƒ‰
880
- - ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์›๋ฌธ์€ ์•ฑ ์„ธ์…˜ ๋™์•ˆ ์บ์‹œ
881
- ์›์น™:
882
- 1. ๊ธˆ์ง€ ์„ฑ๋ถ„์€ ํ•จ๋Ÿ‰ ์œ ๋ฌด์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ๋ฐฐํ•ฉ๊ธˆ์ง€
883
- 2. ๋ฐฐํ•ฉํ•œ๋„๋Š” ์„ฑ๋ถ„๋ช…/INCI/CAS์™€ % ์ˆ˜์น˜๊ฐ€ ๊ฐ™์€ ๊ทผ์ฒ˜์—์„œ ๊ฒ€์ฆ๋  ๋•Œ๋งŒ ํ‘œ์‹œ
884
- 3. ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/INCI/CAS๊ฐ€ ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์›๋ฌธ์— ์ง์ ‘ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ œํ•œ ๊ทผ๊ฑฐ๋กœ ์‚ฌ์šฉ
885
- 4. ๊ทผ๊ฑฐ๊ฐ€ ์—†์œผ๋ฉด Pass / ํ•ด๋‹น ์ž๋ฃŒ ๋‚ด ์ œํ•œ ๊ทผ๊ฑฐ ์—†์Œ
886
  """
887
  country_col = display_country(country)
888
 
@@ -933,7 +1138,7 @@ def evaluate_component_from_country_files_only(country, material, component, tar
933
  "๋Œ€ํ‘œ INCI": representative,
934
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
935
  "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
936
- "๋น„๊ณ ": cache_status or "ํ•ด๋‹น ๊ตญ๊ฐ€/์ฑ„๋„ ๊ทœ์ œ DB ์—†์Œ",
937
  }
938
  log_search_context(
939
  target_id,
@@ -945,14 +1150,13 @@ def evaluate_component_from_country_files_only(country, material, component, tar
945
  )
946
  return result
947
 
948
- context, has_hit = extract_relevant_context_direct(
949
  raw_text=raw_text,
950
  ingredient_names=[inci_name, korean_name, cas_no],
951
- window=2,
952
- max_blocks=16,
953
  )
954
 
955
- if not has_hit:
956
  result = {
957
  "๋Œ€ํ‘œ INCI": representative,
958
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
@@ -969,9 +1173,98 @@ def evaluate_component_from_country_files_only(country, material, component, tar
969
  )
970
  return result
971
 
 
 
 
 
 
972
  evidence = context[:5000]
973
  lower = evidence.lower()
974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
975
  prohibited_keywords_ko = [
976
  "๋ฐฐํ•ฉ๊ธˆ์ง€",
977
  "์‚ฌ์šฉ๊ธˆ์ง€",
@@ -1019,7 +1312,7 @@ def evaluate_component_from_country_files_only(country, material, component, tar
1019
  country,
1020
  component,
1021
  result["๊ฒฐ๊ณผ"],
1022
- "COUNTRY_FILE_DIRECT_PROHIBITED",
1023
  context,
1024
  )
1025
  return result
@@ -1088,7 +1381,7 @@ def evaluate_component_from_country_files_only(country, material, component, tar
1088
  country,
1089
  component,
1090
  result["๊ฒฐ๊ณผ"],
1091
- "COUNTRY_FILE_DIRECT_RESTRICTION",
1092
  context,
1093
  )
1094
  return result
@@ -1097,20 +1390,19 @@ def evaluate_component_from_country_files_only(country, material, component, tar
1097
  "๋Œ€ํ‘œ INCI": representative,
1098
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
1099
  "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
1100
- "๋น„๊ณ ": "๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ๋‚ด ํ˜„์žฌ ์„ฑ๋ถ„๋ช…์€ ํ™•์ธ๋˜์—ˆ์œผ๋‚˜ ์ œํ•œ/๊ธˆ์ง€/ํ•œ๋„ ํ‚ค์›Œ๋“œ ์—†์Œ",
1101
  }
1102
  log_search_context(
1103
  target_id,
1104
  country,
1105
  component,
1106
  result["๊ฒฐ๊ณผ"],
1107
- "COUNTRY_FILE_DIRECT_NO_RESTRICTION",
1108
  context,
1109
  )
1110
  return result
1111
 
1112
 
1113
-
1114
  def make_single_display_result(final_result, country_col="", component=None):
1115
  """
1116
  ๋‹จ์ผ ์„ฑ๋ถ„ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํ‘œ์˜ '๋ฐฐํ•ฉํ•œ๋„/๊ธˆ์ง€' ์นธ์— ๋“ค์–ด๊ฐˆ ์งง์€ ํ‘œ์‹œ๋ฌธ๊ตฌ๋ฅผ ๋งŒ๋“ ๋‹ค.
@@ -1375,7 +1667,7 @@ def process_single_ingredient(
1375
  f"- ์„ ํƒ ๊ตญ๊ฐ€ ์ˆ˜: {len(countries)}๊ฐœ\n"
1376
  f"- MFDS ์ƒํƒœ: {mfds_live_msg}\n"
1377
  f"- MFDS ์กฐํšŒ ๋Œ€์ƒ: {', '.join(terms) if terms else '์—†์Œ'}\n"
1378
- "- ํŒ์ • ๋ฐฉ์‹: GPT ํŒ์ • ์—†์Œ, ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์ง์ ‘๊ฒ€์ƒ‰ + ์‹์•ฝ์ฒ˜ ์ฒดํฌ ON ์‹œ ์‹ค์‹œ๊ฐ„ ๊ฒฐ๊ณผ ์‚ฌ์šฉ\n"
1379
  "- ๊ธˆ์ง€ ์„ฑ๋ถ„: ํ•จ๋Ÿ‰ ์œ ๋ฌด์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ๋ฐฐํ•ฉ๊ธˆ์ง€ ์šฐ์„  ํ‘œ์‹œ\n"
1380
  )
1381
  return result_df, msg
 
768
 
769
 
770
 
771
+
772
  def get_country_regulatory_text_cached(country):
773
  """
774
+ ๊ตญ๊ฐ€๋ณ„ ๊ทœ์ œ ํŒŒ์ผ ์›๋ฌธ์„ ์ง์ ‘ ์ฝ์–ด์„œ ์บ์‹œํ•œ๋‹ค.
775
+ ๋‹จ์ผ ์„ฑ๋ถ„ ๊ฒ€์ƒ‰์—์„œ๋Š” FAISS/์ž„๋ฒ ๋”ฉ ๋Œ€์‹  ์ด ์›๋ฌธ ์ง์ ‘๊ฒ€์ƒ‰์„ ์‚ฌ์šฉํ•œ๋‹ค.
 
776
  """
777
  global REG_TEXT_CACHE
778
 
 
816
  return combined, "READ_FILES"
817
 
818
 
819
+ def infer_regulation_type_from_source_text(source_text):
820
  """
821
+ ํŒŒ์ผ๋ช…/์‹œํŠธ๋ช…/๋ฌธ์„œ ์ œ๋ชฉ ๊ธฐ์ค€์œผ๋กœ ๊ทœ์ œ ์œ ํ˜•์„ ํŒ๋‹จํ•œ๋‹ค.
822
+
823
+ EU / ASEAN ๊ณตํ†ต:
824
+ - Annex II = Negative List / Prohibited Substances / ์‚ฌ์šฉ ๊ธˆ์ง€
825
+ - Annex III = Negative List / Restricted Substances / ์ œํ•œ ์„ฑ๋ถ„
826
+ - Annex IV = Positive List / Coloring Agents / ํ—ˆ์šฉ ์ƒ‰์†Œ
827
+ - Annex V = Positive List / Preservatives / ํ—ˆ์šฉ ๋ณด์กด์ œ
828
+ - Annex VI = Positive List / UV Filters / ํ—ˆ์šฉ ์ž์™ธ์„ ์ฐจ๋‹จ์ œ
829
+
830
+ ํ•ต์‹ฌ:
831
+ Annex II ์•ˆ์—์„œ ์„ฑ๋ถ„์ด ๋ฐœ๊ฒฌ๋˜๋ฉด ์ฃผ๋ณ€ ํ–‰์— '๊ธˆ์ง€'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์—†์–ด๋„ ๋ฐฐํ•ฉ๊ธˆ์ง€๋กœ ํŒ์ •ํ•œ๋‹ค.
832
+ """
833
+ text = safe_str(source_text)
834
+ low = text.lower()
835
+ compact = normalize_compact(text)
836
+
837
+ def has_any(patterns):
838
+ return any(p in low or normalize_compact(p) in compact for p in patterns)
839
+
840
+ # Annex II / Prohibited / Negative List
841
+ if has_any([
842
+ "annex ii",
843
+ "annex 2",
844
+ "annex_ii",
845
+ "annex-ii",
846
+ "prohibited substances",
847
+ "list of substances prohibited",
848
+ "negative list",
849
+ "์‚ฌ์šฉ ๊ธˆ์ง€",
850
+ "์‚ฌ์šฉ๊ธˆ์ง€",
851
+ "๊ธˆ์ง€ ์„ฑ๋ถ„",
852
+ "๊ธˆ์ง€์„ฑ๋ถ„",
853
+ "๊ธˆ์ง€์›๋ฃŒ",
854
+ "๋ฐฐํ•ฉ๊ธˆ์ง€",
855
+ ]):
856
+ return {
857
+ "type": "prohibited",
858
+ "label": "Annex II / Negative List / Prohibited Substances",
859
+ "display": "๋ฐฐํ•ฉ๊ธˆ์ง€",
860
+ }
861
+
862
+ # Annex III / Restricted / Negative List
863
+ if has_any([
864
+ "annex iii",
865
+ "annex 3",
866
+ "annex_iii",
867
+ "annex-iii",
868
+ "restricted substances",
869
+ "list of substances which cosmetic products must not contain except subject to the restrictions",
870
+ "restriction",
871
+ "restricted",
872
+ "์‚ฌ์šฉ ์ œํ•œ",
873
+ "์‚ฌ์šฉ์ œํ•œ",
874
+ "์ œํ•œ ์„ฑ๋ถ„",
875
+ "์ œํ•œ์„ฑ๋ถ„",
876
+ "๋ฐฐํ•ฉํ•œ๋„",
877
+ ]):
878
+ return {
879
+ "type": "restricted",
880
+ "label": "Annex III / Negative List / Restricted Substances",
881
+ "display": NEED_CHECK_TEXT,
882
+ }
883
+
884
+ # Annex IV / Colorants / Positive List
885
+ if has_any([
886
+ "annex iv",
887
+ "annex 4",
888
+ "annex_iv",
889
+ "annex-iv",
890
+ "coloring agents",
891
+ "colouring agents",
892
+ "colorants",
893
+ "colorant",
894
+ "์ƒ‰์†Œ",
895
+ ]):
896
+ return {
897
+ "type": "positive_colorant",
898
+ "label": "Annex IV / Positive List / Coloring Agents",
899
+ "display": "ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ",
900
+ }
901
+
902
+ # Annex V / Preservatives / Positive List
903
+ if has_any([
904
+ "annex v",
905
+ "annex 5",
906
+ "annex_v",
907
+ "annex-v",
908
+ "preservatives",
909
+ "preservative",
910
+ "๋ณด์กด์ œ",
911
+ "๋ฐฉ๋ถ€์ œ",
912
+ ]):
913
+ return {
914
+ "type": "positive_preservative",
915
+ "label": "Annex V / Positive List / Preservatives",
916
+ "display": "ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ",
917
+ }
918
+
919
+ # Annex VI / UV Filters / Positive List
920
+ if has_any([
921
+ "annex vi",
922
+ "annex 6",
923
+ "annex_vi",
924
+ "annex-vi",
925
+ "uv filters",
926
+ "uv filter",
927
+ "ultraviolet filters",
928
+ "sunscreen agents",
929
+ "์ž์™ธ์„ ",
930
+ "์ž์™ธ์„ ์ฐจ๋‹จ",
931
+ ]):
932
+ return {
933
+ "type": "positive_uv",
934
+ "label": "Annex VI / Positive List / UV Filters",
935
+ "display": "ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ",
936
+ }
937
+
938
+ return {
939
+ "type": "unknown",
940
+ "label": "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜",
941
+ "display": NEED_CHECK_TEXT,
942
+ }
943
+
944
+
945
+ def split_regulatory_text_into_sections(raw_text):
946
+ """
947
+ get_country_regulatory_text_cached()์—์„œ ๋งŒ๋“  ํ…์ŠคํŠธ๋ฅผ ํŒŒ์ผ/์‹œํŠธ/ํŽ˜์ด์ง€ ๋‹จ์œ„ ์„น์…˜์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.
948
+ ๊ฐ ์„น์…˜ ์•ž๋ถ€๋ถ„์—๋Š” [์ถœ์ฒ˜ํŒŒ์ผ: ...] ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ํ•ด๋‹น ํŒŒ์ผ์ด Annex II์ธ์ง€ III์ธ์ง€ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.
949
  """
950
  raw_text = safe_str(raw_text)
951
 
952
  if not raw_text:
953
+ return []
954
+
955
+ parts = re.split(r"(?=\[๊ตญ๊ฐ€/์ฑ„๋„:)", raw_text)
956
+ sections = []
957
+
958
+ for part in parts:
959
+ part = safe_str(part)
960
+
961
+ if not part:
962
+ continue
963
+
964
+ header_lines = part.splitlines()[:8]
965
+ header_text = "\n".join(header_lines)
966
+ sections.append({
967
+ "header": header_text,
968
+ "text": part,
969
+ })
970
+
971
+ return sections
972
+
973
 
974
+ def find_matching_regulatory_sections(raw_text, ingredient_names, max_sections=20):
975
+ """
976
+ ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์›๋ฌธ์—์„œ ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/INCI/CAS๊ฐ€ ์ง์ ‘ ํฌํ•จ๋œ ์„น์…˜์„ ์ฐพ๋Š”๋‹ค.
977
+ ๊ธฐ์กด line-window ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ ์„น์…˜ ์ „์ฒด์˜ ํŒŒ์ผ๋ช…/์‹œํŠธ๋ช…๊นŒ์ง€ ๊ฐ™์ด ๋ณด์กดํ•œ๋‹ค.
978
+ """
979
  names_norm = [
980
  normalize_compact(x)
981
  for x in ingredient_names
 
983
  ]
984
 
985
  if not names_norm:
986
+ return []
987
+
988
+ matches = []
989
+
990
+ for section in split_regulatory_text_into_sections(raw_text):
991
+ text = section["text"]
992
+ text_norm = normalize_compact(text)
993
+
994
+ if any(name in text_norm for name in names_norm):
995
+ # ์„ฑ๋ถ„์ด ์žˆ๋Š” ์ค„ ์ฃผ๋ณ€๋„ ๊ฐ™์ด ์ถ”์ถœ
996
+ lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
997
+ hit_indices = []
998
 
999
+ for idx, line in enumerate(lines):
1000
+ line_norm = normalize_compact(line)
 
1001
 
1002
+ if any(name in line_norm for name in names_norm):
1003
+ hit_indices.append(idx)
1004
 
1005
+ context_lines = []
1006
+ seen = set()
1007
 
1008
+ for idx in hit_indices[:8]:
1009
+ start = max(0, idx - 4)
1010
+ end = min(len(lines), idx + 5)
1011
 
1012
+ for j in range(start, end):
1013
+ if j in seen:
1014
+ continue
1015
+
1016
+ seen.add(j)
1017
+ context_lines.append(lines[j])
1018
+
1019
+ context = section["header"] + "\n" + "\n".join(context_lines)
1020
+ reg_type = infer_regulation_type_from_source_text(section["header"] + "\n" + text[:1200])
1021
+
1022
+ matches.append({
1023
+ "header": section["header"],
1024
+ "context": context[:8000],
1025
+ "regulation_type": reg_type,
1026
+ })
1027
+
1028
+ if len(matches) >= max_sections:
1029
  break
1030
 
1031
+ return matches
1032
+
1033
 
1034
+ def pick_best_regulatory_match(matches):
1035
+ """
1036
+ ์—ฌ๋Ÿฌ ํŒŒ์ผ์—์„œ ๋™์‹œ์— ๋งค์นญ๋  ๋•Œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ •ํ•œ๋‹ค.
1037
+ ๊ธˆ์ง€ > ์ œํ•œ > Positive List > unknown ์ˆœ์„œ.
1038
+ """
1039
+ if not matches:
1040
+ return None
1041
+
1042
+ priority = {
1043
+ "prohibited": 100,
1044
+ "restricted": 80,
1045
+ "positive_uv": 50,
1046
+ "positive_preservative": 50,
1047
+ "positive_colorant": 50,
1048
+ "unknown": 10,
1049
+ }
1050
 
1051
+ matches = sorted(
1052
+ matches,
1053
+ key=lambda m: priority.get(m.get("regulation_type", {}).get("type", "unknown"), 0),
1054
+ reverse=True,
1055
+ )
1056
 
1057
+ return matches[0]
1058
+
1059
+
1060
+ def extract_relevant_context_direct(raw_text, ingredient_names, window=2, max_blocks=12):
1061
+ """
1062
+ ๊ตฌ๋ฒ„์ „ ํ˜ธํ™˜์šฉ ํ•จ์ˆ˜.
1063
+ ์‹ค์ œ ํŒ์ •์€ find_matching_regulatory_sections()๋ฅผ ์šฐ์„  ์‚ฌ์šฉํ•œ๋‹ค.
1064
+ """
1065
+ raw_text = safe_str(raw_text)
1066
 
1067
+ if not raw_text:
1068
+ return "", False
1069
 
1070
+ matches = find_matching_regulatory_sections(raw_text, ingredient_names, max_sections=max_blocks)
1071
+
1072
+ if not matches:
1073
+ return "", False
1074
+
1075
+ context = "\n\n".join([m["context"] for m in matches[:max_blocks]])
1076
 
1077
  return context[:8000], True
1078
 
 
1081
  def evaluate_component_from_country_files_only(country, material, component, target_id=""):
1082
  """
1083
  GPT ํŒ์ • ์—†์ด ์‚ฌ์šฉ์ž๊ฐ€ ์—…๋ฐ์ดํŠธํ•œ ๊ตญ๊ฐ€๋ณ„ ๊ทœ์ œ ํŒŒ์ผ๋งŒ ์ง์ ‘ ๊ฒ€์ƒ‰ํ•ด์„œ ํŒ์ •ํ•œ๋‹ค.
1084
+
1085
+ ๋ณธ์งˆ์  ๊ฐœ์„ :
1086
+ - ์„ฑ๋ถ„ ์ฃผ๋ณ€ ๋ฌธ์žฅ์— '๊ธˆ์ง€' ํ‚ค์›Œ๋“œ๊ฐ€ ์—†๋”๋ผ๋„,
1087
+ Annex II / Prohibited / Negative List ํŒŒ์ผ์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ๋ฐฐํ•ฉ๊ธˆ์ง€๋กœ ํŒ์ •ํ•œ๋‹ค.
1088
+ - Annex III / Restricted ํŒŒ์ผ์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ์ œํ•œ ์„ฑ๋ถ„์œผ๋กœ ํŒ์ •ํ•œ๋‹ค.
1089
+ - Annex IV/V/VI Positive List์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ํ•ด๋‹น ๋ชฉ์ ์˜ ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ๋กœ ํŒ์ •ํ•œ๋‹ค.
1090
+ - ํ•จ๋Ÿ‰ ์œ ๋ฌด๋Š” ๊ธˆ์ง€ ํŒ๋‹จ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.
 
1091
  """
1092
  country_col = display_country(country)
1093
 
 
1138
  "๋Œ€ํ‘œ INCI": representative,
1139
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
1140
  "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
1141
+ "๋น„๊ณ ": cache_status or "ํ•ด๋‹น ๊ตญ๊ฐ€/์ฑ„๋„ ๊ทœ์ œ ํŒŒ์ผ ์—†์Œ",
1142
  }
1143
  log_search_context(
1144
  target_id,
 
1150
  )
1151
  return result
1152
 
1153
+ matches = find_matching_regulatory_sections(
1154
  raw_text=raw_text,
1155
  ingredient_names=[inci_name, korean_name, cas_no],
1156
+ max_sections=30,
 
1157
  )
1158
 
1159
+ if not matches:
1160
  result = {
1161
  "๋Œ€ํ‘œ INCI": representative,
1162
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
 
1173
  )
1174
  return result
1175
 
1176
+ best = pick_best_regulatory_match(matches)
1177
+ reg_type = best.get("regulation_type", {"type": "unknown", "label": "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜"})
1178
+ reg_kind = reg_type.get("type", "unknown")
1179
+ reg_label = reg_type.get("label", "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜")
1180
+ context = best.get("context", "")
1181
  evidence = context[:5000]
1182
  lower = evidence.lower()
1183
 
1184
+ # 1. Annex II / Prohibited / Negative List
1185
+ # ํ•ต์‹ฌ: ์„ฑ๋ถ„ ์ฃผ๋ณ€์— ๊ธˆ์ง€๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์—†์–ด๋„ ํ•ด๋‹น ํŒŒ์ผ์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ๋ฐฐํ•ฉ๊ธˆ์ง€.
1186
+ if reg_kind == "prohibited":
1187
+ result = {
1188
+ "๋Œ€ํ‘œ INCI": representative,
1189
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ์„ฑ๋ถ„ ํ™•์ธ",
1190
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "๋ฐฐํ•ฉ๊ธˆ์ง€",
1191
+ "๋น„๊ณ ": evidence[:1000],
1192
+ }
1193
+ log_search_context(
1194
+ target_id,
1195
+ country,
1196
+ component,
1197
+ result["๊ฒฐ๊ณผ"],
1198
+ "COUNTRY_FILE_ANNEX_II_PROHIBITED",
1199
+ context,
1200
+ )
1201
+ return result
1202
+
1203
+ # 2. Annex III / Restricted / Negative List
1204
+ # ํ•œ๋„ ์ˆ˜์น˜๊ฐ€ ์ถ”์ถœ๋˜๋ฉด ํ•œ๋„ ํ‘œ์‹œ, ์•„๋‹ˆ๋ฉด ํ™•์ธ ํ•„์š”.
1205
+ if reg_kind == "restricted":
1206
+ verified = extract_verified_limit_from_evidence(
1207
+ evidence_text=context,
1208
+ ingredient_names=[inci_name, korean_name, cas_no],
1209
+ )
1210
+
1211
+ if verified is not None:
1212
+ display = verified["limit_text"]
1213
+ note = verified["evidence"]
1214
+ else:
1215
+ display = NEED_CHECK_TEXT
1216
+ note = evidence[:1000]
1217
+
1218
+ result = {
1219
+ "๋Œ€ํ‘œ INCI": representative,
1220
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ์„ฑ๋ถ„ ํ™•์ธ",
1221
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": display,
1222
+ "๋น„๊ณ ": note,
1223
+ }
1224
+ log_search_context(
1225
+ target_id,
1226
+ country,
1227
+ component,
1228
+ result["๊ฒฐ๊ณผ"],
1229
+ "COUNTRY_FILE_ANNEX_III_RESTRICTED",
1230
+ context,
1231
+ )
1232
+ return result
1233
+
1234
+ # 3. Annex IV/V/VI Positive List
1235
+ # ์ด ํŒŒ์ผ๋“ค์€ 'ํ•ด๋‹น ๋ชฉ์  ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๋ชฉ๋ก'์ด๋‹ค. ์กฐ๊ฑด/ํ•œ๋„ ํ‚ค์›Œ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ํ™•์ธ ํ•„์š”.
1236
+ if reg_kind in ["positive_colorant", "positive_preservative", "positive_uv"]:
1237
+ verified = extract_verified_limit_from_evidence(
1238
+ evidence_text=context,
1239
+ ingredient_names=[inci_name, korean_name, cas_no],
1240
+ )
1241
+
1242
+ if verified is not None:
1243
+ display = verified["limit_text"]
1244
+ note = verified["evidence"]
1245
+ else:
1246
+ # Positive List ๋“ฑ์žฌ ์ž์ฒด๋Š” ๊ธˆ์ง€/์ œํ•œ์ด ์•„๋‹ˆ๋ผ ํ—ˆ์šฉ๋ชฉ๋ก ํ™•์ธ.
1247
+ # ๋‹ค๋งŒ ์กฐ๊ฑด ๋ฌธ๊ตฌ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ƒ์„ธ ๋น„๊ณ ์— ๊ทผ๊ฑฐ๋ฅผ ๋‚จ๊น€.
1248
+ display = "ํ—ˆ์šฉ๋ชฉ๋ก ๋“ฑ์žฌ"
1249
+ note = evidence[:1000]
1250
+
1251
+ result = {
1252
+ "๋Œ€ํ‘œ INCI": representative,
1253
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ์„ฑ๋ถ„ ํ™•์ธ",
1254
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": display,
1255
+ "๋น„๊ณ ": note,
1256
+ }
1257
+ log_search_context(
1258
+ target_id,
1259
+ country,
1260
+ component,
1261
+ result["๊ฒฐ๊ณผ"],
1262
+ "COUNTRY_FILE_POSITIVE_LIST",
1263
+ context,
1264
+ )
1265
+ return result
1266
+
1267
+ # 4. ํŒŒ์ผ ์œ ํ˜•์„ ๋ชป ์•Œ์•„๋‚ธ ๊ฒฝ์šฐ์—๋งŒ ๊ธฐ์กด ํ‚ค์›Œ๋“œ ๋ฐฉ์‹์œผ๋กœ ๋ณด์กฐ ํŒ๋‹จ
1268
  prohibited_keywords_ko = [
1269
  "๋ฐฐํ•ฉ๊ธˆ์ง€",
1270
  "์‚ฌ์šฉ๊ธˆ์ง€",
 
1312
  country,
1313
  component,
1314
  result["๊ฒฐ๊ณผ"],
1315
+ "COUNTRY_FILE_DIRECT_PROHIBITED_KEYWORD",
1316
  context,
1317
  )
1318
  return result
 
1381
  country,
1382
  component,
1383
  result["๊ฒฐ๊ณผ"],
1384
+ "COUNTRY_FILE_DIRECT_RESTRICTION_KEYWORD",
1385
  context,
1386
  )
1387
  return result
 
1390
  "๋Œ€ํ‘œ INCI": representative,
1391
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
1392
  "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
1393
+ "๋น„๊ณ ": "๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ๋‚ด ํ˜„์žฌ ์„ฑ๋ถ„๋ช…์€ ํ™•์ธ๋˜์—ˆ์œผ๋‚˜ ํŒŒ์ผ ์œ ํ˜•/์ œํ•œ/๊ธˆ์ง€/ํ•œ๋„ ๊ทผ๊ฑฐ ๋ฏธ๋ถ„๋ฅ˜",
1394
  }
1395
  log_search_context(
1396
  target_id,
1397
  country,
1398
  component,
1399
  result["๊ฒฐ๊ณผ"],
1400
+ "COUNTRY_FILE_DIRECT_UNCLASSIFIED",
1401
  context,
1402
  )
1403
  return result
1404
 
1405
 
 
1406
  def make_single_display_result(final_result, country_col="", component=None):
1407
  """
1408
  ๋‹จ์ผ ์„ฑ๋ถ„ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํ‘œ์˜ '๋ฐฐํ•ฉํ•œ๋„/๊ธˆ์ง€' ์นธ์— ๋“ค์–ด๊ฐˆ ์งง์€ ํ‘œ์‹œ๋ฌธ๊ตฌ๋ฅผ ๋งŒ๋“ ๋‹ค.
 
1667
  f"- ์„ ํƒ ๊ตญ๊ฐ€ ์ˆ˜: {len(countries)}๊ฐœ\n"
1668
  f"- MFDS ์ƒํƒœ: {mfds_live_msg}\n"
1669
  f"- MFDS ์กฐํšŒ ๋Œ€์ƒ: {', '.join(terms) if terms else '์—†์Œ'}\n"
1670
+ "- ํŒ์ • ๋ฐฉ์‹: GPT ํŒ์ • ์—†์Œ, Annex/Negative/Positive List ๊ธฐ๋ฐ˜ ์ง์ ‘๊ฒ€์ƒ‰ + ์‹์•ฝ์ฒ˜ ์ฒดํฌ ON ์‹œ ์‹ค์‹œ๊ฐ„ ๊ฒฐ๊ณผ ์‚ฌ์šฉ\n"
1671
  "- ๊ธˆ์ง€ ์„ฑ๋ถ„: ํ•จ๋Ÿ‰ ์œ ๋ฌด์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ๋ฐฐํ•ฉ๊ธˆ์ง€ ์šฐ์„  ํ‘œ์‹œ\n"
1672
  )
1673
  return result_df, msg