aubaypoc / app.py
gpcerv's picture
Update app.py
beeafe2
raw
history blame
2.83 kB
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 hugchat.login import Login
from numpy.linalg import norm
import datetime
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text
from functools import wraps
import os
import pickle
model_url = "./hub_model/"
hub_layer = hub.load(model_url)
app = Flask(__name__)
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 '<br>' \
'<h3><a href="http://18.102.72.47:5000" target="_blank">Hai acceso il server! Bene!&#128521 Adesso vai alla pagina La7 DEMO!!</a></h3>' \
@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('/requestsSearch',methods=['POST','GET'])
@support_jsonp
def tasksSearch():
domanda=request.args.get('frase').strip()
s=request.args.get('S').strip()
path=s
#dati=moduli.la7_Search(s,domanda,hub_layer)
print('domanda',domanda)
dati=[]
with open ('static/dati/'+str(path)+'/db_relatori_finale_emb', 'rb') as fp:
word = pickle.load(fp)
word_orig=np.array(word, dtype="object")
message_embeddings = hub_layer(domanda)[0].numpy()
ris=[]
for n,w in enumerate(word):
contesto_embeddings=np.array(w[6])
cosine = np.dot(message_embeddings,contesto_embeddings)/(norm(message_embeddings)*norm(contesto_embeddings))
ris.append(cosine)
ris_sort=np.argsort(ris)[::-1]
l=0
for n in ris_sort:
if l==0:
testo=str(word[n][5])
testo_chi=str(word[n][2])+':'+str(word[n][1])
else:
testo=testo+' '+word[n][5]
testo_chi=testo_chi+' --- '+str(word[n][2])+' '+str(word[n][1])
l=len(testo)
if l>800:
break
d={}
d['text']=str(testo)
d['chi']=str(testo_chi)
dati.append(d)
output=dati
response = jsonify(output)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port=7860)