Spaces:
Running
Running
Benedikt Veith
commited on
Commit
·
d43bc72
1
Parent(s):
30a4b6d
Design
Browse files
app.py
CHANGED
|
@@ -2,13 +2,11 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Konfiguration
|
| 6 |
MODEL_1_ID = "patronus-protect/wolf-guard"
|
| 7 |
MODEL_2_ID = "HuggingLil/pii-sensitive-ner-german"
|
| 8 |
TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
CLS_MAP = {"LABEL_0": "Benign", "LABEL_1": "Attack"}
|
| 10 |
|
| 11 |
-
# Pipelines laden (einfachster Weg für verschiedene Modelltypen)
|
| 12 |
pipe_cls = pipeline("text-classification", model=MODEL_1_ID, token=TOKEN)
|
| 13 |
pipe_ner = pipeline("ner", model=MODEL_2_ID, token=TOKEN, aggregation_strategy="simple")
|
| 14 |
|
|
@@ -25,7 +23,7 @@ def analyze(text):
|
|
| 25 |
return (
|
| 26 |
gr.update(visible=True),
|
| 27 |
"Prompt Injection detected",
|
| 28 |
-
[(text, "ATTACK")]
|
| 29 |
)
|
| 30 |
|
| 31 |
has_high_ner = False
|
|
@@ -38,7 +36,7 @@ def analyze(text):
|
|
| 38 |
has_high_ner = True
|
| 39 |
|
| 40 |
highlighted_data.append((text[last_idx:ent['start']], None))
|
| 41 |
-
highlighted_data.append((text[ent['start']:ent['end']], f"
|
| 42 |
last_idx = ent['end']
|
| 43 |
|
| 44 |
highlighted_data.append((text[last_idx:], None))
|
|
@@ -48,31 +46,38 @@ def analyze(text):
|
|
| 48 |
else:
|
| 49 |
return gr.update(visible=True), "Nothing detected", [(text, "CLEAN")]
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
css = """
|
| 53 |
.color-attack { background-color: #ffcccb !important; }
|
| 54 |
.color-ner { background-color: #ffe5b4 !important; }
|
| 55 |
.color-clean { background-color: #d1ffbd !important; }
|
| 56 |
footer {display: none !important;}
|
|
|
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
|
| 59 |
-
with gr.Blocks(css=css, theme=
|
| 60 |
input_text = gr.Textbox(label="Input Scan", placeholder="Text eingeben...", lines=3)
|
| 61 |
|
| 62 |
with gr.Column(visible=False) as results_col:
|
| 63 |
-
status_msg = gr.Markdown()
|
| 64 |
display_output = gr.HighlightedText(
|
| 65 |
-
label="
|
| 66 |
combine_adjacent=True,
|
| 67 |
show_legend=False,
|
| 68 |
-
color_map={"ATTACK": "red", "CLEAN": "green"
|
| 69 |
)
|
|
|
|
| 70 |
|
| 71 |
input_text.change(
|
| 72 |
fn=analyze,
|
| 73 |
inputs=input_text,
|
| 74 |
outputs=[results_col, status_msg, display_output],
|
| 75 |
-
show_progress="
|
| 76 |
)
|
| 77 |
|
| 78 |
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
| 5 |
MODEL_1_ID = "patronus-protect/wolf-guard"
|
| 6 |
MODEL_2_ID = "HuggingLil/pii-sensitive-ner-german"
|
| 7 |
TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
CLS_MAP = {"LABEL_0": "Benign", "LABEL_1": "Attack"}
|
| 9 |
|
|
|
|
| 10 |
pipe_cls = pipeline("text-classification", model=MODEL_1_ID, token=TOKEN)
|
| 11 |
pipe_ner = pipeline("ner", model=MODEL_2_ID, token=TOKEN, aggregation_strategy="simple")
|
| 12 |
|
|
|
|
| 23 |
return (
|
| 24 |
gr.update(visible=True),
|
| 25 |
"Prompt Injection detected",
|
| 26 |
+
[(text, "ATTACK")] # Text komplett rot markieren
|
| 27 |
)
|
| 28 |
|
| 29 |
has_high_ner = False
|
|
|
|
| 36 |
has_high_ner = True
|
| 37 |
|
| 38 |
highlighted_data.append((text[last_idx:ent['start']], None))
|
| 39 |
+
highlighted_data.append((text[ent['start']:ent['end']], f"{ent['entity_group']} ({ent['score']:.2%})"))
|
| 40 |
last_idx = ent['end']
|
| 41 |
|
| 42 |
highlighted_data.append((text[last_idx:], None))
|
|
|
|
| 46 |
else:
|
| 47 |
return gr.update(visible=True), "Nothing detected", [(text, "CLEAN")]
|
| 48 |
|
| 49 |
+
ACCENT_COLOR = "#F5C77A"
|
| 50 |
+
|
| 51 |
+
theme = gr.themes.Soft()
|
| 52 |
+
theme.font = [gr.themes.GoogleFont("Inter"), "sans-serif"]
|
| 53 |
+
|
| 54 |
css = """
|
| 55 |
.color-attack { background-color: #ffcccb !important; }
|
| 56 |
.color-ner { background-color: #ffe5b4 !important; }
|
| 57 |
.color-clean { background-color: #d1ffbd !important; }
|
| 58 |
footer {display: none !important;}
|
| 59 |
+
|
| 60 |
+
.gradio-container {border: none !important;}
|
| 61 |
+
.generating { border-color: """ + ACCENT_COLOR + """ !important; }
|
| 62 |
"""
|
| 63 |
|
| 64 |
+
with gr.Blocks(css=css, theme=theme) as demo:
|
| 65 |
input_text = gr.Textbox(label="Input Scan", placeholder="Text eingeben...", lines=3)
|
| 66 |
|
| 67 |
with gr.Column(visible=False) as results_col:
|
|
|
|
| 68 |
display_output = gr.HighlightedText(
|
| 69 |
+
label="Result",
|
| 70 |
combine_adjacent=True,
|
| 71 |
show_legend=False,
|
| 72 |
+
color_map={"ATTACK": "red", "CLEAN": "green"}
|
| 73 |
)
|
| 74 |
+
status_msg = gr.Markdown()
|
| 75 |
|
| 76 |
input_text.change(
|
| 77 |
fn=analyze,
|
| 78 |
inputs=input_text,
|
| 79 |
outputs=[results_col, status_msg, display_output],
|
| 80 |
+
show_progress="minimal"
|
| 81 |
)
|
| 82 |
|
| 83 |
demo.launch()
|