-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (52 loc) · 1.66 KB
/
Copy pathapp.py
File metadata and controls
64 lines (52 loc) · 1.66 KB
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
"""Starter AI Defense Lab: a tiny web app to try out prompt injection defenses.
Run it with:
python app.py
Then open http://127.0.0.1:5000 and submit some text to see whether the
detector flags it as a possible prompt injection attempt.
"""
from flask import Flask, render_template_string, request
from src.detector import categorize, is_suspicious, sanitize, scan
app = Flask(__name__)
PAGE = """
<!doctype html>
<title>AI Defense Lab</title>
<h1>AI Defense Lab</h1>
<p>Paste some text below to check it for common prompt injection patterns.</p>
<form method="post">
<textarea name="text" rows="6" cols="60" placeholder="Type or paste text here...">{{ text }}</textarea><br>
<button type="submit">Check</button>
</form>
{% if checked %}
<h2>Result</h2>
<p><strong>Suspicious:</strong> {{ suspicious }}</p>
<p><strong>Matched phrases:</strong> {{ matches }}</p>
<p><strong>Attack categories:</strong> {{ categories }}</p>
<p><strong>Sanitized text:</strong> {{ sanitized }}</p>
{% endif %}
"""
@app.route("/", methods=["GET", "POST"])
def index():
text = ""
checked = False
suspicious = False
matches = []
categories = []
sanitized = ""
if request.method == "POST":
text = request.form.get("text", "")
checked = True
matches = scan(text)
categories = categorize(text)
suspicious = is_suspicious(text)
sanitized = sanitize(text)
return render_template_string(
PAGE,
text=text,
checked=checked,
suspicious=suspicious,
matches=matches,
categories=categories,
sanitized=sanitized,
)
if __name__ == "__main__":
app.run(debug=True)