Spaces:
Runtime error
Runtime error
Create api.py
Browse files
api.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# api.py
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import List
|
5 |
+
from app import translate_text
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
class InputData(BaseModel):
|
10 |
+
sentences: List[str]
|
11 |
+
target_lang: str
|
12 |
+
|
13 |
+
@app.get("/health")
|
14 |
+
async def health_check():
|
15 |
+
return {"status": "healthy"}
|
16 |
+
|
17 |
+
@app.post("/translate")
|
18 |
+
async def translate(input_data: InputData):
|
19 |
+
try:
|
20 |
+
result = translate_text(
|
21 |
+
sentences=input_data.sentences,
|
22 |
+
target_lang=input_data.target_lang
|
23 |
+
)
|
24 |
+
return result
|
25 |
+
except Exception as e:
|
26 |
+
raise HTTPException(status_code=500, detail=str(e))
|