Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,18 @@ import torch
|
|
| 3 |
from transformers import (
|
| 4 |
AutoTokenizer,
|
| 5 |
AutoModelForSequenceClassification,
|
| 6 |
-
|
| 7 |
)
|
| 8 |
|
| 9 |
# =====================
|
| 10 |
# DEVICE
|
| 11 |
# =====================
|
| 12 |
-
DEVICE = "
|
| 13 |
|
| 14 |
# =====================
|
| 15 |
# Agreement (MNLI)
|
| 16 |
# =====================
|
| 17 |
-
MNLI_MODEL = "facebook/bart-large-mnli"
|
| 18 |
mnli_tokenizer = None
|
| 19 |
mnli_model = None
|
| 20 |
|
|
@@ -32,7 +32,7 @@ def check_agreement(msg1: str, msg2: str) -> float:
|
|
| 32 |
with torch.no_grad():
|
| 33 |
logits = mnli_model(**inputs).logits
|
| 34 |
probs = torch.softmax(logits, dim=-1)[0]
|
| 35 |
-
#
|
| 36 |
return round((probs[2] - probs[0]).item(), 2)
|
| 37 |
|
| 38 |
# =====================
|
|
@@ -57,40 +57,37 @@ def analyze_sentiment(text: str) -> float:
|
|
| 57 |
logits = sent_model(**inputs).logits
|
| 58 |
probs = torch.softmax(logits, dim=-1)
|
| 59 |
stars = torch.argmax(probs, dim=-1).item() + 1
|
| 60 |
-
#
|
| 61 |
return round((stars - 3) * 2.5, 2)
|
| 62 |
|
| 63 |
# =====================
|
| 64 |
-
#
|
| 65 |
# =====================
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
CATEGORIES = [
|
| 68 |
"politique", "woke", "racism", "crime",
|
| 69 |
"police_abuse", "corruption", "hate_speech", "activism"
|
| 70 |
]
|
| 71 |
-
clf_tokenizer = None
|
| 72 |
-
clf_model = None
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
global
|
| 76 |
-
if
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
problem_type="multi_label_classification"
|
| 82 |
)
|
| 83 |
-
clf_model.to(DEVICE)
|
| 84 |
-
clf_model.eval()
|
| 85 |
|
| 86 |
def classify_message(text: str) -> dict:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
return {
|
| 94 |
|
| 95 |
# =====================
|
| 96 |
# Gradio UI
|
|
@@ -113,11 +110,11 @@ with gr.Blocks(title="Unified NLP API") as demo:
|
|
| 113 |
out_sent = gr.Number(label="Sentiment Score (-5 to +5)")
|
| 114 |
btn_sent.click(fn=analyze_sentiment, inputs=text_sent, outputs=out_sent)
|
| 115 |
|
| 116 |
-
# ----- Multilabel Classification Tab -----
|
| 117 |
with gr.Tab("Multilabel Classification"):
|
| 118 |
text_clf = gr.Textbox(label="Text")
|
| 119 |
btn_clf = gr.Button("Classify")
|
| 120 |
-
out_clf = gr.Label(label="Categories")
|
| 121 |
btn_clf.click(fn=classify_message, inputs=text_clf, outputs=out_clf)
|
| 122 |
|
| 123 |
demo.launch()
|
|
|
|
| 3 |
from transformers import (
|
| 4 |
AutoTokenizer,
|
| 5 |
AutoModelForSequenceClassification,
|
| 6 |
+
pipeline,
|
| 7 |
)
|
| 8 |
|
| 9 |
# =====================
|
| 10 |
# DEVICE
|
| 11 |
# =====================
|
| 12 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
| 14 |
# =====================
|
| 15 |
# Agreement (MNLI)
|
| 16 |
# =====================
|
| 17 |
+
MNLI_MODEL = "facebook/bart-large-mnli"
|
| 18 |
mnli_tokenizer = None
|
| 19 |
mnli_model = None
|
| 20 |
|
|
|
|
| 32 |
with torch.no_grad():
|
| 33 |
logits = mnli_model(**inputs).logits
|
| 34 |
probs = torch.softmax(logits, dim=-1)[0]
|
| 35 |
+
# Считаем: entailment - contradiction
|
| 36 |
return round((probs[2] - probs[0]).item(), 2)
|
| 37 |
|
| 38 |
# =====================
|
|
|
|
| 57 |
logits = sent_model(**inputs).logits
|
| 58 |
probs = torch.softmax(logits, dim=-1)
|
| 59 |
stars = torch.argmax(probs, dim=-1).item() + 1
|
| 60 |
+
# Приводим шкалу 1–5 к -5..+5
|
| 61 |
return round((stars - 3) * 2.5, 2)
|
| 62 |
|
| 63 |
# =====================
|
| 64 |
+
# Zero‑Shot Classification
|
| 65 |
# =====================
|
| 66 |
+
ZS_MODEL = "facebook/bart-large-mnli"
|
| 67 |
+
zs_classifier = None
|
| 68 |
+
|
| 69 |
CATEGORIES = [
|
| 70 |
"politique", "woke", "racism", "crime",
|
| 71 |
"police_abuse", "corruption", "hate_speech", "activism"
|
| 72 |
]
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
def load_zero_shot():
|
| 75 |
+
global zs_classifier
|
| 76 |
+
if zs_classifier is None:
|
| 77 |
+
zs_classifier = pipeline(
|
| 78 |
+
"zero-shot-classification",
|
| 79 |
+
model=ZS_MODEL,
|
| 80 |
+
device=0 if torch.cuda.is_available() else -1
|
|
|
|
| 81 |
)
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def classify_message(text: str) -> dict:
|
| 84 |
+
load_zero_shot()
|
| 85 |
+
# Zero‑shot принимает список меток:
|
| 86 |
+
result = zs_classifier(text, candidate_labels=CATEGORIES)
|
| 87 |
+
scores = result["scores"]
|
| 88 |
+
labels = result["labels"]
|
| 89 |
+
# Возвращаем словарь {label: score}
|
| 90 |
+
return {label: round(score, 3) for label, score in zip(labels, scores)}
|
| 91 |
|
| 92 |
# =====================
|
| 93 |
# Gradio UI
|
|
|
|
| 110 |
out_sent = gr.Number(label="Sentiment Score (-5 to +5)")
|
| 111 |
btn_sent.click(fn=analyze_sentiment, inputs=text_sent, outputs=out_sent)
|
| 112 |
|
| 113 |
+
# ----- Multilabel (Zero‑Shot) Classification Tab -----
|
| 114 |
with gr.Tab("Multilabel Classification"):
|
| 115 |
text_clf = gr.Textbox(label="Text")
|
| 116 |
btn_clf = gr.Button("Classify")
|
| 117 |
+
out_clf = gr.Label(label="Categories & Scores")
|
| 118 |
btn_clf.click(fn=classify_message, inputs=text_clf, outputs=out_clf)
|
| 119 |
|
| 120 |
demo.launch()
|