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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import streamlit as st
import json
import numpy as np
import joblib
from collections import defaultdict
from transformers import AutoTokenizer, AutoModel
from sklearn.metrics.pairwise import cosine_similarity
import torch
import re
from typing import List, Dict, Any
from openai import OpenAI
from dotenv import load_dotenv
import os
from sentence_transformers import SentenceTransformer
from rdflib import Graph, Namespace, URIRef, Literal, RDF, RDFS, XSD
import os
import networkx as nx
from pyvis.network import Network
import streamlit.components.v1 as components

# ---------------------------
# LegalBERT-based compliance checker
# ---------------------------
class GDPRComplianceChecker:
    def __init__(self, model_name="nlpaueb/bert-base-uncased-eurlex"):
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = AutoModel.from_pretrained(model_name)
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model.to(self.device).eval()

    def get_embeddings(self, texts):
        embeddings = []
        for text in texts:
            inputs = self.tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            with torch.no_grad():
                output = self.model(**inputs)
                embedding = output.last_hidden_state[:, 0, :].cpu().numpy()
                embeddings.append(embedding[0])
        return np.array(embeddings)

    def chunk_policy_text(self, text, chunk_size=500):
        paragraphs = re.split(r'\n{2,}|\.\s+', text)
        chunks, current = [], ""
        for para in paragraphs:
            if len(current) + len(para) < chunk_size:
                current += " " + para
            else:
                chunks.append(current.strip())
                current = para
        if current:
            chunks.append(current.strip())
        return [chunk for chunk in chunks if len(chunk) > 50]

    def load_gdpr_articles(self, gdpr_json):
        gdpr_map, texts = {}, []
        for article in gdpr_json:
            number, title = article["article_number"], article["article_title"]
            body = " ".join([f"{k} {v}" for sec in article["sections"] for k, v in sec.items()])
            full_text = f"Article {number}: {title}. {body}"
            gdpr_map[number] = {"title": title, "text": full_text}
            texts.append(full_text)

        embeddings = self.get_embeddings(texts)
        return gdpr_map, embeddings

    def calculate_compliance_score(self, policy_text, gdpr_map, gdpr_embeddings):
        chunks = self.chunk_policy_text(policy_text)
        if not chunks:
            return {"error": "Policy has no meaningful chunks."}
        chunk_embeddings = self.get_embeddings(chunks)
        sim_matrix = cosine_similarity(gdpr_embeddings, chunk_embeddings)

        article_scores = {}
        presence_threshold = 0.35
        total_score, counted_articles = 0, 0

        for i, (art_num, art_data) in enumerate(gdpr_map.items()):
            max_sim = np.max(sim_matrix[i])
            best_idx = np.argmax(sim_matrix[i])

            if max_sim < presence_threshold:
                continue

            score_pct = min(100, max(0, (max_sim - presence_threshold) / (1 - presence_threshold) * 100))
            article_scores[art_num] = {
                "article_title": art_data["title"],
                "compliance_percentage": round(score_pct, 2),
                "similarity_score": round(max_sim, 4),
                "matched_text_snippet": chunks[best_idx][:300] + "..."
            }
            total_score += score_pct
            counted_articles += 1

        overall = round(total_score / counted_articles, 2) if counted_articles else 0
        return {
            "overall_compliance_percentage": overall,
            "relevant_articles_analyzed": counted_articles,
            "total_policy_chunks": len(chunks),
            "article_scores": article_scores
        }


def chunk_policy_text(text, chunk_size=500):
    import re
    paragraphs = re.split(r'\n{2,}|\.\s+', text)
    chunks, current = [], ""
    for para in paragraphs:
        if len(current) + len(para) < chunk_size:
            current += " " + para
        else:
            chunks.append(current.strip())
            current = para
    if current:
        chunks.append(current.strip())
    return [chunk for chunk in chunks if len(chunk) > 50]

def prepare_article_text(article: Dict[str, Any]) -> str:
    body = " ".join(
        " ".join(sec.values()) if isinstance(sec, dict) else str(sec)
        for sec in article.get("sections", [])
    )
    return f"Art. {article['article_number']} – {article['article_title']} {body}"

