Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,100 +2,176 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
import pdfplumber
|
| 4 |
import gradio as gr
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
-
if api_key:
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# 2. ๊ตญ๊ฐ ํด๋ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
| 16 |
def get_country_folders():
|
| 17 |
exclude = ['.git', '.cache', '__pycache__', '.ipynb_checkpoints']
|
| 18 |
-
folders = [
|
|
|
|
|
|
|
|
|
|
| 19 |
return folders if folders else ["ํด๋ ์์"]
|
| 20 |
|
|
|
|
| 21 |
# 3. ์ ํ๋ ๊ตญ๊ฐ์ PDF๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
|
| 22 |
def build_database(country_folder):
|
| 23 |
-
if country_folder == "ํด๋ ์์":
|
| 24 |
-
|
|
|
|
| 25 |
all_texts = []
|
| 26 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
path = f"./{country_folder}"
|
| 30 |
-
files = [
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
for filename in files:
|
| 35 |
try:
|
| 36 |
with pdfplumber.open(os.path.join(path, filename)) as pdf:
|
| 37 |
for page in pdf.pages:
|
| 38 |
text = page.extract_text()
|
| 39 |
-
if text:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# 4. ๋ถ์ ํจ์
|
| 46 |
def process_screening(country, excel_file):
|
| 47 |
-
if not excel_file:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# 5. UI ๊ตฌ์ฑ
|
| 80 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 81 |
gr.Markdown("# ๐งช reBom Global Regulatory AI")
|
| 82 |
gr.Markdown("### ๊ตญ๊ฐ๋ณ ํด๋๋ฅผ ์ ํํ๊ณ ์ฑ๋ถ๋ช
์์
์ ์
๋ก๋ํ์ธ์.")
|
| 83 |
-
|
| 84 |
with gr.Row():
|
| 85 |
-
country_select = gr.Dropdown(
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
f_in = gr.File(label="์ ์ฑ๋ถ ์์
์
๋ก๋ (.xlsx)")
|
| 90 |
btn = gr.Button("๐ ๋ถ์ ์์", variant="primary")
|
| 91 |
-
|
| 92 |
with gr.Row():
|
| 93 |
df_out = gr.Dataframe(label="๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
| 94 |
f_out = gr.File(label="๊ฒฐ๊ณผ ํ์ผ ๋ค์ด๋ก๋")
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
demo.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import pdfplumber
|
| 4 |
import gradio as gr
|
| 5 |
+
|
| 6 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
|
| 10 |
+
|
| 11 |
+
# 1. API ํค ํ์ธ
|
| 12 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
if not api_key:
|
| 14 |
+
print("โ OPENAI_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 15 |
+
else:
|
| 16 |
+
print("โ
OPENAI_API_KEY๊ฐ ์ค์ ๋์ด ์์ต๋๋ค.")
|
| 17 |
+
|
| 18 |
|
| 19 |
+
# 2. ๊ตญ๊ฐ ํด๋ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
| 20 |
def get_country_folders():
|
| 21 |
exclude = ['.git', '.cache', '__pycache__', '.ipynb_checkpoints']
|
| 22 |
+
folders = [
|
| 23 |
+
f for f in os.listdir(".")
|
| 24 |
+
if os.path.isdir(f) and f not in exclude
|
| 25 |
+
]
|
| 26 |
return folders if folders else ["ํด๋ ์์"]
|
| 27 |
|
| 28 |
+
|
| 29 |
# 3. ์ ํ๋ ๊ตญ๊ฐ์ PDF๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
|
| 30 |
def build_database(country_folder):
|
| 31 |
+
if country_folder == "ํด๋ ์์":
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
all_texts = []
|
| 35 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 36 |
+
chunk_size=800,
|
| 37 |
+
chunk_overlap=100
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
path = f"./{country_folder}"
|
| 41 |
+
files = [
|
| 42 |
+
f for f in os.listdir(path)
|
| 43 |
+
if f.lower().endswith(".pdf")
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
if not files:
|
| 47 |
+
return None
|
| 48 |
|
| 49 |
for filename in files:
|
| 50 |
try:
|
| 51 |
with pdfplumber.open(os.path.join(path, filename)) as pdf:
|
| 52 |
for page in pdf.pages:
|
| 53 |
text = page.extract_text()
|
| 54 |
+
if text:
|
| 55 |
+
chunks = text_splitter.split_text(
|
| 56 |
+
f"[์ถ์ฒ: {filename}] {text}"
|
| 57 |
+
)
|
| 58 |
+
all_texts.extend(chunks)
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"PDF ์ฝ๊ธฐ ์ค๋ฅ: {filename} / {e}")
|
| 62 |
+
continue
|
| 63 |
+
|
| 64 |
+
if not all_texts:
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
embeddings = OpenAIEmbeddings(
|
| 68 |
+
model="text-embedding-3-small"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return FAISS.from_texts(all_texts, embeddings)
|
| 72 |
+
|
| 73 |
|
| 74 |
# 4. ๋ถ์ ํจ์
|
| 75 |
def process_screening(country, excel_file):
|
| 76 |
+
if not excel_file:
|
| 77 |
+
return None, "โ ์์
ํ์ผ์ ์
๋ก๋ํด์ฃผ์ธ์."
|
| 78 |
+
|
| 79 |
+
if country == "ํด๋ ์์":
|
| 80 |
+
return None, "โ ๊ตญ๊ฐ ํด๋๋ฅผ ๋จผ์ ์์ฑํ๊ณ PDF๋ฅผ ๋ฃ์ด์ฃผ์ธ์."
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
vector_db = build_database(country)
|
| 84 |
+
|
| 85 |
+
if not vector_db:
|
| 86 |
+
return None, f"โ {country} ํด๋ ๋ด์ ๋ถ์ํ PDF ํ์ผ์ด ์์ต๋๋ค."
|
| 87 |
+
|
| 88 |
+
llm = ChatOpenAI(
|
| 89 |
+
model="gpt-4o-mini",
|
| 90 |
+
temperature=0
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
df = pd.read_excel(excel_file.name)
|
| 94 |
+
|
| 95 |
+
results = []
|
| 96 |
+
|
| 97 |
+
for name in df.iloc[:, 0]:
|
| 98 |
+
ing_name = str(name).strip()
|
| 99 |
+
|
| 100 |
+
if not ing_name or ing_name.lower() == "nan":
|
| 101 |
+
results.append("์ฑ๋ถ๋ช
๏ฟฝ๏ฟฝ์")
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
docs = vector_db.similarity_search(ing_name, k=3)
|
| 106 |
+
context = "\n".join([d.page_content for d in docs])
|
| 107 |
+
|
| 108 |
+
prompt = f"""
|
| 109 |
+
ํ์ฅํ ๊ท์ ์ ๋ฌธ๊ฐ๋ก์ ์๋ ๊ท์ ์๋ฌธ ๋ด์ฉ์ ์ฐธ๊ณ ํด '{ing_name}'์ ๊ท์ ์ฌํญ์ ๊ฒํ ํ์ธ์.
|
| 110 |
+
|
| 111 |
+
๊ฒํ ๊ธฐ์ค:
|
| 112 |
+
- ๊ธ์ง ์ฌ๋ถ
|
| 113 |
+
- ์ฌ์ฉ ํ๋
|
| 114 |
+
- ์ฌ์ฉ ์กฐ๊ฑด
|
| 115 |
+
- ๊ด๋ จ ๋ด์ฉ์ด ์์ผ๋ฉด '์ ๋ณด ์์'์ด๋ผ๊ณ ๋ต๋ณ
|
| 116 |
+
|
| 117 |
+
๋ต๋ณ์ ์งง๊ณ ํฉํธ ์์ฃผ๋ก ์์ฑํ์ธ์.
|
| 118 |
+
|
| 119 |
+
[๊ท์ ์๋ฌธ]
|
| 120 |
+
{context}
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
res = llm.invoke(prompt)
|
| 124 |
+
results.append(res.content)
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
results.append(f"์๋ฌ ๋ฐ์: {str(e)}")
|
| 128 |
+
|
| 129 |
+
df["์คํฌ๋ฆฌ๋ ๊ฒฐ๊ณผ"] = results
|
| 130 |
+
|
| 131 |
+
out_path = f"reBom_Result_{country}.xlsx"
|
| 132 |
+
df.to_excel(out_path, index=False)
|
| 133 |
+
|
| 134 |
+
return df, out_path
|
| 135 |
+
|
| 136 |
+
except Exception as e:
|
| 137 |
+
return None, f"โ ์ ์ฒด ์ฒ๋ฆฌ ์ค๋ฅ: {str(e)}"
|
| 138 |
+
|
| 139 |
|
| 140 |
# 5. UI ๊ตฌ์ฑ
|
| 141 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 142 |
gr.Markdown("# ๐งช reBom Global Regulatory AI")
|
| 143 |
gr.Markdown("### ๊ตญ๊ฐ๋ณ ํด๋๋ฅผ ์ ํํ๊ณ ์ฑ๋ถ๋ช
์์
์ ์
๋ก๋ํ์ธ์.")
|
| 144 |
+
|
| 145 |
with gr.Row():
|
| 146 |
+
country_select = gr.Dropdown(
|
| 147 |
+
choices=get_country_folders(),
|
| 148 |
+
label="๐ ์คํฌ๋ฆฌ๋ ๊ตญ๊ฐ ์ ํ",
|
| 149 |
+
value=get_country_folders()[0]
|
| 150 |
+
)
|
| 151 |
+
refresh_btn = gr.Button(
|
| 152 |
+
"๐ ํด๋ ์๋ก๊ณ ์นจ",
|
| 153 |
+
variant="secondary",
|
| 154 |
+
scale=0
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
with gr.Row():
|
| 158 |
f_in = gr.File(label="์ ์ฑ๋ถ ์์
์
๋ก๋ (.xlsx)")
|
| 159 |
btn = gr.Button("๐ ๋ถ์ ์์", variant="primary")
|
| 160 |
+
|
| 161 |
with gr.Row():
|
| 162 |
df_out = gr.Dataframe(label="๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
| 163 |
f_out = gr.File(label="๊ฒฐ๊ณผ ํ์ผ ๋ค์ด๋ก๋")
|
| 164 |
|
| 165 |
+
refresh_btn.click(
|
| 166 |
+
fn=lambda: gr.update(choices=get_country_folders()),
|
| 167 |
+
outputs=country_select
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
btn.click(
|
| 171 |
+
process_screening,
|
| 172 |
+
inputs=[country_select, f_in],
|
| 173 |
+
outputs=[df_out, f_out]
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
|
| 177 |
demo.launch()
|