Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from pathlib import Path
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List
|
| 6 |
-
|
| 7 |
import torch
|
| 8 |
from transformers import (
|
| 9 |
AutoTokenizer,
|
|
@@ -13,12 +13,6 @@ from transformers import (
|
|
| 13 |
)
|
| 14 |
import os
|
| 15 |
|
| 16 |
-
|
| 17 |
-
print("π ZAWARTOΕΔ /app/bert-aspect-ner:")
|
| 18 |
-
print(os.listdir("/app/bert-aspect-ner") if os.path.exists("/app/bert-aspect-ner") else "β NIE ISTNIEJE")
|
| 19 |
-
|
| 20 |
-
print("π ZAWARTOΕΔ /app/absa-roberta:")
|
| 21 |
-
print(os.listdir("/app/absa-roberta") if os.path.exists("/app/absa-roberta") else "β NIE ISTNIEJE")
|
| 22 |
# ββββββββββββββββββββββ konfiguracja ββββββββββββββββββββββ
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
|
|
@@ -43,17 +37,26 @@ sentiment_model = AutoModelForSequenceClassification.from_pretrained(
|
|
| 43 |
).to(device)
|
| 44 |
|
| 45 |
# ββββββββββββββββββββββ modele tΕumaczeΕ (on-line) βββββββββ
|
| 46 |
-
pl_to_en = pipeline(
|
| 47 |
-
"translation",
|
| 48 |
-
model="Helsinki-NLP/opus-mt-pl-en",
|
| 49 |
-
device=0 if device == "cuda" else -1,
|
| 50 |
-
)
|
| 51 |
-
en_to_pl = pipeline(
|
| 52 |
-
"translation",
|
| 53 |
-
model="gsarti/opus-mt-tc-en-pl",
|
| 54 |
-
device=0 if device == "cuda" else -1,
|
| 55 |
-
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# ββββββββββββββββββββββ schemy Pydantic ββββββββββββββββββββ
|
| 58 |
class Comment(BaseModel):
|
| 59 |
text: str
|
|
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List
|
| 6 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 7 |
import torch
|
| 8 |
from transformers import (
|
| 9 |
AutoTokenizer,
|
|
|
|
| 13 |
)
|
| 14 |
import os
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# ββββββββββββββββββββββ konfiguracja ββββββββββββββββββββββ
|
| 17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
|
|
|
|
| 37 |
).to(device)
|
| 38 |
|
| 39 |
# ββββββββββββββββββββββ modele tΕumaczeΕ (on-line) βββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
model_name = "Helsinki-NLP/opus-mt-pl-en"
|
| 42 |
+
pl_to_en_tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 43 |
+
pl_to_en_model = MarianMTModel.from_pretrained(model_name).to(device)
|
| 44 |
+
|
| 45 |
+
def translate_pl_to_en(texts):
|
| 46 |
+
inputs = pl_to_en_tokenizer(texts, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
translated = pl_to_en_model.generate(**inputs)
|
| 49 |
+
return pl_to_en_tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 50 |
+
|
| 51 |
+
en_to_pl_model_name = "gsarti/opus-mt-tc-en-pl"
|
| 52 |
+
en_to_pl_tokenizer = MarianTokenizer.from_pretrained(en_to_pl_model_name)
|
| 53 |
+
en_to_pl_model = MarianMTModel.from_pretrained(en_to_pl_model_name).to(device)
|
| 54 |
+
|
| 55 |
+
def translate_en_to_pl(texts):
|
| 56 |
+
inputs = en_to_pl_tokenizer(texts, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 57 |
+
with torch.no_grad():
|
| 58 |
+
translated = en_to_pl_model.generate(**inputs)
|
| 59 |
+
return en_to_pl_tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 60 |
# ββββββββββββββββββββββ schemy Pydantic ββββββββββββββββββββ
|
| 61 |
class Comment(BaseModel):
|
| 62 |
text: str
|