fdaudens commited on
Commit
7dc197f
·
verified ·
1 Parent(s): 14bf37d

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +107 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ models = {
5
+ "MoritzLaurer/deberta-v3-large-zeroshot-v2.0 (best, English)": "MoritzLaurer/deberta-v3-large-zeroshot-v2.0",
6
+ "MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7 (multilingual incl. Dutch)": "MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7",
7
+ "facebook/bart-large-mnli (classic)": "facebook/bart-large-mnli",
8
+ }
9
+
10
+ pipes = {}
11
+
12
+
13
+ def get_pipe(model_name):
14
+ if model_name not in pipes:
15
+ pipes[model_name] = pipeline(
16
+ "zero-shot-classification",
17
+ model=models[model_name],
18
+ )
19
+ return pipes[model_name]
20
+
21
+
22
+ PRESETS = {
23
+ "Custom (type your own)": "",
24
+ "News categories": "politics, economy, sports, culture, technology, health, crime, environment",
25
+ "Sentiment": "positive, negative, neutral",
26
+ "Urgency": "urgent, important, routine, not relevant",
27
+ "Story type": "breaking news, investigation, feature, opinion, analysis",
28
+ "Tips inbox triage": "actionable tip, complaint, spam, press release, personal story",
29
+ }
30
+
31
+
32
+ def classify(text, model_choice, labels_text, preset, multi_label):
33
+ if not text.strip():
34
+ return "Enter some text to classify."
35
+
36
+ if preset != "Custom (type your own)" and not labels_text.strip():
37
+ labels_text = PRESETS[preset]
38
+
39
+ if not labels_text.strip():
40
+ return "Enter at least two labels separated by commas."
41
+
42
+ labels = [l.strip() for l in labels_text.split(",") if l.strip()]
43
+ if len(labels) < 2:
44
+ return "Need at least two labels."
45
+
46
+ pipe = get_pipe(model_choice)
47
+ result = pipe(text, candidate_labels=labels, multi_label=multi_label)
48
+
49
+ output = ""
50
+ for label, score in zip(result["labels"], result["scores"]):
51
+ bar = "█" * int(score * 30)
52
+ output += f"{label:.<30s} {score:.1%} {bar}\n"
53
+
54
+ return output
55
+
56
+
57
+ def update_labels(preset):
58
+ if preset == "Custom (type your own)":
59
+ return ""
60
+ return PRESETS.get(preset, "")
61
+
62
+
63
+ with gr.Blocks(title="Zero-Shot Classification — KRO-NCRV Workshop") as demo:
64
+ gr.Markdown("# Zero-Shot Classification")
65
+ gr.Markdown(
66
+ "Classify text into **any categories you define** — no training needed. "
67
+ "Works in Dutch and English."
68
+ )
69
+
70
+ with gr.Row():
71
+ with gr.Column():
72
+ text_input = gr.Textbox(
73
+ label="Text to classify",
74
+ lines=6,
75
+ placeholder="Paste an article, tip, tweet, or paragraph...",
76
+ )
77
+ model_choice = gr.Dropdown(
78
+ choices=list(models.keys()),
79
+ value=list(models.keys())[1],
80
+ label="Model",
81
+ )
82
+ preset = gr.Dropdown(
83
+ choices=list(PRESETS.keys()),
84
+ value="Custom (type your own)",
85
+ label="Label preset",
86
+ )
87
+ labels_input = gr.Textbox(
88
+ label="Labels (comma-separated)",
89
+ placeholder="politics, economy, sports, culture",
90
+ )
91
+ multi_label = gr.Checkbox(
92
+ label="Multi-label (text can belong to multiple categories)",
93
+ value=False,
94
+ )
95
+ btn = gr.Button("Classify", variant="primary")
96
+
97
+ with gr.Column():
98
+ output = gr.Textbox(label="Results", lines=15, show_copy_button=True)
99
+
100
+ preset.change(fn=update_labels, inputs=[preset], outputs=[labels_input])
101
+ btn.click(
102
+ fn=classify,
103
+ inputs=[text_input, model_choice, labels_input, preset, multi_label],
104
+ outputs=[output],
105
+ )
106
+
107
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio