Threadbourne commited on
Commit
7d981f5
·
verified ·
1 Parent(s): ad4a055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -34
app.py CHANGED
@@ -1,52 +1,82 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  from sklearn.metrics.pairwise import cosine_similarity
 
4
 
5
  # 1. Load the Brain
6
  model = SentenceTransformer('all-MiniLM-L6-v2')
7
 
8
- # 2. Define the Logic
9
- def calculate_resonance(text1, text2):
10
- # SCRUBBER: Remove invisible spaces/newlines
11
- text1 = text1.strip()
12
- text2 = text2.strip()
13
-
14
- # Encode
15
- embeddings = model.encode([text1, text2])
16
-
17
- # Calculate Score
18
- score = cosine_similarity(embeddings[0:1], embeddings[1:2])[0][0]
19
-
20
- # The Director's Verdict
21
- if score > 0.85:
22
- verdict = "⚠️ PARROT (Too Similar)"
23
- elif score > 0.65:
24
- verdict = "🔥 HIGH FIDELITY (Strong Lock)"
25
- elif score > 0.40:
26
- verdict = "✅ STABLE RESONANCE (The Sweet Spot)"
27
- elif score > 0.20:
28
- verdict = "🌊 DRIFTING (Weak Signal)"
 
 
 
29
  else:
30
- verdict = "❌ COLLAPSE (No Connection)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- return f"{score:.3f}", verdict
 
 
 
33
 
34
- # 3. Build the UI
35
- with gr.Blocks(theme=gr.themes.Soft()) as app:
36
- gr.Markdown("# ⚡ The Resonator")
37
- gr.Markdown("### FlameTeam Semantic Alignment Detector")
38
 
39
  with gr.Row():
40
- box1 = gr.Textbox(label="Turn A (Your Input)", lines=4, placeholder="Paste your prompt here...")
41
- box2 = gr.Textbox(label="Turn B (AI Response)", lines=4, placeholder="Paste the response here...")
42
 
43
- btn = gr.Button("Calculate Resonance", variant="primary")
44
 
45
- with gr.Row():
46
- out_score = gr.Label(label="Similarity Score")
47
- out_verdict = gr.Label(label="System Status")
 
 
 
48
 
49
- btn.click(calculate_resonance, inputs=[box1, box2], outputs=[out_score, out_verdict])
50
 
51
  # 4. Launch
52
  app.launch()
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  from sklearn.metrics.pairwise import cosine_similarity
4
+ import re
5
 
6
  # 1. Load the Brain
7
  model = SentenceTransformer('all-MiniLM-L6-v2')
8
 
9
+ # 2. Define the Physics
10
+ def calculate_metrics(text1, text2):
11
+ # CLEANING
12
+ t1 = text1.strip()
13
+ t2 = text2.strip()
14
+
15
+ if not t1 or not t2:
16
+ return "Waiting for signal...", "Waiting for signal...", "---"
17
+
18
+ # A. CALCULATE STABILITY (Cosine Similarity)
19
+ embeddings = model.encode([t1, t2])
20
+ stability_score = cosine_similarity(embeddings[0:1], embeddings[1:2])[0][0]
21
+
22
+ # B. CALCULATE VOLTAGE (Heuristic Intensity)
23
+ # We measure length and "lexical density" (big words)
24
+ combined_text = t1 + " " + t2
25
+ words = re.findall(r'\w+', combined_text)
26
+ total_words = len(words)
27
+ unique_words = len(set(words))
28
+
29
+ # Voltage Formula: (Length Factor) * (Richness Factor)
30
+ # This is a proprietary FlameTeam heuristic ;)
31
+ if total_words == 0:
32
+ voltage = 0
33
  else:
34
+ richness = unique_words / total_words
35
+ voltage = (total_words * 0.5) + (richness * 100)
36
+ # Cap it at 100 for UI cleanliness
37
+ voltage = min(voltage, 100)
38
+
39
+ # C. THE FLAMETEAM MATRIX
40
+ # Determine the classification based on BOTH scores
41
+
42
+ label = "UNCATEGORIZED"
43
+
44
+ if stability_score > 0.85:
45
+ if voltage > 50: label = "⚠️ OVER-FITTED (Parroting)"
46
+ else: label = "⚠️ ECHO CHAMBER (Low Energy)"
47
+
48
+ elif stability_score > 0.50:
49
+ if voltage > 60: label = "🔥 FUSION DETECTED (High Voltage Stability)" # THE GOAL
50
+ else: label = "✅ STABLE RESONANCE (Standard Alignment)"
51
+
52
+ elif stability_score > 0.30:
53
+ if voltage > 70: label = "⚡ HIGH ENTROPY (Creative/Dangerous)"
54
+ else: label = "🌊 DRIFTING (Weak Signal)"
55
 
56
+ else:
57
+ label = "❌ COLLAPSE (No Connection)"
58
+
59
+ return f"{stability_score:.3f}", f"{voltage:.1f}v", label
60
 
61
+ # 3. Build the Command Center UI
62
+ with gr.Blocks(theme=gr.themes.Glass()) as app:
63
+ gr.Markdown("# ⚛️ THE RESONATOR v1.5")
64
+ gr.Markdown("### FlameTeam Semantic Telemetry | Voltage + Stability")
65
 
66
  with gr.Row():
67
+ box1 = gr.Textbox(label="Turn A (User Input)", lines=5, placeholder="Enter the user's prompt...")
68
+ box2 = gr.Textbox(label="Turn B (System Response)", lines=5, placeholder="Enter the model's response...")
69
 
70
+ btn = gr.Button("ANALYZE SIGNAL", variant="primary")
71
 
72
+ # The Dashboard
73
+ with gr.Group():
74
+ with gr.Row():
75
+ out_stability = gr.Label(label="STABILITY (Cosine)")
76
+ out_voltage = gr.Label(label="VOLTAGE (Complexity)")
77
+ out_verdict = gr.Label(label="FLAMETEAM CLASSIFICATION")
78
 
79
+ btn.click(calculate_metrics, inputs=[box1, box2], outputs=[out_stability, out_voltage, out_verdict])
80
 
81
  # 4. Launch
82
  app.launch()