itssoumyad commited on
Commit
35bbdb3
·
verified ·
1 Parent(s): ba17c6a

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,10 +1,26 @@
1
  ---
2
- title: Graphrag Codeaudit
3
- emoji: 📚
4
- colorFrom: yellow
5
- colorTo: indigo
6
- sdk: static
 
7
  pinned: false
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: GraphRAG CodeAudit Explorer
3
+ colorFrom: green
4
+ colorTo: gray
5
+ sdk: gradio
6
+ sdk_version: 5.0.0
7
+ app_file: app.py
8
  pinned: false
9
+ license: mit
10
+ tags:
11
+ - arxiv:2404.16130
12
+ - graphrag
13
+ - code-audit
14
+ - communities
15
  ---
16
 
17
+ # GraphRAG CodeAudit Explorer
18
+
19
+ Local entity-neighbourhood search and global community-report search over a
20
+ synthetic repository. Built on
21
+ [GraphRAG-CodeAudit](https://github.com/itsoumya-d/GraphRAG-CodeAudit).
22
+
23
+ Paper page: [From Local to Global: A Graph RAG Approach](https://huggingface.co/papers/2404.16130)
24
+
25
+ The deterministic judge and sample repository are used; this is not the paper's
26
+ LLM-as-a-judge replication.
__pycache__/app.cpython-314.pyc ADDED
Binary file (2.87 kB). View file
 
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+ import json
3
+ from pathlib import Path
4
+ import gradio as gr
5
+ from graphrag_audit.communities import LeidenDetector
6
+ from graphrag_audit.eval import run_evaluation
7
+ from graphrag_audit.extract import extract_repository
8
+ from graphrag_audit.graph import KnowledgeGraph
9
+ from graphrag_audit.query import GraphRAG
10
+ from graphrag_audit.summarize import summarize_communities
11
+
12
+ ROOT = Path(__file__).resolve().parent / "sample_repo"
13
+ CORPUS = extract_repository(ROOT)
14
+ GRAPH = KnowledgeGraph().from_corpus(CORPUS)
15
+ HIERARCHY = LeidenDetector().detect(GRAPH)
16
+ REPORTS = summarize_communities(HIERARCHY, CORPUS)
17
+ RAG = GraphRAG(CORPUS, GRAPH, REPORTS)
18
+ METRICS = run_evaluation(str(ROOT), n=100)
19
+
20
+ def ask(question, mode):
21
+ result = RAG.query(question, mode=mode)
22
+ return json.dumps({
23
+ "answer": result.answer,
24
+ "citations": result.citations,
25
+ "communities": result.communities,
26
+ "method": HIERARCHY.method,
27
+ "communities_total": len(HIERARCHY.communities),
28
+ "eval": {"graph": METRICS["graph"], "flat": METRICS["flat"], "win_rate": METRICS["win_rate"]},
29
+ }, indent=2)
30
+
31
+ with gr.Blocks(title="GraphRAG CodeAudit Explorer") as demo:
32
+ gr.Markdown("# GraphRAG CodeAudit Explorer\nAsk a local or global question over the synthetic sample repository.")
33
+ question = gr.Textbox(label="Question", value="What security risks exist in auth.py?")
34
+ mode = gr.Radio(["global", "local"], value="global", label="Search mode")
35
+ button = gr.Button("Ask")
36
+ output = gr.Code(label="GraphRAG response", language="json")
37
+ button.click(ask, inputs=[question, mode], outputs=output)
38
+ demo.load(ask, inputs=[question, mode], outputs=output)
39
+
40
+ if __name__ == "__main__":
41
+ demo.launch()
demo.json ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "queries": [
3
+ {
4
+ "question": "What security risks exist in auth.py?",
5
+ "mode": "global",
6
+ "answer": "Global GraphRAG synthesized 3 community report(s). Key risks: no high-signal risks extracted. Related components: login, register, restore_snapshot, auth, db, files, api.py. Key risks: hardcoded SECRET_KEY: SECRET_KEY = \"dev-secret-change-me\"; weak hash algorithm: return hashlib.md5(password.encode()).hexdigest(). Related components: hash_password, issue_token, verify_password, hashlib, os, auth.py. Key risks: SQL built from a string: cursor.execute(\"INSERT INTO users (email) VALUES (?)\", (email,)); SQL built from a string: cursor.execute(f\"SELECT id, email FROM users WHERE email = '{email}'\"). Key risks: SQL built from a string: cursor.execute(\"INSERT INTO users (email) VALUES (?)\", (email,)); SQL built from a string: cursor.execute(f\"SELECT id, email FROM users WHERE email = '{email}'\"); hardcoded SECRET_KEY: SECRET_KEY = \"dev-secret-change-me\"; weak hash algorithm: return hashlib.md5(password.encode()).hexdigest(). Sources: api.py:1-20, auth.py:1-18, db.py:1-17",
7
+ "citations": [
8
+ "api.py:1-20",
9
+ "auth.py:1-18",
10
+ "db.py:1-17"
11
+ ],
12
+ "communities": [
13
+ "L0-0",
14
+ "L0-1",
15
+ "L0-2"
16
+ ]
17
+ },
18
+ {
19
+ "question": "What does files.py do and what risks does it carry?",
20
+ "mode": "local",
21
+ "answer": "Local GraphRAG around login, register, restore_snapshot, convert_image, list_dir, load_snapshot, auth, files: 4 risk signal(s). Risks: os.system call: os.system(f\"ls {path}\"); pickle deserialization: return pickle.loads(handle.read()); subprocess with shell=True: # shell=True: flagged by the extractor.; subprocess with shell=True: return subprocess.check_output(f\"convert {path} /tmp/out.png\", shell=True, text=True). Sources: api.py:1-20, files.py:1-20",
22
+ "citations": [
23
+ "api.py:1-20",
24
+ "files.py:1-20"
25
+ ],
26
+ "communities": []
27
+ },
28
+ {
29
+ "question": "Which modules depend on auth.py?",
30
+ "mode": "global",
31
+ "answer": "Global GraphRAG synthesized 3 community report(s). Related components: login, register, restore_snapshot, auth, db, files, api.py. Related components: hash_password, issue_token, verify_password, hashlib, os, auth.py. Community L0-2 at level 0 contains 4 entities (dependency, function, module). Key risks: SQL built from a string: cursor.execute(\"INSERT INTO users (email) VALUES (?)\", (email,)); SQL built from a string: cursor.execute(f\"SELECT id, email FROM users WHERE email = '{email}'\"). Key risks: SQL built from a string: cursor.execute(\"INSERT INTO users (email) VALUES (?)\", (email,)); SQL built from a string: cursor.execute(f\"SELECT id, email FROM users WHERE email = '{email}'\"); hardcoded SECRET_KEY: SECRET_KEY = \"dev-secret-change-me\"; weak hash algorithm: return hashlib.md5(password.encode()).hexdigest(). Sources: api.py:1-20, auth.py:1-18, db.py:1-17",
32
+ "citations": [
33
+ "api.py:1-20",
34
+ "auth.py:1-18",
35
+ "db.py:1-17"
36
+ ],
37
+ "communities": [
38
+ "L0-0",
39
+ "L0-1",
40
+ "L0-2"
41
+ ]
42
+ },
43
+ {
44
+ "question": "Show password and secret handling risks.",
45
+ "mode": "local",
46
+ "answer": "Local GraphRAG around hash_password, issue_token, verify_password, hashlib, os, auth.py, files.py: 2 risk signal(s). Risks: hardcoded SECRET_KEY: SECRET_KEY = \"dev-secret-change-me\"; weak hash algorithm: return hashlib.md5(password.encode()).hexdigest(). Sources: auth.py:1-18, files.py:1-20",
47
+ "citations": [
48
+ "auth.py:1-18",
49
+ "files.py:1-20"
50
+ ],
51
+ "communities": []
52
+ }
53
+ ],
54
+ "method": "louvain-fallback",
55
+ "communities": 5,
56
+ "graph": {
57
+ "comprehensiveness": 8.58,
58
+ "diversity": 10.0,
59
+ "empowerment": 4.01,
60
+ "directness": 7.72
61
+ },
62
+ "flat": {
63
+ "comprehensiveness": 8.58,
64
+ "diversity": 10.0,
65
+ "empowerment": 2.28,
66
+ "directness": 7.4
67
+ },
68
+ "win_rate": 0.64
69
+ }
index.html CHANGED
@@ -1,19 +1,40 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <!doctype html>
2
+ <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
3
+ <title>GraphRAG CodeAudit Explorer</title>
4
+ <style>
5
+ :root{--paper:#f7f5f2;--ink:#161616;--muted:#6b655d;--line:#e4e0da;--accent:#067647}
6
+ *{box-sizing:border-box}body{margin:0;background:var(--paper);color:var(--ink);font:16px/1.6 ui-sans-serif,system-ui,sans-serif}
7
+ main{max-width:980px;margin:0 auto;padding:48px 24px}h1{font-size:40px;line-height:1.05;letter-spacing:-.03em;margin:0 0 12px}
8
+ .lead{color:var(--muted);max-width:72ch}.mono{font-family:ui-monospace,SFMono-Regular,monospace}
9
+ .layout{display:grid;grid-template-columns:300px 1fr;gap:18px;margin-top:24px}@media(max-width:760px){.layout{grid-template-columns:1fr}}
10
+ .card{background:#fff;border:1px solid var(--line);border-radius:16px;padding:18px}
11
+ button{display:block;width:100%;text-align:left;min-height:48px;margin:0 0 8px;padding:10px 12px;border:1px solid var(--line);border-radius:12px;background:#fff;font:13px/1.4 inherit;cursor:pointer}
12
+ button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}button.active{border-color:var(--accent);background:#ecfdf3}
13
+ .answer{white-space:pre-wrap;font-size:14px;line-height:1.65}.meta{font-size:12px;color:var(--muted);margin-top:12px}
14
+ table{width:100%;border-collapse:collapse;margin-top:10px;font-size:13px}td,th{padding:6px 0;border-bottom:1px solid var(--line);text-align:left}
15
+ .tag{display:inline-block;border:1px solid var(--line);border-radius:99px;padding:2px 8px;font-size:12px;margin-right:6px}
16
+ a{color:var(--accent)}code{font-family:ui-monospace,monospace;font-size:12px}
17
+ </style></head><body><main>
18
+ <p class="mono" style="color:var(--accent);font-size:12px;letter-spacing:.18em;text-transform:uppercase">GraphRAG-CodeAudit · local-to-global</p>
19
+ <h1>Ask a codebase <span style="color:var(--accent)">for its risk shape</span></h1>
20
+ <p class="lead">Extract entities, typed edges, and security claims; partition the graph into communities; answer through entity neighbourhoods or community reports. The sample repository is synthetic and the judge is deterministic.</p>
21
+ <div class="layout">
22
+ <div class="card"><p class="mono" style="font-size:12px;color:var(--muted)">SAMPLE QUESTIONS</p><div id="buttons"></div><p class="meta" id="meta"></p></div>
23
+ <div class="card"><h2 id="q" style="margin-top:0;font-size:18px"></h2><div class="answer" id="answer"></div><p class="meta" id="citations"></p></div>
24
+ </div>
25
+ <div class="card" style="margin-top:18px"><h2 style="margin-top:0;font-size:18px">GraphRAG vs flat vector RAG</h2><table><thead><tr><th>Metric</th><th>GraphRAG</th><th>Flat</th></tr></thead><tbody id="metrics"></tbody></table><p class="meta" id="summary"></p></div>
26
+ <p class="meta"><span class="tag">arXiv:2404.16130</span> Source: <a href="https://github.com/itsoumya-d/GraphRAG-CodeAudit">GraphRAG-CodeAudit</a> · Dataset: <a href="https://huggingface.co/datasets/itssoumyad/graphrag-questions-100">graphrag-questions-100</a></p>
27
+ </main>
28
+ <script>
29
+ fetch('./demo.json').then(r=>r.json()).then(data=>{
30
+ const buttons=document.getElementById('buttons'); let active=0;
31
+ const render=(i)=>{active=i;const d=data.queries[i];document.getElementById('q').textContent=d.question;document.getElementById('answer').textContent=d.answer;document.getElementById('citations').textContent='Mode: '+d.mode+' · Citations: '+d.citations.join(', ')+' · Communities: '+(d.communities.join(', ')||'—');
32
+ [...buttons.children].forEach((b,j)=>b.classList.toggle('active',j===i));};
33
+ data.queries.forEach((q,i)=>{const b=document.createElement('button');b.type='button';b.textContent=q.question;b.addEventListener('click',()=>render(i));buttons.appendChild(b)});
34
+ document.getElementById('meta').textContent='Method: '+data.method+' · '+data.communities+' communities';
35
+ const body=document.getElementById('metrics');for(const k of ['comprehensiveness','diversity','empowerment','directness']){const tr=document.createElement('tr');tr.innerHTML='<td>'+k+'</td><td>'+data.graph[k].toFixed(2)+'</td><td>'+data.flat[k].toFixed(2)+'</td>';body.appendChild(tr)}
36
+ const tr=document.createElement('tr');tr.innerHTML='<td><strong>average</strong></td><td><strong>'+((data.graph.comprehensiveness+data.graph.diversity+data.graph.empowerment+data.graph.directness)/4).toFixed(2)+'</strong></td><td><strong>'+((data.flat.comprehensiveness+data.flat.diversity+data.flat.empowerment+data.flat.directness)/4).toFixed(2)+'</strong></td>';body.appendChild(tr);
37
+ document.getElementById('summary').textContent='Win rate: '+(data.win_rate*100).toFixed(1)+'% on 100 synthetic questions. Deterministic rubric, not the paper LLM judge.';
38
+ render(0);
39
+ }).catch(e=>{document.getElementById('answer').textContent='Demo data failed to load: '+e});
40
+ </script></body></html>
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio>=5.0
2
+ git+https://github.com/itsoumya-d/GraphRAG-CodeAudit.git
sample_repo/api.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """API layer that ties authentication, database, and files together."""
2
+ from auth import issue_token, verify_password
3
+ from db import find_user, save_user
4
+ from files import load_snapshot
5
+
6
+
7
+ def login(email: str, password: str):
8
+ rows = find_user(email)
9
+ if rows and verify_password(password, rows[0][1]):
10
+ return {"token": issue_token(str(rows[0][0]))}
11
+ return {"error": "invalid credentials"}
12
+
13
+
14
+ def restore_snapshot(path: str):
15
+ return load_snapshot(path)
16
+
17
+
18
+ def register(email: str):
19
+ save_user(email)
20
+ return {"status": "created"}
sample_repo/auth.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Authentication helpers with deliberate audit findings."""
2
+ import hashlib
3
+ import os
4
+
5
+ SECRET_KEY = "dev-secret-change-me"
6
+
7
+
8
+ def hash_password(password: str) -> str:
9
+ # Weak hash: flagged by the extractor.
10
+ return hashlib.md5(password.encode()).hexdigest()
11
+
12
+
13
+ def verify_password(password: str, digest: str) -> bool:
14
+ return hash_password(password) == digest
15
+
16
+
17
+ def issue_token(user_id: str) -> str:
18
+ return f"{user_id}:{SECRET_KEY}"
sample_repo/db.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Database access layer with a deliberate SQL construction issue."""
2
+ import sqlite3
3
+
4
+ CONNECTION = sqlite3.connect(":memory:")
5
+
6
+
7
+ def find_user(email: str):
8
+ cursor = CONNECTION.cursor()
9
+ # SQL built from a string: flagged by the extractor.
10
+ cursor.execute(f"SELECT id, email FROM users WHERE email = '{email}'")
11
+ return cursor.fetchall()
12
+
13
+
14
+ def save_user(email: str):
15
+ cursor = CONNECTION.cursor()
16
+ cursor.execute("INSERT INTO users (email) VALUES (?)", (email,))
17
+ CONNECTION.commit()
sample_repo/files.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """File import helpers with unsafe deserialization and command execution."""
2
+ import os
3
+ import pickle
4
+ import subprocess
5
+
6
+
7
+ def load_snapshot(path: str):
8
+ with open(path, "rb") as handle:
9
+ # Insecure deserialization: flagged by the extractor.
10
+ return pickle.loads(handle.read())
11
+
12
+
13
+ def convert_image(path: str) -> str:
14
+ # shell=True: flagged by the extractor.
15
+ return subprocess.check_output(f"convert {path} /tmp/out.png", shell=True, text=True)
16
+
17
+
18
+ def list_dir(path: str):
19
+ # os.system: flagged by the extractor.
20
+ os.system(f"ls {path}")
sample_repo/payments.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Payment helpers with a privacy logging finding."""
2
+ import logging
3
+
4
+ logger = logging.getLogger(__name__)
5
+
6
+
7
+ def charge(customer_email: str, amount: float) -> dict:
8
+ logger.info("charging customer %s for %s", customer_email, amount)
9
+ total = round(amount * 1.03, 2)
10
+ return {"email": customer_email, "amount": total, "status": "ok"}
11
+
12
+
13
+ def refund(customer_email: str, amount: float) -> dict:
14
+ return {"email": customer_email, "amount": amount, "status": "refunded"}