Update app.py
Browse files
app.py
CHANGED
|
@@ -3,85 +3,51 @@ import sqlite3
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
#
|
| 9 |
UPLOAD_DIR = "uploaded_dbs"
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
db_registry = []
|
| 14 |
|
| 15 |
-
|
| 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 |
-
|
| 21 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 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):
|
|
@@ -148,5 +114,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 148 |
category_dd.change(display_data, [standard_dd, version_dd, category_dd], output_df)
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
-
|
| 152 |
demo.launch()
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
+
from huggingface_hub import HfApi # μΆκ°λ¨
|
| 7 |
|
| 8 |
+
# --- μ€μ λΆ ---
|
| 9 |
UPLOAD_DIR = "uploaded_dbs"
|
| 10 |
+
# λ΄ μ€νμ΄μ€ μ 보λ₯Ό μλμΌλ‘ κ°μ Έμ΅λλ€.
|
| 11 |
+
REPO_ID = os.environ.get("SPACE_ID") # μ: "μμ΄λ/μ€νμ΄μ€μ΄λ¦"
|
| 12 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # μ°κΈ° κΆνμ΄ μλ ν ν° νμ
|
|
|
|
| 13 |
|
| 14 |
+
api = HfApi()
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
if not os.path.exists(UPLOAD_DIR):
|
| 17 |
+
os.makedirs(UPLOAD_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# ... (κΈ°μ‘΄ natural_sort_key, refresh_registry_data λ±μ λμΌ) ...
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# 2οΈβ£ UI μ
λ°μ΄νΈ ν¨μ μμ
|
| 22 |
def handle_upload(files):
|
| 23 |
if files is None: return "νμΌμ΄ μμ΅λλ€.", gr.Dropdown()
|
| 24 |
+
|
| 25 |
+
saved_count = 0
|
| 26 |
for file in files:
|
| 27 |
+
filename = os.path.basename(file.name)
|
| 28 |
+
# 1. μΌλ¨ νμ¬ μ€ν νκ²½(μμ)μ μ μ₯
|
| 29 |
+
local_path = os.path.join(UPLOAD_DIR, filename)
|
| 30 |
+
with open(file.name, "rb") as f_in:
|
| 31 |
+
with open(local_path, "wb") as f_out:
|
| 32 |
+
f_out.write(f_in.read())
|
| 33 |
+
|
| 34 |
+
# 2. Hugging Face μ μ₯μλ‘ 'μꡬ μ
λ‘λ' μ€ν (Git Commit)
|
| 35 |
+
if REPO_ID and HF_TOKEN:
|
| 36 |
+
try:
|
| 37 |
+
api.upload_file(
|
| 38 |
+
path_or_fileobj=local_path,
|
| 39 |
+
path_in_repo=f"uploaded_dbs/{filename}", # μ μ₯μ λ΄ κ²½λ‘
|
| 40 |
+
repo_id=REPO_ID,
|
| 41 |
+
repo_type="space",
|
| 42 |
+
token=HF_TOKEN
|
| 43 |
+
)
|
| 44 |
+
saved_count += 1
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"μ μ₯μ μ
λ‘λ μλ¬: {e}", gr.Dropdown()
|
| 47 |
|
| 48 |
+
refresh_registry_data()
|
| 49 |
+
return f"{saved_count}κ° νμΌμ΄ μ μ₯μμ μꡬ μ μ₯λμμ΅λλ€! (μ μ ν μ±μ΄ μ¬μμλ μ μμ΅λλ€.)", gr.Dropdown(choices=refresh_registry_data(), value=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# 3οΈβ£ λ°μ΄ν° μ‘°ν λ‘μ§
|
| 53 |
def display_data(standard, version, selection):
|
|
|
|
| 114 |
category_dd.change(display_data, [standard_dd, version_dd, category_dd], output_df)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
|
| 118 |
demo.launch()
|