Spaces:
Running
Running
File size: 2,124 Bytes
acf8bfe 1fd0997 129257a acf8bfe 129257a fc58506 5dc46ff fc58506 acf8bfe 1fd0997 a371d81 1fd0997 acf8bfe 1fd0997 acf8bfe 1fd0997 acf8bfe fc58506 129257a a371d81 129257a fc58506 acf8bfe 129257a 1fd0997 a371d81 1fd0997 acf8bfe 129257a |
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 48 49 50 51 52 53 54 55 56 57 58 59 |
from fastapi import FastAPI, HTTPException
from transformers import pipeline
import langdetect
import logging
import os
os.environ["HF_HOME"] = "/app/cache"
os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
app = FastAPI()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
MODEL_MAP = {
"id": "Helsinki-NLP/opus-mt-id-en",
"th": "Helsinki-NLP/opus-mt-th-en",
"fr": "Helsinki-NLP/opus-mt-fr-en",
"es": "Helsinki-NLP/opus-mt-es-en",
"ja": "Helsinki-NLP/opus-mt-ja-en",
"zh": "Helsinki-NLP/opus-mt-zh-en", # Satu entri untuk Mandarin (Sederhana dan Tradisional)
"vi": "Helsinki-NLP/opus-mt-vi-en",
}
translators = {}
try:
for lang, model_name in MODEL_MAP.items():
logger.info(f"Loading model for {lang}...")
translators[lang] = pipeline("translation", model=model_name)
logger.info(f"Model for {lang} loaded successfully")
except Exception as e:
logger.error(f"Model initialization failed: {str(e)}")
raise Exception(f"Model initialization failed: {str(e)}")
def detect_language(text: str) -> str:
try:
lang = langdetect.detect(text)
return lang if lang in MODEL_MAP else "en"
except Exception as e:
logger.warning(f"Language detection failed: {str(e)}, defaulting to English")
return "en"
@app.post("/translate")
async def translate(text: str):
if not text:
raise HTTPException(status_code=400, detail="Text input is required")
try:
source_lang = detect_language(text)
logger.info(f"Detected source language: {source_lang}")
if source_lang == "en":
return {"translated_text": text}
translator = translators.get(source_lang)
if not translator:
raise HTTPException(status_code=400, detail=f"Translation not supported for language: {source_lang}")
result = translator(text)
return {"translated_text": result[0]["translation_text"]}
except Exception as e:
logger.error(f"Processing failed: {str(e)}")
raise HTTPException(status_code=500, detail=f"Processing failed: {str(e)}") |