Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -738,15 +738,15 @@ def registry_terms_for_component(material=None, component=None, extra_terms=None
|
|
| 738 |
material.get("Trade name(์๋ฌธ)", ""),
|
| 739 |
] + list(extra_terms or [])
|
| 740 |
|
| 741 |
-
|
| 742 |
|
| 743 |
hits = []
|
| 744 |
|
| 745 |
for entry in REGULATORY_SYNONYM_REGISTRY:
|
| 746 |
terms = entry.get("terms", [])
|
| 747 |
-
term_norms = [normalize_compact(t) for t in terms if safe_str(t)]
|
| 748 |
|
| 749 |
-
|
|
|
|
| 750 |
for t in terms:
|
| 751 |
if safe_str(t) and t not in hits:
|
| 752 |
hits.append(safe_str(t))
|
|
@@ -768,17 +768,17 @@ def registry_direct_regulation_fallback(country, material, component, target_id=
|
|
| 768 |
country_col = display_country(country)
|
| 769 |
representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ๊ธ ์ฑ๋ถ๋ช
", "")) or registry_terms[0]
|
| 770 |
|
| 771 |
-
|
| 772 |
-
|
| 773 |
for t in registry_terms
|
| 774 |
if safe_str(t) and len(normalize_compact(t)) >= 3
|
| 775 |
]
|
| 776 |
|
| 777 |
for section in sections:
|
| 778 |
text = safe_str(section.get("text", ""))
|
| 779 |
-
text_norm = normalize_compact(text)
|
| 780 |
|
| 781 |
-
|
|
|
|
| 782 |
continue
|
| 783 |
|
| 784 |
header = safe_str(section.get("header", ""))
|
|
@@ -791,8 +791,7 @@ def registry_direct_regulation_fallback(country, material, component, target_id=
|
|
| 791 |
seen = set()
|
| 792 |
|
| 793 |
for idx, line in enumerate(lines):
|
| 794 |
-
|
| 795 |
-
if any(tn in ln for tn in registry_norms):
|
| 796 |
for j in range(max(0, idx - 5), min(len(lines), idx + 6)):
|
| 797 |
if j not in seen:
|
| 798 |
seen.add(j)
|
|
@@ -955,13 +954,12 @@ def find_chemical_variant_match_in_country_text(country, material, component, ta
|
|
| 955 |
|
| 956 |
for section in sections:
|
| 957 |
text = safe_str(section.get("text", ""))
|
| 958 |
-
text_norm = normalize_compact(text)
|
| 959 |
header = safe_str(section.get("header", ""))
|
| 960 |
reg_type = infer_regulation_type_from_source_text(header + "\n" + text[:1500], country=country)
|
| 961 |
reg_kind = reg_type.get("type", "unknown")
|
| 962 |
reg_label = reg_type.get("label", "๊ท์ ์ ํ ๋ฏธ๋ถ๋ฅ")
|
| 963 |
|
| 964 |
-
hit_terms = [v for v in expanded if
|
| 965 |
if not hit_terms:
|
| 966 |
continue
|
| 967 |
|
|
@@ -970,8 +968,7 @@ def find_chemical_variant_match_in_country_text(country, material, component, ta
|
|
| 970 |
seen = set()
|
| 971 |
|
| 972 |
for idx, line in enumerate(lines):
|
| 973 |
-
|
| 974 |
-
if any(normalize_compact(v) in line_norm for v in hit_terms):
|
| 975 |
for j in range(max(0, idx - 4), min(len(lines), idx + 5)):
|
| 976 |
if j not in seen:
|
| 977 |
seen.add(j)
|
|
@@ -1471,9 +1468,8 @@ def mfds_click_probable_result(page, term):
|
|
| 1471 |
except Exception:
|
| 1472 |
continue
|
| 1473 |
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
if term_norm and term_norm in row_norm:
|
| 1477 |
row.click(timeout=3000)
|
| 1478 |
page.wait_for_timeout(1000)
|
| 1479 |
return True
|
|
@@ -1737,7 +1733,8 @@ def mfds_lookup_one_term_with_retry(page, term):
|
|
| 1737 |
return [], ""
|
| 1738 |
|
| 1739 |
# ๊ฒ์์ด๊ฐ ํ๋ฉด์ ์์ง๋ง ๊ท์ ํ๋๊ฐ ์์ผ๋ฉด ์ผ๋ฐ ์๋ฃ ์์ธ/๋ฌด๊ท์ ๊ฒฐ๊ณผ๋ก ํ๋จ
|
| 1740 |
-
|
|
|
|
| 1741 |
return [], ""
|
| 1742 |
|
| 1743 |
if not clicked:
|
|
@@ -2697,37 +2694,38 @@ def get_country_section_index_cached(country):
|
|
| 2697 |
def find_matching_regulatory_sections_fast(country, ingredient_names, max_sections=30):
|
| 2698 |
"""
|
| 2699 |
๊ณ ์ ๋จ์ผ ๊ฒ์.
|
| 2700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2701 |
"""
|
| 2702 |
sections, status = get_country_section_index_cached(country)
|
| 2703 |
|
| 2704 |
-
|
| 2705 |
-
|
| 2706 |
for x in ingredient_names
|
| 2707 |
-
if safe_str(x) and len(normalize_compact(x)) >= 3
|
| 2708 |
]
|
| 2709 |
|
| 2710 |
-
if not
|
| 2711 |
return [], status
|
| 2712 |
|
| 2713 |
matches = []
|
| 2714 |
|
| 2715 |
for section in sections:
|
| 2716 |
-
|
| 2717 |
|
| 2718 |
-
if not any(name
|
| 2719 |
continue
|
| 2720 |
|
| 2721 |
-
text = safe_str(section.get("text", ""))
|
| 2722 |
header = safe_str(section.get("header", ""))
|
| 2723 |
lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
|
| 2724 |
|
| 2725 |
hit_indices = []
|
| 2726 |
|
| 2727 |
for idx, line in enumerate(lines):
|
| 2728 |
-
|
| 2729 |
-
|
| 2730 |
-
if any(name in line_norm for name in names_norm):
|
| 2731 |
hit_indices.append(idx)
|
| 2732 |
|
| 2733 |
if len(hit_indices) >= 10:
|
|
@@ -2752,7 +2750,6 @@ def find_matching_regulatory_sections_fast(country, ingredient_names, max_sectio
|
|
| 2752 |
else:
|
| 2753 |
context = header + "\n" + text[:2000]
|
| 2754 |
|
| 2755 |
-
# ๋์คํฌ ์บ์๊ฐ ์ค๋๋ label์ ๊ฐ์ง ์ ์์ผ๋ฏ๋ก header ๊ธฐ์ค์ผ๋ก ํ๋ฒ ๋ ๋ณด์
|
| 2756 |
reg_type = infer_regulation_type_from_source_text(
|
| 2757 |
header + "\n" + text[:1500],
|
| 2758 |
country=country,
|
|
@@ -2769,65 +2766,62 @@ def find_matching_regulatory_sections_fast(country, ingredient_names, max_sectio
|
|
| 2769 |
|
| 2770 |
return matches, status
|
| 2771 |
|
| 2772 |
-
|
| 2773 |
def find_matching_regulatory_sections(raw_text, ingredient_names, max_sections=20):
|
| 2774 |
"""
|
| 2775 |
-
๊ตฌ๋ฒ์ ํธํ์ฉ.
|
| 2776 |
"""
|
| 2777 |
-
|
| 2778 |
-
|
| 2779 |
for x in ingredient_names
|
| 2780 |
-
if safe_str(x) and len(normalize_compact(x)) >= 3
|
| 2781 |
]
|
| 2782 |
|
| 2783 |
-
if not
|
| 2784 |
return []
|
| 2785 |
|
| 2786 |
matches = []
|
| 2787 |
|
| 2788 |
for section in split_regulatory_text_into_sections(raw_text):
|
| 2789 |
text = section["text"]
|
| 2790 |
-
text_norm = normalize_compact(text)
|
| 2791 |
|
| 2792 |
-
if any(name
|
| 2793 |
-
|
| 2794 |
-
hit_indices = []
|
| 2795 |
|
| 2796 |
-
|
| 2797 |
-
|
| 2798 |
|
| 2799 |
-
|
| 2800 |
-
|
|
|
|
| 2801 |
|
| 2802 |
-
|
| 2803 |
-
|
| 2804 |
|
| 2805 |
-
|
| 2806 |
-
|
| 2807 |
-
|
| 2808 |
|
| 2809 |
-
|
| 2810 |
-
|
| 2811 |
-
|
| 2812 |
|
| 2813 |
-
|
| 2814 |
-
|
| 2815 |
|
| 2816 |
-
|
| 2817 |
-
|
| 2818 |
|
| 2819 |
-
|
| 2820 |
-
|
| 2821 |
-
|
| 2822 |
-
|
| 2823 |
-
|
| 2824 |
|
| 2825 |
-
|
| 2826 |
-
|
| 2827 |
|
| 2828 |
return matches
|
| 2829 |
|
| 2830 |
-
|
| 2831 |
def pick_best_regulatory_match(matches):
|
| 2832 |
"""
|
| 2833 |
์ฌ๋ฌ ํ์ผ์์ ๋์์ ๋งค์นญ๋ ๋ ์ฐ๏ฟฝ๏ฟฝ๏ฟฝ์์๋ฅผ ์ ํ๋ค.
|
|
@@ -3142,8 +3136,11 @@ def evaluate_component_from_uploaded_source_records(country, material, component
|
|
| 3142 |
๊ตญ๊ฐ ํด๋ ํ์ผ๋ง ๋ณด๊ณ ํ์ .
|
| 3143 |
ํ์ผ record ์์ ํ์ฌ ์ฑ๋ถ๋ช
/๊ตญ๋ฌธ/CAS๊ฐ ์ง์ ์์ ๋๋ง ํ์ ํ๋ค.
|
| 3144 |
"""
|
| 3145 |
-
terms =
|
| 3146 |
-
|
|
|
|
|
|
|
|
|
|
| 3147 |
|
| 3148 |
representative = (
|
| 3149 |
safe_str(component.get("INCI Name", ""))
|
|
@@ -3152,16 +3149,17 @@ def evaluate_component_from_uploaded_source_records(country, material, component
|
|
| 3152 |
or "-"
|
| 3153 |
)
|
| 3154 |
|
| 3155 |
-
if not
|
| 3156 |
return None
|
| 3157 |
|
| 3158 |
records, status = build_country_source_record_index(country)
|
| 3159 |
hits = []
|
| 3160 |
|
| 3161 |
for rec in records:
|
| 3162 |
-
|
| 3163 |
|
| 3164 |
-
|
|
|
|
| 3165 |
hits.append(rec)
|
| 3166 |
|
| 3167 |
if not hits:
|
|
|
|
| 738 |
material.get("Trade name(์๋ฌธ)", ""),
|
| 739 |
] + list(extra_terms or [])
|
| 740 |
|
| 741 |
+
identity_raw = " ".join(safe_str(x) for x in identity_values if safe_str(x))
|
| 742 |
|
| 743 |
hits = []
|
| 744 |
|
| 745 |
for entry in REGULATORY_SYNONYM_REGISTRY:
|
| 746 |
terms = entry.get("terms", [])
|
|
|
|
| 747 |
|
| 748 |
+
# ๋ถ๋ถ ๋ฌธ์์ด ๋งค์นญ ๊ธ์ง: biotin์ด meabiotin/biotinate์ ๊ฑธ๋ฆฌ๋ ๋ฌธ์ ๋ฐฉ์ง
|
| 749 |
+
if any(exact_ingredient_match(t, identity_raw) for t in terms if safe_str(t)):
|
| 750 |
for t in terms:
|
| 751 |
if safe_str(t) and t not in hits:
|
| 752 |
hits.append(safe_str(t))
|
|
|
|
| 768 |
country_col = display_country(country)
|
| 769 |
representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ๊ธ ์ฑ๋ถ๋ช
", "")) or registry_terms[0]
|
| 770 |
|
| 771 |
+
registry_terms = [
|
| 772 |
+
safe_str(t)
|
| 773 |
for t in registry_terms
|
| 774 |
if safe_str(t) and len(normalize_compact(t)) >= 3
|
| 775 |
]
|
| 776 |
|
| 777 |
for section in sections:
|
| 778 |
text = safe_str(section.get("text", ""))
|
|
|
|
| 779 |
|
| 780 |
+
# ๋ถ๋ถ ๋ฌธ์์ด ๋งค์นญ ๊ธ์ง
|
| 781 |
+
if not any(exact_ingredient_match(t, text) for t in registry_terms):
|
| 782 |
continue
|
| 783 |
|
| 784 |
header = safe_str(section.get("header", ""))
|
|
|
|
| 791 |
seen = set()
|
| 792 |
|
| 793 |
for idx, line in enumerate(lines):
|
| 794 |
+
if any(exact_ingredient_match(t, line) for t in registry_terms):
|
|
|
|
| 795 |
for j in range(max(0, idx - 5), min(len(lines), idx + 6)):
|
| 796 |
if j not in seen:
|
| 797 |
seen.add(j)
|
|
|
|
| 954 |
|
| 955 |
for section in sections:
|
| 956 |
text = safe_str(section.get("text", ""))
|
|
|
|
| 957 |
header = safe_str(section.get("header", ""))
|
| 958 |
reg_type = infer_regulation_type_from_source_text(header + "\n" + text[:1500], country=country)
|
| 959 |
reg_kind = reg_type.get("type", "unknown")
|
| 960 |
reg_label = reg_type.get("label", "๊ท์ ์ ํ ๋ฏธ๋ถ๋ฅ")
|
| 961 |
|
| 962 |
+
hit_terms = [v for v in expanded if exact_ingredient_match(v, text)]
|
| 963 |
if not hit_terms:
|
| 964 |
continue
|
| 965 |
|
|
|
|
| 968 |
seen = set()
|
| 969 |
|
| 970 |
for idx, line in enumerate(lines):
|
| 971 |
+
if any(exact_ingredient_match(v, line) for v in hit_terms):
|
|
|
|
| 972 |
for j in range(max(0, idx - 4), min(len(lines), idx + 5)):
|
| 973 |
if j not in seen:
|
| 974 |
seen.add(j)
|
|
|
|
| 1468 |
except Exception:
|
| 1469 |
continue
|
| 1470 |
|
| 1471 |
+
# ๋ถ๋ถ ๋ฌธ์์ด ํด๋ฆญ ๊ธ์ง: biotin ๊ฒ์ ์ MEA-Biotinate ๊ฒฐ๊ณผ ํด๋ฆญ ๋ฐฉ์ง
|
| 1472 |
+
if exact_ingredient_match(term, row_text):
|
|
|
|
| 1473 |
row.click(timeout=3000)
|
| 1474 |
page.wait_for_timeout(1000)
|
| 1475 |
return True
|
|
|
|
| 1733 |
return [], ""
|
| 1734 |
|
| 1735 |
# ๊ฒ์์ด๊ฐ ํ๋ฉด์ ์์ง๋ง ๊ท์ ํ๋๊ฐ ์์ผ๋ฉด ์ผ๋ฐ ์๋ฃ ์์ธ/๋ฌด๊ท์ ๊ฒฐ๊ณผ๋ก ํ๋จ
|
| 1736 |
+
# ๋จ, ๋ถ๋ถ ๋ฌธ์์ด์ ๊ธ์ง: biotin์ด meabiotin/biotinate์ ๊ฑธ๋ฆฌ๋ฉด ์ ๋จ
|
| 1737 |
+
if exact_ingredient_match(term, body_text):
|
| 1738 |
return [], ""
|
| 1739 |
|
| 1740 |
if not clicked:
|
|
|
|
| 2694 |
def find_matching_regulatory_sections_fast(country, ingredient_names, max_sections=30):
|
| 2695 |
"""
|
| 2696 |
๊ณ ์ ๋จ์ผ ๊ฒ์.
|
| 2697 |
+
์ฑ๋ถ๋ช
/INCI/CAS๋ฅผ ๊ตญ๊ฐ๋ณ ์น์
์ธ๋ฑ์ค์์ ์ง์ ๊ฒ์ํ๋ค.
|
| 2698 |
+
|
| 2699 |
+
์ค์:
|
| 2700 |
+
- normalize_compact ๊ธฐ๋ฐ ๋ถ๋ถ ๋ฌธ์์ด ๊ฒ์์ ์ฐ์ง ์๋๋ค.
|
| 2701 |
+
- Biotin ๊ฒ์ ์ MEA-Biotinate, Biotinate, Biotinyl- ๊ณ์ด์ Biotin์ผ๋ก ์ค์ธํ์ง ์๋๋ค.
|
| 2702 |
"""
|
| 2703 |
sections, status = get_country_section_index_cached(country)
|
| 2704 |
|
| 2705 |
+
names = [
|
| 2706 |
+
safe_str(x)
|
| 2707 |
for x in ingredient_names
|
| 2708 |
+
if safe_str(x) and len(normalize_compact(x)) >= 3 and not is_broad_lookup_term(x)
|
| 2709 |
]
|
| 2710 |
|
| 2711 |
+
if not names:
|
| 2712 |
return [], status
|
| 2713 |
|
| 2714 |
matches = []
|
| 2715 |
|
| 2716 |
for section in sections:
|
| 2717 |
+
text = safe_str(section.get("text", ""))
|
| 2718 |
|
| 2719 |
+
if not any(exact_ingredient_match(name, text) for name in names):
|
| 2720 |
continue
|
| 2721 |
|
|
|
|
| 2722 |
header = safe_str(section.get("header", ""))
|
| 2723 |
lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
|
| 2724 |
|
| 2725 |
hit_indices = []
|
| 2726 |
|
| 2727 |
for idx, line in enumerate(lines):
|
| 2728 |
+
if any(exact_ingredient_match(name, line) for name in names):
|
|
|
|
|
|
|
| 2729 |
hit_indices.append(idx)
|
| 2730 |
|
| 2731 |
if len(hit_indices) >= 10:
|
|
|
|
| 2750 |
else:
|
| 2751 |
context = header + "\n" + text[:2000]
|
| 2752 |
|
|
|
|
| 2753 |
reg_type = infer_regulation_type_from_source_text(
|
| 2754 |
header + "\n" + text[:1500],
|
| 2755 |
country=country,
|
|
|
|
| 2766 |
|
| 2767 |
return matches, status
|
| 2768 |
|
|
|
|
| 2769 |
def find_matching_regulatory_sections(raw_text, ingredient_names, max_sections=20):
|
| 2770 |
"""
|
| 2771 |
+
๊ตฌ๋ฒ์ ํธํ์ฉ. ๋ถ๋ถ ๋ฌธ์์ด ๊ฒ์ ๊ธ์ง.
|
| 2772 |
"""
|
| 2773 |
+
names = [
|
| 2774 |
+
safe_str(x)
|
| 2775 |
for x in ingredient_names
|
| 2776 |
+
if safe_str(x) and len(normalize_compact(x)) >= 3 and not is_broad_lookup_term(x)
|
| 2777 |
]
|
| 2778 |
|
| 2779 |
+
if not names:
|
| 2780 |
return []
|
| 2781 |
|
| 2782 |
matches = []
|
| 2783 |
|
| 2784 |
for section in split_regulatory_text_into_sections(raw_text):
|
| 2785 |
text = section["text"]
|
|
|
|
| 2786 |
|
| 2787 |
+
if not any(exact_ingredient_match(name, text) for name in names):
|
| 2788 |
+
continue
|
|
|
|
| 2789 |
|
| 2790 |
+
lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
|
| 2791 |
+
hit_indices = []
|
| 2792 |
|
| 2793 |
+
for idx, line in enumerate(lines):
|
| 2794 |
+
if any(exact_ingredient_match(name, line) for name in names):
|
| 2795 |
+
hit_indices.append(idx)
|
| 2796 |
|
| 2797 |
+
context_lines = []
|
| 2798 |
+
seen = set()
|
| 2799 |
|
| 2800 |
+
for idx in hit_indices[:8]:
|
| 2801 |
+
start = max(0, idx - 4)
|
| 2802 |
+
end = min(len(lines), idx + 5)
|
| 2803 |
|
| 2804 |
+
for j in range(start, end):
|
| 2805 |
+
if j in seen:
|
| 2806 |
+
continue
|
| 2807 |
|
| 2808 |
+
seen.add(j)
|
| 2809 |
+
context_lines.append(lines[j])
|
| 2810 |
|
| 2811 |
+
context = section["header"] + "\n" + "\n".join(context_lines)
|
| 2812 |
+
reg_type = infer_regulation_type_from_source_text(section["header"] + "\n" + text[:1200], country="")
|
| 2813 |
|
| 2814 |
+
matches.append({
|
| 2815 |
+
"header": section["header"],
|
| 2816 |
+
"context": context[:8000],
|
| 2817 |
+
"regulation_type": reg_type,
|
| 2818 |
+
})
|
| 2819 |
|
| 2820 |
+
if len(matches) >= max_sections:
|
| 2821 |
+
break
|
| 2822 |
|
| 2823 |
return matches
|
| 2824 |
|
|
|
|
| 2825 |
def pick_best_regulatory_match(matches):
|
| 2826 |
"""
|
| 2827 |
์ฌ๋ฌ ํ์ผ์์ ๋์์ ๋งค์นญ๋ ๋ ์ฐ๏ฟฝ๏ฟฝ๏ฟฝ์์๋ฅผ ์ ํ๋ค.
|
|
|
|
| 3136 |
๊ตญ๊ฐ ํด๋ ํ์ผ๋ง ๋ณด๊ณ ํ์ .
|
| 3137 |
ํ์ผ record ์์ ํ์ฌ ์ฑ๋ถ๋ช
/๊ตญ๋ฌธ/CAS๊ฐ ์ง์ ์์ ๋๋ง ํ์ ํ๋ค.
|
| 3138 |
"""
|
| 3139 |
+
terms = [
|
| 3140 |
+
safe_str(t)
|
| 3141 |
+
for t in source_match_terms_for_component(material, component)
|
| 3142 |
+
if safe_str(t) and len(normalize_compact(t)) >= 3 and not is_broad_lookup_term(t)
|
| 3143 |
+
]
|
| 3144 |
|
| 3145 |
representative = (
|
| 3146 |
safe_str(component.get("INCI Name", ""))
|
|
|
|
| 3149 |
or "-"
|
| 3150 |
)
|
| 3151 |
|
| 3152 |
+
if not terms:
|
| 3153 |
return None
|
| 3154 |
|
| 3155 |
records, status = build_country_source_record_index(country)
|
| 3156 |
hits = []
|
| 3157 |
|
| 3158 |
for rec in records:
|
| 3159 |
+
rec_text = rec.get("text", "")
|
| 3160 |
|
| 3161 |
+
# ๋ถ๋ถ ๋ฌธ์์ด ๋งค์นญ ๊ธ์ง: biotin์ด MEA-Biotinate์ ๊ฑธ๋ฆฌ๋ ๋ฌธ์ ๋ฐฉ์ง
|
| 3162 |
+
if any(exact_ingredient_match(t, rec_text) for t in terms):
|
| 3163 |
hits.append(rec)
|
| 3164 |
|
| 3165 |
if not hits:
|