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
|
| 4 |
+
import os
|
| 5 |
+
import re
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
# 1๏ธโฃ ์ ์ฅ์ ์ค์
|
| 9 |
+
UPLOAD_DIR = "uploaded_dbs"
|
| 10 |
+
if not os.path.exists(UPLOAD_DIR):
|
| 11 |
+
os.makedirs(UPLOAD_DIR)
|
| 12 |
+
|
| 13 |
+
db_registry = []
|
| 14 |
+
|
| 15 |
+
def natural_sort_key(s):
|
| 16 |
+
if s is None: return []
|
| 17 |
+
return [int(text) if text.isdigit() else text.lower()
|
| 18 |
+
for text in re.split(r'(\d+)', str(s))]
|
| 19 |
+
|
| 20 |
+
def refresh_registry_data():
|
| 21 |
+
global db_registry
|
| 22 |
+
db_registry = []
|
| 23 |
+
if not os.path.exists(UPLOAD_DIR): return []
|
| 24 |
+
|
| 25 |
+
db_files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".db")]
|
| 26 |
+
for filename in db_files:
|
| 27 |
+
name_only = filename.replace(".db", "")
|
| 28 |
+
parts = name_only.split("_")
|
| 29 |
+
if len(parts) >= 2:
|
| 30 |
+
db_registry.append({
|
| 31 |
+
"path": os.path.join(UPLOAD_DIR, filename),
|
| 32 |
+
"standard": parts[0],
|
| 33 |
+
"version": parts[1]
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
return sorted(list(set([db["standard"] for db in db_registry])))
|
| 37 |
+
|
| 38 |
+
# 2๏ธโฃ UI ์
๋ฐ์ดํธ ํจ์๋ค
|
| 39 |
+
def on_load():
|
| 40 |
+
standards = refresh_registry_data()
|
| 41 |
+
return gr.Dropdown(choices=standards, value=None)
|
| 42 |
+
|
| 43 |
+
def handle_upload(files):
|
| 44 |
+
if files is None: return "ํ์ผ์ด ์์ต๋๋ค.", gr.Dropdown()
|
| 45 |
+
for file in files:
|
| 46 |
+
shutil.copy(file.name, os.path.join(UPLOAD_DIR, os.path.basename(file.name)))
|
| 47 |
+
refresh_registry_data()
|
| 48 |
+
return "ํ์ผ ์ ์ฅ ์๋ฃ!", gr.Dropdown(choices=refresh_registry_data(), value=None)
|
| 49 |
+
|
| 50 |
+
def update_version_dd(standard):
|
| 51 |
+
if not standard: return gr.Dropdown(choices=[], value=None)
|
| 52 |
+
versions = sorted(list(set([db["version"] for db in db_registry if db["standard"] == standard])))
|
| 53 |
+
return gr.Dropdown(choices=versions, value=None)
|
| 54 |
+
|
| 55 |
+
def update_category_dd(standard, version):
|
| 56 |
+
if not standard or not version: return gr.Dropdown(choices=[], value=None)
|
| 57 |
+
|
| 58 |
+
choices = []
|
| 59 |
+
try:
|
| 60 |
+
target_db = next(db for db in db_registry if db["standard"] == standard and db["version"] == version)
|
| 61 |
+
conn = sqlite3.connect(target_db["path"])
|
| 62 |
+
all_tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
|
| 63 |
+
|
| 64 |
+
main_t = f"{standard}_{version}"
|
| 65 |
+
if main_t not in all_tables:
|
| 66 |
+
main_t = all_tables[0]
|
| 67 |
+
|
| 68 |
+
choices.append("ALL")
|
| 69 |
+
|
| 70 |
+
cols = pd.read_sql(f"PRAGMA table_info([{main_t}])", conn)['name'].tolist()
|
| 71 |
+
if 'chapter' in cols and 'category' in cols:
|
| 72 |
+
df_cat = pd.read_sql(f"SELECT DISTINCT chapter, category FROM [{main_t}]", conn)
|
| 73 |
+
cats = [f"{row['chapter']}.{row['category']}" for _, row in df_cat.iterrows()]
|
| 74 |
+
cats.sort(key=natural_sort_key)
|
| 75 |
+
choices.extend(cats)
|
| 76 |
+
|
| 77 |
+
prefix = f"{standard}_{version}_"
|
| 78 |
+
sub_tables = [t.replace(prefix, "") for t in all_tables if t != main_t]
|
| 79 |
+
choices.extend(sorted(sub_tables))
|
| 80 |
+
|
| 81 |
+
conn.close()
|
| 82 |
+
return gr.Dropdown(choices=choices, value=None)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return gr.Dropdown(choices=["ALL"], value=None)
|
| 85 |
+
|
| 86 |
+
# 3๏ธโฃ ๋ฐ์ดํฐ ์กฐํ ๋ก์ง (์ปฌ๋ผ ํํฐ๋ง ์ถ๊ฐ)
|
| 87 |
+
def display_data(standard, version, selection):
|
| 88 |
+
if not all([standard, version, selection]): return None
|
| 89 |
+
try:
|
| 90 |
+
target_db = next(db for db in db_registry if db["standard"] == standard and db["version"] == version)
|
| 91 |
+
conn = sqlite3.connect(target_db["path"])
|
| 92 |
+
all_tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
|
| 93 |
+
|
| 94 |
+
main_t = f"{standard}_{version}"
|
| 95 |
+
if main_t not in all_tables: main_t = all_tables[0]
|
| 96 |
+
|
| 97 |
+
is_sub_table = False # ์ถ๊ฐ ํ
์ด๋ธ์ธ์ง ํ์ธ์ฉ ํ๋๊ทธ
|
| 98 |
+
|
| 99 |
+
if selection == "ALL":
|
| 100 |
+
df = pd.read_sql(f"SELECT * FROM [{main_t}]", conn)
|
| 101 |
+
elif "." in selection:
|
| 102 |
+
ch, ca = selection.split('.', 1)
|
| 103 |
+
df = pd.read_sql(f"SELECT * FROM [{main_t}] WHERE chapter=? AND category=?", conn, params=[ch, ca])
|
| 104 |
+
else:
|
| 105 |
+
is_sub_table = True
|
| 106 |
+
actual_table = f"{standard}_{version}_{selection}"
|
| 107 |
+
if actual_table not in all_tables:
|
| 108 |
+
actual_table = next((t for t in all_tables if t.endswith(selection)), selection)
|
| 109 |
+
df = pd.read_sql(f"SELECT * FROM [{actual_table}]", conn)
|
| 110 |
+
|
| 111 |
+
conn.close()
|
| 112 |
+
|
| 113 |
+
if not df.empty:
|
| 114 |
+
# 1. ์ ๋ ฌ ์ ์ฉ
|
| 115 |
+
sort_col = 'section' if 'section' in df.columns else df.columns[0]
|
| 116 |
+
df['sort_key'] = df[sort_col].apply(natural_sort_key)
|
| 117 |
+
df = df.sort_values(by='sort_key').drop(columns=['sort_key'])
|
| 118 |
+
|
| 119 |
+
# 2. [์ถ๊ฐ ์๊ตฌ์ฌํญ] ๋ณธ๋ฌธ(ALL ๋๋ ์นดํ
๊ณ ๋ฆฌ)์ผ ๋๋ง section, description๋ง ๋จ๊ธฐ๊ธฐ
|
| 120 |
+
if not is_sub_table:
|
| 121 |
+
available_cols = [c for c in ['section', 'description'] if c in df.columns]
|
| 122 |
+
df = df[available_cols]
|
| 123 |
+
else:
|
| 124 |
+
# ์ถ๊ฐ ํ
์ด๋ธ์ผ ๊ฒฝ์ฐ ๋ด๋ถ ์ ๋ ฌ์ฉ ์ปฌ๋ผ๋ง ์ ๊ฑฐํ๊ณ ์ ์ฒด ์ ์ง
|
| 125 |
+
df = df[[c for c in df.columns if not c.startswith('sort_')]]
|
| 126 |
+
|
| 127 |
+
return df
|
| 128 |
+
except Exception as e:
|
| 129 |
+
return pd.DataFrame({"Error": [f"์กฐํ ์คํจ: {str(e)}"]})
|
| 130 |
+
|
| 131 |
+
# 4๏ธโฃ UI ๊ตฌ์ฑ
|
| 132 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 133 |
+
gr.Markdown("# ๐ Regulation ")
|
| 134 |
+
|
| 135 |
+
with gr.Accordion("โ DB ๊ด๋ฆฌ", open=False):
|
| 136 |
+
file_input = gr.File(label="DB ์
๋ก๋", file_count="multiple", file_types=[".db"])
|
| 137 |
+
upload_btn = gr.Button("์๋ฒ ์ ์ฅ", variant="primary")
|
| 138 |
+
upload_status = gr.Markdown("")
|
| 139 |
+
|
| 140 |
+
with gr.Row():
|
| 141 |
+
standard_dd = gr.Dropdown(label="1. ๋ฒ๊ท ์ ํ")
|
| 142 |
+
version_dd = gr.Dropdown(label="2. Version ์ ํ")
|
| 143 |
+
category_dd = gr.Dropdown(label="3. Category / Table ์ ํ")
|
| 144 |
+
|
| 145 |
+
output_df = gr.Dataframe(wrap=True, interactive=False)
|
| 146 |
+
|
| 147 |
+
demo.load(on_load, None, standard_dd)
|
| 148 |
+
upload_btn.click(handle_upload, file_input, [upload_status, standard_dd])
|
| 149 |
+
standard_dd.change(update_version_dd, standard_dd, version_dd)
|
| 150 |
+
version_dd.change(update_category_dd, [standard_dd, version_dd], category_dd)
|
| 151 |
+
category_dd.change(display_data, [standard_dd, version_dd, category_dd], output_df)
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
|
| 155 |
+
demo.launch(share=True)
|