Spaces:
Running
Running
johnpaulbin
commited on
Commit
•
c807e55
1
Parent(s):
138349b
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ from hypercorn.asyncio import serve
|
|
7 |
from hypercorn.config import Config
|
8 |
import os
|
9 |
os.environ['CURL_CA_BUNDLE'] = ''
|
10 |
-
from googletranslate import translate
|
11 |
import json
|
12 |
import random
|
13 |
import re
|
@@ -159,7 +159,7 @@ def random_spanish_pair2():
|
|
159 |
|
160 |
|
161 |
|
162 |
-
|
163 |
@app.route('/translate', methods=['POST'])
|
164 |
def dotranslate():
|
165 |
data = request.get_json()
|
@@ -177,7 +177,48 @@ def dotranslate():
|
|
177 |
return jsonify({'translation': translation}), 200
|
178 |
else:
|
179 |
return jsonify({'error': 'No text provided'}), 400
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
if __name__ == "__main__":
|
183 |
config = Config()
|
|
|
7 |
from hypercorn.config import Config
|
8 |
import os
|
9 |
os.environ['CURL_CA_BUNDLE'] = ''
|
10 |
+
#from googletranslate import translate
|
11 |
import json
|
12 |
import random
|
13 |
import re
|
|
|
159 |
|
160 |
|
161 |
|
162 |
+
"""
|
163 |
@app.route('/translate', methods=['POST'])
|
164 |
def dotranslate():
|
165 |
data = request.get_json()
|
|
|
177 |
return jsonify({'translation': translation}), 200
|
178 |
else:
|
179 |
return jsonify({'error': 'No text provided'}), 400
|
180 |
+
"""
|
181 |
+
|
182 |
+
|
183 |
+
from transformers import M2M100ForConditionalGeneration
|
184 |
+
from tokenization_small100 import SMALL100Tokenizer
|
185 |
+
|
186 |
+
model_name = "alirezamsh/small100"
|
187 |
+
model = M2M100ForConditionalGeneration.from_pretrained(model_name)
|
188 |
+
tokenizer = SMALL100Tokenizer.from_pretrained(model_name)
|
189 |
+
|
190 |
+
@app.route('/translate', methods=['POST'])
|
191 |
+
def dotranslate():
|
192 |
+
data = request.get_json()
|
193 |
+
txt = data.get('txt')
|
194 |
+
src = data.get('src', 'en')
|
195 |
+
dest = data.get('dest', 'es')
|
196 |
+
|
197 |
+
if txt:
|
198 |
+
cache_key = f"{txt}_{src}_{dest}"
|
199 |
+
translation = cache.get(cache_key)
|
200 |
+
if translation is None:
|
201 |
+
# Set the source and target languages
|
202 |
+
tokenizer.src_lang = src
|
203 |
+
tokenizer.tgt_lang = dest
|
204 |
+
|
205 |
+
# Tokenize the input text
|
206 |
+
encoded = tokenizer(txt, return_tensors="pt")
|
207 |
+
|
208 |
+
# Generate translation
|
209 |
+
generated_tokens = model.generate(
|
210 |
+
**encoded,
|
211 |
+
forced_bos_token_id=tokenizer.get_lang_id(dest)
|
212 |
+
)
|
213 |
+
|
214 |
+
# Decode the generated tokens
|
215 |
+
translation = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
216 |
+
|
217 |
+
# Cache the translation
|
218 |
+
cache.set(cache_key, translation)
|
219 |
+
return jsonify({'translation': translation}), 200
|
220 |
+
else:
|
221 |
+
return jsonify({'error': 'No text provided'}), 400
|
222 |
|
223 |
if __name__ == "__main__":
|
224 |
config = Config()
|