Spaces:
Sleeping
Sleeping
Florin Bobiș
commited on
Commit
•
cfab274
1
Parent(s):
d1a1a6f
added flask api
Browse files- flask_api.py +21 -0
flask_api.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import MT5ForConditionalGeneration, T5Tokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
model = MT5ForConditionalGeneration.from_pretrained('iliemihai/mt5-base-romanian-diacritics')
|
8 |
+
tokenizer = T5Tokenizer.from_pretrained('iliemihai/mt5-base-romanian-diacritics', legacy=False)
|
9 |
+
|
10 |
+
@app.route('/generate', methods=['POST'])
|
11 |
+
def generate_text():
|
12 |
+
input_text = request.get_json()['input_text']
|
13 |
+
if input_text is None:
|
14 |
+
return jsonify({'error': 'No input text provided'})
|
15 |
+
inputs = tokenizer(input_text, max_length=256, truncation=True, return_tensors="pt")
|
16 |
+
outputs = model.generate(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
|
17 |
+
output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
return jsonify({'output': output})
|
19 |
+
|
20 |
+
if __name__ == '__main__':
|
21 |
+
app.run(debug=False)
|