File size: 1,579 Bytes
de8ea8e
 
57fc410
86cef54
57fc410
 
3e62e94
 
57fc410
 
 
 
86cef54
57fc410
 
 
86cef54
57fc410
 
 
 
 
 
de8ea8e
da66d9c
 
 
de8ea8e
57fc410
 
de8ea8e
 
 
57fc410
de8ea8e
da66d9c
 
de8ea8e
57fc410
da66d9c
 
 
 
 
 
de8ea8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

# --- Performance: Load models once at startup ---
model_id = "rrrr66254/Glossa-BART"

# This model loading now relies on the environment variables 
# set in apiRoutes.py to correctly define the cache location.
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id, trust_remote_code=True)
model.eval()

# Move model to CUDA if available, once at startup
if torch.cuda.is_available():
    model = model.to("cuda")

# ----------------------------------------------------------------------

def translateGloss(gloss: str) -> str:
    """
    Translates a sign language gloss sequence into a natural language sentence 
    using the pre-loaded Glossa-BART model.
    """
    
    # --- DEBUG STEP 1: Log Input ---
    print(f"DEBUG: Starting translation for gloss: '{gloss}'")
    
    inputs = tokenizer(gloss, return_tensors="pt", padding=True, truncation=True)
    
    # Move inputs to CUDA if the model is on CUDA
    if torch.cuda.is_available():
        inputs = {k: v.to("cuda") for k,v in inputs.items()}
    
    # Generate the translated sentence
    outputs = model.generate(**inputs, max_new_tokens=50, do_sample=False)
    
    # Decode the output
    result = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # Clean up any residual whitespace
    result = result.strip()
    
    # --- DEBUG STEP 2: Log Output ---
    print(f"DEBUG: Model generated raw result: '{result}'")
    
    return result