Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -121,15 +121,16 @@ def find_main_columns(df):
|
|
| 121 |
"content", "amount", "concentration", "%"
|
| 122 |
]
|
| 123 |
|
| 124 |
-
# 1) μλ£λͺ
μ»¬λΌ μ°ΎκΈ°
|
| 125 |
raw_scores = []
|
| 126 |
|
| 127 |
for idx, col in enumerate(df.columns):
|
| 128 |
values = df[col].dropna().astype(str).str.strip()
|
|
|
|
| 129 |
if values.empty:
|
| 130 |
continue
|
| 131 |
|
| 132 |
numeric_ratio = values.apply(is_numeric_like).mean()
|
|
|
|
| 133 |
if numeric_ratio > 0.7:
|
| 134 |
continue
|
| 135 |
|
|
@@ -147,7 +148,6 @@ def find_main_columns(df):
|
|
| 147 |
if any(k.lower() in header for k in ingredient_keywords):
|
| 148 |
score -= 5
|
| 149 |
|
| 150 |
-
# λ³ν© μ
μλ£λͺ
μ λ³΄ν΅ μΌλΆ νλ§ κ°μ΄ μμ
|
| 151 |
if 0 < non_null_ratio < 0.7:
|
| 152 |
score += 4
|
| 153 |
|
|
@@ -167,7 +167,6 @@ def find_main_columns(df):
|
|
| 167 |
else:
|
| 168 |
raw_col = df.columns[0]
|
| 169 |
|
| 170 |
-
# 2) κ΅¬μ± μ±λΆ μ»¬λΌ μ°ΎκΈ°
|
| 171 |
ingredient_scores = []
|
| 172 |
|
| 173 |
known_ingredients = [
|
|
@@ -181,10 +180,12 @@ def find_main_columns(df):
|
|
| 181 |
continue
|
| 182 |
|
| 183 |
values = df[col].dropna().astype(str).str.strip()
|
|
|
|
| 184 |
if values.empty:
|
| 185 |
continue
|
| 186 |
|
| 187 |
numeric_ratio = values.apply(is_numeric_like).mean()
|
|
|
|
| 188 |
if numeric_ratio > 0.5:
|
| 189 |
continue
|
| 190 |
|
|
@@ -213,7 +214,6 @@ def find_main_columns(df):
|
|
| 213 |
else:
|
| 214 |
ingredient_col = df.columns[1] if len(df.columns) > 1 else raw_col
|
| 215 |
|
| 216 |
-
# 3) μ«μ μ»¬λΌ μ°ΎκΈ°
|
| 217 |
numeric_candidates = []
|
| 218 |
|
| 219 |
for idx, col in enumerate(df.columns):
|
|
@@ -222,7 +222,6 @@ def find_main_columns(df):
|
|
| 222 |
|
| 223 |
header = str(col).lower()
|
| 224 |
|
| 225 |
-
# λ²νΈ/μλ² μ»¬λΌ μ μΈ
|
| 226 |
if header in ["no", "λ²νΈ", "μλ²", "index", "#"]:
|
| 227 |
continue
|
| 228 |
|
|
@@ -259,14 +258,12 @@ def find_main_columns(df):
|
|
| 259 |
final_amount_col = None
|
| 260 |
|
| 261 |
if numeric_candidates:
|
| 262 |
-
# μ΅μ’
κ΅¬μ± μ±λΆ ν¨λμ λ³΄ν΅ κ°μ΄ λ§μ΄ μ±μμ§ μ«μ 컬λΌ
|
| 263 |
final_amount_col = sorted(
|
| 264 |
numeric_candidates,
|
| 265 |
key=lambda x: (x["non_null_count"], x["idx"]),
|
| 266 |
reverse=True
|
| 267 |
)[0]["col"]
|
| 268 |
|
| 269 |
-
# μλ£ ν¬μ
λμ λ³΄ν΅ λ³ν© μ
μ΄λΌ κ°μ΄ μ κ² μμ
|
| 270 |
low_fill_candidates = [
|
| 271 |
c for c in numeric_candidates
|
| 272 |
if c["col"] != final_amount_col
|
|
@@ -318,6 +315,7 @@ def build_database(country_folders):
|
|
| 318 |
with pdfplumber.open(os.path.join(folder, filename)) as pdf:
|
| 319 |
for page in pdf.pages:
|
| 320 |
text = page.extract_text()
|
|
|
|
| 321 |
if text:
|
| 322 |
chunks = splitter.split_text(
|
| 323 |
f"[κ΅κ°/ν΄λ: {folder}] [μΆμ²: {filename}] {text}"
|
|
@@ -351,12 +349,16 @@ def parse_response(text):
|
|
| 351 |
|
| 352 |
if line.startswith("κ·μ λμ:"):
|
| 353 |
result["κ·μ λμ"] = line.split(":", 1)[1].strip() or "-"
|
|
|
|
| 354 |
elif line.startswith("μλ£ ν¬μ
λ:"):
|
| 355 |
result["μλ£ ν¬μ
λ"] = line.split(":", 1)[1].strip() or "-"
|
|
|
|
| 356 |
elif line.startswith("μ ν© μ¬λΆ:"):
|
| 357 |
result["μ ν© μ¬λΆ"] = line.split(":", 1)[1].strip() or "νμΈ νμ"
|
|
|
|
| 358 |
elif line.startswith("λΆμ ν© μ¬μ :"):
|
| 359 |
result["λΆμ ν© μ¬μ "] = line.split(":", 1)[1].strip() or "-"
|
|
|
|
| 360 |
elif line.startswith("κ·μ μ λ:"):
|
| 361 |
result["κ·μ μ λ"] = line.split(":", 1)[1].strip() or "μ 보 μμ"
|
| 362 |
|
|
@@ -388,12 +390,10 @@ def style_excel(path):
|
|
| 388 |
status = str(ws.cell(row, status_col).value or "")
|
| 389 |
reg = str(ws.cell(row, reg_col).value or "")
|
| 390 |
|
| 391 |
-
# λΆμ ν©λ§ λΉ¨κ°μ
|
| 392 |
if "λΆμ ν©" in status:
|
| 393 |
for col in range(1, ws.max_column + 1):
|
| 394 |
ws.cell(row, col).fill = red
|
| 395 |
|
| 396 |
-
# κ·μ μλ κ²λ§ λ
Έλμ
|
| 397 |
elif (
|
| 398 |
reg.strip()
|
| 399 |
and "μ 보 μμ" not in reg
|
|
@@ -440,7 +440,6 @@ def process(country_folders, file):
|
|
| 440 |
|
| 441 |
raw_col, ingredient_col, amount_col, final_amount_col = find_main_columns(df)
|
| 442 |
|
| 443 |
-
# λ³ν© μ
λλ¬Έμ λΉμ΄ μλ μλ£λͺ
μ μ κ°μΌλ‘ μ±μ
|
| 444 |
df[raw_col] = df[raw_col].ffill()
|
| 445 |
|
| 446 |
llm = ChatOpenAI(
|
|
@@ -469,6 +468,7 @@ def process(country_folders, file):
|
|
| 469 |
|
| 470 |
if final_amount_col:
|
| 471 |
val = row.get(final_amount_col)
|
|
|
|
| 472 |
if not pd.isna(val):
|
| 473 |
final_amounts.append(str(val))
|
| 474 |
|
|
@@ -476,6 +476,7 @@ def process(country_folders, file):
|
|
| 476 |
|
| 477 |
if amount_col:
|
| 478 |
raw_amount_values = group[amount_col].dropna().tolist()
|
|
|
|
| 479 |
if raw_amount_values:
|
| 480 |
raw_amount = raw_amount_values[0]
|
| 481 |
|
|
@@ -597,7 +598,7 @@ def process(country_folders, file):
|
|
| 597 |
# =========================
|
| 598 |
# UI
|
| 599 |
# =========================
|
| 600 |
-
with gr.Blocks(
|
| 601 |
gr.Markdown("# π reBom Regulatory Screening AI")
|
| 602 |
gr.Markdown("### κΈλ‘λ² νμ₯ν κ·μ κΈ°λ° μ μ±λΆ μ€ν¬λ¦¬λ")
|
| 603 |
|
|
@@ -646,4 +647,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 646 |
)
|
| 647 |
|
| 648 |
|
| 649 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 148 |
if any(k.lower() in header for k in ingredient_keywords):
|
| 149 |
score -= 5
|
| 150 |
|
|
|
|
| 151 |
if 0 < non_null_ratio < 0.7:
|
| 152 |
score += 4
|
| 153 |
|
|
|
|
| 167 |
else:
|
| 168 |
raw_col = df.columns[0]
|
| 169 |
|
|
|
|
| 170 |
ingredient_scores = []
|
| 171 |
|
| 172 |
known_ingredients = [
|
|
|
|
| 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 |
|
|
|
|
| 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):
|
|
|
|
| 222 |
|
| 223 |
header = str(col).lower()
|
| 224 |
|
|
|
|
| 225 |
if header in ["no", "λ²νΈ", "μλ²", "index", "#"]:
|
| 226 |
continue
|
| 227 |
|
|
|
|
| 258 |
final_amount_col = None
|
| 259 |
|
| 260 |
if numeric_candidates:
|
|
|
|
| 261 |
final_amount_col = sorted(
|
| 262 |
numeric_candidates,
|
| 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
|
|
|
|
| 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}"
|
|
|
|
| 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 |
|
|
|
|
| 390 |
status = str(ws.cell(row, status_col).value or "")
|
| 391 |
reg = str(ws.cell(row, reg_col).value or "")
|
| 392 |
|
|
|
|
| 393 |
if "λΆμ ν©" in status:
|
| 394 |
for col in range(1, ws.max_column + 1):
|
| 395 |
ws.cell(row, col).fill = red
|
| 396 |
|
|
|
|
| 397 |
elif (
|
| 398 |
reg.strip()
|
| 399 |
and "μ 보 μμ" not in reg
|
|
|
|
| 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 |
llm = ChatOpenAI(
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 598 |
# =========================
|
| 599 |
# UI
|
| 600 |
# =========================
|
| 601 |
+
with gr.Blocks() as demo:
|
| 602 |
gr.Markdown("# π reBom Regulatory Screening AI")
|
| 603 |
gr.Markdown("### κΈλ‘λ² νμ₯ν κ·μ κΈ°λ° μ μ±λΆ μ€ν¬λ¦¬λ")
|
| 604 |
|
|
|
|
| 647 |
)
|
| 648 |
|
| 649 |
|
| 650 |
+
demo.launch(
|
| 651 |
+
ssr_mode=False,
|
| 652 |
+
css=custom_css
|
| 653 |
+
)
|