radinhas commited on
Commit
1e78474
1 Parent(s): 7d69da5

Update apis/chat_api.py

Browse files
Files changed (1) hide show
  1. apis/chat_api.py +12 -1
apis/chat_api.py CHANGED
@@ -3,6 +3,7 @@ import uvicorn
3
  import sys
4
  import json
5
 
 
6
  from fastapi import FastAPI
7
  from fastapi.encoders import jsonable_encoder
8
  from fastapi.responses import JSONResponse
@@ -11,6 +12,7 @@ from sse_starlette.sse import EventSourceResponse
11
  from utils.logger import logger
12
  from networks.message_streamer import MessageStreamer
13
  from messagers.message_composer import MessageComposer
 
14
 
15
 
16
  class ChatAPIApp:
@@ -43,11 +45,20 @@ class ChatAPIApp:
43
  )
44
 
45
  def chat_completions(self, item: ChatCompletionsPostItem):
 
 
 
 
 
 
 
 
 
46
  item_response = {
47
  "from_language": item.from_language,
48
  "to_language": item.to_language,
49
  "text": item.text,
50
- "translate": ""
51
  }
52
  json_compatible_item_data = jsonable_encoder(item_response)
53
  return JSONResponse(content=json_compatible_item_data)
 
3
  import sys
4
  import json
5
 
6
+
7
  from fastapi import FastAPI
8
  from fastapi.encoders import jsonable_encoder
9
  from fastapi.responses import JSONResponse
 
12
  from utils.logger import logger
13
  from networks.message_streamer import MessageStreamer
14
  from messagers.message_composer import MessageComposer
15
+ from googletrans import Translator
16
 
17
 
18
  class ChatAPIApp:
 
45
  )
46
 
47
  def chat_completions(self, item: ChatCompletionsPostItem):
48
+ translator = Translator()
49
+ f = open('apis/lang_name.json', "r")
50
+ available_langs = json.loads(f.read())
51
+ from_lang = "en"
52
+ for lang_item in available_langs:
53
+ if item.to_language == lang_item['code']:
54
+ from_lang = item.to_language
55
+ break
56
+
57
  item_response = {
58
  "from_language": item.from_language,
59
  "to_language": item.to_language,
60
  "text": item.text,
61
+ "translate": translator.translate(item.text, dest=from_lang)
62
  }
63
  json_compatible_item_data = jsonable_encoder(item_response)
64
  return JSONResponse(content=json_compatible_item_data)