Spaces:
Running on Zero
Running on Zero
dexifried commited on
Commit ·
a96be22
1
Parent(s): 2349170
Architecture: Stripped deprecated pixel constraints, implemented fluid Vega-Lite containers for iOS
Browse files
app.py
CHANGED
|
@@ -28,21 +28,18 @@ DEFAULT_MODEL_REPO = "Dexifried/dex-router-model"
|
|
| 28 |
# 📊 DEEP AUDIT: VEGA-LITE ENGINE
|
| 29 |
# ==========================================
|
| 30 |
def run_lab_audit():
|
| 31 |
-
"""
|
| 32 |
if not os.path.exists(DATA_PATH):
|
| 33 |
-
|
| 34 |
-
return pd.DataFrame(columns=["Intent", "Samples", "Percentage"]), "⚠️ **CRITICAL:** `intent_dataset.csv` not found in Space."
|
| 35 |
|
| 36 |
try:
|
| 37 |
df = pd.read_csv(DATA_PATH)
|
| 38 |
total_samples = len(df)
|
| 39 |
|
| 40 |
-
# Calculate matrix distributions
|
| 41 |
counts = df['label'].value_counts().reset_index()
|
| 42 |
counts.columns = ['Intent', 'Samples']
|
| 43 |
counts['Percentage'] = (counts['Samples'] / total_samples * 100).round(2)
|
| 44 |
|
| 45 |
-
# Signal to Noise Physics
|
| 46 |
major_class = counts.iloc[0]['Intent']
|
| 47 |
minor_class = counts.iloc[-1]['Intent']
|
| 48 |
major_count = counts.iloc[0]['Samples']
|
|
@@ -69,21 +66,21 @@ def run_lab_audit():
|
|
| 69 |
return pd.DataFrame(columns=["Intent", "Samples", "Percentage"]), f"⚠️ **Audit Failed:** {str(e)}"
|
| 70 |
|
| 71 |
# ==========================================
|
| 72 |
-
# 🔥 THE OVEN: ZEROGPU
|
| 73 |
# ==========================================
|
| 74 |
@spaces.GPU(duration=600)
|
| 75 |
def start_bake(hf_token, target_repo):
|
| 76 |
-
"""
|
| 77 |
if not hf_token or len(hf_token) < 10:
|
| 78 |
raise gr.Error("Authentication Error: Valid HF Write Token required.")
|
| 79 |
|
| 80 |
if not os.path.exists(DATA_PATH):
|
| 81 |
raise gr.Error("Data Missing: intent_dataset.csv is not loaded.")
|
| 82 |
|
| 83 |
-
yield "⏳ **Phase 1/5:** Analyzing DNA & Prepping Auto-Deploy..."
|
| 84 |
-
time.sleep(1) # UX
|
| 85 |
|
| 86 |
-
# --- 1. Repository Auto-Provisioning (The 1GB
|
| 87 |
try:
|
| 88 |
api = HfApi()
|
| 89 |
create_repo(
|
|
@@ -94,18 +91,16 @@ def start_bake(hf_token, target_repo):
|
|
| 94 |
private=True
|
| 95 |
)
|
| 96 |
except Exception as e:
|
| 97 |
-
raise gr.Error(f"Repo Setup Failed.
|
| 98 |
|
| 99 |
-
# --- 2. Dynamic DNA Mapping ---
|
| 100 |
df = pd.read_csv(DATA_PATH)
|
| 101 |
unique_labels = sorted(df['label'].unique().tolist())
|
| 102 |
num_labels = len(unique_labels)
|
| 103 |
label2id = {label: i for i, label in enumerate(unique_labels)}
|
| 104 |
id2label = {i: label for i, label in enumerate(unique_labels)}
|
| 105 |
|
| 106 |
-
yield f"🧬 **Phase 2/5:** Tokenizing {len(df)} samples across {num_labels} intents..."
|
| 107 |
|
| 108 |
-
# --- 3. Tokenization ---
|
| 109 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 110 |
def tokenize_func(examples):
|
| 111 |
return tokenizer(examples["text"], truncation=True, padding=True, max_length=128)
|
|
@@ -114,8 +109,7 @@ def start_bake(hf_token, target_repo):
|
|
| 114 |
dataset = Dataset.from_pandas(df)
|
| 115 |
tokenized_dataset = dataset.map(tokenize_func, batched=True)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
yield "🧠 **Phase 3/5:** Instantiating ModernBERT & Allocating ZeroGPU Hardware..."
|
| 119 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 120 |
BASE_MODEL_ID,
|
| 121 |
num_labels=num_labels,
|
|
@@ -144,24 +138,21 @@ def start_bake(hf_token, target_repo):
|
|
| 144 |
data_collator=DataCollatorWithPadding(tokenizer=tokenizer),
|
| 145 |
)
|
| 146 |
|
| 147 |
-
# --- 5. The Bake ---
|
| 148 |
yield "🔥 **Phase 4/5:** Synaptic Bake Commencing! A100 Matrix engaged (Est. 30-45s)..."
|
| 149 |
try:
|
| 150 |
trainer.train()
|
| 151 |
|
| 152 |
-
# Save locally to container RAM
|
| 153 |
model.save_pretrained(OUTPUT_DIR)
|
| 154 |
tokenizer.save_pretrained(OUTPUT_DIR)
|
| 155 |
|
| 156 |
-
yield "📡 **Phase 5/5:** Bake Complete. Bypassing 1GB limit ->
|
| 157 |
|
| 158 |
-
# --- 6. The 50GB Limit Upload Hook ---
|
| 159 |
api.upload_folder(
|
| 160 |
folder_path=OUTPUT_DIR,
|
| 161 |
repo_id=target_repo,
|
| 162 |
repo_type="model",
|
| 163 |
token=hf_token,
|
| 164 |
-
commit_message=f"Dex
|
| 165 |
)
|
| 166 |
|
| 167 |
yield f"✅ **SUCCESS:** {num_labels}-Intent Brain deployed to `https://huggingface.co/{target_repo}`"
|
|
@@ -169,38 +160,46 @@ def start_bake(hf_token, target_repo):
|
|
| 169 |
raise gr.Error(f"Lab Failure during execution: {str(e)}")
|
| 170 |
|
| 171 |
# ==========================================
|
| 172 |
-
# 📱 MOBILE-NATIVE UX (iPhone 12 Mini
|
| 173 |
# ==========================================
|
| 174 |
custom_css = """
|
| 175 |
-
/*
|
| 176 |
.gr-button-primary {
|
| 177 |
-
background: linear-gradient(135deg, #
|
| 178 |
border: none !important;
|
| 179 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.
|
| 180 |
-
transition:
|
| 181 |
}
|
| 182 |
.gr-button-primary:active {
|
| 183 |
-
transform: scale(0.
|
|
|
|
| 184 |
}
|
| 185 |
.gr-box {
|
| 186 |
border-radius: 16px !important;
|
| 187 |
-
border: 1px solid rgba(255,255,255,0.
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
-
/* Force inputs to
|
| 190 |
input, textarea { font-size: 16px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
"""
|
| 192 |
|
| 193 |
-
# Note: CSS and Theme parameters are now strictly bound to the launch() method per Gradio 6.0 specs
|
| 194 |
with gr.Blocks(title="Dex Mission Control") as demo:
|
| 195 |
|
| 196 |
-
with gr.Column(
|
| 197 |
gr.Markdown("# 🧠 Dex Sovereign Lab")
|
| 198 |
gr.Markdown("*High-Fidelity Neural Baking & Diagnostics*")
|
| 199 |
|
| 200 |
with gr.Tabs():
|
| 201 |
# --- TAB 1: THE OVEN ---
|
| 202 |
with gr.TabItem("🔥 The Oven"):
|
| 203 |
-
gr.Markdown("
|
| 204 |
|
| 205 |
with gr.Column(variant="panel"):
|
| 206 |
token_input = gr.Textbox(
|
|
@@ -211,13 +210,11 @@ with gr.Blocks(title="Dex Mission Control") as demo:
|
|
| 211 |
repo_input = gr.Textbox(
|
| 212 |
label="Target Repository",
|
| 213 |
value=DEFAULT_MODEL_REPO,
|
| 214 |
-
info="Auto-provisioned
|
| 215 |
)
|
| 216 |
|
| 217 |
bake_btn = gr.Button("🚀 INITIATE BAKE & DEPLOY", variant="primary", size="lg")
|
| 218 |
-
|
| 219 |
-
# Real-time Telemetry feed
|
| 220 |
-
status_output = gr.Markdown("### 💤 Telemetry: Idle\nWaiting for deployment authorization.")
|
| 221 |
|
| 222 |
bake_btn.click(
|
| 223 |
fn=start_bake,
|
|
@@ -227,21 +224,19 @@ with gr.Blocks(title="Dex Mission Control") as demo:
|
|
| 227 |
|
| 228 |
# --- TAB 2: DNA AUDIT ---
|
| 229 |
with gr.TabItem("📊 DNA Audit"):
|
| 230 |
-
gr.Markdown("Verify dataset equilibrium natively on
|
| 231 |
|
| 232 |
audit_btn = gr.Button("🔍 Execute Deep Audit", variant="secondary", size="lg")
|
| 233 |
-
|
| 234 |
audit_md = gr.Markdown("Hit the button to analyze `intent_dataset.csv`.")
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
)
|
| 245 |
|
| 246 |
audit_btn.click(
|
| 247 |
fn=run_lab_audit,
|
|
@@ -250,7 +245,6 @@ with gr.Blocks(title="Dex Mission Control") as demo:
|
|
| 250 |
)
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|
| 253 |
-
|
| 254 |
-
demo.launch(css=custom_css, theme=gr.themes.Base())
|
| 255 |
|
| 256 |
|
|
|
|
| 28 |
# 📊 DEEP AUDIT: VEGA-LITE ENGINE
|
| 29 |
# ==========================================
|
| 30 |
def run_lab_audit():
|
| 31 |
+
"""Computes the Signal-to-Noise Ratio and formats for Gradio 6 Fluid BarPlot."""
|
| 32 |
if not os.path.exists(DATA_PATH):
|
| 33 |
+
return pd.DataFrame(columns=["Intent", "Samples", "Percentage"]), "⚠️ **CRITICAL:** `intent_dataset.csv` not found."
|
|
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
df = pd.read_csv(DATA_PATH)
|
| 37 |
total_samples = len(df)
|
| 38 |
|
|
|
|
| 39 |
counts = df['label'].value_counts().reset_index()
|
| 40 |
counts.columns = ['Intent', 'Samples']
|
| 41 |
counts['Percentage'] = (counts['Samples'] / total_samples * 100).round(2)
|
| 42 |
|
|
|
|
| 43 |
major_class = counts.iloc[0]['Intent']
|
| 44 |
minor_class = counts.iloc[-1]['Intent']
|
| 45 |
major_count = counts.iloc[0]['Samples']
|
|
|
|
| 66 |
return pd.DataFrame(columns=["Intent", "Samples", "Percentage"]), f"⚠️ **Audit Failed:** {str(e)}"
|
| 67 |
|
| 68 |
# ==========================================
|
| 69 |
+
# 🔥 THE OVEN: ASYNCHRONOUS ZEROGPU SEQUENCE
|
| 70 |
# ==========================================
|
| 71 |
@spaces.GPU(duration=600)
|
| 72 |
def start_bake(hf_token, target_repo):
|
| 73 |
+
"""ZeroGPU training loop with real-time yielding and Auto-Repo Provisioning."""
|
| 74 |
if not hf_token or len(hf_token) < 10:
|
| 75 |
raise gr.Error("Authentication Error: Valid HF Write Token required.")
|
| 76 |
|
| 77 |
if not os.path.exists(DATA_PATH):
|
| 78 |
raise gr.Error("Data Missing: intent_dataset.csv is not loaded.")
|
| 79 |
|
| 80 |
+
yield "⏳ **Phase 1/5:** Analyzing DNA & Prepping Sovereign Auto-Deploy..."
|
| 81 |
+
time.sleep(1) # UX observation padding
|
| 82 |
|
| 83 |
+
# --- 1. Repository Auto-Provisioning (The 1GB Space Limit Bypass) ---
|
| 84 |
try:
|
| 85 |
api = HfApi()
|
| 86 |
create_repo(
|
|
|
|
| 91 |
private=True
|
| 92 |
)
|
| 93 |
except Exception as e:
|
| 94 |
+
raise gr.Error(f"Repo Setup Failed. Verify Write Token permissions. Error: {e}")
|
| 95 |
|
|
|
|
| 96 |
df = pd.read_csv(DATA_PATH)
|
| 97 |
unique_labels = sorted(df['label'].unique().tolist())
|
| 98 |
num_labels = len(unique_labels)
|
| 99 |
label2id = {label: i for i, label in enumerate(unique_labels)}
|
| 100 |
id2label = {i: label for i, label in enumerate(unique_labels)}
|
| 101 |
|
| 102 |
+
yield f"🧬 **Phase 2/5:** Tokenizing {len(df)} High-Fidelity samples across {num_labels} intents..."
|
| 103 |
|
|
|
|
| 104 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 105 |
def tokenize_func(examples):
|
| 106 |
return tokenizer(examples["text"], truncation=True, padding=True, max_length=128)
|
|
|
|
| 109 |
dataset = Dataset.from_pandas(df)
|
| 110 |
tokenized_dataset = dataset.map(tokenize_func, batched=True)
|
| 111 |
|
| 112 |
+
yield "🧠 **Phase 3/5:** Instantiating ModernBERT & Allocating ZeroGPU A100 Hardware..."
|
|
|
|
| 113 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 114 |
BASE_MODEL_ID,
|
| 115 |
num_labels=num_labels,
|
|
|
|
| 138 |
data_collator=DataCollatorWithPadding(tokenizer=tokenizer),
|
| 139 |
)
|
| 140 |
|
|
|
|
| 141 |
yield "🔥 **Phase 4/5:** Synaptic Bake Commencing! A100 Matrix engaged (Est. 30-45s)..."
|
| 142 |
try:
|
| 143 |
trainer.train()
|
| 144 |
|
|
|
|
| 145 |
model.save_pretrained(OUTPUT_DIR)
|
| 146 |
tokenizer.save_pretrained(OUTPUT_DIR)
|
| 147 |
|
| 148 |
+
yield "📡 **Phase 5/5:** Bake Complete. Bypassing 1GB limit -> Executing LFS Upload to Dedicated Repo..."
|
| 149 |
|
|
|
|
| 150 |
api.upload_folder(
|
| 151 |
folder_path=OUTPUT_DIR,
|
| 152 |
repo_id=target_repo,
|
| 153 |
repo_type="model",
|
| 154 |
token=hf_token,
|
| 155 |
+
commit_message=f"Dex Neural Framework: Stabilized {num_labels}-Intent Matrix"
|
| 156 |
)
|
| 157 |
|
| 158 |
yield f"✅ **SUCCESS:** {num_labels}-Intent Brain deployed to `https://huggingface.co/{target_repo}`"
|
|
|
|
| 160 |
raise gr.Error(f"Lab Failure during execution: {str(e)}")
|
| 161 |
|
| 162 |
# ==========================================
|
| 163 |
+
# 📱 MOBILE-NATIVE UX (iPhone 12 Mini Architecture)
|
| 164 |
# ==========================================
|
| 165 |
custom_css = """
|
| 166 |
+
/* Mobile-First Fluid Overrides */
|
| 167 |
.gr-button-primary {
|
| 168 |
+
background: linear-gradient(135deg, #0f2027 0%, #203a43 50%, #2c5364 100%) !important;
|
| 169 |
border: none !important;
|
| 170 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.3) !important;
|
| 171 |
+
transition: transform 0.2s ease, box-shadow 0.2s ease !important;
|
| 172 |
}
|
| 173 |
.gr-button-primary:active {
|
| 174 |
+
transform: scale(0.97);
|
| 175 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.2) !important;
|
| 176 |
}
|
| 177 |
.gr-box {
|
| 178 |
border-radius: 16px !important;
|
| 179 |
+
border: 1px solid rgba(255,255,255,0.05) !important;
|
| 180 |
+
background: rgba(20, 20, 20, 0.4) !important;
|
| 181 |
+
backdrop-filter: blur(10px);
|
| 182 |
}
|
| 183 |
+
/* Force inputs to 16px to prevent iOS Safari auto-zoom */
|
| 184 |
input, textarea { font-size: 16px !important; }
|
| 185 |
+
|
| 186 |
+
/* Fluid Chart Container for iPhone */
|
| 187 |
+
#fluid_chart {
|
| 188 |
+
width: 100% !important;
|
| 189 |
+
min-height: 400px !important;
|
| 190 |
+
}
|
| 191 |
"""
|
| 192 |
|
|
|
|
| 193 |
with gr.Blocks(title="Dex Mission Control") as demo:
|
| 194 |
|
| 195 |
+
with gr.Column():
|
| 196 |
gr.Markdown("# 🧠 Dex Sovereign Lab")
|
| 197 |
gr.Markdown("*High-Fidelity Neural Baking & Diagnostics*")
|
| 198 |
|
| 199 |
with gr.Tabs():
|
| 200 |
# --- TAB 1: THE OVEN ---
|
| 201 |
with gr.TabItem("🔥 The Oven"):
|
| 202 |
+
gr.Markdown("Execute ZeroGPU bake and route weights directly to 50GB Model Repo.")
|
| 203 |
|
| 204 |
with gr.Column(variant="panel"):
|
| 205 |
token_input = gr.Textbox(
|
|
|
|
| 210 |
repo_input = gr.Textbox(
|
| 211 |
label="Target Repository",
|
| 212 |
value=DEFAULT_MODEL_REPO,
|
| 213 |
+
info="Auto-provisioned to bypass Space constraints."
|
| 214 |
)
|
| 215 |
|
| 216 |
bake_btn = gr.Button("🚀 INITIATE BAKE & DEPLOY", variant="primary", size="lg")
|
| 217 |
+
status_output = gr.Markdown("### 💤 Telemetry: Idle\nAwaiting authorization.")
|
|
|
|
|
|
|
| 218 |
|
| 219 |
bake_btn.click(
|
| 220 |
fn=start_bake,
|
|
|
|
| 224 |
|
| 225 |
# --- TAB 2: DNA AUDIT ---
|
| 226 |
with gr.TabItem("📊 DNA Audit"):
|
| 227 |
+
gr.Markdown("Verify dataset equilibrium natively on your device.")
|
| 228 |
|
| 229 |
audit_btn = gr.Button("🔍 Execute Deep Audit", variant="secondary", size="lg")
|
|
|
|
| 230 |
audit_md = gr.Markdown("Hit the button to analyze `intent_dataset.csv`.")
|
| 231 |
|
| 232 |
+
# Fluid Flexbox Layout: width/height removed, relying on CSS & Gr.Column inheritance
|
| 233 |
+
with gr.Column(elem_id="fluid_chart"):
|
| 234 |
+
audit_plot = gr.BarPlot(
|
| 235 |
+
x="Samples", # Numerical X
|
| 236 |
+
y="Intent", # Categorical Y creates the Mobile-friendly Horizontal Chart
|
| 237 |
+
title="Neural Pathway Distribution",
|
| 238 |
+
tooltip=["Intent", "Samples", "Percentage"]
|
| 239 |
+
)
|
|
|
|
| 240 |
|
| 241 |
audit_btn.click(
|
| 242 |
fn=run_lab_audit,
|
|
|
|
| 245 |
)
|
| 246 |
|
| 247 |
if __name__ == "__main__":
|
| 248 |
+
demo.launch(css=custom_css, theme=gr.themes.Monochrome())
|
|
|
|
| 249 |
|
| 250 |
|