File size: 1,286 Bytes
c732202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pathlib import Path
import sys

# Get the absolute path to the 'src' directory
src_path = Path(__file__).resolve().parent / 'src'

# Add 'src' directory to the Python path (sys.path)
sys.path.append(str(src_path))

from src.classmodels.translationinput import TranslationInput
from src.classmodels.translationoutput import TranslationOutput
from fastapi.middleware.cors import CORSMiddleware
from src.translation import translate_text

app = FastAPI()

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]    
)
@app.post("/cmsai/translate", response_model=TranslationOutput)
async def translate(input: TranslationInput):
    try:       
        output = translate_text(input.text_to_translate, input.target_language)
        if output is not None:
            return TranslationOutput(status_code=200, translated_text=output)
        else:
            return TranslationOutput(status_code=400, message="target language is not supported")
    except Exception as e:
        return TranslationOutput(status_code=500, message=str(e))

#if __name__ == "__main__":
    #translate(TranslationInput(text_to_translate="Sample",target_language="zh-Cn"))