Spaces:
Runtime error
Runtime error
Download app/utils/hf_client.py from ismaileghazi/test: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/ismaileghazi/test/resolve/main/app/utils/hf_client.py
- Command line
-
hf download hf://spaces/ismaileghazi/test/app/utils/hf_client.py
-
curl -L -o hf_client.py https://huggingface.co/spaces/ismaileghazi/test/resolve/main/app/utils/hf_client.py
1.17 kB
| 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 | |