text-summarizer / app.py
datien228's picture
fix CORS
6550152
raw
history blame
2.31 kB
from flask import Flask, render_template, request, jsonify, make_response
from modules.model import summarize
from flask_cors import CORS, cross_origin
import __main__
app = Flask(__name__)
cors = CORS(app, supports_credentials=True, resources={r"/summarize/*": {"origins": "http://0.0.0.0/summarize"}})
# shortTokenizer = BartTokenizer.from_pretrained('sshleifer/distilbart-xsum-12-6')
# shortModel = BartForConditionalGeneration.from_pretrained('sshleifer/distilbart-xsum-12-6')
# longTokenizer = BartTokenizer.from_pretrained('sshleifer/distilbart-cnn-12-6')
# longModel = BartForConditionalGeneration.from_pretrained('sshleifer/distilbart-cnn-12-6')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/summarize', methods=['POST', 'OPTIONS'])
@cross_origin()
def recommend():
if request.method == "POST":
# Get form data
request_data = request.get_json()
input_text = request_data['input_text']
# Call the function summarize to run the text summarization
try:
short_output_summary, long_output_summary = summarize(input_text)
response = jsonify({'short': short_output_summary.strip(), 'long': long_output_summary.strip()})
# Pass output summary to the output template
response.headers['Access-Control-Allow-Methods'] = '*'
response.headers['Access-Control-Allow-Domain'] = '*'
response.headers['Access-Control-Allow-Credentials'] = True
return response
except Exception as e:
return render_template('index.html', query=e)
elif request.method == "OPTIONS":
resp = make_response("OK")
resp.status_code = 201
# resp.headers['Access-Control-Allow-Origin'] = 'http://0.0.0.0'
resp.headers['Access-Control-Allow-Methods'] = '*'
resp.headers['Access-Control-Allow-Domain'] = '*'
resp.headers['Access-Control-Allow-Credentials'] = True
# Debug
print("Right")
return resp
pass
def main():
# app.config['TEMPLATES_AUTO_RELOAD'] = True
# app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
# app.run(debug=True)
app.run(host='0.0.0.0', port=7860)
if __name__ == '__main__':
print("Loading BART model and tokenzier . . .")
main()