Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import pdfplumber
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
from openpyxl import load_workbook
|
| 8 |
-
from openpyxl.styles import PatternFill, Font, Alignment
|
| 9 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
@@ -83,7 +84,6 @@ def extract_number(value):
|
|
| 83 |
|
| 84 |
return None
|
| 85 |
|
| 86 |
-
|
| 87 |
def is_numeric_like(value):
|
| 88 |
if pd.isna(value):
|
| 89 |
return False
|
|
@@ -103,178 +103,91 @@ def find_main_columns(df):
|
|
| 103 |
- amount_col: ์๋ฃ ํฌ์
๋
|
| 104 |
- final_amount_col: ์ต์ข
์ ํ ๋ด ๊ตฌ์ฑ ์ฑ๋ถ ํจ๋
|
| 105 |
"""
|
| 106 |
-
|
| 107 |
n = max(len(df), 1)
|
| 108 |
|
| 109 |
-
raw_keywords = [
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
]
|
| 113 |
-
|
| 114 |
-
ingredient_keywords = [
|
| 115 |
-
"์ฑ๋ถ", "์ ์ฑ๋ถ", "inci", "ingredient",
|
| 116 |
-
"composition", "๊ตฌ์ฑ"
|
| 117 |
-
]
|
| 118 |
-
|
| 119 |
-
amount_keywords = [
|
| 120 |
-
"ํจ๋", "๋ฐฐํฉ๋", "๋๋", "ํฌ์
", "์ฌ์ฉ๋",
|
| 121 |
-
"content", "amount", "concentration", "%"
|
| 122 |
-
]
|
| 123 |
|
| 124 |
raw_scores = []
|
| 125 |
-
|
| 126 |
for idx, col in enumerate(df.columns):
|
| 127 |
values = df[col].dropna().astype(str).str.strip()
|
| 128 |
-
|
| 129 |
-
if values.empty:
|
| 130 |
-
continue
|
| 131 |
-
|
| 132 |
numeric_ratio = values.apply(is_numeric_like).mean()
|
| 133 |
-
|
| 134 |
-
if numeric_ratio > 0.7:
|
| 135 |
-
continue
|
| 136 |
|
| 137 |
header = str(col).lower()
|
| 138 |
sample_text = " ".join(values.head(30).tolist()).lower()
|
| 139 |
-
|
| 140 |
non_null_ratio = len(values) / n
|
| 141 |
unique_ratio = values.nunique() / max(len(values), 1)
|
| 142 |
|
| 143 |
score = 0
|
| 144 |
-
|
| 145 |
-
if any(k.lower() in header for k in
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
if
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
if 0 < non_null_ratio < 0.7:
|
| 152 |
-
score += 4
|
| 153 |
-
|
| 154 |
-
if unique_ratio < 0.8:
|
| 155 |
-
score += 2
|
| 156 |
-
|
| 157 |
-
if idx <= 3:
|
| 158 |
-
score += 1
|
| 159 |
-
|
| 160 |
-
if "โข" in sample_text or "ยฎ" in sample_text:
|
| 161 |
-
score += 2
|
| 162 |
|
| 163 |
raw_scores.append((score, col))
|
| 164 |
|
| 165 |
-
|
| 166 |
-
raw_col = sorted(raw_scores, reverse=True, key=lambda x: x[0])[0][1]
|
| 167 |
-
else:
|
| 168 |
-
raw_col = df.columns[0]
|
| 169 |
|
| 170 |
ingredient_scores = []
|
| 171 |
-
|
| 172 |
-
known_ingredients = [
|
| 173 |
-
"aqua", "water", "glycerin", "retinol", "retinyl",
|
| 174 |
-
"retinal", "acid", "extract", "lecithin", "palmitate",
|
| 175 |
-
"hexanediol", "caprylic", "triglyceride"
|
| 176 |
-
]
|
| 177 |
|
| 178 |
for idx, col in enumerate(df.columns):
|
| 179 |
-
if col == raw_col:
|
| 180 |
-
continue
|
| 181 |
-
|
| 182 |
values = df[col].dropna().astype(str).str.strip()
|
| 183 |
-
|
| 184 |
-
if values.empty:
|
| 185 |
-
continue
|
| 186 |
-
|
| 187 |
numeric_ratio = values.apply(is_numeric_like).mean()
|
| 188 |
-
|
| 189 |
-
if numeric_ratio > 0.5:
|
| 190 |
-
continue
|
| 191 |
|
| 192 |
header = str(col).lower()
|
| 193 |
sample_text = " ".join(values.head(50).tolist()).lower()
|
| 194 |
non_null_ratio = len(values) / n
|
| 195 |
|
| 196 |
score = 0
|
| 197 |
-
|
| 198 |
-
if any(k
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
if any(k in sample_text for k in known_ingredients):
|
| 202 |
-
score += 5
|
| 203 |
-
|
| 204 |
-
if non_null_ratio > 0.5:
|
| 205 |
-
score += 3
|
| 206 |
-
|
| 207 |
-
if any(k.lower() in header for k in raw_keywords):
|
| 208 |
-
score -= 4
|
| 209 |
|
| 210 |
ingredient_scores.append((score, col))
|
| 211 |
|
| 212 |
-
|
| 213 |
-
ingredient_col = sorted(ingredient_scores, reverse=True, key=lambda x: x[0])[0][1]
|
| 214 |
-
else:
|
| 215 |
-
ingredient_col = df.columns[1] if len(df.columns) > 1 else raw_col
|
| 216 |
|
| 217 |
numeric_candidates = []
|
| 218 |
-
|
| 219 |
for idx, col in enumerate(df.columns):
|
| 220 |
-
if col in [raw_col, ingredient_col]:
|
| 221 |
-
continue
|
| 222 |
-
|
| 223 |
header = str(col).lower()
|
| 224 |
-
|
| 225 |
-
if header in ["no", "๋ฒํธ", "์๋ฒ", "index", "#"]:
|
| 226 |
-
continue
|
| 227 |
|
| 228 |
values = df[col].dropna()
|
| 229 |
-
|
| 230 |
-
if values.empty:
|
| 231 |
-
continue
|
| 232 |
|
| 233 |
numeric_like_count = values.apply(is_numeric_like).sum()
|
| 234 |
converted = pd.to_numeric(values, errors="coerce")
|
| 235 |
numeric_count = converted.notna().sum()
|
| 236 |
-
|
| 237 |
count = max(numeric_like_count, numeric_count)
|
| 238 |
|
| 239 |
-
if count <= 0:
|
| 240 |
-
continue
|
| 241 |
-
|
| 242 |
non_null_count = len(values)
|
| 243 |
|
| 244 |
score = 0
|
|
|
|
| 245 |
|
| 246 |
-
|
| 247 |
-
score += 5
|
| 248 |
-
|
| 249 |
-
numeric_candidates.append({
|
| 250 |
-
"col": col,
|
| 251 |
-
"idx": idx,
|
| 252 |
-
"count": count,
|
| 253 |
-
"non_null_count": non_null_count,
|
| 254 |
-
"score": score
|
| 255 |
-
})
|
| 256 |
|
| 257 |
amount_col = None
|
| 258 |
final_amount_col = None
|
| 259 |
|
| 260 |
if numeric_candidates:
|
| 261 |
-
final_amount_col = sorted(
|
| 262 |
-
|
| 263 |
-
key=lambda x: (x["non_null_count"], x["idx"]),
|
| 264 |
-
reverse=True
|
| 265 |
-
)[0]["col"]
|
| 266 |
-
|
| 267 |
-
low_fill_candidates = [
|
| 268 |
-
c for c in numeric_candidates
|
| 269 |
-
if c["col"] != final_amount_col
|
| 270 |
-
]
|
| 271 |
|
| 272 |
if low_fill_candidates:
|
| 273 |
-
amount_col = sorted(
|
| 274 |
-
low_fill_candidates,
|
| 275 |
-
key=lambda x: (x["score"], -x["non_null_count"]),
|
| 276 |
-
reverse=True
|
| 277 |
-
)[0]["col"]
|
| 278 |
else:
|
| 279 |
amount_col = final_amount_col
|
| 280 |
|
|
@@ -292,36 +205,22 @@ def build_database(country_folders):
|
|
| 292 |
country_folders = [country_folders]
|
| 293 |
|
| 294 |
texts = []
|
| 295 |
-
|
| 296 |
-
splitter = RecursiveCharacterTextSplitter(
|
| 297 |
-
chunk_size=800,
|
| 298 |
-
chunk_overlap=100
|
| 299 |
-
)
|
| 300 |
|
| 301 |
for folder in country_folders:
|
| 302 |
-
if folder == "ํด๋ ์์":
|
| 303 |
-
continue
|
| 304 |
-
|
| 305 |
-
if not os.path.isdir(folder):
|
| 306 |
continue
|
| 307 |
|
| 308 |
-
files = [
|
| 309 |
-
f for f in os.listdir(folder)
|
| 310 |
-
if f.lower().endswith(".pdf")
|
| 311 |
-
]
|
| 312 |
|
| 313 |
for filename in files:
|
| 314 |
try:
|
| 315 |
with pdfplumber.open(os.path.join(folder, filename)) as pdf:
|
| 316 |
for page in pdf.pages:
|
| 317 |
text = page.extract_text()
|
| 318 |
-
|
| 319 |
if text:
|
| 320 |
-
chunks = splitter.split_text(
|
| 321 |
-
f"[๊ตญ๊ฐ/ํด๋: {folder}] [์ถ์ฒ: {filename}] {text}"
|
| 322 |
-
)
|
| 323 |
texts.extend(chunks)
|
| 324 |
-
|
| 325 |
except Exception as e:
|
| 326 |
print(f"PDF ์ค๋ฅ: {folder}/{filename} / {e}")
|
| 327 |
|
|
@@ -333,89 +232,66 @@ def build_database(country_folders):
|
|
| 333 |
|
| 334 |
|
| 335 |
# =========================
|
| 336 |
-
#
|
| 337 |
# =========================
|
| 338 |
-
def
|
| 339 |
-
result = {
|
| 340 |
-
"๊ท์ ๋์": "-",
|
| 341 |
-
"์๋ฃ ํฌ์
๋": "-",
|
| 342 |
-
"์ ํฉ ์ฌ๋ถ": "ํ์ธ ํ์",
|
| 343 |
-
"๋ถ์ ํฉ ์ฌ์ ": "-",
|
| 344 |
-
"๊ท์ ์ ๋": "์ ๋ณด ์์"
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
for line in text.splitlines():
|
| 348 |
-
line = line.strip()
|
| 349 |
-
|
| 350 |
-
if line.startswith("๊ท์ ๋์:"):
|
| 351 |
-
result["๊ท์ ๋์"] = line.split(":", 1)[1].strip() or "-"
|
| 352 |
-
|
| 353 |
-
elif line.startswith("์๋ฃ ํฌ์
๋:"):
|
| 354 |
-
result["์๋ฃ ํฌ์
๋"] = line.split(":", 1)[1].strip() or "-"
|
| 355 |
-
|
| 356 |
-
elif line.startswith("์ ํฉ ์ฌ๋ถ:"):
|
| 357 |
-
result["์ ํฉ ์ฌ๋ถ"] = line.split(":", 1)[1].strip() or "ํ์ธ ํ์"
|
| 358 |
-
|
| 359 |
-
elif line.startswith("๋ถ์ ํฉ ์ฌ์ :"):
|
| 360 |
-
result["๋ถ์ ํฉ ์ฌ์ "] = line.split(":", 1)[1].strip() or "-"
|
| 361 |
-
|
| 362 |
-
elif line.startswith("๊ท์ ์ ๋:"):
|
| 363 |
-
result["๊ท์ ์ ๋"] = line.split(":", 1)[1].strip() or "์ ๋ณด ์์"
|
| 364 |
-
|
| 365 |
-
return result
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
# =========================
|
| 369 |
-
# ์์
์คํ์ผ
|
| 370 |
-
# =========================
|
| 371 |
-
def style_excel(path):
|
| 372 |
wb = load_workbook(path)
|
| 373 |
ws = wb.active
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
cell.alignment = Alignment(horizontal="center", vertical="center")
|
| 383 |
|
|
|
|
|
|
|
|
|
|
| 384 |
headers = [c.value for c in ws[1]]
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
for col in ws.columns:
|
| 409 |
-
max_len = 0
|
| 410 |
col_letter = col[0].column_letter
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
|
|
|
|
|
|
| 419 |
|
| 420 |
ws.freeze_panes = "A2"
|
| 421 |
wb.save(path)
|
|
@@ -432,165 +308,114 @@ def process(country_folders, file):
|
|
| 432 |
return None, "โ ์คํฌ๋ฆฌ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํด์ฃผ์ธ์."
|
| 433 |
|
| 434 |
db = build_database(country_folders)
|
| 435 |
-
|
| 436 |
if not db:
|
| 437 |
return None, "โ ์ ํํ ๊ตญ๊ฐ ํด๋์ PDF๊ฐ ์๊ฑฐ๋ ์ฝ์ ์ ์์ต๋๋ค."
|
| 438 |
|
| 439 |
df = pd.read_excel(file.name)
|
| 440 |
-
|
| 441 |
raw_col, ingredient_col, amount_col, final_amount_col = find_main_columns(df)
|
| 442 |
-
|
| 443 |
df[raw_col] = df[raw_col].ffill()
|
| 444 |
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
)
|
| 449 |
|
| 450 |
output_rows = []
|
|
|
|
|
|
|
| 451 |
|
| 452 |
for raw_material, group in df.groupby(raw_col, dropna=True):
|
| 453 |
raw_material = str(raw_material).strip()
|
| 454 |
-
|
| 455 |
if not raw_material or raw_material.lower() == "nan":
|
| 456 |
continue
|
| 457 |
|
| 458 |
-
group_records = group.to_dict(orient="records")
|
| 459 |
-
|
| 460 |
ingredients = []
|
| 461 |
final_amounts = []
|
| 462 |
|
| 463 |
for _, row in group.iterrows():
|
| 464 |
ing = str(row.get(ingredient_col, "")).strip()
|
| 465 |
-
|
| 466 |
if ing and ing.lower() != "nan":
|
| 467 |
ingredients.append(ing)
|
| 468 |
|
| 469 |
if final_amount_col:
|
| 470 |
val = row.get(final_amount_col)
|
| 471 |
-
|
| 472 |
if not pd.isna(val):
|
| 473 |
final_amounts.append(str(val))
|
| 474 |
|
| 475 |
raw_amount = "-"
|
| 476 |
-
|
| 477 |
if amount_col:
|
| 478 |
raw_amount_values = group[amount_col].dropna().tolist()
|
| 479 |
-
|
| 480 |
if raw_amount_values:
|
| 481 |
raw_amount = raw_amount_values[0]
|
| 482 |
|
| 483 |
-
search_text = f""
|
| 484 |
-
|
| 485 |
-
๊ตฌ์ฑ์ฑ๋ถ: {' / '.join(ingredients)}
|
| 486 |
-
์๋ฃํฌ์
๋: {raw_amount}
|
| 487 |
-
์ต์ข
ํจ๋๋ค: {' / '.join(final_amounts)}
|
| 488 |
-
์ ์ฒด์ ๋ณด: {group_records}
|
| 489 |
-
"""
|
| 490 |
-
|
| 491 |
-
docs = db.similarity_search(search_text, k=6)
|
| 492 |
context = "\n".join([d.page_content for d in docs])
|
| 493 |
|
| 494 |
prompt = f"""
|
| 495 |
-
๋๋ ํ์ฅํ
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
{raw_amount}
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
์ ํฉ ์ฌ๋ถ:
|
| 523 |
-
๋ถ์ ํฉ ์ฌ์ :
|
| 524 |
-
๊ท์ ์ ๋:
|
| 525 |
-
|
| 526 |
-
ํ๋จ ์์น:
|
| 527 |
-
1. ์ด ์๋ฃ๊ฐ ๋ณตํฉ ์๋ฃ๋ผ๋ฉด ๊ตฌ์ฑ ์ฑ๋ถ ์ ์ฒด๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋จํ๋ค.
|
| 528 |
-
2. ๊ท์ ๋์์ ์๋ฃ๋ช
์์ฒด๊ฐ ์๋๋ผ ์ค์ ๊ตฌ์ฑ ์ฑ๋ถ ๋๋ ๊ท์ ์๋ฌธ์ ๋ฑ์ฌ๋ ์ฑ๋ถ๋ช
์ด๋ค.
|
| 529 |
-
3. ๊ฐ์ ๊ท์ ๊ตฐ์ ์ํ๋ ์ฑ๋ถ์ด ์ฌ๋ฌ ๊ฐ ์์ผ๋ฉด ์ต์ข
์ ํ ๋ด ํจ๋ ๊ธฐ์ค์ผ๋ก ํฉ์ฐํ๋ค.
|
| 530 |
-
4. ์ ํ ํจ๋, ์๋ฃ ํจ๋, ์๋ฃ ๋ด ํจ๋์ด ๊ตฌ๋ถ๋์ด ์์ผ๋ฉด ์ต์ข
์ ํ ๋ด ์ค์ ํจ๋ ๊ธฐ์ค์ผ๋ก ํ๋จํ๋ค.
|
| 531 |
-
5. ๊ท์ ์๋ฌธ์ ๊ธ์ง ์ฑ๋ถ์ผ๋ก ๋ช
ํํ ํ์ธ๋๋ฉด '๋ถ์ ํฉ'์ผ๋ก ํ๋จํ๋ค.
|
| 532 |
-
6. ๊ท์ ์๋ฌธ์ ์ต๋ ์ฌ์ฉ ํ๋ ๋๋ ์ ํ ์กฐ๊ฑด์ด ์๊ณ , ์ ํ ๋ด ์ค์ ํจ๋์ด ๊ธฐ์ค์ ์ด๊ณผํ๋ฉด '๋ถ์ ํฉ'์ผ๋ก ํ๋จํ๋ค.
|
| 533 |
-
7. ๊ท์ ์๋ฌธ์ ์ต๋ ์ฌ์ฉ ํ๋ ๋๋ ์ ํ ์กฐ๊ฑด์ด ์๊ณ , ์ ํ ๋ด ์ค์ ํจ๋์ด ๊ธฐ์ค ์ด๋ด์ด๋ฉด 'ํต๊ณผ' ๋๋ '์ ํฉ'์ผ๋ก ํ๋จํ๋ค.
|
| 534 |
-
8. ๊ท์ ๋ ์์ผ๋ ์ ํ ์ ํ, ์ฌ์ฉ ๋ถ์, leave-on/rinse-off ์ฌ๋ถ, ํจ๋ ๋จ์ ๋ฑ์ด ๋ถ์กฑํด ํ๋จ์ด ์ด๋ ค์ฐ๋ฉด 'ํ์ธ ํ์'๋ก ํ๋จํ๋ค.
|
| 535 |
-
9. ๊ฒ์๋ ๊ท์ ์๋ฌธ์ ํด๋น ์๋ฃ์ ๊ตฌ์ฑ ์ฑ๋ถ, ๋์์ด, ๊ท์ ๊ตฐ์ด ์ง์ ๊ด๋ จ๋์ง ์์ผ๋ฉด ๋ค๋ฅธ ์ฑ๋ถ ๊ท์ ๋ฅผ ์ ์ฉํ์ง ๋ง๊ณ '์ ๋ณด ์์'์ผ๋ก ๋ต๋ณํ๋ค.
|
| 536 |
-
10. ๋ถ์ ํฉ ์ฌ์ ๋ ๋ถ์ ํฉ์ผ ๋๋ง ์์ฑํ๊ณ , ์๋๋ฉด '-'๋ก ์์ฑํ๋ค.
|
| 537 |
-
11. ๊ท์ ์ ๋์๋ ๊ท์ ๊ตฐ, ์ด๋ ๊ณ์ฐ, ํ๋, ํ๋จ ๊ฒฐ๊ณผ๋ฅผ ์งง๊ฒ ์์ฑํ๋ค.
|
| 538 |
-
12. ๋ต๋ณ์ ํ ์๋ฃ๋น ํ ์ค ๊ฒฐ๊ณผ๋ก ์์ฝํ๋ค.
|
| 539 |
-
|
| 540 |
-
๋ต๋ณ ์์:
|
| 541 |
-
๊ท์ ๋์: Retinol ํฌํจ ๋ณตํฉ
|
| 542 |
-
์๋ฃ ํฌ์
๋: 0.200
|
| 543 |
-
์ ํฉ ์ฌ๋ถ: ํต๊ณผ
|
| 544 |
-
๋ถ์ ํฉ ์ฌ์ : -
|
| 545 |
-
๊ท์ ์ ๋: ๋นํ๋ฏผA ๊ท์ / ์ด Retinol 0.1002%(=0.0002+0.100), ํ๋ 0.3% RE ์ด๋ด(ํต๊ณผ)
|
| 546 |
-
|
| 547 |
-
๋ต๋ณ ์์:
|
| 548 |
-
๊ท์ ๋์: -
|
| 549 |
-
์๋ฃ ํฌ์
๋: 1.000
|
| 550 |
-
์ ํฉ ์ฌ๋ถ: ์ ๋ณด ์์
|
| 551 |
-
๋ถ์ ํฉ ์ฌ์ : -
|
| 552 |
-
๊ท์ ์ ๋: ๊ด๋ จ ๊ท์ ํ์ธ๋์ง ์์
|
| 553 |
-
"""
|
| 554 |
|
| 555 |
try:
|
| 556 |
response = llm.invoke(prompt).content
|
| 557 |
-
|
| 558 |
-
|
| 559 |
except Exception as e:
|
| 560 |
-
|
| 561 |
-
"๊ท์ ๋์": "-",
|
| 562 |
-
"์๋ฃ ํฌ์
๋": str(raw_amount),
|
| 563 |
-
"์ ํฉ ์ฌ๋ถ": "ํ์ธ ํ์",
|
| 564 |
-
"๋ถ์ ํฉ ์ฌ์ ": str(e),
|
| 565 |
-
"๊ท์ ์ ๋": "์๋ฌ"
|
| 566 |
-
}
|
| 567 |
-
|
| 568 |
-
output_rows.append({
|
| 569 |
-
"์๋ฃ๋ช
": raw_material,
|
| 570 |
-
"๊ท์ ๋์": parsed["๊ท์ ๋์"],
|
| 571 |
-
"์๋ฃ ํฌ์
๋": parsed["์๋ฃ ํฌ์
๋"] if parsed["์๋ฃ ํฌ์
๋"] != "-" else raw_amount,
|
| 572 |
-
"์ ํฉ ์ฌ๋ถ": parsed["์ ํฉ ์ฌ๋ถ"],
|
| 573 |
-
"๋ถ์ ํฉ ์ฌ์ ": parsed["๋ถ์ ํฉ ์ฌ์ "],
|
| 574 |
-
"๊ท์ ์ ๋": parsed["๊ท์ ์ ๋"]
|
| 575 |
-
})
|
| 576 |
|
| 577 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
else:
|
| 586 |
-
country_name = str(country_folders)
|
| 587 |
|
| 588 |
-
|
| 589 |
|
| 590 |
-
|
|
|
|
|
|
|
| 591 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
result_df.to_excel(out, index=False)
|
| 593 |
-
style_excel(out)
|
| 594 |
|
| 595 |
return result_df, out
|
| 596 |
|
|
@@ -600,7 +425,7 @@ def process(country_folders, file):
|
|
| 600 |
# =========================
|
| 601 |
with gr.Blocks() as demo:
|
| 602 |
gr.Markdown("# ๐ reBom Regulatory Screening AI")
|
| 603 |
-
gr.Markdown("### ๊ธ๋ก๋ฒ ํ์ฅํ ๊ท์ ๊ธฐ๋ฐ ์ ์ฑ๋ถ ์คํฌ๋ฆฌ๋")
|
| 604 |
|
| 605 |
country = gr.CheckboxGroup(
|
| 606 |
choices=get_country_folders(),
|
|
@@ -646,7 +471,6 @@ with gr.Blocks() as demo:
|
|
| 646 |
outputs=[df_out, file_out]
|
| 647 |
)
|
| 648 |
|
| 649 |
-
|
| 650 |
demo.launch(
|
| 651 |
ssr_mode=False,
|
| 652 |
css=custom_css
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import json
|
| 4 |
import pandas as pd
|
| 5 |
import pdfplumber
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
from openpyxl import load_workbook
|
| 9 |
+
from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
|
| 10 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 11 |
from langchain_community.vectorstores import FAISS
|
| 12 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
| 84 |
|
| 85 |
return None
|
| 86 |
|
|
|
|
| 87 |
def is_numeric_like(value):
|
| 88 |
if pd.isna(value):
|
| 89 |
return False
|
|
|
|
| 103 |
- amount_col: ์๋ฃ ํฌ์
๋
|
| 104 |
- final_amount_col: ์ต์ข
์ ํ ๋ด ๊ตฌ์ฑ ์ฑ๋ถ ํจ๋
|
| 105 |
"""
|
|
|
|
| 106 |
n = max(len(df), 1)
|
| 107 |
|
| 108 |
+
raw_keywords = ["์๋ฃ", "์๋ฃ๋ช
", "์ํ", "์ํ๋ช
", "trade", "material", "raw", "blend", "๋ณตํฉ"]
|
| 109 |
+
ingredient_keywords = ["์ฑ๋ถ", "์ ์ฑ๋ถ", "inci", "ingredient", "composition", "๊ตฌ์ฑ"]
|
| 110 |
+
amount_keywords = ["ํจ๋", "๋ฐฐํฉ๋", "๋๋", "ํฌ์
", "์ฌ์ฉ๋", "content", "amount", "concentration", "%"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
raw_scores = []
|
|
|
|
| 113 |
for idx, col in enumerate(df.columns):
|
| 114 |
values = df[col].dropna().astype(str).str.strip()
|
| 115 |
+
if values.empty: continue
|
|
|
|
|
|
|
|
|
|
| 116 |
numeric_ratio = values.apply(is_numeric_like).mean()
|
| 117 |
+
if numeric_ratio > 0.7: continue
|
|
|
|
|
|
|
| 118 |
|
| 119 |
header = str(col).lower()
|
| 120 |
sample_text = " ".join(values.head(30).tolist()).lower()
|
|
|
|
| 121 |
non_null_ratio = len(values) / n
|
| 122 |
unique_ratio = values.nunique() / max(len(values), 1)
|
| 123 |
|
| 124 |
score = 0
|
| 125 |
+
if any(k.lower() in header for k in raw_keywords): score += 10
|
| 126 |
+
if any(k.lower() in header for k in ingredient_keywords): score -= 5
|
| 127 |
+
if 0 < non_null_ratio < 0.7: score += 4
|
| 128 |
+
if unique_ratio < 0.8: score += 2
|
| 129 |
+
if idx <= 3: score += 1
|
| 130 |
+
if "โข" in sample_text or "ยฎ" in sample_text: score += 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
raw_scores.append((score, col))
|
| 133 |
|
| 134 |
+
raw_col = sorted(raw_scores, reverse=True, key=lambda x: x[0])[0][1] if raw_scores else df.columns[0]
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
ingredient_scores = []
|
| 137 |
+
known_ingredients = ["aqua", "water", "glycerin", "retinol", "retinyl", "retinal", "acid", "extract", "lecithin", "palmitate", "hexanediol", "caprylic", "triglyceride"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
for idx, col in enumerate(df.columns):
|
| 140 |
+
if col == raw_col: continue
|
|
|
|
|
|
|
| 141 |
values = df[col].dropna().astype(str).str.strip()
|
| 142 |
+
if values.empty: continue
|
|
|
|
|
|
|
|
|
|
| 143 |
numeric_ratio = values.apply(is_numeric_like).mean()
|
| 144 |
+
if numeric_ratio > 0.5: continue
|
|
|
|
|
|
|
| 145 |
|
| 146 |
header = str(col).lower()
|
| 147 |
sample_text = " ".join(values.head(50).tolist()).lower()
|
| 148 |
non_null_ratio = len(values) / n
|
| 149 |
|
| 150 |
score = 0
|
| 151 |
+
if any(k.lower() in header for k in ingredient_keywords): score += 10
|
| 152 |
+
if any(k in sample_text for k in known_ingredients): score += 5
|
| 153 |
+
if non_null_ratio > 0.5: score += 3
|
| 154 |
+
if any(k.lower() in header for k in raw_keywords): score -= 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
ingredient_scores.append((score, col))
|
| 157 |
|
| 158 |
+
ingredient_col = sorted(ingredient_scores, reverse=True, key=lambda x: x[0])[0][1] if ingredient_scores else (df.columns[1] if len(df.columns) > 1 else raw_col)
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
numeric_candidates = []
|
|
|
|
| 161 |
for idx, col in enumerate(df.columns):
|
| 162 |
+
if col in [raw_col, ingredient_col]: continue
|
|
|
|
|
|
|
| 163 |
header = str(col).lower()
|
| 164 |
+
if header in ["no", "๋ฒํธ", "์๋ฒ", "index", "#"]: continue
|
|
|
|
|
|
|
| 165 |
|
| 166 |
values = df[col].dropna()
|
| 167 |
+
if values.empty: continue
|
|
|
|
|
|
|
| 168 |
|
| 169 |
numeric_like_count = values.apply(is_numeric_like).sum()
|
| 170 |
converted = pd.to_numeric(values, errors="coerce")
|
| 171 |
numeric_count = converted.notna().sum()
|
|
|
|
| 172 |
count = max(numeric_like_count, numeric_count)
|
| 173 |
|
| 174 |
+
if count <= 0: continue
|
|
|
|
|
|
|
| 175 |
non_null_count = len(values)
|
| 176 |
|
| 177 |
score = 0
|
| 178 |
+
if any(k.lower() in header for k in amount_keywords): score += 5
|
| 179 |
|
| 180 |
+
numeric_candidates.append({"col": col, "idx": idx, "count": count, "non_null_count": non_null_count, "score": score})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
amount_col = None
|
| 183 |
final_amount_col = None
|
| 184 |
|
| 185 |
if numeric_candidates:
|
| 186 |
+
final_amount_col = sorted(numeric_candidates, key=lambda x: (x["non_null_count"], x["idx"]), reverse=True)[0]["col"]
|
| 187 |
+
low_fill_candidates = [c for c in numeric_candidates if c["col"] != final_amount_col]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
if low_fill_candidates:
|
| 190 |
+
amount_col = sorted(low_fill_candidates, key=lambda x: (x["score"], -x["non_null_count"]), reverse=True)[0]["col"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
else:
|
| 192 |
amount_col = final_amount_col
|
| 193 |
|
|
|
|
| 205 |
country_folders = [country_folders]
|
| 206 |
|
| 207 |
texts = []
|
| 208 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
for folder in country_folders:
|
| 211 |
+
if folder == "ํด๋ ์์" or not os.path.isdir(folder):
|
|
|
|
|
|
|
|
|
|
| 212 |
continue
|
| 213 |
|
| 214 |
+
files = [f for f in os.listdir(folder) if f.lower().endswith(".pdf")]
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
for filename in files:
|
| 217 |
try:
|
| 218 |
with pdfplumber.open(os.path.join(folder, filename)) as pdf:
|
| 219 |
for page in pdf.pages:
|
| 220 |
text = page.extract_text()
|
|
|
|
| 221 |
if text:
|
| 222 |
+
chunks = splitter.split_text(f"[๊ตญ๊ฐ: {folder}] [์ถ์ฒ: {filename}] {text}")
|
|
|
|
|
|
|
| 223 |
texts.extend(chunks)
|
|
|
|
| 224 |
except Exception as e:
|
| 225 |
print(f"PDF ์ค๋ฅ: {folder}/{filename} / {e}")
|
| 226 |
|
|
|
|
| 232 |
|
| 233 |
|
| 234 |
# =========================
|
| 235 |
+
# ์ฌ์ง ์์ ๊ธฐ๋ฐ ์์
์คํ์ผ๋ง
|
| 236 |
# =========================
|
| 237 |
+
def style_excel(path, country_folders):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
wb = load_workbook(path)
|
| 239 |
ws = wb.active
|
| 240 |
|
| 241 |
+
# ํ
๋๋ฆฌ ์คํ์ผ ์ค์ (๋ชจ๋ ์
์ ์์ ์ )
|
| 242 |
+
thin_border = Border(
|
| 243 |
+
left=Side(style='thin', color='000000'),
|
| 244 |
+
right=Side(style='thin', color='000000'),
|
| 245 |
+
top=Side(style='thin', color='000000'),
|
| 246 |
+
bottom=Side(style='thin', color='000000')
|
| 247 |
+
)
|
|
|
|
| 248 |
|
| 249 |
+
# ํค๋ ์คํ์ผ (์
์ ํ์)
|
| 250 |
+
header_fill = PatternFill(start_color="D9D9D9", end_color="D9D9D9", fill_type="solid")
|
| 251 |
+
|
| 252 |
headers = [c.value for c in ws[1]]
|
| 253 |
+
country_cols = [headers.index(c) + 1 for c in country_folders if c in headers]
|
| 254 |
+
|
| 255 |
+
red_font = Font(color="FF0000", bold=True)
|
| 256 |
+
black_font = Font(color="000000")
|
| 257 |
+
header_font = Font(bold=True, color="000000")
|
| 258 |
+
|
| 259 |
+
# ์ ์ฒด ๋ฐ์ดํฐ ์์ญ ์คํ์ผ๋ง
|
| 260 |
+
for row in range(1, ws.max_row + 1):
|
| 261 |
+
for col in range(1, ws.max_column + 1):
|
| 262 |
+
cell = ws.cell(row, col)
|
| 263 |
+
|
| 264 |
+
# ์ค์ ์ ๋ ฌ ๋ฐ ์๋ ์ค๋ฐ๊ฟ
|
| 265 |
+
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
| 266 |
+
# ๋ชจ๋ ์
์ ํ
๋๋ฆฌ ์ ์ฉ
|
| 267 |
+
cell.border = thin_border
|
| 268 |
+
|
| 269 |
+
# ์ฒซ ๋ฒ์งธ ์ค(ํค๋) ์ค์
|
| 270 |
+
if row == 1:
|
| 271 |
+
cell.fill = header_fill
|
| 272 |
+
cell.font = header_font
|
| 273 |
+
else:
|
| 274 |
+
# ๊ตญ๊ฐ ๊ท์ ๊ฒฐ๊ณผ ์ปฌ๋ผ์์ ํน์ ํค์๋ ๊ฐ์ง ์ ๊ธ์๋ฅผ ๋นจ๊ฐ์์ผ๋ก ๋ณ๊ฒฝ
|
| 275 |
+
if col in country_cols:
|
| 276 |
+
val = str(cell.value or "")
|
| 277 |
+
if "๊ท์ " in val or "๋ถ์ ํฉ" in val or "ํ์ธ ํ์" in val:
|
| 278 |
+
cell.font = red_font
|
| 279 |
+
else:
|
| 280 |
+
cell.font = black_font
|
| 281 |
+
|
| 282 |
+
# ์ปฌ๋ผ ๋๋น ์ต์ ํ
|
| 283 |
for col in ws.columns:
|
|
|
|
| 284 |
col_letter = col[0].column_letter
|
| 285 |
+
if col_letter == "A": # NO
|
| 286 |
+
ws.column_dimensions[col_letter].width = 5
|
| 287 |
+
elif col_letter in ["B", "C"]: # Trade name, INCI
|
| 288 |
+
ws.column_dimensions[col_letter].width = 28
|
| 289 |
+
elif col_letter == "D": # ํจ๋
|
| 290 |
+
ws.column_dimensions[col_letter].width = 12
|
| 291 |
+
elif col_letter == ws.cell(1, ws.max_column).column_letter: # ๋ง์ง๋ง '๋น๊ณ ' ์ปฌ๋ผ
|
| 292 |
+
ws.column_dimensions[col_letter].width = 15
|
| 293 |
+
else: # ๊ตญ๊ฐ๋ค
|
| 294 |
+
ws.column_dimensions[col_letter].width = 30
|
| 295 |
|
| 296 |
ws.freeze_panes = "A2"
|
| 297 |
wb.save(path)
|
|
|
|
| 308 |
return None, "โ ์คํฌ๋ฆฌ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํด์ฃผ์ธ์."
|
| 309 |
|
| 310 |
db = build_database(country_folders)
|
|
|
|
| 311 |
if not db:
|
| 312 |
return None, "โ ์ ํํ ๊ตญ๊ฐ ํด๋์ PDF๊ฐ ์๊ฑฐ๋ ์ฝ์ ์ ์์ต๋๋ค."
|
| 313 |
|
| 314 |
df = pd.read_excel(file.name)
|
|
|
|
| 315 |
raw_col, ingredient_col, amount_col, final_amount_col = find_main_columns(df)
|
|
|
|
| 316 |
df[raw_col] = df[raw_col].ffill()
|
| 317 |
|
| 318 |
+
# JSON ์ถ๋ ฅ์ ๊ฐ์ ํ๊ธฐ ์ํด json_object ๋ชจ๋ ํ์ฑํ (์์ ์ฑ ๊ฐํ)
|
| 319 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0).bind(
|
| 320 |
+
response_format={"type": "json_object"}
|
| 321 |
)
|
| 322 |
|
| 323 |
output_rows = []
|
| 324 |
+
idx = 1
|
| 325 |
+
countries_str = ", ".join(country_folders)
|
| 326 |
|
| 327 |
for raw_material, group in df.groupby(raw_col, dropna=True):
|
| 328 |
raw_material = str(raw_material).strip()
|
|
|
|
| 329 |
if not raw_material or raw_material.lower() == "nan":
|
| 330 |
continue
|
| 331 |
|
|
|
|
|
|
|
| 332 |
ingredients = []
|
| 333 |
final_amounts = []
|
| 334 |
|
| 335 |
for _, row in group.iterrows():
|
| 336 |
ing = str(row.get(ingredient_col, "")).strip()
|
|
|
|
| 337 |
if ing and ing.lower() != "nan":
|
| 338 |
ingredients.append(ing)
|
| 339 |
|
| 340 |
if final_amount_col:
|
| 341 |
val = row.get(final_amount_col)
|
|
|
|
| 342 |
if not pd.isna(val):
|
| 343 |
final_amounts.append(str(val))
|
| 344 |
|
| 345 |
raw_amount = "-"
|
|
|
|
| 346 |
if amount_col:
|
| 347 |
raw_amount_values = group[amount_col].dropna().tolist()
|
|
|
|
| 348 |
if raw_amount_values:
|
| 349 |
raw_amount = raw_amount_values[0]
|
| 350 |
|
| 351 |
+
search_text = f"์๋ฃ๋ช
: {raw_material} / ๊ตฌ์ฑ์ฑ๋ถ: {' / '.join(ingredients)}"
|
| 352 |
+
docs = db.similarity_search(search_text, k=8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
context = "\n".join([d.page_content for d in docs])
|
| 354 |
|
| 355 |
prompt = f"""
|
| 356 |
+
๋๋ ๊ธ๋ก๋ฒ ํ์ฅํ ์ฑ๋ถ ๊ท์ ์ ๋ฌธ๊ฐ์ผ.
|
| 357 |
+
์ ๊ณต๋ ๊ท์ ์๋ฌธ์ ๋ฐํ์ผ๋ก ์๋ [๋์ ๊ตญ๊ฐ] ๊ฐ๊ฐ์ ๋ํด ์ ํฉ ์ฌ๋ถ๋ฅผ ํ๊ฐํด.
|
| 358 |
+
|
| 359 |
+
[๋์ ๊ตญ๊ฐ]
|
| 360 |
+
{countries_str}
|
| 361 |
+
|
| 362 |
+
[๋ถ์ ๋์ ์๋ฃ ์ ๋ณด]
|
| 363 |
+
- Trade name: {raw_material}
|
| 364 |
+
- INCI (๊ตฌ์ฑ ์ฑ๋ถ): {', '.join(ingredients)}
|
| 365 |
+
- ํจ๋ (%): {raw_amount}
|
| 366 |
+
- ์ต์ข
ํจ๋ ๋ชฉ๋ก: {', '.join(final_amounts) if final_amounts else '-'}
|
| 367 |
+
|
| 368 |
+
[๊ฒ์๋ ๊ท์ ์๋ฌธ]
|
| 369 |
+
{context}
|
| 370 |
+
|
| 371 |
+
[์์ฑ ์์น]
|
| 372 |
+
1. ์๋ฌธ์ ํด๋น ์ฑ๋ถ์ ๊ท์ ๊ฐ ์๊ฑฐ๋ ๋ฌด๊ดํ๋ฉด "์ ํ ์์ (ํต๊ณผ)" ๋๋ "ํต๊ณผ"๋ก ์์ฑ.
|
| 373 |
+
2. ๊ท์ ๊ฐ ์กด์ฌํ์ง๋ง ์ ํ ๋ด ํ๋ ์ด๋ด์ฌ์ ์์ ํ๋ค๋ฉด ๊ตฌ์ฒด์ ์ฌ์ ํฌํจ (์: "๋นํ๋ฏผA ๊ท์ / ์ด Retinol 0.1002%, ํ๋ 0.3% RE ์ด๋ด(ํต๊ณผ)").
|
| 374 |
+
3. ๊ธ์ง ์ฑ๋ถ์ด๊ฑฐ๋ ํ๋๋ฅผ ์ด๊ณผํ๋ฉด "๋ถ์ ํฉ / [์ฌ์ ]".
|
| 375 |
+
4. ํ๋จ ๋ณด๋ฅ ์ํฉ์ด๋ฉด "ํ์ธ ํ์ / [์ฌ์ ]".
|
| 376 |
+
|
| 377 |
+
๋ฐ๋์ ์๋ JSON ํ์์ผ๋ก๋ง ๋ต๋ณํด.
|
| 378 |
+
{{
|
| 379 |
+
"{country_folders[0]}": "๊ฒฐ๊ณผ๊ฐ",
|
| 380 |
+
...
|
| 381 |
+
}}
|
| 382 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
try:
|
| 385 |
response = llm.invoke(prompt).content
|
| 386 |
+
parsed_result = json.loads(response)
|
|
|
|
| 387 |
except Exception as e:
|
| 388 |
+
parsed_result = {c: "์๋ฌ (ํ์ธ ํ์)" for c in country_folders}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
+
# ๊ฒฐ๊ณผ Row ์์ฑ
|
| 391 |
+
row_data = {
|
| 392 |
+
"NO": idx,
|
| 393 |
+
"Trade name": raw_material,
|
| 394 |
+
"INCI": "\n".join(ingredients) if ingredients else "-",
|
| 395 |
+
"ํจ๋ (%)": raw_amount,
|
| 396 |
+
"๋น๊ณ ": "" # ๋น ๋น๊ณ ๋ ์ถ๊ฐ
|
| 397 |
+
}
|
| 398 |
|
| 399 |
+
# ๊ตญ๊ฐ๋ณ ์ปฌ๋ผ ๋ฐ์ดํฐ ๋งคํ
|
| 400 |
+
for c in country_folders:
|
| 401 |
+
row_data[c] = parsed_result.get(c, "ํ์ธ ํ์")
|
| 402 |
|
| 403 |
+
output_rows.append(row_data)
|
| 404 |
+
idx += 1
|
|
|
|
|
|
|
| 405 |
|
| 406 |
+
result_df = pd.DataFrame(output_rows)
|
| 407 |
|
| 408 |
+
# ์ปฌ๋ผ ์์ ๋ณด์ฅ (์ด๋ฏธ์ง ์์๊ณผ ๋์ผํ๊ฒ)
|
| 409 |
+
columns_order = ["NO", "Trade name", "INCI", "ํจ๋ (%)"] + country_folders + ["๋น๊ณ "]
|
| 410 |
+
result_df = result_df[[col for col in columns_order if col in result_df.columns]]
|
| 411 |
|
| 412 |
+
# ์์
์ ์ฅ
|
| 413 |
+
safe_country_name = "_".join(country_folders) if isinstance(country_folders, list) else str(country_folders)
|
| 414 |
+
safe_country_name = re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ_-]", "_", safe_country_name)
|
| 415 |
+
out = f"result_{safe_country_name}.xlsx"
|
| 416 |
+
|
| 417 |
result_df.to_excel(out, index=False)
|
| 418 |
+
style_excel(out, country_folders)
|
| 419 |
|
| 420 |
return result_df, out
|
| 421 |
|
|
|
|
| 425 |
# =========================
|
| 426 |
with gr.Blocks() as demo:
|
| 427 |
gr.Markdown("# ๐ reBom Regulatory Screening AI")
|
| 428 |
+
gr.Markdown("### ๊ธ๋ก๋ฒ ํ์ฅํ ๊ท์ ๊ธฐ๋ฐ ์ ์ฑ๋ถ ์คํฌ๋ฆฌ๋ (๊ตญ๊ฐ๋ณ ๋ถ์)")
|
| 429 |
|
| 430 |
country = gr.CheckboxGroup(
|
| 431 |
choices=get_country_folders(),
|
|
|
|
| 471 |
outputs=[df_out, file_out]
|
| 472 |
)
|
| 473 |
|
|
|
|
| 474 |
demo.launch(
|
| 475 |
ssr_mode=False,
|
| 476 |
css=custom_css
|