Spaces:
Sleeping
Sleeping
File size: 4,092 Bytes
cb442f8 1c094f3 cb442f8 cb7c9b7 a7d104e cb7c9b7 a7d104e cb7c9b7 cb442f8 1c094f3 cb442f8 1c094f3 cb442f8 1c094f3 cb442f8 1c094f3 cb442f8 1c094f3 cb442f8 1c094f3 cb442f8 1c094f3 cb7c9b7 1c094f3 cb442f8 1c094f3 cb442f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import os, base64, zipfile, io, sys, tempfile, subprocess
from pathlib import Path
from dotenv import load_dotenv
# .env νμΌ λ‘λ (λ‘컬 κ°λ°μ©)
load_dotenv()
def setup_environment():
"""
νκ²½λ³μ μλ μ€μ :
- λ‘컬: .env νμΌμμ λ‘λ
- Hugging Face Space: νκ²½λ³μμμ μλ μ€μ
"""
print("π§ νκ²½λ³μ μ€μ μ€...")
# Hugging Face Dataset μ€μ (κΈ°λ³Έκ° μ€μ )
if not os.getenv("HF_DATA_REPO_ID"):
os.environ["HF_DATA_REPO_ID"] = os.getenv("HF_DATA_REPO_ID")
print("π HF_DATA_REPO_ID κΈ°λ³Έκ° μ€μ ")
if not os.getenv("HF_DATA_TOKEN"):
os.environ["HF_DATA_TOKEN"] = os.getenv("HF_DATA_TOKEN")
print("π HF_DATA_TOKEN κΈ°λ³Έκ° μ€μ ")
# Oracle DB μ€μ νμΈ
required_vars = ["DB_USER", "DB_PASSWORD", "WALLET_DIR", "WALLET_PASSWORD"]
missing_vars = []
for var in required_vars:
if not os.getenv(var):
missing_vars.append(var)
if missing_vars:
print(f"β οΈ λλ½λ νκ²½λ³μ: {', '.join(missing_vars)}")
print("π‘ .env νμΌμ νμΈνκ±°λ νκ²½λ³μλ₯Ό μ€μ ν΄μ£ΌμΈμ.")
# λ‘컬 κ°λ°μ μν κΈ°λ³Έκ° μ€μ (κ²½κ³ μ ν¨κ»)
if not os.getenv("DB_USER"):
os.environ["DB_USER"] = "ADMIN"
print("π DB_USER κΈ°λ³Έκ° μ€μ : ADMIN")
if not os.getenv("DB_PASSWORD"):
os.environ["DB_PASSWORD"] = "YourDBPassword123!"
print("π DB_PASSWORD κΈ°λ³Έκ° μ€μ : YourDBPassword123!")
if not os.getenv("WALLET_PASSWORD"):
os.environ["WALLET_PASSWORD"] = "YourWalletPassword123!"
print("π WALLET_PASSWORD κΈ°λ³Έκ° μ€μ : YourWalletPassword123!")
else:
print("β
λͺ¨λ νμ νκ²½λ³μκ° μ€μ λμμ΅λλ€")
print("π§ νκ²½λ³μ μ€μ μλ£")
def prepare_wallet_dir():
"""
Wallet μ€λΉ:
- λ°°ν¬ νκ²½: WALLET_ZIP_B64μμ λμ½λ©
- λ‘컬 νκ²½: WALLET_DIR κ²½λ‘ μ¬μ©
"""
# λ‘컬 κ°λ°: WALLET_DIRμ΄ μ΄λ―Έ μ€μ λμ΄ μλ κ²½μ°
if "WALLET_DIR" in os.environ:
wallet_dir = os.environ["WALLET_DIR"]
if Path(wallet_dir).exists():
print(f"π λ‘컬 Wallet μ¬μ©: {wallet_dir}")
return wallet_dir
# λ°°ν¬ νκ²½: WALLET_ZIP_B64μμ λμ½λ©
if "WALLET_ZIP_B64" not in os.environ:
raise RuntimeError(
"β WALLET_ZIP_B64 λλ WALLET_DIR νκ²½ λ³μκ° νμν©λλ€.\n"
" λ‘컬: .env νμΌμ WALLET_DIR μ€μ \n"
" λ°°ν¬: Hugging Face Secretsμ WALLET_ZIP_B64 μ€μ "
)
print("π Wallet Base64 λμ½λ© μμ...")
b64 = os.environ["WALLET_ZIP_B64"]
wallet_password = os.environ["WALLET_PASSWORD"]
# walletμ λ©λͺ¨λ¦¬->μμλλ ν λ¦¬λ‘ λ³΅μ
wallet_dir = tempfile.mkdtemp(prefix="wallet_")
print(f"π μμ λλ ν 리: {wallet_dir}")
zip_bytes = base64.b64decode(b64)
with zipfile.ZipFile(io.BytesIO(zip_bytes)) as z:
z.extractall(wallet_dir)
# μμ μ μν΄ κΆν μΆμ
for root, _, files in os.walk(wallet_dir):
for f in files:
os.chmod(os.path.join(root, f), 0o600)
print("β
Wallet μμΆ ν΄μ μλ£")
# μ§κ° λΉλ°λ²νΈλ oracledbμμ μ¬μ©
os.environ["WALLET_DIR"] = wallet_dir
os.environ["WALLET_PASSWORD"] = wallet_password
return wallet_dir
if __name__ == "__main__":
print("=" * 60)
print("π MuscleCare FastAPI μλ² μμ")
print("=" * 60)
# νκ²½λ³μ μλ μ€μ
setup_environment()
# Wallet μ€λΉ
try:
prepare_wallet_dir()
except Exception as e:
print(f"β Wallet μ€λΉ μ€ν¨: {e}")
sys.exit(1)
# Uvicorn κΈ°λ
print("\nπ FastAPI μλ² μ€ν...")
print("=" * 60)
subprocess.run([sys.executable, "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"])
|