Guiziii commited on
Commit
edfb74a
1 Parent(s): ab0a177

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -0
main.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ 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
+ 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
+
19
+ if __name__ == '__main__':
20
+ app.run(debug=True)