Spaces:
Running
Running
Update apis/chat_api.py
Browse files- apis/chat_api.py +71 -2
apis/chat_api.py
CHANGED
@@ -4,6 +4,7 @@ import sys
|
|
4 |
import os
|
5 |
import io
|
6 |
from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
|
|
|
7 |
import time
|
8 |
import json
|
9 |
from typing import List
|
@@ -137,7 +138,75 @@ class ChatAPIApp:
|
|
137 |
}
|
138 |
json_compatible_item_data = jsonable_encoder(item_response)
|
139 |
return JSONResponse(content=json_compatible_item_data)
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
class DetectLanguagePostItem(BaseModel):
|
142 |
input_text: str = Field(
|
143 |
default="Hello, how are you?",
|
@@ -201,7 +270,7 @@ class ChatAPIApp:
|
|
201 |
self.app.post(
|
202 |
prefix + "/translate/ai",
|
203 |
summary="translate text with ai",
|
204 |
-
)(self.
|
205 |
|
206 |
|
207 |
self.app.post(
|
|
|
4 |
import os
|
5 |
import io
|
6 |
from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
|
7 |
+
from transformers import pipeline
|
8 |
import time
|
9 |
import json
|
10 |
from typing import List
|
|
|
138 |
}
|
139 |
json_compatible_item_data = jsonable_encoder(item_response)
|
140 |
return JSONResponse(content=json_compatible_item_data)
|
141 |
+
class TranslateAiPostItem(BaseModel):
|
142 |
+
model: str = Field(
|
143 |
+
default="t5-base",
|
144 |
+
description="(str) `Model Name`",
|
145 |
+
)
|
146 |
+
from_language: str = Field(
|
147 |
+
default="en",
|
148 |
+
description="(str) `Detect`",
|
149 |
+
)
|
150 |
+
to_language: str = Field(
|
151 |
+
default="fa",
|
152 |
+
description="(str) `en`",
|
153 |
+
)
|
154 |
+
input_text: str = Field(
|
155 |
+
default="Hello",
|
156 |
+
description="(str) `Text for translate`",
|
157 |
+
)
|
158 |
+
def ai_translate(self, item:TranslateAiPostItem):
|
159 |
+
MODEL_MAP = {
|
160 |
+
"t5-base": "t5-base",
|
161 |
+
"t5-small": "t5-small",
|
162 |
+
"t5-large": "t5-3b",
|
163 |
+
"t5-3b": "t5-3b",
|
164 |
+
"mbart-large-50-many-to-many-mmt": "facebook/mbart-large-50-many-to-many-mmt",
|
165 |
+
"nllb-200-distilled-600M": "facebook/nllb-200-distilled-600M",
|
166 |
+
"madlad400-3b-mt": "jbochi/madlad400-3b-mt",
|
167 |
+
"default": "t5-base",
|
168 |
+
}
|
169 |
+
if item.model in MODEL_MAP.keys():
|
170 |
+
target_model = item.model
|
171 |
+
else:
|
172 |
+
target_model = "default"
|
173 |
+
|
174 |
+
model_fullname = MODEL_MAP[target_model]
|
175 |
+
request_url = (
|
176 |
+
f"https://api-inference.huggingface.co/models/{model_fullname}"
|
177 |
+
)
|
178 |
+
request_headers = {
|
179 |
+
"Content-Type": "application/json",
|
180 |
+
}
|
181 |
+
request_body = {
|
182 |
+
"inputs": item.input_text,
|
183 |
+
"parameters": {
|
184 |
+
"src_lang": item.from_language,
|
185 |
+
"tgt_lang": item.to_language,
|
186 |
+
"return_full_text": True,
|
187 |
+
},
|
188 |
+
"stream": False,
|
189 |
+
}
|
190 |
+
enver.set_envs(proxies=True)
|
191 |
+
stream = requests.post(
|
192 |
+
request_url,
|
193 |
+
headers=request_headers,
|
194 |
+
json=request_body,
|
195 |
+
proxies=enver.requests_proxies,
|
196 |
+
stream=False,
|
197 |
+
)
|
198 |
+
status_code = stream.status_code
|
199 |
+
if status_code == 200:
|
200 |
+
result = stream.json()
|
201 |
+
else:
|
202 |
+
result = 'unknown error'
|
203 |
+
|
204 |
+
item_response = {
|
205 |
+
"statue": status_code,
|
206 |
+
"result": result,
|
207 |
+
}
|
208 |
+
json_compatible_item_data = jsonable_encoder(item_response)
|
209 |
+
return JSONResponse(content=json_compatible_item_data)
|
210 |
class DetectLanguagePostItem(BaseModel):
|
211 |
input_text: str = Field(
|
212 |
default="Hello, how are you?",
|
|
|
270 |
self.app.post(
|
271 |
prefix + "/translate/ai",
|
272 |
summary="translate text with ai",
|
273 |
+
)(self.ai_translate)
|
274 |
|
275 |
|
276 |
self.app.post(
|