fullhtml / app.py
arudradey's picture
Update app.py
056dea6 verified
Raw
History Blame Contribute Delete
3.99 kB
import gradio as gr
import subprocess
import os
import time
import shutil
from huggingface_hub import HfApi, snapshot_download, list_repo_files
# --- 1. CONFIGURATION ---
STORAGE_REPO = "arudradey/flst"
LOCAL_ROOT = "/tmp/emsdk"
DATA_DIR = "/data"
# --- 2. FUNCTIONS (Defined before UI) ---
def verify_and_repair_sdk():
"""Compares local /tmp/emsdk with arudradey/flst to ensure no files are missed."""
print("πŸ” Starting Integrity Check against arudradey/flst...")
if not os.path.exists(LOCAL_ROOT):
return "❌ Local SDK directory missing. Please run install/boot first."
try:
# Get the list of truth from the repo
repo_files = list_repo_files(repo_id=STORAGE_REPO, repo_type="model")
# Smart download: snapshot_download only fetches missing or changed files
snapshot_download(
repo_id=STORAGE_REPO,
repo_type="model",
local_dir=LOCAL_ROOT,
token=os.environ.get("HF_TOKEN")
)
# Verify local count for logs
local_count = 0
for root, dirs, files in os.walk(LOCAL_ROOT):
local_count += len(files)
return f"βœ… Integrity Verified. Repo files: {len(repo_files)} | Local files: {local_count}. System is 100% synced."
except Exception as e:
return f"❌ Integrity Check/Repair Failed: {e}"
def boot_system():
"""Initial boot logic: tries to pull from storage or clean-installs if empty."""
if os.path.exists(os.path.join(LOCAL_ROOT, "upstream", "emscripten")):
return "βœ… SDK ready in RAM."
try:
print(f"πŸ” Checking Model Repo {STORAGE_REPO}...")
snapshot_download(
repo_id=STORAGE_REPO,
local_dir=LOCAL_ROOT,
token=os.environ.get("HF_TOKEN")
)
if os.path.exists(os.path.join(LOCAL_ROOT, "emsdk_env.sh")):
return "πŸš€ Fast-boot: SDK pulled from storage."
except Exception as e:
print(f"ℹ️ Storage empty or inaccessible. Preparing fresh install...")
# Cleanup to avoid 'git clone' into non-empty directory error
if os.path.exists(LOCAL_ROOT):
shutil.rmtree(LOCAL_ROOT)
print("πŸ“₯ Installing fresh SDK...")
subprocess.run(f"git clone https://github.com/emscripten-core/emsdk.git {LOCAL_ROOT}", shell=True, check=True)
subprocess.run(f"{LOCAL_ROOT}/emsdk install latest", shell=True, check=True)
subprocess.run(f"{LOCAL_ROOT}/emsdk activate latest", shell=True, check=True)
return "βœ… Fresh install complete."
def save_large_to_repo():
"""Uses Large Folder API to handle the 2.6GB and 50k+ files."""
api = HfApi()
try:
api.upload_large_folder(
folder_path=LOCAL_ROOT,
repo_id=STORAGE_REPO,
repo_type="model",
token=os.environ.get("HF_TOKEN")
)
return "πŸ”₯ Successfully saved 2.6GB build to arudradey/flst!"
except Exception as e:
return f"❌ Upload failed: {e}"
# --- 3. INITIALIZATION ---
boot_msg = boot_system()
# Set PATH environment variables
EMCC_BIN = os.path.join(LOCAL_ROOT, "upstream", "emscripten")
CLANG_DIR = os.path.join(LOCAL_ROOT, "upstream", "bin")
os.environ["PATH"] = f"{EMCC_BIN}:{CLANG_DIR}:" + os.environ["PATH"]
# --- 4. GRADIO UI ---
with gr.Blocks(title="Emscripten Cloud") as demo:
gr.Markdown(f"### πŸ›‘οΈ SDK Control Center\n**Status:** {boot_msg}")
with gr.Row():
check_btn = gr.Button("πŸ” VERIFY INTEGRITY", variant="secondary")
save_btn = gr.Button("πŸ’Ύ UPLOAD LARGE FOLDER", variant="primary")
test_btn = gr.Button("πŸ§ͺ TEST COMPILER")
logs = gr.Textbox(label="System Output", lines=12)
# Event handlers (Functions are now defined)
check_btn.click(verify_and_repair_sdk, None, logs)
save_btn.click(save_large_to_repo, None, logs)
test_btn.click(lambda: subprocess.getoutput("emcc --version"), None, logs)
demo.launch()