File size: 1,584 Bytes
3c821f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, field_validator
from translator import translate_singlish

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "https://singlish-translator.netlify.app",  # your Netlify URL
        "http://localhost:5500",                     # VS Code Live Server
        "http://localhost:3000",                     # local dev
        "http://127.0.0.1:5500",                     # VS Code Live Server alt
        "*"                                          # fallback β€” remove after testing
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class TranslationRequest(BaseModel):
    text: str

    @field_validator("text")
    def text_not_empty(cls, v):
        if not v or not v.strip():
            raise ValueError("Text cannot be empty")
        return v


@app.get("/")
def home():
    return {"message": "Singlish β†’ English Translator is running πŸš€"}


@app.post("/translate")
def translate(req: TranslationRequest):
    result = translate_singlish(req.text)

    if "error" in result:
        return {
            "bot_message": "Oops! Something went wrong πŸ˜•",
            "error": result["error"]
        }

    return {
        "bot_message": "Here's the English translation 😊",
        "original_text": result["original_text"],
        "translation": result["translated_text"],
        "source_language": result["source_language"],
        "target_language": result["target_language"]
    }