Spaces:
Running
Running
Update apis/chat_api.py
Browse files- apis/chat_api.py +30 -6
apis/chat_api.py
CHANGED
@@ -43,6 +43,7 @@ class ChatAPIApp:
|
|
43 |
default="Hello",
|
44 |
description="(str) `Text for translate`",
|
45 |
)
|
|
|
46 |
|
47 |
def chat_completions(self, item: ChatCompletionsPostItem):
|
48 |
translator = Translator()
|
@@ -51,22 +52,40 @@ class ChatAPIApp:
|
|
51 |
from_lang = 'en'
|
52 |
to_lang = 'en'
|
53 |
for lang_item in available_langs:
|
54 |
-
if item.from_language == lang_item['code']:
|
55 |
-
from_lang = item.from_language
|
56 |
if item.to_language == lang_item['code']:
|
57 |
to_lang = item.to_language
|
|
|
58 |
|
59 |
|
60 |
-
translated = translator.translate(item.input_text, dest=to_lang)
|
61 |
item_response = {
|
62 |
-
"from_language":
|
63 |
-
"to_language":
|
64 |
"text": item.input_text,
|
65 |
-
"translate": translated
|
66 |
}
|
67 |
json_compatible_item_data = jsonable_encoder(item_response)
|
68 |
return JSONResponse(content=json_compatible_item_data)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def setup_routes(self):
|
71 |
for prefix in ["", "/v1"]:
|
72 |
self.app.get(
|
@@ -79,6 +98,11 @@ class ChatAPIApp:
|
|
79 |
summary="translate text",
|
80 |
)(self.chat_completions)
|
81 |
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
class ArgParser(argparse.ArgumentParser):
|
84 |
def __init__(self, *args, **kwargs):
|
|
|
43 |
default="Hello",
|
44 |
description="(str) `Text for translate`",
|
45 |
)
|
46 |
+
|
47 |
|
48 |
def chat_completions(self, item: ChatCompletionsPostItem):
|
49 |
translator = Translator()
|
|
|
52 |
from_lang = 'en'
|
53 |
to_lang = 'en'
|
54 |
for lang_item in available_langs:
|
|
|
|
|
55 |
if item.to_language == lang_item['code']:
|
56 |
to_lang = item.to_language
|
57 |
+
break
|
58 |
|
59 |
|
60 |
+
translated = translator.translate(item.input_text, dest=to_lang)
|
61 |
item_response = {
|
62 |
+
"from_language": translated.src,
|
63 |
+
"to_language": translated.dest,
|
64 |
"text": item.input_text,
|
65 |
+
"translate": translated.text
|
66 |
}
|
67 |
json_compatible_item_data = jsonable_encoder(item_response)
|
68 |
return JSONResponse(content=json_compatible_item_data)
|
69 |
|
70 |
+
|
71 |
+
class DetectLanguagePostItem(BaseModel)
|
72 |
+
input_text: str = Field(
|
73 |
+
default="Hello",
|
74 |
+
description="(str) `Text for translate`",
|
75 |
+
)
|
76 |
+
|
77 |
+
def detect_language(self, item: DetectLanguagePostItem)
|
78 |
+
translator = Translator()
|
79 |
+
detected = translator.detect(item.input_text)
|
80 |
+
|
81 |
+
item_response = {
|
82 |
+
"lang": detected.lang,
|
83 |
+
"confidence": detected.confidence,
|
84 |
+
}
|
85 |
+
json_compatible_item_data = jsonable_encoder(item_response)
|
86 |
+
return JSONResponse(content=json_compatible_item_data)
|
87 |
+
|
88 |
+
|
89 |
def setup_routes(self):
|
90 |
for prefix in ["", "/v1"]:
|
91 |
self.app.get(
|
|
|
98 |
summary="translate text",
|
99 |
)(self.chat_completions)
|
100 |
|
101 |
+
self.app.post(
|
102 |
+
prefix + "/detect",
|
103 |
+
summary="detect language",
|
104 |
+
)(self.detect_language)
|
105 |
+
|
106 |
|
107 |
class ArgParser(argparse.ArgumentParser):
|
108 |
def __init__(self, *args, **kwargs):
|