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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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)