Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
AutoTokenizer, AutoModelForTokenClassification,
|
| 4 |
+
AutoModelForSequenceClassification,
|
| 5 |
+
MarianMTModel, MarianTokenizer
|
| 6 |
+
)
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
# === Tokenizery i modele ABSA ===
|
| 12 |
+
aspect_tokenizer = AutoTokenizer.from_pretrained("EfektMotyla/bert-aspect-ner")
|
| 13 |
+
aspect_model = AutoModelForTokenClassification.from_pretrained("EfektMotyla/bert-aspect-ner").to(device)
|
| 14 |
+
|
| 15 |
+
sentiment_tokenizer = AutoTokenizer.from_pretrained("EfektMotyla/absa-roberta")
|
| 16 |
+
sentiment_model = AutoModelForSequenceClassification.from_pretrained("EfektMotyla/absa-roberta").to(device)
|
| 17 |
+
|
| 18 |
+
en_to_pl_tokenizer = MarianTokenizer.from_pretrained("gsarti/opus-mt-tc-en-pl")
|
| 19 |
+
en_to_pl_model = MarianMTModel.from_pretrained("gsarti/opus-mt-tc-en-pl").to(device)
|
| 20 |
+
|
| 21 |
+
pl_to_en_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-pl-en")
|
| 22 |
+
pl_to_en_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-pl-en").to(device)
|
| 23 |
+
|
| 24 |
+
def translate(texts, tokenizer, model):
|
| 25 |
+
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 26 |
+
translated = model.generate(**inputs)
|
| 27 |
+
return tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 28 |
+
|
| 29 |
+
def translate_pl_to_en(texts): return translate(texts, pl_to_en_tokenizer, pl_to_en_model)
|
| 30 |
+
def translate_en_to_pl(texts): return translate(texts, en_to_pl_tokenizer, en_to_pl_model)
|
| 31 |
+
|
| 32 |
+
def extract_aspects(text):
|
| 33 |
+
inputs = aspect_tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = aspect_model(**inputs)
|
| 36 |
+
preds = torch.argmax(outputs.logits, dim=2)[0].cpu().numpy()
|
| 37 |
+
tokens = aspect_tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 38 |
+
labels = [aspect_model.config.id2label[p] for p in preds]
|
| 39 |
+
|
| 40 |
+
aspects = []
|
| 41 |
+
current_tokens = []
|
| 42 |
+
for token, label in zip(tokens, labels):
|
| 43 |
+
if label == "B-ASP":
|
| 44 |
+
if current_tokens:
|
| 45 |
+
aspects.append(aspect_tokenizer.convert_tokens_to_string(current_tokens).strip())
|
| 46 |
+
current_tokens = []
|
| 47 |
+
current_tokens = [token]
|
| 48 |
+
elif label == "I-ASP" and current_tokens:
|
| 49 |
+
current_tokens.append(token)
|
| 50 |
+
else:
|
| 51 |
+
if current_tokens:
|
| 52 |
+
aspects.append(aspect_tokenizer.convert_tokens_to_string(current_tokens).strip())
|
| 53 |
+
current_tokens = []
|
| 54 |
+
if current_tokens:
|
| 55 |
+
aspects.append(aspect_tokenizer.convert_tokens_to_string(current_tokens).strip())
|
| 56 |
+
return list(set(aspects))
|
| 57 |
+
|
| 58 |
+
def analyze(text_pl):
|
| 59 |
+
try:
|
| 60 |
+
text_en = translate_pl_to_en([text_pl])[0]
|
| 61 |
+
aspects_en = extract_aspects(text_en)
|
| 62 |
+
if not aspects_en:
|
| 63 |
+
return "Nie wykryto żadnych aspektów."
|
| 64 |
+
|
| 65 |
+
results = []
|
| 66 |
+
for asp in aspects_en:
|
| 67 |
+
input_text = f"{text_en} [SEP] {asp}"
|
| 68 |
+
inputs = sentiment_tokenizer(input_text, return_tensors="pt", truncation=True, padding=True).to(device)
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
logits = sentiment_model(**inputs).logits
|
| 71 |
+
predicted_class_id = int(logits.argmax().cpu())
|
| 72 |
+
sentiment_label = {0: "negatywny", 1: "neutralny", 2: "pozytywny", 3: "konfliktowy"}[predicted_class_id]
|
| 73 |
+
asp_pl = translate_en_to_pl([asp])[0]
|
| 74 |
+
results.append(f"{asp_pl.capitalize()} -> **{sentiment_label}**")
|
| 75 |
+
return "\n".join(results)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Błąd: {str(e)}"
|
| 78 |
+
|
| 79 |
+
# === Gradio UI ===
|
| 80 |
+
demo = gr.Interface(
|
| 81 |
+
fn=analyze,
|
| 82 |
+
inputs=gr.Textbox(label="Komentarz po polsku", placeholder="Np. Pizza była pyszna, ale kelner był nieuprzejmy."),
|
| 83 |
+
outputs=gr.Markdown(label="Wyniki analizy"),
|
| 84 |
+
title="ABSA – Analiza komentarzy restauracyjnych",
|
| 85 |
+
description="Wykrywa aspekty i przypisuje im sentymenty (pozytywny / negatywny / neutralny / konfliktowy)."
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
demo.launch()
|