GirishaBuilds01 commited on
Commit
15d573f
·
verified ·
1 Parent(s): 8c34f70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -13
app.py CHANGED
@@ -1,30 +1,51 @@
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
 
@@ -34,22 +55,56 @@ def run_cortex(model_choice, calibration_text):
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import torch
3
+ import pandas as pd
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.visualization import create_heatmap
10
 
11
+ # =====================================================
12
+ # 🔥 LOAD MODEL ONCE AT STARTUP (IMPORTANT)
13
+ # =====================================================
14
+
15
+ print("Loading model at startup...")
16
+
17
+ MODEL_CHOICE_DEFAULT = "DistilGPT2 (Fast CPU)"
18
+ model, tokenizer = load_model(MODEL_CHOICE_DEFAULT)
19
+
20
+ if tokenizer.pad_token is None:
21
+ tokenizer.pad_token = tokenizer.eos_token
22
+
23
+ model.eval()
24
 
25
+ print("Model loaded successfully.")
 
 
26
 
27
+ # =====================================================
28
+ # MAIN FUNCTION
29
+ # =====================================================
30
+
31
+ def run_cortex(calibration_text):
32
+
33
+ try:
34
  texts = calibration_text.strip().split("\n")
35
  texts = [t.strip() for t in texts if t.strip() != ""]
36
 
37
  if len(texts) == 0:
38
  return {"error": "No valid input sentences provided."}, None, 0.0, None
39
 
40
+ inputs = tokenizer(
41
+ texts,
42
+ return_tensors="pt",
43
+ padding=True,
44
+ truncation=True,
45
+ max_length=64
46
+ )
47
 
48
+ # ===== Activation Profiling =====
49
  profiler = ActivationProfiler(model)
50
  profiler.register()
51
 
 
55
  profiler.remove()
56
  stats = profiler.get()
57
 
58
+ # ===== SAFE LIMITED STRESS TEST =====
59
  stress_results = {}
60
+ layer_names = list(stats.keys())[:3] # 🔥 LIMIT TO 3 LAYERS
61
 
62
+ for name in layer_names:
 
63
  mse_val, kl_val = stress_layer(model, name, inputs, bits=8)
64
  stress_results[name] = (mse_val, kl_val)
65
 
66
+ # ===== Sensitivity Index =====
67
  scores = compute_si(stats, stress_results)
68
  policy = assign_policy(scores)
69
 
70
+ # Skip heavy reevaluation for HF CPU
71
+ degradation = 0.0
72
 
73
  heatmap = create_heatmap(scores)
74
 
75
+ df = pd.DataFrame({
76
+ "Layer": list(scores.keys()),
77
+ "Sensitivity_Index": list(scores.values()),
78
+ "Policy": [policy.get(k, "FP16") for k in scores]
79
+ })
80
+
81
+ csv_path = "layer_metrics.csv"
82
+ df.to_csv(csv_path, index=False)
83
+
84
+ return policy, heatmap, degradation, csv_path
85
 
86
  except Exception as e:
87
  return {"error": str(e)}, None, 0.0, None
88
 
89
+
90
+ # =====================================================
91
+ # GRADIO UI
92
+ # =====================================================
93
+
94
+ demo = gr.Interface(
95
+ fn=run_cortex,
96
+ inputs=gr.Textbox(
97
+ label="Calibration Text (One sentence per line)",
98
+ lines=6
99
+ ),
100
+ outputs=[
101
+ gr.JSON(label="Precision Policy"),
102
+ gr.Image(label="Critical Layer Map"),
103
+ gr.Number(label="Model Degradation (Approx)"),
104
+ gr.File(label="Download CSV Metrics")
105
+ ],
106
+ title="Cortex in the Loop — HF Stable Mode",
107
+ description="Runtime Sensitivity Analyzer (HF CPU Safe Version)"
108
+ )
109
+
110
+ demo.launch(server_name="0.0.0.0", server_port=7860)