EfektMotyla commited on
Commit
9de93b0
Β·
verified Β·
1 Parent(s): 777bb09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -17
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