File size: 1,168 Bytes
21f81f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import os

HF_TOKEN = os.getenv("HF_TOKEN")
API_URL_TEMPLATE = "https://router.huggingface.co/hf-inference/models/{model}"

MODELS = {
    "fr-en": "Helsinki-NLP/opus-mt-fr-en",
    "en-fr": "Helsinki-NLP/opus-mt-en-fr"
}

def translate_text(text: str, direction: str):
    if direction not in MODELS:
        raise ValueError("Invalid direction")
    
    model = MODELS[direction]
    api_url = API_URL_TEMPLATE.format(model=model)
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}
    payload = {"inputs": text}

    try:
        response = requests.post(api_url, headers=headers, json=payload, timeout=10)
        
        if response.status_code == 503:
             # Model loading
             return {"error": "Model is loading, please try again shortly", "status": 503}

        response.raise_for_status()
        result = response.json()
        
        if isinstance(result, list) and len(result) > 0 and 'translation_text' in result[0]:
            return result[0]['translation_text']
        else:
            return None
    except requests.exceptions.RequestException as e:
        print(f"HF API Error: {e}")
        return None