Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -279,6 +279,131 @@ def normalize_formula_country_text(text,country_col="",inci_name="",amount_text=
|
|
| 279 |
if any(k in text for k in ["๋ฐฐํฉํ๋","๋ฐฐํฉ ํ๋","ํ๋","์ ํ"]) or any(k in lower for k in ["maximum","limit","restricted"]): return LIMIT_CHECK_TEXT
|
| 280 |
return text[:120]
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
# input extraction
|
| 283 |
STANDARD_INPUT_COLUMNS=["NO.","ํ๊ธ ํ์คํ ๋ช
์นญ","INGREDIENT NAME (INCI)","FUNCTION","CAS.No","SOURCE","%(w/w)"]
|
| 284 |
|
|
@@ -355,12 +480,26 @@ def extract_formula_table_from_pdf(path):
|
|
| 355 |
raise ValueError("PDF์์ ์ ์ฑ๋ถํ๋ฅผ ์ถ์ถํ์ง ๋ชปํ์ต๋๋ค.")
|
| 356 |
|
| 357 |
def load_input_file_to_dataframe(file):
|
| 358 |
-
if not file:
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
def preview_input_file(file):
|
| 366 |
try: return load_input_file_to_dataframe(file) if file else None
|
|
@@ -508,7 +647,10 @@ def clean_input_df(df,cols):
|
|
| 508 |
vals=[safe_str(v) for v in row.values]; txt=" ".join(v for v in vals if v).strip(); low=txt.lower()
|
| 509 |
if not txt: continue
|
| 510 |
no=safe_str(row.get(cols["no_col"],"")).lower(); ko=safe_str(row.get(cols["korean_ing_col"],"")).lower(); inci=safe_str(row.get(cols["inci_col"],"")).lower()
|
| 511 |
-
if no in ["no","no.","๋ฒํธ","์๋ฒ"] or ko in ["ํ๊ธ ํ์คํ ๋ช
์นญ","ํ๊ธ ์ฑ๋ถ๋ช
","๊ตญ๋ฌธ ์ฑ๋ถ๋ช
"] or inci in ["ingredient name (inci)","inci","inci name"]: continue
|
|
|
|
|
|
|
|
|
|
| 512 |
if is_total_like_text(no) or is_total_like_text(ko) or is_total_like_text(inci) or row_has_total_like(vals): footer=True; continue
|
| 513 |
if any(m in low for m in ["commitment of the manufacturer","rebom co","tel :","fax :","lab no."]): footer=True; continue
|
| 514 |
if footer: continue
|
|
@@ -2788,6 +2930,7 @@ def process_single_ingredient_with_auth(user, countries, single_query, single_am
|
|
| 2788 |
# - ์๋ USER_DB์ ๋ฉ์ผ/๋น๋ฐ๋ฒํธ๋ฅผ ์ง์ ๋ฑ๋ก
|
| 2789 |
|
| 2790 |
|
|
|
|
| 2791 |
# ============================================================
|
| 2792 |
# ๋ก๊ทธ์ธ ์ธ์ฆ
|
| 2793 |
# - Gradio ๊ธฐ๋ณธ ๋ก๊ทธ์ธ์ฐฝ ์ฌ์ฉ
|
|
|
|
| 279 |
if any(k in text for k in ["๋ฐฐํฉํ๋","๋ฐฐํฉ ํ๋","ํ๋","์ ํ"]) or any(k in lower for k in ["maximum","limit","restricted"]): return LIMIT_CHECK_TEXT
|
| 280 |
return text[:120]
|
| 281 |
|
| 282 |
+
|
| 283 |
+
def standardize_formula_dataframe(df):
|
| 284 |
+
"""
|
| 285 |
+
์์
/PDF/์ด๋ฏธ์ง ์ถ์ถ ๊ฒฐ๊ณผ์์ ์ค์ ์ ์ฑ๋ถ ํค๋๊ฐ ์ค๊ฐ์ ์๋ ๊ฒฝ์ฐ๋ฅผ ๋ณด์ ํ๋ค.
|
| 286 |
+
|
| 287 |
+
๋ฐฉ์งํ๋ ๋ฌธ์ :
|
| 288 |
+
- PRODUCT NAME ํ์ด ์ฑ๋ถ์ฒ๋ผ ๋ค์ด๊ฐ
|
| 289 |
+
- 'INGREDIENT NAME', 'NO.', 'FUNCTION' ๊ฐ์ ๋ฐ๋ณต ํค๋๊ฐ ์ฑ๋ถ์ฒ๋ผ ๋ค์ด๊ฐ
|
| 290 |
+
- ์ค์ ํ ์์ ์ ์ ๋ชฉ/๊ณต๋ฐฑ/๋ฉํ ์์ญ์ด ๊ฒฐ๊ณผ์ ์์
|
| 291 |
+
"""
|
| 292 |
+
if df is None or df.empty:
|
| 293 |
+
return df
|
| 294 |
+
|
| 295 |
+
work = df.copy()
|
| 296 |
+
work = work.dropna(how="all").reset_index(drop=True)
|
| 297 |
+
|
| 298 |
+
if work.empty:
|
| 299 |
+
return work
|
| 300 |
+
|
| 301 |
+
# 1) ์ค์ ํค๋ ํ ํ์ง
|
| 302 |
+
best_idx = None
|
| 303 |
+
best_score = -1
|
| 304 |
+
|
| 305 |
+
for idx, row in work.iterrows():
|
| 306 |
+
row_text = " ".join(safe_str(v) for v in row.values if safe_str(v))
|
| 307 |
+
low = row_text.lower()
|
| 308 |
+
compact = normalize_compact(row_text)
|
| 309 |
+
|
| 310 |
+
score = 0
|
| 311 |
+
|
| 312 |
+
if "no" in low or "๋ฒํธ" in low or "์๋ฒ" in low:
|
| 313 |
+
score += 1
|
| 314 |
+
if "ํ๊ธ" in low and ("ํ์ค" in low or "์ฑ๋ถ" in low):
|
| 315 |
+
score += 2
|
| 316 |
+
if "ingredient" in low or "inci" in low or "ingredientnameinci" in compact:
|
| 317 |
+
score += 4
|
| 318 |
+
if "function" in low or "๊ธฐ๋ฅ" in low:
|
| 319 |
+
score += 2
|
| 320 |
+
if "cas" in low:
|
| 321 |
+
score += 2
|
| 322 |
+
if "source" in low or "์ถ์ฒ" in low:
|
| 323 |
+
score += 2
|
| 324 |
+
if "w/w" in low or "%(w/w)" in low or "ํจ๋" in low:
|
| 325 |
+
score += 2
|
| 326 |
+
|
| 327 |
+
if score > best_score:
|
| 328 |
+
best_score = score
|
| 329 |
+
best_idx = idx
|
| 330 |
+
|
| 331 |
+
# 2) ์ค์ ํค๋๊ฐ ์ค๊ฐ์ ์์ผ๋ฉด ๊ทธ ํ์ ์ปฌ๋ผ์ผ๋ก ์น๊ฒฉ
|
| 332 |
+
if best_idx is not None and best_score >= 5:
|
| 333 |
+
header = []
|
| 334 |
+
for i, v in enumerate(work.iloc[best_idx].tolist()):
|
| 335 |
+
h = safe_str(v)
|
| 336 |
+
header.append(h if h else f"Unnamed_{i}")
|
| 337 |
+
|
| 338 |
+
body = work.iloc[best_idx + 1:].copy().reset_index(drop=True)
|
| 339 |
+
body.columns = header
|
| 340 |
+
work = body
|
| 341 |
+
|
| 342 |
+
# 3) ์ปฌ๋ผ๋ช
์ค๋ณต ๋ฐฉ์ง
|
| 343 |
+
seen = {}
|
| 344 |
+
new_cols = []
|
| 345 |
+
|
| 346 |
+
for col in work.columns:
|
| 347 |
+
c = safe_str(col) or "Unnamed"
|
| 348 |
+
if c in seen:
|
| 349 |
+
seen[c] += 1
|
| 350 |
+
c = f"{c}_{seen[c]}"
|
| 351 |
+
else:
|
| 352 |
+
seen[c] = 0
|
| 353 |
+
new_cols.append(c)
|
| 354 |
+
|
| 355 |
+
work.columns = new_cols
|
| 356 |
+
|
| 357 |
+
# 4) ์ ํ๋ช
/์ ๋ชฉ/๋ฐ๋ณต ํค๋/๋น ํ ์ ๊ฑฐ
|
| 358 |
+
drop_indices = []
|
| 359 |
+
|
| 360 |
+
for idx, row in work.iterrows():
|
| 361 |
+
vals = [safe_str(v) for v in row.values]
|
| 362 |
+
row_text = " ".join(v for v in vals if v)
|
| 363 |
+
low = row_text.lower()
|
| 364 |
+
compact = normalize_compact(row_text)
|
| 365 |
+
|
| 366 |
+
if not compact:
|
| 367 |
+
drop_indices.append(idx)
|
| 368 |
+
continue
|
| 369 |
+
|
| 370 |
+
if "productname" in compact:
|
| 371 |
+
drop_indices.append(idx)
|
| 372 |
+
continue
|
| 373 |
+
|
| 374 |
+
if "formularingredientsstatement" in compact or "formulaingredientsstatement" in compact:
|
| 375 |
+
drop_indices.append(idx)
|
| 376 |
+
continue
|
| 377 |
+
|
| 378 |
+
if "ingredientnameinci" in compact or ("ingredient name" in low and ("function" in low or "cas" in low or "source" in low)):
|
| 379 |
+
drop_indices.append(idx)
|
| 380 |
+
continue
|
| 381 |
+
|
| 382 |
+
# 'INGREDIENT NAME / NO. / FUNCTION / CAS.No / SOURCE' ๊ฐ์ ํ ๋ด๋ถ ๋ฐ๋ณต ํค๋ ์ ๊ฑฐ
|
| 383 |
+
header_like_count = 0
|
| 384 |
+
for v in vals:
|
| 385 |
+
vn = normalize_compact(v)
|
| 386 |
+
if vn in [
|
| 387 |
+
"no", "no.", "๋ฒํธ", "์๋ฒ",
|
| 388 |
+
"ํ๊ธํ์คํ๋ช
์นญ", "ํ๊ธ์ฑ๋ถ๋ช
", "๊ตญ๋ฌธ์ฑ๋ถ๋ช
",
|
| 389 |
+
"ingredientname", "ingredientnameinci", "inci", "inciname",
|
| 390 |
+
"function", "๊ธฐ๋ฅ",
|
| 391 |
+
"casno", "cas.no", "cas",
|
| 392 |
+
"source", "์ถ์ฒ",
|
| 393 |
+
"ww", "%ww", "%(ww)", "%(w/w)", "ํจ๋",
|
| 394 |
+
]:
|
| 395 |
+
header_like_count += 1
|
| 396 |
+
|
| 397 |
+
if header_like_count >= 3:
|
| 398 |
+
drop_indices.append(idx)
|
| 399 |
+
continue
|
| 400 |
+
|
| 401 |
+
if drop_indices:
|
| 402 |
+
work = work.drop(index=drop_indices).reset_index(drop=True)
|
| 403 |
+
|
| 404 |
+
return work
|
| 405 |
+
|
| 406 |
+
|
| 407 |
# input extraction
|
| 408 |
STANDARD_INPUT_COLUMNS=["NO.","ํ๊ธ ํ์คํ ๋ช
์นญ","INGREDIENT NAME (INCI)","FUNCTION","CAS.No","SOURCE","%(w/w)"]
|
| 409 |
|
|
|
|
| 480 |
raise ValueError("PDF์์ ์ ์ฑ๋ถํ๋ฅผ ์ถ์ถํ์ง ๋ชปํ์ต๋๋ค.")
|
| 481 |
|
| 482 |
def load_input_file_to_dataframe(file):
|
| 483 |
+
if not file:
|
| 484 |
+
raise ValueError("ํ์ผ์ด ์์ต๋๋ค.")
|
| 485 |
+
|
| 486 |
+
path = file.name
|
| 487 |
+
ext = os.path.splitext(path)[1].lower()
|
| 488 |
+
|
| 489 |
+
if ext not in SUPPORTED_INPUT_EXTENSIONS:
|
| 490 |
+
raise ValueError("์ง์ํ์ง ์๋ ํ์ผ ํ์์
๋๋ค. xlsx, xls, pdf, png, jpg, jpeg, webp๋ง ์ง์ํฉ๋๋ค.")
|
| 491 |
+
|
| 492 |
+
if ext in [".xlsx", ".xls"]:
|
| 493 |
+
# ์ค์:
|
| 494 |
+
# header=None์ผ๋ก ์ฝ์ด์ผ ์ค์ ํค๋๊ฐ ์ค๊ฐ์ ์๋ ์์์์
|
| 495 |
+
# PRODUCT NAME/์๋จ ๋ฉํ ํ์ด ์ฑ๋ถ์ผ๋ก ๋ค์ด๊ฐ์ง ์๋๋ค.
|
| 496 |
+
raw = pd.read_excel(path, header=None, dtype=str)
|
| 497 |
+
return standardize_formula_dataframe(raw)
|
| 498 |
+
|
| 499 |
+
if ext == ".pdf":
|
| 500 |
+
return standardize_formula_dataframe(extract_formula_table_from_pdf(path))
|
| 501 |
+
|
| 502 |
+
return standardize_formula_dataframe(extract_formula_table_from_image(path))
|
| 503 |
|
| 504 |
def preview_input_file(file):
|
| 505 |
try: return load_input_file_to_dataframe(file) if file else None
|
|
|
|
| 647 |
vals=[safe_str(v) for v in row.values]; txt=" ".join(v for v in vals if v).strip(); low=txt.lower()
|
| 648 |
if not txt: continue
|
| 649 |
no=safe_str(row.get(cols["no_col"],"")).lower(); ko=safe_str(row.get(cols["korean_ing_col"],"")).lower(); inci=safe_str(row.get(cols["inci_col"],"")).lower()
|
| 650 |
+
if no in ["no","no.","๋ฒํธ","์๋ฒ"] or ko in ["ํ๊ธ ํ์คํ ๋ช
์นญ","ํ๊ธ ์ฑ๋ถ๋ช
","๊ตญ๋ฌธ ์ฑ๋ถ๋ช
"] or inci in ["ingredient name (inci)","ingredient name","inci","inci name"]: continue
|
| 651 |
+
if "product name" in low or "productname" in normalize_compact(txt): continue
|
| 652 |
+
if "formular ingredients statement" in low or "formula ingredients statement" in low: continue
|
| 653 |
+
if "ingredient name" in low and ("function" in low or "cas" in low or "source" in low): continue
|
| 654 |
if is_total_like_text(no) or is_total_like_text(ko) or is_total_like_text(inci) or row_has_total_like(vals): footer=True; continue
|
| 655 |
if any(m in low for m in ["commitment of the manufacturer","rebom co","tel :","fax :","lab no."]): footer=True; continue
|
| 656 |
if footer: continue
|
|
|
|
| 2930 |
# - ์๋ USER_DB์ ๋ฉ์ผ/๋น๋ฐ๋ฒํธ๋ฅผ ์ง์ ๋ฑ๋ก
|
| 2931 |
|
| 2932 |
|
| 2933 |
+
|
| 2934 |
# ============================================================
|
| 2935 |
# ๋ก๊ทธ์ธ ์ธ์ฆ
|
| 2936 |
# - Gradio ๊ธฐ๋ณธ ๋ก๊ทธ์ธ์ฐฝ ์ฌ์ฉ
|