def get_embedding(text: str) -> List[float]:
    # If input is a list of strings, clean each string
    if isinstance(text, list):
        cleaned_text = [t.replace("\n", " ") for t in text]
    else:  # single string
        cleaned_text = text.replace("\n", " ")
    resp = client.embeddings.create(model=EMBED_MODEL, input=cleaned_text)
    if isinstance(cleaned_text, list):
        return [item.embedding for item in resp.data]
    else:
        return resp.data[0].embedding

def rdflib_to_networkx(rdflib_graph):
    nx_graph = nx.MultiDiGraph()
    for s, p, o in rdflib_graph:
        nx_graph.add_edge(str(s), str(o), label=str(p))
    return nx_graph

def draw_pyvis_graph(nx_graph):
    net = Network(height="600px", width="100%", directed=True, notebook=False)
    net.from_nx(nx_graph)
    net.repulsion(node_distance=200, central_gravity=0.33, spring_length=100, spring_strength=0.10, damping=0.95)
    return net
# ---------------------------
# Streamlit interface
# ---------------------------
st.set_page_config(page_title="GDPR Compliance Checker", layout="wide")
st.title("πŸ›‘οΈ GDPR Compliance Checker")

with st.sidebar:
    st.header("Upload Files")
    gdpr_file = st.file_uploader("GDPR JSON File", type=["json"])
    policy_file = st.file_uploader("Company Policy (.txt)", type=["txt"])

