Spaces:
Running
Running
Benedikt Veith commited on
Commit ·
1e6fb4e
1
Parent(s): b54ff30
Test update
Browse files
app.py
CHANGED
|
@@ -1,40 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
import os
|
| 4 |
-
from transformers import
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
def
|
| 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 |
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()
|