Update main.py
Browse files
main.py
CHANGED
@@ -3,18 +3,17 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained("unicamp-dl/translation-en-pt-t5")
|
7 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("unicamp-dl/translation-en-pt-t5")
|
8 |
-
enpt_pipeline = pipeline('text2text-generation', model=model, tokenizer=tokenizer, max_length=512, num_beams=5)
|
9 |
-
|
10 |
-
|
11 |
@app.route('/translate', methods=['POST'])
|
12 |
def translate():
|
|
|
|
|
|
|
|
|
|
|
13 |
content = request.json
|
14 |
-
|
15 |
result = enpt_pipeline(f"translate English to Portuguese: {text_to_translate}")
|
16 |
return jsonify({"translated_text": result[0]["generated_text"]})
|
17 |
|
18 |
-
|
19 |
if __name__ == '__main__':
|
20 |
-
app.run(debug=True)
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
@app.route('/translate', methods=['POST'])
|
7 |
def translate():
|
8 |
+
# Load the model and tokenizer for each request
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("unicamp-dl/translation-en-pt-t5")
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("unicamp-dl/translation-en-pt-t5")
|
11 |
+
enpt_pipeline = pipeline('text2text-generation', model=model, tokenizer=tokenizer, max_length=512, num_beams=5)
|
12 |
+
|
13 |
content = request.json
|
14 |
+
text_to translate = content['text']
|
15 |
result = enpt_pipeline(f"translate English to Portuguese: {text_to_translate}")
|
16 |
return jsonify({"translated_text": result[0]["generated_text"]})
|
17 |
|
|
|
18 |
if __name__ == '__main__':
|
19 |
+
app.run(debug=True)
|