Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,18 @@ import torch
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
# === Tokenizery i modele ABSA ===
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 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)
|
|
@@ -55,15 +56,19 @@ def extract_aspects(text):
|
|
| 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():
|
|
@@ -74,7 +79,7 @@ def analyze(text_pl):
|
|
| 74 |
results.append(f"{asp_pl.capitalize()} -> **{sentiment_label}**")
|
| 75 |
return "\n".join(results)
|
| 76 |
except Exception as e:
|
| 77 |
-
return f"Błąd: {
|
| 78 |
|
| 79 |
# === Gradio UI ===
|
| 80 |
demo = gr.Interface(
|
|
@@ -82,7 +87,8 @@ demo = gr.Interface(
|
|
| 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(
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
# === Tokenizery i modele ABSA ===
|
| 12 |
+
with gr.StatusTracker("Ładowanie modeli..."):
|
| 13 |
+
aspect_tokenizer = AutoTokenizer.from_pretrained("EfektMotyla/bert-aspect-ner")
|
| 14 |
+
aspect_model = AutoModelForTokenClassification.from_pretrained("EfektMotyla/bert-aspect-ner").to(device)
|
| 15 |
|
| 16 |
+
sentiment_tokenizer = AutoTokenizer.from_pretrained("EfektMotyla/absa-roberta")
|
| 17 |
+
sentiment_model = AutoModelForSequenceClassification.from_pretrained("EfektMotyla/absa-roberta").to(device)
|
| 18 |
|
| 19 |
+
en_to_pl_tokenizer = MarianTokenizer.from_pretrained("gsarti/opus-mt-tc-en-pl")
|
| 20 |
+
en_to_pl_model = MarianMTModel.from_pretrained("gsarti/opus-mt-tc-en-pl").to(device)
|
| 21 |
|
| 22 |
+
pl_to_en_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-pl-en")
|
| 23 |
+
pl_to_en_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-pl-en").to(device)
|
| 24 |
|
| 25 |
def translate(texts, tokenizer, model):
|
| 26 |
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True).to(device)
|
|
|
|
| 56 |
aspects.append(aspect_tokenizer.convert_tokens_to_string(current_tokens).strip())
|
| 57 |
return list(set(aspects))
|
| 58 |
|
| 59 |
+
def analyze(text_pl, progress=gr.Progress()):
|
| 60 |
try:
|
| 61 |
+
progress(0, desc="Tłumaczenie na angielski...")
|
| 62 |
text_en = translate_pl_to_en([text_pl])[0]
|
| 63 |
+
|
| 64 |
+
progress(0.3, desc="Wykrywanie aspektów...")
|
| 65 |
aspects_en = extract_aspects(text_en)
|
| 66 |
if not aspects_en:
|
| 67 |
return "Nie wykryto żadnych aspektów."
|
| 68 |
+
|
| 69 |
results = []
|
| 70 |
+
for i, asp in enumerate(aspects_en):
|
| 71 |
+
progress(0.4 + i/len(aspects_en)*0.6, desc=f"Analiza aspektu: {asp}")
|
| 72 |
input_text = f"{text_en} [SEP] {asp}"
|
| 73 |
inputs = sentiment_tokenizer(input_text, return_tensors="pt", truncation=True, padding=True).to(device)
|
| 74 |
with torch.no_grad():
|
|
|
|
| 79 |
results.append(f"{asp_pl.capitalize()} -> **{sentiment_label}**")
|
| 80 |
return "\n".join(results)
|
| 81 |
except Exception as e:
|
| 82 |
+
return f"Błąd podczas analizy: {e}"
|
| 83 |
|
| 84 |
# === Gradio UI ===
|
| 85 |
demo = gr.Interface(
|
|
|
|
| 87 |
inputs=gr.Textbox(label="Komentarz po polsku", placeholder="Np. Pizza była pyszna, ale kelner był nieuprzejmy."),
|
| 88 |
outputs=gr.Markdown(label="Wyniki analizy"),
|
| 89 |
title="ABSA – Analiza komentarzy restauracyjnych",
|
| 90 |
+
description="Wykrywa aspekty i przypisuje im sentymenty (pozytywny / negatywny / neutralny / konfliktowy).",
|
| 91 |
+
allow_flagging="never"
|
| 92 |
)
|
| 93 |
|
| 94 |
+
demo.launch()
|