Skip to content

Instantly share code, notes, and snippets.

@kusal1990
Last active June 28, 2022 05:41
Show Gist options
  • Save kusal1990/4ed3923b73a9f21810ee0129f2a9d9eb to your computer and use it in GitHub Desktop.
Save kusal1990/4ed3923b73a9f21810ee0129f2a9d9eb to your computer and use it in GitHub Desktop.
from flask import Flask, jsonify, request
# In[ ]:
import flask
app = Flask(__name__)
# In[ ]:
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/index8051')
def index8050():
return flask.render_template('index8051.html')
@app.route('/predict', methods=['POST'])
def predict():
to_predict_list=request.form.to_dict()
only_question = extract_question(to_predict_list['question'])
only_answers = extract_answers(to_predict_list['question'])
options_list = data_generator(only_answers)
input_string = only_question + '\\n ' + only_answers + ' </s>'
input_ids = tokenizer.encode(input_string, truncation=True, max_length=MAX_LEN, return_tensors='pt')
# Converting input ids to torch tensor of Long type.
input_ids = input_ids.type(torch.LongTensor).to(device)
pred = run_model(input_ids.reshape((1,-1)))[0] #Reshaping because model takes input like (batch_size, sequence_length)
#Pred is in the form like '<pad> answer </s>'. So we have to remove <pad> and </s>.
pred = pred.replace('<pad>','')
pred = pred.replace('</s>','')
pred = pred.lstrip()
return jsonify({"Answer":pred})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8051)
@kusal1990
Copy link
Author

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment