Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
| 7 |
+
|
| 8 |
+
# 1. Load the Brain
|
| 9 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
+
|
| 11 |
+
def analyze_trinity(text_a, label_a, text_b, label_b, text_c, label_c):
|
| 12 |
+
# FALLBACK LABELS (If user leaves them blank)
|
| 13 |
+
if not label_a.strip(): label_a = "Input A"
|
| 14 |
+
if not label_b.strip(): label_b = "Input B"
|
| 15 |
+
if not label_c.strip(): label_c = "Input C"
|
| 16 |
+
|
| 17 |
+
clean_labels = [label_a, label_b, label_c]
|
| 18 |
+
texts = [text_a.strip(), text_b.strip(), text_c.strip()]
|
| 19 |
+
|
| 20 |
+
# Validation
|
| 21 |
+
if not any(texts):
|
| 22 |
+
return pd.DataFrame(), "Waiting for signals...", "Waiting for signals..."
|
| 23 |
+
|
| 24 |
+
# --- PART 1: THE ALIGNMENT MATRIX (With Custom Names) ---
|
| 25 |
+
# We use embeddings to calculate how 'close' each text is to the others
|
| 26 |
+
embeddings = model.encode(texts)
|
| 27 |
+
matrix = cosine_similarity(embeddings)
|
| 28 |
+
|
| 29 |
+
# Create the DataFrame with YOUR custom names
|
| 30 |
+
df_matrix = pd.DataFrame(matrix, columns=clean_labels, index=clean_labels)
|
| 31 |
+
df_matrix = df_matrix.round(3)
|
| 32 |
+
|
| 33 |
+
# --- PART 2: FINGERPRINTS (Unique Vibe) ---
|
| 34 |
+
# TF-IDF to find words unique to each specific input
|
| 35 |
+
try:
|
| 36 |
+
tfidf = TfidfVectorizer(stop_words='english')
|
| 37 |
+
tfidf_matrix = tfidf.fit_transform(texts)
|
| 38 |
+
feature_names = np.array(tfidf.get_feature_names_out())
|
| 39 |
+
|
| 40 |
+
signatures = ""
|
| 41 |
+
for i, label in enumerate(clean_labels):
|
| 42 |
+
if not texts[i]: continue # Skip empty boxes
|
| 43 |
+
|
| 44 |
+
row = tfidf_matrix[i].toarray().flatten()
|
| 45 |
+
top_indices = row.argsort()[-5:][::-1]
|
| 46 |
+
top_words = feature_names[top_indices]
|
| 47 |
+
|
| 48 |
+
# Only keep words with actual weight
|
| 49 |
+
valid_words = [w for w, idx in zip(top_words, top_indices) if row[idx] > 0]
|
| 50 |
+
|
| 51 |
+
signatures += f"🔹 {label.upper()}: {', '.join(valid_words)}\n"
|
| 52 |
+
|
| 53 |
+
except:
|
| 54 |
+
signatures = "Not enough data for fingerprinting."
|
| 55 |
+
|
| 56 |
+
# --- PART 3: THE CONSENSUS (Universal vs Majority) ---
|
| 57 |
+
vectorizer = CountVectorizer(stop_words='english')
|
| 58 |
+
try:
|
| 59 |
+
dtm = vectorizer.fit_transform(texts)
|
| 60 |
+
vocab = vectorizer.get_feature_names_out()
|
| 61 |
+
presence = (dtm.toarray() > 0).astype(int)
|
| 62 |
+
|
| 63 |
+
# UNIVERSAL: Present in all 3 (Sum = 3)
|
| 64 |
+
univ_indices = np.where(presence.sum(axis=0) == 3)[0]
|
| 65 |
+
univ_words = vocab[univ_indices]
|
| 66 |
+
|
| 67 |
+
# MAJORITY: Present in 2 (Sum = 2)
|
| 68 |
+
maj_indices = np.where(presence.sum(axis=0) == 2)[0]
|
| 69 |
+
maj_words = vocab[maj_indices]
|
| 70 |
+
|
| 71 |
+
consensus_text = ""
|
| 72 |
+
|
| 73 |
+
if len(univ_words) > 0:
|
| 74 |
+
consensus_text += f"🔥 UNIVERSAL TRUTH (3/3): {', '.join(univ_words)}\n\n"
|
| 75 |
+
else:
|
| 76 |
+
consensus_text += "❌ NO UNIVERSAL TRUTH FOUND.\n\n"
|
| 77 |
+
|
| 78 |
+
if len(maj_words) > 0:
|
| 79 |
+
consensus_text += f"⚠️ MAJORITY REPORT (2/3): {', '.join(maj_words)}"
|
| 80 |
+
else:
|
| 81 |
+
consensus_text += "⚠️ NO PARTIAL ALIGNMENT FOUND."
|
| 82 |
+
|
| 83 |
+
except:
|
| 84 |
+
consensus_text = "Waiting for more data..."
|
| 85 |
+
|
| 86 |
+
return df_matrix, signatures, consensus_text
|
| 87 |
+
|
| 88 |
+
# --- UI BUILD (Flexible Inputs) ---
|
| 89 |
+
with gr.Blocks(theme=gr.themes.Glass()) as app:
|
| 90 |
+
gr.Markdown("# 🕸️ THE ARRAY v0.2")
|
| 91 |
+
gr.Markdown("### FlameTeam Multi-Model Alignment System")
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
# COLUMN 1
|
| 95 |
+
with gr.Column():
|
| 96 |
+
lbl_a = gr.Textbox(label="Label 1", value="Me (Prompt)", placeholder="Name this input...")
|
| 97 |
+
box_a = gr.TextArea(show_label=False, placeholder="Paste text here...", lines=4)
|
| 98 |
+
# COLUMN 2
|
| 99 |
+
with gr.Column():
|
| 100 |
+
lbl_b = gr.Textbox(label="Label 2", value="Model A", placeholder="Name this input...")
|
| 101 |
+
box_b = gr.TextArea(show_label=False, placeholder="Paste text here...", lines=4)
|
| 102 |
+
# COLUMN 3
|
| 103 |
+
with gr.Column():
|
| 104 |
+
lbl_c = gr.Textbox(label="Label 3", value="Model B", placeholder="Name this input...")
|
| 105 |
+
box_c = gr.TextArea(show_label=False, placeholder="Paste text here...", lines=4)
|
| 106 |
+
|
| 107 |
+
btn = gr.Button("RUN THE ARRAY", variant="primary")
|
| 108 |
+
|
| 109 |
+
# RESULTS
|
| 110 |
+
gr.Markdown("### 1. The Alignment Matrix")
|
| 111 |
+
out_matrix = gr.Dataframe(label="Cosine Similarity Grid")
|
| 112 |
+
|
| 113 |
+
with gr.Row():
|
| 114 |
+
with gr.Column():
|
| 115 |
+
gr.Markdown("### 2. Unique Fingerprints")
|
| 116 |
+
out_signatures = gr.Textbox(label="Distinctive Terms (TF-IDF)", lines=5)
|
| 117 |
+
with gr.Column():
|
| 118 |
+
gr.Markdown("### 3. The Consensus")
|
| 119 |
+
out_consensus = gr.Textbox(label="Shared Concepts", lines=5)
|
| 120 |
+
|
| 121 |
+
btn.click(analyze_trinity, inputs=[box_a, lbl_a, box_b, lbl_b, box_c, lbl_c], outputs=[out_matrix, out_signatures, out_consensus])
|
| 122 |
+
|
| 123 |
+
app.launch()
|