File size: 4,941 Bytes
19c34a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0403faa
 
19c34a0
 
0403faa
 
19c34a0
 
0403faa
 
19c34a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
import pandas as pd
import numpy as np
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

# 1. Load the Brain
model = SentenceTransformer('all-MiniLM-L6-v2')

def analyze_trinity(text_a, label_a, text_b, label_b, text_c, label_c):
    # FALLBACK LABELS (If user leaves them blank)
    if not label_a.strip(): label_a = "Input A"
    if not label_b.strip(): label_b = "Input B"
    if not label_c.strip(): label_c = "Input C"
    
    clean_labels = [label_a, label_b, label_c]
    texts = [text_a.strip(), text_b.strip(), text_c.strip()]
    
    # Validation
    if not any(texts):
        return pd.DataFrame(), "Waiting for signals...", "Waiting for signals..."

    # --- PART 1: THE ALIGNMENT MATRIX (With Custom Names) ---
    # We use embeddings to calculate how 'close' each text is to the others
    embeddings = model.encode(texts)
    matrix = cosine_similarity(embeddings)
    
    # Create the DataFrame with YOUR custom names
    df_matrix = pd.DataFrame(matrix, columns=clean_labels, index=clean_labels)
    df_matrix = df_matrix.round(3)

    # --- PART 2: FINGERPRINTS (Unique Vibe) ---
    # TF-IDF to find words unique to each specific input
    try:
        tfidf = TfidfVectorizer(stop_words='english')
        tfidf_matrix = tfidf.fit_transform(texts)
        feature_names = np.array(tfidf.get_feature_names_out())
        
        signatures = ""
        for i, label in enumerate(clean_labels):
            if not texts[i]: continue # Skip empty boxes
            
            row = tfidf_matrix[i].toarray().flatten()
            top_indices = row.argsort()[-5:][::-1]
            top_words = feature_names[top_indices]
            
            # Only keep words with actual weight
            valid_words = [w for w, idx in zip(top_words, top_indices) if row[idx] > 0]
            
            signatures += f"🔹 {label.upper()}: {', '.join(valid_words)}\n"
            
    except:
        signatures = "Not enough data for fingerprinting."

    # --- PART 3: THE CONSENSUS (Universal vs Majority) ---
    vectorizer = CountVectorizer(stop_words='english')
    try:
        dtm = vectorizer.fit_transform(texts)
        vocab = vectorizer.get_feature_names_out()
        presence = (dtm.toarray() > 0).astype(int)
        
        # UNIVERSAL: Present in all 3 (Sum = 3)
        univ_indices = np.where(presence.sum(axis=0) == 3)[0]
        univ_words = vocab[univ_indices]
        
        # MAJORITY: Present in 2 (Sum = 2)
        maj_indices = np.where(presence.sum(axis=0) == 2)[0]
        maj_words = vocab[maj_indices]
        
        consensus_text = ""
        
        if len(univ_words) > 0:
            consensus_text += f"🔥 UNIVERSAL TRUTH (3/3): {', '.join(univ_words)}\n\n"
        else:
            consensus_text += "❌ NO UNIVERSAL TRUTH FOUND.\n\n"
            
        if len(maj_words) > 0:
            consensus_text += f"⚠️ MAJORITY REPORT (2/3): {', '.join(maj_words)}"
        else:
            consensus_text += "⚠️ NO PARTIAL ALIGNMENT FOUND."
            
    except:
        consensus_text = "Waiting for more data..."

    return df_matrix, signatures, consensus_text

# --- UI BUILD (Flexible Inputs) ---
with gr.Blocks(theme=gr.themes.Glass()) as app:
    gr.Markdown("# 🕸️ THE ARRAY v0.2")
    gr.Markdown("### FlameTeam Multi-Model Alignment System")
    
    with gr.Row():
        # COLUMN 1
        with gr.Column(min_width=100):
            lbl_a = gr.Textbox(label="Label 1", value="Model A", placeholder="Name this input...")
            box_a = gr.TextArea(show_label=False, placeholder="Paste text here...", lines=4)
        # COLUMN 2
        with gr.Column(min_width=100):
            lbl_b = gr.Textbox(label="Label 2", value="Model B", placeholder="Name this input...")
            box_b = gr.TextArea(show_label=False, placeholder="Paste text here...", lines=4)
        # COLUMN 3
        with gr.Column(min_width=100):
            lbl_c = gr.Textbox(label="Label 3", value="Model C", placeholder="Name this input...")
            box_c = gr.TextArea(show_label=False, placeholder="Paste text here...", lines=4)
        
    btn = gr.Button("RUN THE ARRAY", variant="primary")
    
    # RESULTS
    gr.Markdown("### 1. The Alignment Matrix")
    out_matrix = gr.Dataframe(label="Cosine Similarity Grid")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### 2. Unique Fingerprints")
            out_signatures = gr.Textbox(label="Distinctive Terms (TF-IDF)", lines=5)
        with gr.Column():
            gr.Markdown("### 3. The Consensus")
            out_consensus = gr.Textbox(label="Shared Concepts", lines=5)

    btn.click(analyze_trinity, inputs=[box_a, lbl_a, box_b, lbl_b, box_c, lbl_c], outputs=[out_matrix, out_signatures, out_consensus])

app.launch()