GirishaBuilds01 commited on
Commit
004763d
·
verified ·
1 Parent(s): bad889b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -43
app.py CHANGED
@@ -9,62 +9,47 @@ from core.sensitivity import compute_si, assign_policy
9
  from core.reevaluator import reevaluate
10
  from core.visualization import create_heatmap
11
 
12
-
13
  def run_cortex(model_choice, calibration_text):
 
 
14
 
15
- model, tokenizer = load_model(model_choice)
16
-
17
- texts = calibration_text.split("\n")
18
- inputs = tokenizer(texts, return_tensors="pt", padding=True)
19
 
20
- profiler = ActivationProfiler(model)
21
- profiler.register()
22
 
23
- with torch.no_grad():
24
- model(**inputs)
25
 
26
- profiler.remove()
27
- stats = profiler.get()
28
 
29
- stress_results = {}
 
30
 
31
- for name in stats:
32
- mse_val, kl_val = stress_layer(model, name, inputs, bits=8)
33
- stress_results[name] = (mse_val, kl_val)
34
 
35
- scores = compute_si(stats, stress_results)
36
- policy = assign_policy(scores)
37
 
38
- policy, degradation = reevaluate(model, tokenizer, texts, policy)
39
 
40
- heatmap = create_heatmap(scores)
 
 
 
41
 
42
- df = pd.DataFrame({
43
- "Layer": list(scores.keys()),
44
- "Sensitivity_Index": list(scores.values()),
45
- "Policy": [policy[k] for k in scores]
46
- })
47
 
48
- csv_path = "layer_metrics.csv"
49
- df.to_csv(csv_path, index=False)
50
 
51
- return policy, heatmap, degradation, csv_path
52
 
 
53
 
54
- demo = gr.Interface(
55
- fn=run_cortex,
56
- inputs=[
57
- gr.Dropdown(list(SUPPORTED_MODELS.keys()), label="Select Model"),
58
- gr.Textbox(label="Calibration Text (One sentence per line)", lines=8)
59
- ],
60
- outputs=[
61
- gr.JSON(label="Precision Policy"),
62
- gr.Image(label="Critical Layer Map"),
63
- gr.Number(label="Final Model Degradation (MSE)"),
64
- gr.File(label="Download CSV Metrics")
65
- ],
66
- title="Cortex in the Loop — Research Edition",
67
- description="Runtime Sensitivity Analyzer with Batch Calibration and CSV Export"
68
- )
69
 
70
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
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