Benedikt Veith commited on
Commit
1e6fb4e
·
1 Parent(s): b54ff30

Test update

Browse files
Files changed (1) hide show
  1. app.py +54 -30
app.py CHANGED
@@ -1,40 +1,64 @@
1
  import gradio as gr
2
- import torch
3
  import os
4
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
 
6
- MODEL_ID = "patronus-protect/wolf-guard"
 
 
7
  TOKEN = os.getenv("HF_TOKEN")
 
8
 
9
- # Modell und Tokenizer beim Start der App laden
10
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=TOKEN)
11
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID, token=TOKEN)
12
 
13
- def predict(text):
14
- label_names = {0: "benign", 1: "attack"}
15
-
16
- # Text verarbeiten
17
- enc = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
18
-
19
- with torch.no_grad():
20
- logits = model(**enc).logits
21
- probs = torch.softmax(logits, dim=-1).squeeze()
22
-
23
- pred_id = int(probs.argmax())
24
- label = label_names.get(pred_id, f"ID {pred_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Rückgabe als Dictionary für das Gradio "Label"-Feld
27
- # Zeigt die Wahrscheinlichkeiten beider Klassen an
28
- return {label_names[i]: float(probs[i]) for i in range(len(label_names))}
29
-
30
- # Gradio Interface Setup
31
- demo = gr.Interface(
32
- fn=predict,
33
- inputs=gr.Textbox(label="Eingabe Text", placeholder="Text hier einfügen...", lines=3),
34
- outputs=gr.Label(label="Klassifizierung"),
35
- title="Sicherheits-Check",
36
- description="Dieses Modell erkennt Angriffe in Texten."
37
- )
 
 
 
 
38
 
39
  if __name__ == "__main__":
40
  demo.launch()
 
1
  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
+ LABEL_NAMES = {0: "benign", 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
 
15
+ def process_realtime(text):
16
+ if not text or text.strip() == "":
17
+ return {}, []
18
+
19
+ # Modell 1: Classification (Benign vs Attack)
20
+ # return_all_scores=True gibt uns die Prozentzahlen für alle Klassen
21
+ res_cls = pipe_cls(text, top_k=None)
22
+ cls_output = {item['label']: float(item['score']) for item in res_cls}
23
+
24
+ # Modell 2: NER
25
+ res_ner = pipe_ner(text)
26
+ # Wir formatieren die NER-Ergebnisse als Liste von Tupeln für die Anzeige
27
+ # Oder als Dictionary für ein zweites Label-Feld, wenn du nur die Entities willst
28
+ ner_output = {f"{item['entity_group']}: {item['word']}": float(item['score']) for item in res_ner}
29
+
30
+ return cls_output, ner_output
31
+
32
+ # Custom CSS für Framer-Integration
33
+ css = """
34
+ footer {display: none !important;}
35
+ .gradio-container {background: transparent !important; padding: 0 !important;}
36
+ """
37
+
38
+ with gr.Blocks(css=css) as demo:
39
+ input_text = gr.Textbox(
40
+ label="Eingabe Analyse",
41
+ placeholder="Text eingeben...",
42
+ lines=3,
43
+ interactive=True
44
+ )
45
 
46
+ with gr.Row():
47
+ with gr.Column():
48
+ gr.Markdown("### Modell 1: Klassifizierung")
49
+ output_cls = gr.Label(label="Wahrscheinlichkeit")
50
+
51
+ with gr.Column():
52
+ gr.Markdown("### Modell 2: NER")
53
+ output_ner = gr.Label(label="Erkannte Entitäten")
54
+
55
+ # Realtime Trigger
56
+ input_text.change(
57
+ fn=process_realtime,
58
+ inputs=input_text,
59
+ outputs=[output_cls, output_ner],
60
+ show_progress="hidden"
61
+ )
62
 
63
  if __name__ == "__main__":
64
  demo.launch()