QIDNLF commited on
Commit
fe12817
Β·
verified Β·
1 Parent(s): 12e2a33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -69
app.py CHANGED
@@ -3,85 +3,51 @@ import sqlite3
3
  import pandas as pd
4
  import os
5
  import re
6
- import shutil
7
 
8
- # 1️⃣ μ €μž₯μ†Œ μ„€μ • (폴더 μ•ˆμ— DBλ₯Ό λͺ¨μœΌκΈ°λ‘œ ν–ˆμœΌλ―€λ‘œ μœ μ§€)
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):
@@ -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
- # ν—ˆκΉ…νŽ˜μ΄μŠ€ 배포 μ‹œμ—λŠ” share=Trueκ°€ ν•„μš” μ—†μœΌλ―€λ‘œ μ œκ±°ν•©λ‹ˆλ‹€.
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()