diff --git a/main.py b/main.py index f1c83b8..fb940bf 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ from starlette.middleware.cors import CORSMiddleware from core.sql_util import check_and_create_table -from routers import deepl, location, subway, weather, auth +from routers import translate, location, subway, weather, auth # from service.summary.nlp_util import update_nlp_client @@ -24,7 +24,7 @@ app.include_router(weather.router, prefix="/service/weather", tags=["weather"]) # app.include_router(summary.router, prefix="/service/summary", tags=["summary"]) app.include_router(subway.router, prefix="/service/subway", tags=["subway"]) -app.include_router(deepl.router, prefix="/service/deepl", tags=["deepl"]) +app.include_router(translate.router, prefix="/service/translate", tags=["translate"]) app.include_router(location.router, prefix="/service/location", tags=["ipinfo"]) check_and_create_table() diff --git a/routers/deepl.py b/routers/deepl.py deleted file mode 100644 index 6cc202f..0000000 --- a/routers/deepl.py +++ /dev/null @@ -1,19 +0,0 @@ -from fastapi import APIRouter, HTTPException - -from service.deepl import translate, LangCode - -router = APIRouter() - - -@router.post("/") -async def root_translate( - content: str, - source: LangCode | str | None, - target: LangCode | str | None -): - res = translate(content, source, target) - - if res is None: - raise HTTPException(status_code=500, detail="Translation failed") - - return res diff --git a/routers/translate.py b/routers/translate.py new file mode 100644 index 0000000..a95c162 --- /dev/null +++ b/routers/translate.py @@ -0,0 +1,14 @@ +import os + +import deepl +from fastapi import APIRouter + +router = APIRouter() + +API_KEY = os.environ.get("DEEPL_KEY") +translator = deepl.Translator("b00f5dce-5f8a-44a6-c6cd-3c972604600f:fx") + + +@router.post("/toKr") +async def translate(text: str): + return translator.translate_text(text, source_lang="EN", target_lang="KO") diff --git a/service/deepl.py b/service/deepl.py deleted file mode 100644 index ba01063..0000000 --- a/service/deepl.py +++ /dev/null @@ -1,41 +0,0 @@ -import json -import os -from enum import Enum -from urllib import parse - -import requests - -_URL = "https://api-free.deepl.com/v2/translate" -_DEEPL_API_KEY = os.environ.get("DEEPL_API_KEY") - - -class LangCode(str, Enum): - KO = "KO" - EN = "EN" - - -def translate( - text: str, - source: LangCode | None = None, - target: LangCode = LangCode.KO, -) -> str | None: - headers = { - "Authorization": f"DeepL-Auth-Key {_DEEPL_API_KEY}", - } - - data = { - "text": text, - "source_lang": source, - "target_lang": target, - } - - res = requests.post(url=_URL, headers=headers, data=data) - - if res.status_code != 200: - print(res) - print(res.text) - return None - - json_obj = json.loads(res.text) - - return parse.unquote(json_obj["translations"][0]["text"])