Skip to content

Instantly share code, notes, and snippets.

@Soumi7
Last active March 18, 2021 04:30
Show Gist options
  • Save Soumi7/fb82e00f85f630f983eb88d197d9b169 to your computer and use it in GitHub Desktop.
Save Soumi7/fb82e00f85f630f983eb88d197d9b169 to your computer and use it in GitHub Desktop.
import numpy as np
from flask import Flask, request, jsonify, render_template
import simpletransformers
import pandas as pd
import requests
from simpletransformers.ner import NERModel
import json
import io
from flask_csv import send_csv
from flask import make_response
from flask import session, redirect
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict',methods=['POST',"GET"])
def predict():
'''
For rendering results on HTML GUI
'''
int_features = [x for x in request.form.values()]
print(int_features)
sentence = int_features[0]
if int_features[1] == 'display':
model1 = NERModel('bert', 'MODEL',
labels=[LIST OF UNIQUE LABELS],
args={"save_eval_checkpoints": False,
"save_steps": -1,
"output_dir": "MODEL",
'overwrite_output_dir': True,
"save_model_every_epoch": False,
'reprocess_input_data': True,
"train_batch_size": 10,'num_train_epochs': 5,"max_seq_length": 256}, use_cuda=False)
predictions, raw_outputs = model1.predict([sentence])
result = json.dumps(predictions[0])
return render_template('index.html', prediction_text=result)
elif int_features[1] == 'getcsv':
model1 = NERModel('bert', 'MODEL',
labels=[LIST OF LABELS],
args={"save_eval_checkpoints": False,
"save_steps": -1,
"output_dir": "MODEL",
'overwrite_output_dir': True,
"save_model_every_epoch": False,
'reprocess_input_data': True,
"train_batch_size": 10,'num_train_epochs': 5,"max_seq_length": 256}, use_cuda=False)
predictions, raw_outputs = model1.predict([sentence])
l=[]
for i in predictions[0]:
dic={}
for j in i.keys():
dic['word']=j
dic['tag']=i[j]
l.append(dic)
return send_csv(l,"tags.csv",["word","tag"])
if __name__ == "__main__":
app.run(debug=True)
@higopires
Copy link

Hello, Soumi.

Came here from this post of you. Can you tell me, please, where can I find the index.html file?

Best regards,

H.

@Soumi7
Copy link
Author

Soumi7 commented Mar 18, 2021

@higopires
Copy link

Thank you!

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