Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,55 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
from core.model_loader import load_model, SUPPORTED_MODELS
|
| 6 |
-
from core.profiler import ActivationProfiler
|
| 7 |
-
from core.stress import stress_layer
|
| 8 |
-
from core.sensitivity import compute_si, assign_policy
|
| 9 |
-
from core.reevaluator import reevaluate
|
| 10 |
-
from core.visualization import create_heatmap
|
| 11 |
-
|
| 12 |
-
def run_cortex(model_choice, calibration_text):
|
| 13 |
-
try:
|
| 14 |
-
model, tokenizer = load_model(model_choice)
|
| 15 |
-
|
| 16 |
-
# Fix GPT2 padding issue
|
| 17 |
-
if tokenizer.pad_token is None:
|
| 18 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
-
|
| 20 |
-
texts = calibration_text.strip().split("\n")
|
| 21 |
-
texts = [t.strip() for t in texts if t.strip() != ""]
|
| 22 |
-
|
| 23 |
-
if len(texts) == 0:
|
| 24 |
-
return {"error": "No valid input sentences provided."}, None, 0.0, None
|
| 25 |
-
|
| 26 |
-
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
| 27 |
-
|
| 28 |
-
profiler = ActivationProfiler(model)
|
| 29 |
-
profiler.register()
|
| 30 |
-
|
| 31 |
-
with torch.no_grad():
|
| 32 |
-
model(**inputs)
|
| 33 |
-
|
| 34 |
-
profiler.remove()
|
| 35 |
-
stats = profiler.get()
|
| 36 |
-
|
| 37 |
-
stress_results = {}
|
| 38 |
-
|
| 39 |
-
# 🔥 LIMIT layers for stability (important for HF CPU)
|
| 40 |
-
for name in list(stats.keys())[:6]:
|
| 41 |
-
mse_val, kl_val = stress_layer(model, name, inputs, bits=8)
|
| 42 |
-
stress_results[name] = (mse_val, kl_val)
|
| 43 |
-
|
| 44 |
-
scores = compute_si(stats, stress_results)
|
| 45 |
-
policy = assign_policy(scores)
|
| 46 |
-
|
| 47 |
-
policy, degradation = reevaluate(model, tokenizer, texts, policy)
|
| 48 |
-
|
| 49 |
-
heatmap = create_heatmap(scores)
|
| 50 |
-
|
| 51 |
-
return policy, heatmap, float(degradation), "layer_metrics.csv"
|
| 52 |
-
|
| 53 |
-
except Exception as e:
|
| 54 |
-
return {"error": str(e)}, None, 0.0, None
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|