Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,30 +5,9 @@ import os
|
|
| 5 |
import threading
|
| 6 |
import pandas as pd
|
| 7 |
from datasets import load_dataset
|
| 8 |
-
import huggingface_hub
|
| 9 |
-
from huggingface_hub import Repository
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
DATA_FILENAME = "global_data.csv"
|
| 14 |
-
DATA_DIR = "data"
|
| 15 |
-
DATA_FILE = os.path.join(DATA_DIR, DATA_FILENAME)
|
| 16 |
-
# 아래 URL은 참고용입니다. 본인의 repository URL로 변경하세요.
|
| 17 |
-
DATASET_REPO_URL = "https://huggingface.co/datasets/gaeunseo/Interface1"
|
| 18 |
-
|
| 19 |
-
# 데이터 폴더가 없으면 생성합니다.
|
| 20 |
-
os.makedirs(DATA_DIR, exist_ok=True)
|
| 21 |
-
|
| 22 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 23 |
-
|
| 24 |
-
# Repository 객체를 생성하여 로컬 폴더와 Hugging Face repo를 연결합니다.
|
| 25 |
-
repo = Repository(
|
| 26 |
-
local_dir=DATA_DIR,
|
| 27 |
-
clone_from=DATASET_REPO_URL,
|
| 28 |
-
use_auth_token=HF_TOKEN
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# 동시 접근을 위한 Lock 선언
|
| 32 |
data_lock = threading.Lock()
|
| 33 |
|
| 34 |
def initialize_global_data():
|
|
@@ -47,28 +26,22 @@ def initialize_global_data():
|
|
| 47 |
if "text" not in data.columns:
|
| 48 |
data["text"] = ""
|
| 49 |
data.to_csv(DATA_FILE, index=False)
|
| 50 |
-
repo.push_to_hub() # 새 CSV 파일을 Hugging Face repo에 업로드합니다.
|
| 51 |
return data
|
| 52 |
else:
|
| 53 |
-
repo.git_pull() # 최신 데이터로 업데이트합니다.
|
| 54 |
with data_lock:
|
| 55 |
df = pd.read_csv(DATA_FILE)
|
| 56 |
return df
|
| 57 |
|
| 58 |
def load_global_data():
|
| 59 |
"""CSV 파일에서 global_data DataFrame을 읽어옵니다."""
|
| 60 |
-
repo.git_pull() # 최신 변경사항을 반영합니다.
|
| 61 |
with data_lock:
|
| 62 |
df = pd.read_csv(DATA_FILE)
|
| 63 |
return df
|
| 64 |
|
| 65 |
def save_global_data(df):
|
| 66 |
-
"""DataFrame을 CSV 파일에
|
| 67 |
with data_lock:
|
| 68 |
df.to_csv(DATA_FILE, index=False)
|
| 69 |
-
repo.push_to_hub() # 업데이트된 파일을 푸시합니다.
|
| 70 |
-
# --- 데이터 연결 관련 수정 끝 ---
|
| 71 |
-
|
| 72 |
|
| 73 |
# CSV 파일에 저장된 global_data 초기화
|
| 74 |
global_data = initialize_global_data()
|
|
@@ -328,4 +301,4 @@ with gr.Blocks() as demo:
|
|
| 328 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 329 |
submit_button.click(fn=submit_edit, inputs=edited_text_input, outputs=[human_bubble, ai_bubble])
|
| 330 |
|
| 331 |
-
demo.launch()
|
|
|
|
| 5 |
import threading
|
| 6 |
import pandas as pd
|
| 7 |
from datasets import load_dataset
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# CSV 파일 경로와 동시 접근을 위한 Lock 선언
|
| 10 |
+
DATA_FILE = "global_data.csv"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
data_lock = threading.Lock()
|
| 12 |
|
| 13 |
def initialize_global_data():
|
|
|
|
| 26 |
if "text" not in data.columns:
|
| 27 |
data["text"] = ""
|
| 28 |
data.to_csv(DATA_FILE, index=False)
|
|
|
|
| 29 |
return data
|
| 30 |
else:
|
|
|
|
| 31 |
with data_lock:
|
| 32 |
df = pd.read_csv(DATA_FILE)
|
| 33 |
return df
|
| 34 |
|
| 35 |
def load_global_data():
|
| 36 |
"""CSV 파일에서 global_data DataFrame을 읽어옵니다."""
|
|
|
|
| 37 |
with data_lock:
|
| 38 |
df = pd.read_csv(DATA_FILE)
|
| 39 |
return df
|
| 40 |
|
| 41 |
def save_global_data(df):
|
| 42 |
+
"""DataFrame을 CSV 파일에 저장합니다."""
|
| 43 |
with data_lock:
|
| 44 |
df.to_csv(DATA_FILE, index=False)
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# CSV 파일에 저장된 global_data 초기화
|
| 47 |
global_data = initialize_global_data()
|
|
|
|
| 301 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 302 |
submit_button.click(fn=submit_edit, inputs=edited_text_input, outputs=[human_bubble, ai_bubble])
|
| 303 |
|
| 304 |
+
demo.launch()
|