forked from akash-network/awesome-akash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
31 lines (26 loc) · 1.12 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from flask import Flask, request, jsonify, render_template
from transformers import BertTokenizer, BertForMaskedLM
app = Flask(__name__)
MODEL_NAME = 'bert-base-uncased'
tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
model = BertForMaskedLM.from_pretrained(MODEL_NAME)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
text = request.form['text']
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
predicted_token = tokenizer.decode(output.logits.argmax(-1).tolist()[0])
return render_template('index.html', output=predicted_token)
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
text = data.get('text')
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
predicted_token = tokenizer.decode(output.logits.argmax(-1).tolist()[0])
return jsonify({'predicted_token': predicted_token})
if __name__ == '__main__':
print("BERT deployed successfully!")
app.run(host='0.0.0.0', port=80)