Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 9 |
-
def
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# 3. Build the UI
|
| 35 |
-
with gr.Blocks(theme=gr.themes.
|
| 36 |
-
gr.Markdown("#
|
| 37 |
-
gr.Markdown("### FlameTeam Semantic
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
-
box1 = gr.Textbox(label="Turn A (
|
| 41 |
-
box2 = gr.Textbox(label="Turn B (
|
| 42 |
|
| 43 |
-
btn = gr.Button("
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
btn.click(
|
| 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()
|