from flask import Flask from flask import request from flask import render_template from flask import redirect from flask import jsonify from flask import send_from_directory import json import numpy as np import moduli from hugchat import hugchat from numpy.linalg import norm import datetime import tensorflow as tf import tensorflow_hub as hub import tensorflow_text from functools import wraps model_url = "./hub_model/" hub_layer = hub.load(model_url) path='./' app = Flask(__name__, template_folder=path+'/templates',static_url_path='',static_folder=path+'/static') def support_jsonp(func): """Wraps JSONified output for JSONP requests.""" @wraps(func) def decorated_function(*args, **kwargs): callback = request.args.get('callback', False) if callback: resp = func(*args, **kwargs) resp.set_data('{}({})'.format( str(callback), resp.get_data(as_text=True) )) resp.mimetype = 'application/javascript' return resp else: return func(*args, **kwargs) return decorated_function @app.route("/") def flask_app(): return '
' \ '

Vai alla 7Demo!

' \ '
' \ '

Vai alla Question Answering Demo!

' @app.route('/broadcast_labels',methods=['POST','GET']) @support_jsonp def broadcast_labels(): if request.method == 'GET': s=request.args.get('S') dati=moduli.la7_labels(s) output=dati response = jsonify(output) return response @app.route('/requestsQA',methods=['POST','GET']) @support_jsonp def tasksQA(): ris='' if request.method == 'POST': domanda = str(request.form['domanda']).strip() s = str(request.form['S']).strip() ris=moduli.la7_QA(s,domanda,nlp_p) return ris @app.route('/requestsSearch',methods=['POST','GET']) @support_jsonp def tasksSearch(): response='' if request.method == 'POST': domanda = str(request.form['frase']).strip() s = str(request.form['S']).strip() dati=moduli.la7_Search(s,domanda,hub_layer) output=dati response = jsonify(output) return response @app.route('/predict', methods=['POST','GET']) @support_jsonp def predict(): dati=[] #context=request.args.get('testo') context=str(request.form['testo']).strip() #question=request.args.get('testo_domanda') question=str(request.form['testo_domanda']).strip() w=moduli.la7_Bot(context,question,hugchat) d={} d['risultato']=w d['nome']='ok' data=jsonify(d) data.headers.add('Access-Control-Allow-Origin', '*') return data if __name__ == "__main__": app.run(host='0.0.0.0', port=7870)