Spaces:
Running
Running
Delete translation_service.py
Browse files- translation_service.py +0 -51
translation_service.py
DELETED
@@ -1,51 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import logging
|
3 |
-
import requests
|
4 |
-
from pydantic import BaseModel
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
|
7 |
-
# Load environment variables
|
8 |
-
load_dotenv()
|
9 |
-
|
10 |
-
class TranslationRequest(BaseModel):
|
11 |
-
src: str
|
12 |
-
tgt: str
|
13 |
-
use_multi: str
|
14 |
-
text: str
|
15 |
-
|
16 |
-
class Config:
|
17 |
-
populate_by_name = True
|
18 |
-
|
19 |
-
class TranslationService:
|
20 |
-
def __init__(self):
|
21 |
-
self.api_url = os.getenv('TRANSLATION_API_URL')
|
22 |
-
if not self.api_url:
|
23 |
-
raise ValueError("TRANSLATION_API_URL environment variable is not set")
|
24 |
-
|
25 |
-
def translate(self, text: str, src_language: str, tgt_language: str) -> str:
|
26 |
-
try:
|
27 |
-
payload = TranslationRequest(
|
28 |
-
src=src_language,
|
29 |
-
tgt=tgt_language,
|
30 |
-
use_multi="MULTI",
|
31 |
-
text=text
|
32 |
-
)
|
33 |
-
|
34 |
-
response = requests.post(
|
35 |
-
self.api_url,
|
36 |
-
headers={
|
37 |
-
"accept": "application/json",
|
38 |
-
"Content-Type": "application/json"
|
39 |
-
},
|
40 |
-
json=payload.model_dump()
|
41 |
-
)
|
42 |
-
|
43 |
-
if response.status_code == 200:
|
44 |
-
return response.json().get("translation")
|
45 |
-
elif response.status_code == 406:
|
46 |
-
raise ValueError("Invalid language pair selected")
|
47 |
-
else:
|
48 |
-
raise ValueError(f"Translation failed with status code {response.status_code}")
|
49 |
-
except Exception as e:
|
50 |
-
logging.error(f"Translation error: {str(e)}")
|
51 |
-
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|