Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1438,45 +1438,205 @@ def build_integrated_result(rd,country_cols):
|
|
| 1438 |
return "PASS" if not issues else "์ฌ๊ฒํ ํ์: "+", ".join(issues)
|
| 1439 |
|
| 1440 |
def process(countries,file,product_name="",use_mfds_live=False,live_max_terms=30,live_headless=True):
|
| 1441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1442 |
try:
|
| 1443 |
-
if not file:
|
| 1444 |
-
|
| 1445 |
-
|
| 1446 |
-
if not
|
| 1447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1448 |
for mat in materials:
|
| 1449 |
-
comp=mat["Components"][0]
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1453 |
for country in countries:
|
| 1454 |
-
|
| 1455 |
-
|
| 1456 |
-
|
| 1457 |
-
|
| 1458 |
-
|
| 1459 |
-
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
|
| 1464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1465 |
for item in row_items:
|
| 1466 |
-
|
| 1467 |
-
|
| 1468 |
-
|
| 1469 |
-
|
| 1470 |
-
|
| 1471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1472 |
for item in row_items:
|
| 1473 |
-
item["row_data"]["Result"]=build_integrated_result(item["row_data"],country_cols)
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1478 |
except Exception as e:
|
| 1479 |
-
tb=traceback.format_exc()
|
|
|
|
|
|
|
|
|
|
| 1480 |
|
| 1481 |
|
| 1482 |
def extract_single_lookup_terms(query, material, component):
|
|
|
|
| 1438 |
return "PASS" if not issues else "์ฌ๊ฒํ ํ์: "+", ".join(issues)
|
| 1439 |
|
| 1440 |
def process(countries,file,product_name="",use_mfds_live=False,live_max_terms=30,live_headless=True):
|
| 1441 |
+
"""
|
| 1442 |
+
์ ์ฑ๋ถ ์ ์ฒด ์คํฌ๋ฆฌ๋ ๊ณ ์ ๋ฒ์ .
|
| 1443 |
+
|
| 1444 |
+
๋ณ๊ฒฝ ๋ด์ฉ:
|
| 1445 |
+
- ์ ์ฒด ์คํฌ๋ฆฌ๋์์ GPT/LLM ํธ์ถ ์ ๊ฑฐ
|
| 1446 |
+
- ๊ตญ๊ฐ๋ณ ํ์ผ ์ง์ ๊ฒ์(COUNTRY_FILE_FAST) ์ฐ์
|
| 1447 |
+
- ์ ํ ๊ตญ๊ฐ๋ณ ์น์
์ธ๋ฑ์ค 1ํ ์๋ฐ์
|
| 1448 |
+
- ์์ฝ์ฒ ์ค์๊ฐ ์กฐํ๋ ์ฒดํฌ ON์ผ ๋๋ง ์ฌ์ฉ
|
| 1449 |
+
"""
|
| 1450 |
+
global RAW_SEARCH_LOGS
|
| 1451 |
+
RAW_SEARCH_LOGS = []
|
| 1452 |
+
|
| 1453 |
try:
|
| 1454 |
+
if not file:
|
| 1455 |
+
return None, None, "โ ํ์ผ์ ์
๋ก๋ํด์ฃผ์ธ์."
|
| 1456 |
+
|
| 1457 |
+
if not countries:
|
| 1458 |
+
return None, None, "โ ์คํฌ๋ฆฌ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํด์ฃผ์ธ์."
|
| 1459 |
+
|
| 1460 |
+
df = load_input_file_to_dataframe(file)
|
| 1461 |
+
materials, meta = make_grouped_materials(df)
|
| 1462 |
+
|
| 1463 |
+
if not materials:
|
| 1464 |
+
return None, None, "โ ํ์ผ์์ ์ ์ฑ๋ถ ์ ๋ณด๋ฅผ ์ฝ์ง ๋ชปํ์ต๋๋ค."
|
| 1465 |
+
|
| 1466 |
+
row_items = []
|
| 1467 |
+
|
| 1468 |
for mat in materials:
|
| 1469 |
+
comp = mat["Components"][0]
|
| 1470 |
+
rd = {
|
| 1471 |
+
"ํ๊ธ ์ฑ๋ถ๋ช
": comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", ""),
|
| 1472 |
+
"INCI Name": comp.get("INCI Name", ""),
|
| 1473 |
+
"In formula\n(w/w %)": comp.get("In formula(w/w %)", ""),
|
| 1474 |
+
"Result": "",
|
| 1475 |
+
}
|
| 1476 |
+
|
| 1477 |
+
row_items.append({
|
| 1478 |
+
"target_id": f"row_{len(row_items)}",
|
| 1479 |
+
"material": mat,
|
| 1480 |
+
"component": comp,
|
| 1481 |
+
"row_data": rd,
|
| 1482 |
+
"notes": [],
|
| 1483 |
+
})
|
| 1484 |
+
|
| 1485 |
+
index_messages = []
|
| 1486 |
+
|
| 1487 |
for country in countries:
|
| 1488 |
+
try:
|
| 1489 |
+
sections, index_status = get_country_section_index_cached(country)
|
| 1490 |
+
index_messages.append(f"{display_country(country)}={index_status}/{len(sections)} sections")
|
| 1491 |
+
except Exception as e:
|
| 1492 |
+
index_messages.append(f"{display_country(country)}=index error: {e}")
|
| 1493 |
+
|
| 1494 |
+
terms = extract_terms_for_live_lookup(row_items)
|
| 1495 |
+
mfds_df = pd.DataFrame()
|
| 1496 |
+
mfds_msg = "์ฌ์ฉ ์ ํจ"
|
| 1497 |
+
|
| 1498 |
+
if use_mfds_live:
|
| 1499 |
+
mfds_df, mfds_msg = mfds_live_lookup_terms(terms, live_max_terms, True)
|
| 1500 |
+
|
| 1501 |
+
for country in countries:
|
| 1502 |
+
col = display_country(country)
|
| 1503 |
+
|
| 1504 |
for item in row_items:
|
| 1505 |
+
mat = item["material"]
|
| 1506 |
+
comp = item["component"]
|
| 1507 |
+
final_result = None
|
| 1508 |
+
source = ""
|
| 1509 |
+
|
| 1510 |
+
final_result = evaluate_critical_country_rule(
|
| 1511 |
+
country=country,
|
| 1512 |
+
material=mat,
|
| 1513 |
+
component=comp,
|
| 1514 |
+
)
|
| 1515 |
+
|
| 1516 |
+
if final_result is not None:
|
| 1517 |
+
source = "CRITICAL_COUNTRY_RULE"
|
| 1518 |
+
|
| 1519 |
+
if final_result is None and is_always_pass_component(mat, comp):
|
| 1520 |
+
final_result = {
|
| 1521 |
+
"๋ํ INCI": safe_str(comp.get("INCI Name", "")) or "Water",
|
| 1522 |
+
"๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
|
| 1523 |
+
"ํ์๋ฌธ๊ตฌ": "Pass",
|
| 1524 |
+
"๋น๊ณ ": "์ ์ ์/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ๋ฆฌ",
|
| 1525 |
+
}
|
| 1526 |
+
source = "ALWAYS_PASS_RULE"
|
| 1527 |
+
|
| 1528 |
+
if final_result is None:
|
| 1529 |
+
fragrance_result = evaluate_plain_fragrance_rule(
|
| 1530 |
+
country=country,
|
| 1531 |
+
material=mat,
|
| 1532 |
+
component=comp,
|
| 1533 |
+
)
|
| 1534 |
+
|
| 1535 |
+
if fragrance_result is not None:
|
| 1536 |
+
final_result = fragrance_result
|
| 1537 |
+
source = "FRAGRANCE_RULE"
|
| 1538 |
+
|
| 1539 |
+
if final_result is None and use_mfds_live and mfds_df is not None and not mfds_df.empty:
|
| 1540 |
+
mfds_result = evaluate_live_detail_rule(
|
| 1541 |
+
country=country,
|
| 1542 |
+
material=mat,
|
| 1543 |
+
component=comp,
|
| 1544 |
+
live_df=mfds_df,
|
| 1545 |
+
)
|
| 1546 |
+
|
| 1547 |
+
if mfds_result is not None:
|
| 1548 |
+
final_result = mfds_result
|
| 1549 |
+
source = "MFDS_LIVE"
|
| 1550 |
+
|
| 1551 |
+
if final_result is None:
|
| 1552 |
+
file_result = evaluate_component_from_country_files_only(
|
| 1553 |
+
country=country,
|
| 1554 |
+
material=mat,
|
| 1555 |
+
component=comp,
|
| 1556 |
+
target_id=item["target_id"],
|
| 1557 |
+
)
|
| 1558 |
+
|
| 1559 |
+
if file_result is not None:
|
| 1560 |
+
final_result = file_result
|
| 1561 |
+
source = "COUNTRY_FILE_FAST"
|
| 1562 |
+
|
| 1563 |
+
if final_result is None:
|
| 1564 |
+
local_result = evaluate_local_rules(
|
| 1565 |
+
country=country,
|
| 1566 |
+
material=mat,
|
| 1567 |
+
component=comp,
|
| 1568 |
+
ingredient_totals=meta["ingredient_totals"],
|
| 1569 |
+
ingredient_amount_parts=meta["ingredient_amount_parts"],
|
| 1570 |
+
)
|
| 1571 |
+
|
| 1572 |
+
if local_result is not None:
|
| 1573 |
+
final_result = local_result
|
| 1574 |
+
source = "LOCAL_RULE"
|
| 1575 |
+
|
| 1576 |
+
if final_result is None:
|
| 1577 |
+
final_result = {
|
| 1578 |
+
"๋ํ INCI": safe_str(comp.get("INCI Name", "")) or safe_str(comp.get("ํ๊ธ ์ฑ๋ถ๋ช
", "")) or "-",
|
| 1579 |
+
"๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
|
| 1580 |
+
"ํ์๋ฌธ๊ตฌ": "Pass",
|
| 1581 |
+
"๋น๊ณ ": "๊ตญ๊ฐ๋ณ ํ์ผ ๋ด ํ์ฌ ์ฑ๋ถ ์ง์ ๊ทผ๊ฑฐ ์์",
|
| 1582 |
+
}
|
| 1583 |
+
source = "NO_EVIDENCE"
|
| 1584 |
+
|
| 1585 |
+
item["row_data"][col] = final_result.get("๊ฒฐ๊ณผ", "")
|
| 1586 |
+
item["row_data"][col + "_display"] = final_result.get("ํ์๋ฌธ๊ตฌ", "Pass")
|
| 1587 |
+
|
| 1588 |
+
note = safe_str(final_result.get("๋น๊ณ ", "-"))
|
| 1589 |
+
|
| 1590 |
+
if note and note != "-":
|
| 1591 |
+
item["notes"].append(f"{col}: {note}")
|
| 1592 |
+
|
| 1593 |
+
log_search_context(
|
| 1594 |
+
item["target_id"],
|
| 1595 |
+
country,
|
| 1596 |
+
comp,
|
| 1597 |
+
final_result.get("๊ฒฐ๊ณผ", ""),
|
| 1598 |
+
source,
|
| 1599 |
+
note,
|
| 1600 |
+
)
|
| 1601 |
+
|
| 1602 |
+
country_cols = [display_country(c) for c in countries]
|
| 1603 |
+
|
| 1604 |
for item in row_items:
|
| 1605 |
+
item["row_data"]["Result"] = build_integrated_result(item["row_data"], country_cols)
|
| 1606 |
+
item["row_data"]["๋น๊ณ "] = " / ".join(item["notes"]) if item["notes"] else "-"
|
| 1607 |
+
|
| 1608 |
+
formula_df = make_formula_statement_rows(row_items, country_cols)
|
| 1609 |
+
regulatory_df = make_regulatory_summary_rows(row_items, country_cols)
|
| 1610 |
+
raw_df = pd.DataFrame(RAW_SEARCH_LOGS) if RAW_SEARCH_LOGS else pd.DataFrame(
|
| 1611 |
+
columns=["target_id", "๊ตญ๊ฐ/์ฑ๋", "์ฑ๋ถ๋ช
", "INCI Name", "ํ์ ", "์ถ์ฒ", "๊ฒ์ ์๋ฌธ"]
|
| 1612 |
+
)
|
| 1613 |
+
|
| 1614 |
+
out = str(
|
| 1615 |
+
OUTPUT_DIR /
|
| 1616 |
+
f"screening_result_{safe_filename(product_name) if product_name else 'product'}_{safe_filename('_'.join(countries))}.xlsx"
|
| 1617 |
+
)
|
| 1618 |
+
|
| 1619 |
+
write_output_workbook(out, product_name, formula_df, regulatory_df, raw_df)
|
| 1620 |
+
|
| 1621 |
+
msg = (
|
| 1622 |
+
"โ
๋ถ์ ์๋ฃ\n"
|
| 1623 |
+
f"- Product Name: {safe_str(product_name) or '-'}\n"
|
| 1624 |
+
f"- ์ ์ฑ๋ถ ํ ์: {len(formula_df)}๊ฐ\n"
|
| 1625 |
+
f"- In formula ํฉ๊ณ: {format_amount(sum_formula_percent(formula_df))}%\n"
|
| 1626 |
+
f"- ์ฌ๊ฒํ ํ์ ์ฑ๋ถ: {len(regulatory_df)}๊ฐ\n"
|
| 1627 |
+
f"- MFDS ์ํ: {mfds_msg}\n"
|
| 1628 |
+
"- ํ์ ๋ฐฉ์: GPT ํ์ ์์ / ๊ตญ๊ฐ๋ณ ํ์ผ ์ง์ ๊ฒ์ / COUNTRY_FILE_FAST\n"
|
| 1629 |
+
f"- ๊ตญ๊ฐ๋ณ ์ธ๋ฑ์ค ์ํ: {' | '.join(index_messages)}\n"
|
| 1630 |
+
"- ์๋ ๊ฐ์ : ์ ์ฒด ์คํฌ๋ฆฌ๋์์ GPT/LLM ํธ์ถ ์ ๊ฑฐ\n"
|
| 1631 |
+
)
|
| 1632 |
+
|
| 1633 |
+
return formula_df, out, msg
|
| 1634 |
+
|
| 1635 |
except Exception as e:
|
| 1636 |
+
tb = traceback.format_exc()
|
| 1637 |
+
print(tb)
|
| 1638 |
+
return None, None, f"โ ์ค๋ฅ ๋ฐ์: {e}\n\n{tb[-3000:]}"
|
| 1639 |
+
|
| 1640 |
|
| 1641 |
|
| 1642 |
def extract_single_lookup_terms(query, material, component):
|