Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,13 @@ from collections import defaultdict
|
|
| 25 |
import pandas as pd
|
| 26 |
import pdfplumber
|
| 27 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
from openpyxl import load_workbook
|
| 30 |
from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
|
|
@@ -48,6 +55,7 @@ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
|
| 48 |
|
| 49 |
FAISS_CACHE_DIR = ".faiss_cache"
|
| 50 |
MFDS_DETAIL_DB_PATH = BASE_DIR / "MFDS_๊ตญ๊ฐ๋ณ_๊ธ์ง์ ํ_์์ธ.xlsx"
|
|
|
|
| 51 |
|
| 52 |
VECTOR_DB_CACHE = {}
|
| 53 |
EVAL_CACHE = {}
|
|
@@ -686,6 +694,75 @@ def download_mfds_template():
|
|
| 686 |
return None, f"โ ํ
ํ๋ฆฟ ์์ฑ ์คํจ: {e}"
|
| 687 |
|
| 688 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
# ============================================================
|
| 690 |
# 4. ๊ตญ๊ฐ ํด๋ ์๋ ์ธ์
|
| 691 |
# ============================================================
|
|
@@ -838,9 +915,30 @@ def find_main_columns(df):
|
|
| 838 |
trade_ko_col = find_best_col(df, ["trade name(๊ตญ๋ฌธ)", "trade name ๊ตญ๋ฌธ", "๊ตญ๋ฌธ", "์๋ฃ๋ช
", "์ํ๋ช
"], default_index=1, exclude_cols=[no_col])
|
| 839 |
trade_en_col = find_best_col(df, ["trade name(์๋ฌธ)", "trade name ์๋ฌธ", "์๋ฌธ", "trade name", "trade", "raw material", "material"], default_index=2 if len(df.columns) > 2 else 1, exclude_cols=[no_col, trade_ko_col])
|
| 840 |
korean_ing_col = find_best_col(df, ["ํ๊ธ ์ฑ๋ถ๋ช
", "๊ตญ๋ฌธ ์ฑ๋ถ๋ช
", "ํ๊ธ์ฑ๋ถ๋ช
", "korean", "๊ตญ๋ฌธinci", "์ฑ๋ถ๋ช
"], default_index=3 if len(df.columns) > 3 else None, exclude_cols=[no_col, trade_ko_col, trade_en_col])
|
| 841 |
-
inci_col = find_best_col(df, ["inci name", "inci", "ingredient", "์ฑ๋ถ", "์ ์ฑ๋ถ", "๊ตฌ์ฑ"
|
| 842 |
contents_col = find_numeric_col(df, ["contents", "content", "contenta", "์๋ฃ ๋ด", "์๋ฃ๋ด", "ํจ์ ๋", "์กฐ์ฑ๋น"], default_index=5 if len(df.columns) > 5 else None, exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col], prefer_many_values=True)
|
| 843 |
-
formula_col = find_numeric_col(df, ["in formula", "formula", "์ฒ๋ฐฉ", "์ต์ข
", "์ ํ ๋ด", "์ ํ๋ด", "๋ฐฐํฉ๋", "ํจ๋"], default_index=6 if len(df.columns) > 6 else None, exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col], prefer_many_values=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
|
| 845 |
if contents_col == formula_col:
|
| 846 |
numeric_cols = []
|
|
@@ -862,6 +960,9 @@ def find_main_columns(df):
|
|
| 862 |
"inci_col": inci_col,
|
| 863 |
"contents_col": contents_col,
|
| 864 |
"formula_col": formula_col,
|
|
|
|
|
|
|
|
|
|
| 865 |
}
|
| 866 |
|
| 867 |
|
|
@@ -943,6 +1044,9 @@ def make_grouped_materials(df):
|
|
| 943 |
inci_col = cols["inci_col"]
|
| 944 |
contents_col = cols["contents_col"]
|
| 945 |
formula_col = cols["formula_col"]
|
|
|
|
|
|
|
|
|
|
| 946 |
|
| 947 |
work_df = cleaned_df.copy()
|
| 948 |
for col in [no_col, trade_ko_col, trade_en_col]:
|
|
@@ -972,9 +1076,19 @@ def make_grouped_materials(df):
|
|
| 972 |
continue
|
| 973 |
contents_value = format_amount(row.get(contents_col)) if contents_col and contents_col in row else ""
|
| 974 |
formula_value = format_amount(row.get(formula_col)) if formula_col and formula_col in row else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
components.append({
|
| 976 |
"ํ๊ธ ์ฑ๋ถ๋ช
": korean_ing,
|
| 977 |
"INCI Name": inci_name,
|
|
|
|
|
|
|
|
|
|
| 978 |
"Contents(w/w %)": contents_value,
|
| 979 |
"In formula(w/w %)": formula_value,
|
| 980 |
"amount_float": to_number(row.get(formula_col)) if formula_col and formula_col in row else None,
|
|
@@ -1570,20 +1684,47 @@ def evaluate_components_with_llm_batch(country, targets, llm):
|
|
| 1570 |
# 10. ์ถ๋ ฅ ๋ฐ์ดํฐ ๊ตฌ์ฑ
|
| 1571 |
# ============================================================
|
| 1572 |
|
| 1573 |
-
def make_formula_statement_rows(row_items):
|
| 1574 |
rows = []
|
| 1575 |
for item in row_items:
|
| 1576 |
material = item["material"]
|
| 1577 |
comp = item["component"]
|
| 1578 |
-
|
|
|
|
|
|
|
| 1579 |
"NO.": material.get("NO", ""),
|
| 1580 |
"ํ๊ธ ํ์คํ ๋ช
์นญ": comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
| 1581 |
"INGREDIENT NAME (INCI)": comp.get("INCI Name", ""),
|
| 1582 |
-
"FUNCTION": "",
|
| 1583 |
-
"CAS.No": "",
|
| 1584 |
-
"SOURCE":
|
| 1585 |
"%(w/w)": comp.get("In formula(w/w %)", ""),
|
| 1586 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1587 |
return pd.DataFrame(rows)
|
| 1588 |
|
| 1589 |
|
|
@@ -1617,91 +1758,160 @@ def make_regulatory_summary_rows(row_items, country_cols):
|
|
| 1617 |
|
| 1618 |
def write_output_workbook(out_path, product_name, formula_df, regulatory_df, raw_df):
|
| 1619 |
with pd.ExcelWriter(out_path, engine="openpyxl") as writer:
|
| 1620 |
-
formula_df.to_excel(writer, index=False, sheet_name="Formula Ingredients Statement", startrow=
|
| 1621 |
regulatory_df.to_excel(writer, index=False, sheet_name="Regulatory Screening Summary")
|
| 1622 |
-
raw_df.to_excel(writer, index=False, sheet_name="
|
| 1623 |
|
| 1624 |
wb = load_workbook(out_path)
|
| 1625 |
ws = wb["Formula Ingredients Statement"]
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=
|
| 1629 |
-
|
|
|
|
|
|
|
| 1630 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1631 |
style_output_workbook(out_path)
|
| 1632 |
|
| 1633 |
|
| 1634 |
def style_output_workbook(path):
|
| 1635 |
-
"""
|
| 1636 |
-
MergedCell ์ค๋ฅ ์์ ๋ฒ์ .
|
| 1637 |
-
๊ธฐ์กด ws.columns + col_cells[0].column_letter ๋ฐฉ์์ ๋ณํฉ์
์์ ์ค๋ฅ๊ฐ ๋๋ฏ๋ก,
|
| 1638 |
-
get_column_letter(col_idx)๋ก ์ปฌ๋ผ ๋ฌธ์๋ฅผ ์์ ํ๊ฒ ์์ฑํ๋ค.
|
| 1639 |
-
"""
|
| 1640 |
wb = load_workbook(path)
|
| 1641 |
|
| 1642 |
thin_border = Border(
|
| 1643 |
-
left=Side(style="thin", color="
|
| 1644 |
-
right=Side(style="thin", color="
|
| 1645 |
-
top=Side(style="thin", color="
|
| 1646 |
-
bottom=Side(style="thin", color="
|
| 1647 |
)
|
| 1648 |
|
| 1649 |
-
header_fill = PatternFill(start_color="
|
| 1650 |
issue_fill = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")
|
| 1651 |
-
|
|
|
|
|
|
|
| 1652 |
|
| 1653 |
-
|
| 1654 |
-
|
| 1655 |
-
|
|
|
|
| 1656 |
|
| 1657 |
for ws in wb.worksheets:
|
| 1658 |
-
|
| 1659 |
-
|
| 1660 |
-
|
| 1661 |
-
|
| 1662 |
-
|
| 1663 |
-
|
| 1664 |
-
|
| 1665 |
-
|
| 1666 |
-
|
| 1667 |
-
|
| 1668 |
-
|
| 1669 |
-
|
| 1670 |
-
|
| 1671 |
-
|
| 1672 |
-
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
| 1677 |
-
|
| 1678 |
-
|
| 1679 |
-
|
| 1680 |
-
|
| 1681 |
-
|
| 1682 |
-
|
| 1683 |
-
|
| 1684 |
-
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
-
|
| 1688 |
-
|
| 1689 |
-
|
| 1690 |
-
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
|
| 1694 |
-
|
| 1695 |
-
|
| 1696 |
-
|
| 1697 |
-
|
| 1698 |
-
|
| 1699 |
-
|
| 1700 |
-
|
| 1701 |
-
|
| 1702 |
-
ws.
|
| 1703 |
-
|
| 1704 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1705 |
|
| 1706 |
wb.save(path)
|
| 1707 |
|
|
@@ -1796,7 +2006,7 @@ def process(countries, file, product_name="", use_mfds_db=True):
|
|
| 1796 |
row_data["Result"] = build_integrated_result(row_data, country_cols)
|
| 1797 |
row_data["๋น๊ณ "] = " / ".join(item["notes"]) if item["notes"] else "-"
|
| 1798 |
|
| 1799 |
-
formula_df = make_formula_statement_rows(row_items)
|
| 1800 |
regulatory_df = make_regulatory_summary_rows(row_items, country_cols)
|
| 1801 |
|
| 1802 |
raw_columns = ["target_id", "๊ตญ๊ฐ/์ฑ๋", "์ฑ๋ถ๋ช
", "INCI Name", "ํ์ ", "์ถ์ฒ", "๊ฒ์ ์๋ฌธ"]
|
|
@@ -1827,7 +2037,7 @@ def process(countries, file, product_name="", use_mfds_db=True):
|
|
| 1827 |
f"- ์ฌ๊ฒํ ํ์ ์ฑ๋ถ: {issue_count}๊ฐ\n"
|
| 1828 |
f"- MFDS ์์ธ DB ํ ์: {mfds_count}๊ฐ\n"
|
| 1829 |
"- ๊ฒฐ๊ณผ ํ์ผ์ 3๊ฐ ์ํธ๋ก ์ ์ฅํ์ต๋๋ค.\n"
|
| 1830 |
-
" 1) Formula Ingredients Statement:
|
| 1831 |
" 2) Regulatory Screening Summary: ๋ฌธ์ ์๋ ์ฑ๋ถ๋ง ๊ตญ๊ฐ๋ณ ์์ฝ\n"
|
| 1832 |
" 3) Raw Search Log: ๋ก์ปฌ ๊ท์น/MFDS/GPT ๊ฒ์ ๊ทผ๊ฑฐ ๋ก๊ทธ\n"
|
| 1833 |
"- ํ์ ์ฐ์ ์์: ๋ก์ปฌ ๊ณ ์ ๊ท์น โ MFDS ์์ธ DB โ ๊ตญ๊ฐ๋ณ FAISS/GPT\n"
|
|
|
|
| 25 |
import pandas as pd
|
| 26 |
import pdfplumber
|
| 27 |
import gradio as gr
|
| 28 |
+
from bs4 import BeautifulSoup
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
from playwright.sync_api import sync_playwright
|
| 32 |
+
PLAYWRIGHT_AVAILABLE = True
|
| 33 |
+
except Exception:
|
| 34 |
+
PLAYWRIGHT_AVAILABLE = False
|
| 35 |
|
| 36 |
from openpyxl import load_workbook
|
| 37 |
from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
|
|
|
|
| 55 |
|
| 56 |
FAISS_CACHE_DIR = ".faiss_cache"
|
| 57 |
MFDS_DETAIL_DB_PATH = BASE_DIR / "MFDS_๊ตญ๊ฐ๋ณ_๊ธ์ง์ ํ_์์ธ.xlsx"
|
| 58 |
+
MFDS_LIVE_URL = "https://nedrug.mfds.go.kr/pbp/CCBDF01"
|
| 59 |
|
| 60 |
VECTOR_DB_CACHE = {}
|
| 61 |
EVAL_CACHE = {}
|
|
|
|
| 694 |
return None, f"โ ํ
ํ๋ฆฟ ์์ฑ ์คํจ: {e}"
|
| 695 |
|
| 696 |
|
| 697 |
+
# ============================================================
|
| 698 |
+
# 3-1. MFDS ์์ฝ์ฒ ์ฌ์ดํธ ์ค์๊ฐ ์กฐํ
|
| 699 |
+
# ============================================================
|
| 700 |
+
|
| 701 |
+
def extract_terms_for_mfds_live(row_items):
|
| 702 |
+
terms = []
|
| 703 |
+
for item in row_items:
|
| 704 |
+
comp = item["component"]
|
| 705 |
+
for value in [comp.get("INCI Name", ""), comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", "")]:
|
| 706 |
+
value = safe_str(value)
|
| 707 |
+
if value and len(normalize_compact(value)) >= 3:
|
| 708 |
+
terms.append(value)
|
| 709 |
+
|
| 710 |
+
seen = set()
|
| 711 |
+
unique = []
|
| 712 |
+
for term in terms:
|
| 713 |
+
key = normalize_compact(term)
|
| 714 |
+
if key and key not in seen:
|
| 715 |
+
seen.add(key)
|
| 716 |
+
unique.append(term)
|
| 717 |
+
return unique
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
def mfds_live_visible_text(page):
|
| 721 |
+
try:
|
| 722 |
+
html = page.content()
|
| 723 |
+
soup = BeautifulSoup(html, "html.parser")
|
| 724 |
+
for tag in soup(["script", "style", "nav", "footer", "header"]):
|
| 725 |
+
tag.decompose()
|
| 726 |
+
return soup.get_text(separator="\n")
|
| 727 |
+
except Exception:
|
| 728 |
+
try:
|
| 729 |
+
return page.locator("body").inner_text(timeout=5000)
|
| 730 |
+
except Exception:
|
| 731 |
+
return ""
|
| 732 |
+
|
| 733 |
+
|
| 734 |
+
def mfds_live_fill_search_input(page, term):
|
| 735 |
+
selectors = ["input[type='search']", "input[type='text']", "input:not([type])"]
|
| 736 |
+
for selector in selectors:
|
| 737 |
+
try:
|
| 738 |
+
loc = page.locator(selector)
|
| 739 |
+
for i in range(min(loc.count(), 20)):
|
| 740 |
+
item = loc.nth(i)
|
| 741 |
+
try:
|
| 742 |
+
if item.is_visible(timeout=1000) and item.is_enabled(timeout=1000):
|
| 743 |
+
item.fill(term, timeout=3000)
|
| 744 |
+
return True
|
| 745 |
+
except Exception:
|
| 746 |
+
continue
|
| 747 |
+
except Exception:
|
| 748 |
+
continue
|
| 749 |
+
return False
|
| 750 |
+
|
| 751 |
+
|
| 752 |
+
def mfds_live_click_search(page):
|
| 753 |
+
for txt in ["๊ฒ์", "์กฐํ", "Search"]:
|
| 754 |
+
try:
|
| 755 |
+
loc = page.get_by_text(txt, exact=True)
|
| 756 |
+
if loc.count() > 0:
|
| 757 |
+
loc.first.click(timeout=3000)
|
| 758 |
+
return True
|
| 759 |
+
except Exception:
|
| 760 |
+
pass
|
| 761 |
+
|
| 762 |
+
selectors = ["button:has-text('๊ฒ์')", "a:has-text('๊ฒ์')", "input[type='submit']", "button[type='submit']", ".btn_search", "#btnSearch", "button"]
|
| 763 |
+
for
|
| 764 |
+
|
| 765 |
+
|
| 766 |
# ============================================================
|
| 767 |
# 4. ๊ตญ๊ฐ ํด๋ ์๋ ์ธ์
|
| 768 |
# ============================================================
|
|
|
|
| 915 |
trade_ko_col = find_best_col(df, ["trade name(๊ตญ๋ฌธ)", "trade name ๊ตญ๋ฌธ", "๊ตญ๋ฌธ", "์๋ฃ๋ช
", "์ํ๋ช
"], default_index=1, exclude_cols=[no_col])
|
| 916 |
trade_en_col = find_best_col(df, ["trade name(์๋ฌธ)", "trade name ์๋ฌธ", "์๋ฌธ", "trade name", "trade", "raw material", "material"], default_index=2 if len(df.columns) > 2 else 1, exclude_cols=[no_col, trade_ko_col])
|
| 917 |
korean_ing_col = find_best_col(df, ["ํ๊ธ ์ฑ๋ถ๋ช
", "๊ตญ๋ฌธ ์ฑ๋ถ๋ช
", "ํ๊ธ์ฑ๋ถ๋ช
", "korean", "๊ตญ๋ฌธinci", "์ฑ๋ถ๋ช
"], default_index=3 if len(df.columns) > 3 else None, exclude_cols=[no_col, trade_ko_col, trade_en_col])
|
| 918 |
+
inci_col = find_best_col(df, ["inci name", "inci", "ingredient", "์ฑ๋ถ", "์ ์ฑ๋ถ", "๊ตฌ์ฑ"], default_index=4 if len(df.columns) > 4 else 2, exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col])
|
| 919 |
contents_col = find_numeric_col(df, ["contents", "content", "contenta", "์๋ฃ ๋ด", "์๋ฃ๋ด", "ํจ์ ๋", "์กฐ์ฑ๋น"], default_index=5 if len(df.columns) > 5 else None, exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col], prefer_many_values=True)
|
| 920 |
+
formula_col = find_numeric_col(df, ["in formula", "formula", "์ฒ๋ฐฉ", "์ต์ข
", "์ ํ ๋ด", "์ ํ๋ด", "๋ฐฐํฉ๋", "ํจ๋", "%"], default_index=6 if len(df.columns) > 6 else None, exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col], prefer_many_values=True)
|
| 921 |
+
|
| 922 |
+
function_col = find_best_col(
|
| 923 |
+
df,
|
| 924 |
+
["function", "๊ธฐ๋ฅ", "์ฉ๋"],
|
| 925 |
+
default_index=None,
|
| 926 |
+
exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col, contents_col, formula_col],
|
| 927 |
+
)
|
| 928 |
+
|
| 929 |
+
cas_col = find_best_col(
|
| 930 |
+
df,
|
| 931 |
+
["cas.no", "cas no", "cas", "cas๋ฒํธ", "cas no."],
|
| 932 |
+
default_index=None,
|
| 933 |
+
exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col, contents_col, formula_col, function_col],
|
| 934 |
+
)
|
| 935 |
+
|
| 936 |
+
source_col = find_best_col(
|
| 937 |
+
df,
|
| 938 |
+
["source", "์ถ์ฒ", "icid"],
|
| 939 |
+
default_index=None,
|
| 940 |
+
exclude_cols=[no_col, trade_ko_col, trade_en_col, korean_ing_col, inci_col, contents_col, formula_col, function_col, cas_col],
|
| 941 |
+
)
|
| 942 |
|
| 943 |
if contents_col == formula_col:
|
| 944 |
numeric_cols = []
|
|
|
|
| 960 |
"inci_col": inci_col,
|
| 961 |
"contents_col": contents_col,
|
| 962 |
"formula_col": formula_col,
|
| 963 |
+
"function_col": function_col,
|
| 964 |
+
"cas_col": cas_col,
|
| 965 |
+
"source_col": source_col,
|
| 966 |
}
|
| 967 |
|
| 968 |
|
|
|
|
| 1044 |
inci_col = cols["inci_col"]
|
| 1045 |
contents_col = cols["contents_col"]
|
| 1046 |
formula_col = cols["formula_col"]
|
| 1047 |
+
function_col = cols.get("function_col")
|
| 1048 |
+
cas_col = cols.get("cas_col")
|
| 1049 |
+
source_col = cols.get("source_col")
|
| 1050 |
|
| 1051 |
work_df = cleaned_df.copy()
|
| 1052 |
for col in [no_col, trade_ko_col, trade_en_col]:
|
|
|
|
| 1076 |
continue
|
| 1077 |
contents_value = format_amount(row.get(contents_col)) if contents_col and contents_col in row else ""
|
| 1078 |
formula_value = format_amount(row.get(formula_col)) if formula_col and formula_col in row else ""
|
| 1079 |
+
function_value = safe_str(row.get(function_col, "")) if function_col and function_col in row else ""
|
| 1080 |
+
cas_value = safe_str(row.get(cas_col, "")) if cas_col and cas_col in row else ""
|
| 1081 |
+
source_value = safe_str(row.get(source_col, "")) if source_col and source_col in row else ""
|
| 1082 |
+
|
| 1083 |
+
if not source_value:
|
| 1084 |
+
source_value = "ICID"
|
| 1085 |
+
|
| 1086 |
components.append({
|
| 1087 |
"ํ๊ธ ์ฑ๋ถ๋ช
": korean_ing,
|
| 1088 |
"INCI Name": inci_name,
|
| 1089 |
+
"FUNCTION": function_value,
|
| 1090 |
+
"CAS.No": cas_value,
|
| 1091 |
+
"SOURCE": source_value,
|
| 1092 |
"Contents(w/w %)": contents_value,
|
| 1093 |
"In formula(w/w %)": formula_value,
|
| 1094 |
"amount_float": to_number(row.get(formula_col)) if formula_col and formula_col in row else None,
|
|
|
|
| 1684 |
# 10. ์ถ๋ ฅ ๋ฐ์ดํฐ ๊ตฌ์ฑ
|
| 1685 |
# ============================================================
|
| 1686 |
|
| 1687 |
+
def make_formula_statement_rows(row_items, country_cols):
|
| 1688 |
rows = []
|
| 1689 |
for item in row_items:
|
| 1690 |
material = item["material"]
|
| 1691 |
comp = item["component"]
|
| 1692 |
+
row_data = item["row_data"]
|
| 1693 |
+
|
| 1694 |
+
row = {
|
| 1695 |
"NO.": material.get("NO", ""),
|
| 1696 |
"ํ๊ธ ํ์คํ ๋ช
์นญ": comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
| 1697 |
"INGREDIENT NAME (INCI)": comp.get("INCI Name", ""),
|
| 1698 |
+
"FUNCTION": comp.get("FUNCTION", ""),
|
| 1699 |
+
"CAS.No": comp.get("CAS.No", ""),
|
| 1700 |
+
"SOURCE": comp.get("SOURCE", "ICID") or "ICID",
|
| 1701 |
"%(w/w)": comp.get("In formula(w/w %)", ""),
|
| 1702 |
+
}
|
| 1703 |
+
|
| 1704 |
+
for country_col in country_cols:
|
| 1705 |
+
row[country_col] = make_country_cell_status(row_data.get(country_col, ""))
|
| 1706 |
+
|
| 1707 |
+
rows.append(row)
|
| 1708 |
+
|
| 1709 |
+
total = 0.0
|
| 1710 |
+
for row in rows:
|
| 1711 |
+
value = to_number(row.get("%(w/w)", ""))
|
| 1712 |
+
if value is not None:
|
| 1713 |
+
total += value
|
| 1714 |
+
|
| 1715 |
+
total_row = {
|
| 1716 |
+
"NO.": "",
|
| 1717 |
+
"ํ๊ธ ํ์คํ ๋ช
์นญ": "",
|
| 1718 |
+
"INGREDIENT NAME (INCI)": "TOTAL",
|
| 1719 |
+
"FUNCTION": "",
|
| 1720 |
+
"CAS.No": "",
|
| 1721 |
+
"SOURCE": "",
|
| 1722 |
+
"%(w/w)": format_amount(total),
|
| 1723 |
+
}
|
| 1724 |
+
for country_col in country_cols:
|
| 1725 |
+
total_row[country_col] = ""
|
| 1726 |
+
|
| 1727 |
+
rows.append(total_row)
|
| 1728 |
return pd.DataFrame(rows)
|
| 1729 |
|
| 1730 |
|
|
|
|
| 1758 |
|
| 1759 |
def write_output_workbook(out_path, product_name, formula_df, regulatory_df, raw_df):
|
| 1760 |
with pd.ExcelWriter(out_path, engine="openpyxl") as writer:
|
| 1761 |
+
formula_df.to_excel(writer, index=False, sheet_name="Formula Ingredients Statement", startrow=5)
|
| 1762 |
regulatory_df.to_excel(writer, index=False, sheet_name="Regulatory Screening Summary")
|
| 1763 |
+
raw_df.to_excel(writer, index=False, sheet_name="Evidence Log")
|
| 1764 |
|
| 1765 |
wb = load_workbook(out_path)
|
| 1766 |
ws = wb["Formula Ingredients Statement"]
|
| 1767 |
+
max_col = max(1, ws.max_column)
|
| 1768 |
+
|
| 1769 |
+
ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=max_col)
|
| 1770 |
+
ws["A1"] = "Formular Ingredients Statement"
|
| 1771 |
+
ws["A1"].font = Font(bold=True, size=22, underline="single")
|
| 1772 |
+
ws["A1"].alignment = Alignment(horizontal="center", vertical="center")
|
| 1773 |
|
| 1774 |
+
ws.merge_cells(start_row=4, start_column=1, end_row=4, end_column=max_col)
|
| 1775 |
+
ws["A4"] = f"PRODUCT NAME : {safe_str(product_name)}"
|
| 1776 |
+
ws["A4"].font = Font(bold=True, size=13)
|
| 1777 |
+
ws["A4"].alignment = Alignment(horizontal="left", vertical="center")
|
| 1778 |
+
|
| 1779 |
+
wb.save(out_path)
|
| 1780 |
style_output_workbook(out_path)
|
| 1781 |
|
| 1782 |
|
| 1783 |
def style_output_workbook(path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1784 |
wb = load_workbook(path)
|
| 1785 |
|
| 1786 |
thin_border = Border(
|
| 1787 |
+
left=Side(style="thin", color="808080"),
|
| 1788 |
+
right=Side(style="thin", color="808080"),
|
| 1789 |
+
top=Side(style="thin", color="808080"),
|
| 1790 |
+
bottom=Side(style="thin", color="808080"),
|
| 1791 |
)
|
| 1792 |
|
| 1793 |
+
header_fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")
|
| 1794 |
issue_fill = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")
|
| 1795 |
+
fail_fill = PatternFill(start_color="F4CCCC", end_color="F4CCCC", fill_type="solid")
|
| 1796 |
+
pass_fill = PatternFill(start_color="D9EAD3", end_color="D9EAD3", fill_type="solid")
|
| 1797 |
+
label_fill = PatternFill(start_color="D9EAF7", end_color="D9EAF7", fill_type="solid")
|
| 1798 |
|
| 1799 |
+
header_font = Font(color="000000", bold=True, size=10)
|
| 1800 |
+
body_font = Font(color="000000", size=10)
|
| 1801 |
+
title_font = Font(color="000000", bold=True, size=22, underline="single")
|
| 1802 |
+
product_font = Font(color="000000", bold=True, size=13)
|
| 1803 |
|
| 1804 |
for ws in wb.worksheets:
|
| 1805 |
+
if ws.title == "Formula Ingredients Statement":
|
| 1806 |
+
header_row = 6
|
| 1807 |
+
ws.sheet_view.showGridLines = False
|
| 1808 |
+
|
| 1809 |
+
ws.row_dimensions[1].height = 32
|
| 1810 |
+
ws.row_dimensions[2].height = 20
|
| 1811 |
+
ws.row_dimensions[3].height = 20
|
| 1812 |
+
ws.row_dimensions[4].height = 24
|
| 1813 |
+
ws.row_dimensions[5].height = 18
|
| 1814 |
+
ws.row_dimensions[6].height = 26
|
| 1815 |
+
|
| 1816 |
+
ws["A1"].font = title_font
|
| 1817 |
+
ws["A1"].alignment = Alignment(horizontal="center", vertical="center")
|
| 1818 |
+
ws["A4"].font = product_font
|
| 1819 |
+
ws["A4"].alignment = Alignment(horizontal="left", vertical="center")
|
| 1820 |
+
|
| 1821 |
+
for row in range(header_row, ws.max_row + 1):
|
| 1822 |
+
for col in range(1, ws.max_column + 1):
|
| 1823 |
+
cell = ws.cell(row=row, column=col)
|
| 1824 |
+
cell.border = thin_border
|
| 1825 |
+
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
| 1826 |
+
cell.font = body_font
|
| 1827 |
+
|
| 1828 |
+
if row == header_row:
|
| 1829 |
+
cell.fill = header_fill
|
| 1830 |
+
cell.font = header_font
|
| 1831 |
+
|
| 1832 |
+
if row > header_row:
|
| 1833 |
+
value = safe_str(cell.value)
|
| 1834 |
+
if value == "FAIL":
|
| 1835 |
+
cell.fill = fail_fill
|
| 1836 |
+
cell.font = Font(color="9C0006", bold=True)
|
| 1837 |
+
elif value == "CHECK":
|
| 1838 |
+
cell.fill = issue_fill
|
| 1839 |
+
cell.font = Font(color="9C6500", bold=True)
|
| 1840 |
+
elif value == "LABEL":
|
| 1841 |
+
cell.fill = label_fill
|
| 1842 |
+
cell.font = Font(color="0B5394", bold=True)
|
| 1843 |
+
elif value == "PASS":
|
| 1844 |
+
cell.fill = pass_fill
|
| 1845 |
+
cell.font = Font(color="38761D", bold=True)
|
| 1846 |
+
|
| 1847 |
+
total_row = None
|
| 1848 |
+
for row in range(header_row + 1, ws.max_row + 1):
|
| 1849 |
+
if safe_str(ws.cell(row=row, column=3).value).upper() == "TOTAL":
|
| 1850 |
+
total_row = row
|
| 1851 |
+
break
|
| 1852 |
+
|
| 1853 |
+
if total_row:
|
| 1854 |
+
for col in range(1, ws.max_column + 1):
|
| 1855 |
+
cell = ws.cell(row=total_row, column=col)
|
| 1856 |
+
cell.font = Font(bold=True)
|
| 1857 |
+
cell.border = thin_border
|
| 1858 |
+
cell.alignment = Alignment(horizontal="center", vertical="center")
|
| 1859 |
+
try:
|
| 1860 |
+
ws.merge_cells(start_row=total_row, start_column=2, end_row=total_row, end_column=6)
|
| 1861 |
+
ws.cell(row=total_row, column=2).value = "TOTAL"
|
| 1862 |
+
ws.cell(row=total_row, column=2).font = Font(bold=True)
|
| 1863 |
+
ws.cell(row=total_row, column=2).alignment = Alignment(horizontal="center", vertical="center")
|
| 1864 |
+
ws.cell(row=total_row, column=3).value = None
|
| 1865 |
+
except Exception:
|
| 1866 |
+
pass
|
| 1867 |
+
|
| 1868 |
+
widths = {
|
| 1869 |
+
1: 6,
|
| 1870 |
+
2: 28,
|
| 1871 |
+
3: 34,
|
| 1872 |
+
4: 24,
|
| 1873 |
+
5: 16,
|
| 1874 |
+
6: 10,
|
| 1875 |
+
7: 12,
|
| 1876 |
+
}
|
| 1877 |
+
for col_idx in range(1, ws.max_column + 1):
|
| 1878 |
+
col_letter = get_column_letter(col_idx)
|
| 1879 |
+
ws.column_dimensions[col_letter].width = widths.get(col_idx, 10)
|
| 1880 |
+
|
| 1881 |
+
for row in range(header_row + 1, ws.max_row + 1):
|
| 1882 |
+
cell = ws.cell(row=row, column=7)
|
| 1883 |
+
if to_number(cell.value) is not None:
|
| 1884 |
+
cell.number_format = "0.000######"
|
| 1885 |
+
|
| 1886 |
+
ws.freeze_panes = "A7"
|
| 1887 |
+
|
| 1888 |
+
else:
|
| 1889 |
+
header_row = 1
|
| 1890 |
+
ws.sheet_view.showGridLines = False
|
| 1891 |
+
|
| 1892 |
+
for row in range(1, ws.max_row + 1):
|
| 1893 |
+
for col in range(1, ws.max_column + 1):
|
| 1894 |
+
cell = ws.cell(row=row, column=col)
|
| 1895 |
+
cell.border = thin_border
|
| 1896 |
+
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
| 1897 |
+
|
| 1898 |
+
if row == header_row:
|
| 1899 |
+
cell.fill = PatternFill(start_color="D9D9D9", end_color="D9D9D9", fill_type="solid")
|
| 1900 |
+
cell.font = header_font
|
| 1901 |
+
|
| 1902 |
+
for col_idx in range(1, ws.max_column + 1):
|
| 1903 |
+
col_letter = get_column_letter(col_idx)
|
| 1904 |
+
header = safe_str(ws.cell(row=header_row, column=col_idx).value)
|
| 1905 |
+
if header in ["Evidence", "๊ฒ์ ์๋ฌธ", "Evidence Text"]:
|
| 1906 |
+
ws.column_dimensions[col_letter].width = 70
|
| 1907 |
+
elif header in ["Result", "Required Action", "๋น๊ณ "]:
|
| 1908 |
+
ws.column_dimensions[col_letter].width = 45
|
| 1909 |
+
elif header in ["INCI Name", "Ingredient"]:
|
| 1910 |
+
ws.column_dimensions[col_letter].width = 32
|
| 1911 |
+
else:
|
| 1912 |
+
ws.column_dimensions[col_letter].width = 18
|
| 1913 |
+
|
| 1914 |
+
ws.freeze_panes = "A2"
|
| 1915 |
|
| 1916 |
wb.save(path)
|
| 1917 |
|
|
|
|
| 2006 |
row_data["Result"] = build_integrated_result(row_data, country_cols)
|
| 2007 |
row_data["๋น๊ณ "] = " / ".join(item["notes"]) if item["notes"] else "-"
|
| 2008 |
|
| 2009 |
+
formula_df = make_formula_statement_rows(row_items, country_cols)
|
| 2010 |
regulatory_df = make_regulatory_summary_rows(row_items, country_cols)
|
| 2011 |
|
| 2012 |
raw_columns = ["target_id", "๊ตญ๊ฐ/์ฑ๋", "์ฑ๋ถ๋ช
", "INCI Name", "ํ์ ", "์ถ์ฒ", "๊ฒ์ ์๋ฌธ"]
|
|
|
|
| 2037 |
f"- ์ฌ๊ฒํ ํ์ ์ฑ๋ถ: {issue_count}๊ฐ\n"
|
| 2038 |
f"- MFDS ์์ธ DB ํ ์: {mfds_count}๊ฐ\n"
|
| 2039 |
"- ๊ฒฐ๊ณผ ํ์ผ์ 3๊ฐ ์ํธ๋ก ์ ์ฅํ์ต๋๋ค.\n"
|
| 2040 |
+
" 1) Formula Ingredients Statement: ์๋ณธ ์์ ์ ์ฑ๋ถํ + ๊ตญ๊ฐ๋ณ ์งง์ ํ์ \n"
|
| 2041 |
" 2) Regulatory Screening Summary: ๋ฌธ์ ์๋ ์ฑ๋ถ๋ง ๊ตญ๊ฐ๋ณ ์์ฝ\n"
|
| 2042 |
" 3) Raw Search Log: ๋ก์ปฌ ๊ท์น/MFDS/GPT ๊ฒ์ ๊ทผ๊ฑฐ ๋ก๊ทธ\n"
|
| 2043 |
"- ํ์ ์ฐ์ ์์: ๋ก์ปฌ ๊ณ ์ ๊ท์น โ MFDS ์์ธ DB โ ๊ตญ๊ฐ๋ณ FAISS/GPT\n"
|