Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 820 |
"""
|
| 821 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
"""
|
| 823 |
raw_text = safe_str(raw_text)
|
| 824 |
|
| 825 |
if not raw_text:
|
| 826 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 836 |
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
rough_blocks = [safe_str(x) for x in rough_blocks if safe_str(x)]
|
| 840 |
|
| 841 |
-
|
|
|
|
| 842 |
|
| 843 |
-
|
| 844 |
-
|
| 845 |
|
| 846 |
-
|
| 847 |
-
|
|
|
|
| 848 |
|
| 849 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 850 |
break
|
| 851 |
|
| 852 |
-
|
| 853 |
-
|
| 854 |
|
| 855 |
-
|
| 856 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 857 |
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
|
|
|
|
|
|
| 861 |
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
|
| 866 |
-
|
| 867 |
-
|
| 868 |
|
| 869 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 880 |
-
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 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 "ํด๋น ๊ตญ๊ฐ/์ฑ๋ ๊ท์
|
| 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 |
-
|
| 949 |
raw_text=raw_text,
|
| 950 |
ingredient_names=[inci_name, korean_name, cas_no],
|
| 951 |
-
|
| 952 |
-
max_blocks=16,
|
| 953 |
)
|
| 954 |
|
| 955 |
-
if not
|
| 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 |
-
"
|
| 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 |
-
"
|
| 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 |
-
"
|
| 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 ํ์ ์์,
|
| 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
|