if gdpr_file and policy_file:
    model_choice = st.selectbox(
        "Choose the model to use:",
        ["Logistic Regression", "MultinomialNB", "LegalBERT (Eurlex)", "SentenceTransformer", "LLM Model", "Knowledge Graphs"]
    )

    gdpr_data = json.load(gdpr_file)
    article_title_map = {f"Article {a['article_number']}": a['article_title'] for a in gdpr_data}

    policy_text = policy_file.read().decode("utf-8")

    with st.spinner("Analyzing..."):
        if model_choice == "LegalBERT (Eurlex)":
            checker = GDPRComplianceChecker()
            gdpr_map, gdpr_embeddings = checker.load_gdpr_articles(gdpr_data)
            result = checker.calculate_compliance_score(policy_text, gdpr_map, gdpr_embeddings)

        elif model_choice in ["Logistic Regression", "MultinomialNB"]:
            if model_choice == "Logistic Regression":
                model = joblib.load("logistic_regression_model.joblib")
                vectorizer = joblib.load("logistic_regression_vectorizer.joblib")
            else:
                model = joblib.load("multinomialNB_model.joblib")
                vectorizer = joblib.load("multinomialNB_vectorizer.joblib")

            chunks = chunk_policy_text(policy_text)
            chunks = [c.strip() for c in chunks if len(c.strip()) > 40]
            X_tfidf = vectorizer.transform(chunks)
            y_pred = model.predict(X_tfidf)
            y_proba = model.predict_proba(X_tfidf)

            article_scores = defaultdict(lambda: {
                "article_title": "",
                "compliance_percentage": 0.0,
                "similarity_score": 0.0,
                "matched_text_snippet": ""
            })
            total_score = 0
            counted_chunks = 0

            for i, (label, prob_vector) in enumerate(zip(y_pred, y_proba)):
                max_prob = max(prob_vector)
                if max_prob >= 0.35:
                    score_pct = min(100.0, max(0.0, (max_prob - 0.35) / (1 - 0.35) * 100))
                    if score_pct > article_scores[label]["compliance_percentage"]:
                        article_scores[label]["compliance_percentage"] = score_pct
                        article_scores[label]["similarity_score"] = round(max_prob, 4)
                        article_scores[label]["matched_text_snippet"] = chunks[i][:300] + "..."
                    article_scores[label]["article_title"] = article_title_map.get(label, label)
                    total_score += score_pct
                    counted_chunks += 1

            overall = round(total_score / counted_chunks, 2) if counted_chunks else 0
            result = {
                "overall_compliance_percentage": overall,
                "relevant_articles_analyzed": len(article_scores),
                "total_policy_chunks": len(chunks),
                "article_scores": dict(article_scores)
            }

        elif model_choice == "SentenceTransformer":
            model = joblib.load("sentence_transformer_model.joblib")
            gdpr_texts = []
            gdpr_map = {}
            for article in gdpr_data:
                number, title = article["article_number"], article["article_title"]
                body = " ".join([f"{k} {v}" for sec in article["sections"] for k, v in sec.items()])
                full_text = f"Article {number}: {title}. {body}"
                gdpr_map[number] = {
                    "title": title,
                    "text": full_text
                }
                gdpr_texts.append(full_text)

            gdpr_embeddings = model.encode(gdpr_texts, convert_to_numpy=True)

            chunks = chunk_policy_text(policy_text)
            chunk_embeddings = model.encode(chunks, convert_to_numpy=True)

            sim_matrix = cosine_similarity(gdpr_embeddings, chunk_embeddings)

            article_scores = {}
            presence_threshold = 0.35
            total_score, counted_articles = 0, 0

            for i, (art_num, art_data) in enumerate(gdpr_map.items()):
                max_sim = np.max(sim_matrix[i])
                best_idx = np.argmax(sim_matrix[i])

                if max_sim < presence_threshold:
                    continue

                score_pct = min(100, max(0, (max_sim - presence_threshold) / (1 - presence_threshold) * 100))
                article_scores[art_num] = {
                    "article_title": art_data["title"],
                    "compliance_percentage": round(score_pct, 2),
                    "similarity_score": round(max_sim, 4),
                    "matched_text_snippet": chunks[best_idx][:300] + "..."
                }
                total_score += score_pct
                counted_articles += 1

            overall = round(total_score / counted_articles, 2) if counted_articles else 0
            result = {
                "overall_compliance_percentage": overall,
                "relevant_articles_analyzed": counted_articles,
                "total_policy_chunks": len(chunks),
                "article_scores": article_scores
            }

        elif model_choice == "LLM Model":
            load_dotenv()
            api_key = os.getenv("OPENAI_API_KEY")
            client = OpenAI(api_key=api_key)
            EMBED_MODEL = "text-embedding-3-small"
            gdpr_embeddings = {}
            gdpr_map = {}
            for art in gdpr_data:
                number, title = art["article_number"], art["article_title"]
                art_text = prepare_article_text(art)
                gdpr_embeddings[art["article_number"]] = {
                    "embedding": get_embedding(art_text),
                    "title": art["article_title"]
                }
                gdpr_map[number] = {"title": title, "text": art_text}
            chunks = chunk_policy_text(policy_text)
            chunk_embeddings = get_embedding(chunks)
            gdpr_embedding_vectors = [v["embedding"] for v in gdpr_embeddings.values()]
            sim_matrix = cosine_similarity(gdpr_embedding_vectors, chunk_embeddings)

            article_scores = {}
            presence_threshold = 0.35
            total_score, counted_articles = 0, 0

            for i, (art_num, art_data) in enumerate(gdpr_map.items()):
                max_sim = np.max(sim_matrix[i])
                best_idx = np.argmax(sim_matrix[i])

                if max_sim < presence_threshold:
                    continue

                score_pct = min(100, max(0, (max_sim - presence_threshold) / (1 - presence_threshold) * 100))
                article_scores[art_num] = {
                    "article_title": art_data["title"],
                    "compliance_percentage": round(score_pct, 2),
                    "similarity_score": round(max_sim, 4),
                    "matched_text_snippet": chunks[best_idx][:300] + "..."
                }
                total_score += score_pct
                counted_articles += 1

            overall = round(total_score / counted_articles, 2) if counted_articles else 0
            result = {
                "overall_compliance_percentage": overall,
                "relevant_articles_analyzed": counted_articles,
                "total_policy_chunks": len(chunks),
                "article_scores": article_scores
            }
        elif model_choice == "Knowledge Graphs":
            EMBED_MODEL = "all-MiniLM-L6-v2"
            model = SentenceTransformer(EMBED_MODEL)
            TOP_N = 1
            BASE_URI = "http://example.org/gdpr#"
            gdpr_embeddings = {}
            gdpr_map = {}
            for art in gdpr_data:
                number, title = art["article_number"], art["article_title"]
                art_text = prepare_article_text(art)
                gdpr_embeddings[art["article_number"]] = {
                    "embedding": model.encode(art_text),
                    "title": art["article_title"],
                    "uri": URIRef(f"{BASE_URI}Article{art['article_number']}")
                }
                gdpr_map[number] = {"title": title, "text": art_text}
            g = Graph()
            EX = Namespace(BASE_URI)
            g.bind("ex", EX)

            # Add article nodes
            for num, info in gdpr_embeddings.items():
                g.add((info["uri"], RDF.type, EX.Article))
                g.add((info["uri"], RDFS.label, Literal(f"Article {num}: {info['title']}")))
            # Extract GDPR article vectors
            article_nums = list(gdpr_embeddings.keys())
            article_vectors = np.array([gdpr_embeddings[num]["embedding"] for num in article_nums])

            # Score tracking
            total_score = 0
            counted_sections = 0
            chunks = chunk_policy_text(policy_text)
            report = []
            presence_threshold = 0.35

            # Process each policy chunk
            for idx, text in enumerate(chunks, start=1):
                if not text.strip():
                    continue

                # RDF section node
                sec_uri = URIRef(f"{BASE_URI}PolicySection{idx}")
                g.add((sec_uri, RDF.type, EX.PolicySection))
                g.add((sec_uri, RDFS.label, Literal(f"Section {idx}")))

                # Embed section
                sec_emb = model.encode(text)

                # Similarities to all articles
                sims = []
                for i, art_num in enumerate(article_nums):
                    art_emb = article_vectors[i]
                    sim = cosine_similarity([sec_emb], [art_emb])[0][0]
                    sims.append({
                        "article": art_num,
                        "title": gdpr_embeddings[art_num]["title"],
                        "similarity": round(sim, 4),
                        "uri": gdpr_embeddings[art_num]["uri"],
                        "text": gdpr_map[art_num]["text"]
                    })

                # Sort and pick best match
                sims.sort(key=lambda x: x["similarity"], reverse=True)
                top_match = sims[0]

                # Threshold filtering
                if top_match["similarity"] < presence_threshold:
                    continue

                # Compliance score
                score_pct = min(100, max(0, (top_match["similarity"] - presence_threshold) / (1 - presence_threshold) * 100))

                # Add RDF triples
                g.add((sec_uri, EX.relatesTo, top_match["uri"]))
                g.add((sec_uri, EX.similarityScore, Literal(top_match["similarity"], datatype=XSD.float)))

                
                g.serialize(destination="gdpr_policy_graph.ttl", format="turtle")

                total_score += score_pct
                counted_sections += 1

            # Final summary
            overall = round(total_score / counted_sections, 2) if counted_sections else 0
            result = {
                "overall_compliance_percentage": overall,
                "relevant_sections_analyzed": counted_sections,
                "total_policy_sections": len(chunks),
                "ttl": True
            }

        else:
            result = {}

    if result:
        st.subheader(f"βœ… Overall Compliance Score: {result['overall_compliance_percentage']}%")
        st.markdown("---")
        st.subheader("πŸ“‹ Detailed Article Breakdown")
        ttl_file_path = "gdpr_policy_graph.ttl"
        if result.get('article_scores'):
            for art_num, data in sorted(result['article_scores'].items(), key=lambda x: -x[1]['compliance_percentage']):
                with st.expander(f"Article {art_num} - {data['article_title']} ({data['compliance_percentage']}%)"):
                    st.write(f"**Similarity Score**: {data['similarity_score']}")
                    st.write(f"**Matched Text**:\n\n{data['matched_text_snippet']}")
        elif result.get("ttl") and os.path.exists(ttl_file_path):
            st.markdown("---")
            st.subheader("🧠 Interactive RDF Graph Visualization")

            g = Graph()
            g.parse(ttl_file_path, format="ttl")

            nx_graph = rdflib_to_networkx(g)
            net = draw_pyvis_graph(nx_graph)

            # Save the interactive graph temporarily
            net.save_graph("rdf_graph.html")
            HtmlFile = open("rdf_graph.html", "r", encoding="utf-8").read()

            # Display interactive graph inside Streamlit
            components.html(HtmlFile, height=650, scrolling=True)
        
        else:
            st.info("No article scores or RDF graph to display.")

else:
    st.info("Please upload both a GDPR JSON file and a company policy text file to begin.")