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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -7
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: raise ValueError("ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.")
359
- path=file.name; ext=os.path.splitext(path)[1].lower()
360
- if ext not in SUPPORTED_INPUT_EXTENSIONS: raise ValueError("์ง€์›ํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ ํ˜•์‹์ž…๋‹ˆ๋‹ค. xlsx, xls, pdf, png, jpg, jpeg, webp๋งŒ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.")
361
- if ext in [".xlsx",".xls"]: return pd.read_excel(path)
362
- if ext==".pdf": return extract_formula_table_from_pdf(path)
363
- return extract_formula_table_from_image(path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ์ฐฝ ์‚ฌ์šฉ