jin2li commited on
Commit
c81ad85
ยท
verified ยท
1 Parent(s): 49f8842

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +193 -33
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
- global RAW_SEARCH_LOGS; RAW_SEARCH_LOGS=[]
 
 
 
 
 
 
 
 
 
 
 
1442
  try:
1443
- if not file: return None,None,"โŒ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”."
1444
- if not countries: return None,None,"โŒ ์Šคํฌ๋ฆฌ๋‹ ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
1445
- df=load_input_file_to_dataframe(file); materials,meta=make_grouped_materials(df)
1446
- if not materials: return None,None,"โŒ ํŒŒ์ผ์—์„œ ์ „์„ฑ๋ถ„ ์ •๋ณด๋ฅผ ์ฝ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค."
1447
- llm=ChatOpenAI(model="gpt-4o-mini",temperature=0); row_items=[]
 
 
 
 
 
 
 
 
 
1448
  for mat in materials:
1449
- comp=mat["Components"][0]; rd={"ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…":comp.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…",""),"INCI Name":comp.get("INCI Name",""),"In formula\n(w/w %)":comp.get("In formula(w/w %)",""),"Result":""}
1450
- row_items.append({"target_id":f"row_{len(row_items)}","material":mat,"component":comp,"row_data":rd,"notes":[]})
1451
- terms=extract_terms_for_live_lookup(row_items); mfds_df=pd.DataFrame(); mfds_msg="์‚ฌ์šฉ ์•ˆ ํ•จ"
1452
- if use_mfds_live: mfds_df,mfds_msg=mfds_live_lookup_terms(terms,live_max_terms,True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1453
  for country in countries:
1454
- col=display_country(country); gpt=[]
1455
- for item in row_items:
1456
- res=evaluate_critical_country_rule(country,item["material"],item["component"]) or ( {"๊ฒฐ๊ณผ":NO_EVIDENCE_TEXT,"ํ‘œ์‹œ๋ฌธ๊ตฌ":"Pass","๋น„๊ณ ":"์ •์ œ์ˆ˜/Water/Aqua ๊ธฐ๋ณธ Pass ์ฒ˜๋ฆฌ"} if is_always_pass_component(item["material"],item["component"]) else None )
1457
- if res is None: res=evaluate_plain_fragrance_rule(country,item["material"],item["component"])
1458
- if res is None: res=evaluate_live_detail_rule(country,item["material"],item["component"],mfds_df) if use_mfds_live and not mfds_df.empty else None
1459
- if res is None: res=evaluate_local_rules(country,item["material"],item["component"],meta["ingredient_totals"],meta["ingredient_amount_parts"])
1460
- if res is None: gpt.append({"target_id":item["target_id"],"material":item["material"],"component":item["component"]}); continue
1461
- item["row_data"][col]=res.get("๊ฒฐ๊ณผ",""); item["row_data"][col+"_display"]=res.get("ํ‘œ์‹œ๋ฌธ๊ตฌ","Pass")
1462
- note=safe_str(res.get("๋น„๊ณ ","-"));
1463
- if note and note!="-": item["notes"].append(f"{col}: {note}")
1464
- results=evaluate_components_with_llm_batch(country,gpt,llm)
 
 
 
 
 
1465
  for item in row_items:
1466
- if col in item["row_data"]: continue
1467
- res=results.get(item["target_id"],{"๊ฒฐ๊ณผ":"ํ™•์ธ ํ•„์š”","ํ‘œ์‹œ๋ฌธ๊ตฌ":NEED_CHECK_TEXT,"๋น„๊ณ ":"๊ฒฐ๊ณผ ๋ˆ„๋ฝ"})
1468
- item["row_data"][col]=res.get("๊ฒฐ๊ณผ",""); item["row_data"][col+"_display"]=res.get("ํ‘œ์‹œ๋ฌธ๊ตฌ","Pass")
1469
- note=safe_str(res.get("๋น„๊ณ ","-"));
1470
- if note and note!="-": item["notes"].append(f"{col}: {note}")
1471
- country_cols=[display_country(c) for c in countries]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1472
  for item in row_items:
1473
- item["row_data"]["Result"]=build_integrated_result(item["row_data"],country_cols); item["row_data"]["๋น„๊ณ "]=" / ".join(item["notes"]) if item["notes"] else "-"
1474
- formula_df=make_formula_statement_rows(row_items,country_cols); regulatory_df=make_regulatory_summary_rows(row_items,country_cols); raw_df=pd.DataFrame(RAW_SEARCH_LOGS) if RAW_SEARCH_LOGS else pd.DataFrame(columns=["target_id","๊ตญ๊ฐ€/์ฑ„๋„","์„ฑ๋ถ„๋ช…","INCI Name","ํŒ์ •","์ถœ์ฒ˜","๊ฒ€์ƒ‰ ์›๋ฌธ"])
1475
- out=str(OUTPUT_DIR/f"screening_result_{safe_filename(product_name) if product_name else 'product'}_{safe_filename('_'.join(countries))}.xlsx"); write_output_workbook(out,product_name,formula_df,regulatory_df,raw_df)
1476
- msg=f"โœ… ๋ถ„์„ ์™„๋ฃŒ\n- Product Name: {safe_str(product_name) or '-'}\n- ์ „์„ฑ๋ถ„ ํ–‰ ์ˆ˜: {len(formula_df)}๊ฐœ\n- In formula ํ•ฉ๊ณ„: {format_amount(sum_formula_percent(formula_df))}%\n- ์žฌ๊ฒ€ํ†  ํ•„์š” ์„ฑ๋ถ„: {len(regulatory_df)}๊ฐœ\n- MFDS ์ƒํƒœ: {mfds_msg}\n- ์ž…๋ ฅ ํŒŒ์ผ ํ˜•์‹: ์—‘์…€ / PDF / ์ด๋ฏธ์ง€ ์ง€์›\n- ๋ฐฐํ•ฉํ•œ๋„ ๊ฒ€์ฆ: ์›๋ฌธ ๊ทผ๊ฑฐ ์—†๋Š” ์ž๋™ ํ•œ๋„ ํ‘œ์‹œ ์ฐจ๋‹จ\n"
1477
- return formula_df,out,msg
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1478
  except Exception as e:
1479
- tb=traceback.format_exc(); print(tb); return None,None,f"โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}\n\n{tb[-3000:]}"
 
 
 
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):