darshankr commited on
Commit
7c7feed
·
verified ·
1 Parent(s): b9242df

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +18 -7
api.py CHANGED
@@ -1,12 +1,22 @@
 
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
 
@@ -15,12 +25,13 @@ 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))
 
1
+
2
  # api.py
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
  from typing import List
6
+ from fastapi.responses import JSONResponse
7
+ from fastapi.middleware.cors import CORSMiddleware
8
 
9
  app = FastAPI()
10
 
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ class TranslationRequest(BaseModel):
20
  sentences: List[str]
21
  target_lang: str
22
 
 
25
  return {"status": "healthy"}
26
 
27
  @app.post("/translate")
28
+ async def translate(request: TranslationRequest):
29
  try:
30
+ from app import translate_text
31
  result = translate_text(
32
+ sentences=request.sentences,
33
+ target_lang=request.target_lang
34
  )
35
+ return JSONResponse(content=result)
36
  except Exception as e:
37
+ raise HTTPException(status_code=500, detail=str(e))