agritech / app.py
gpcerv's picture
Update app.py
4e143ae verified
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
from flask import send_file
import json
import numpy as np
import pandas as pd
import datetime
from functools import wraps
import os
import pickle
import requests, zipfile, io
from flask_cors import CORS, cross_origin
import time
from openai import OpenAI
assistant_id = os.environ.get('assistant_id')
client = OpenAI(api_key = os.environ.get('api_key'))
my_assistant = client.beta.assistants.retrieve(assistant_id)
#enea
from scipy.signal import savgol_filter
app = Flask(__name__)
cors = CORS(app)
#CORS(app, resources={r"/api/*": {"origins": "https://agritech.unisi.it/"}})
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route("/")
def flask_app():
return '<br>' \
'<h3><a href="http://www.studiogotti.altervista.org/dashboard/wp2-agritech/index.php" target="_blank">Hai acceso il server! Bene!&#128521 Adesso vai alla pagina ESG4AGRI!!</a></h3>' \
@app.route('/scoring',methods=['POST','GET'])
@cross_origin()
def scoring():
input_json = request.get_json()
#dati di input:
percentili = pd.DataFrame({
'variabile': ['ini_fatt', 'form_fatt', 'perc_donne_dip', 'perc_dip', 'en_fatt', 'ghg_fatt', 'acqu_fatt', 'perc_donne_gov', '%_spesa_forn_loc'],
'percentile_0.05': [0,0.3846,0,0,0,0.000025,0.000030,0,10],
'percentile_0.95': [5,25,80,100,0.5,0.1875,0.01,100,100]
})
mediane = pd.DataFrame({
'variabile': ['perc_donne_gov', '%_spesa_forn_loc', 'acqu_fatt', 'en_fatt', 'form_fatt', 'ghg_fatt', 'ini_fatt', 'perc_dip', 'perc_donne_dip'],
'Centro': [30,70,0.001,0.0475,3.75,0.0003,0,59.72972973,33.33333333],
'Nord-est': [30,70,0.000708647,0.04,4.5,0.001073757,0,56.5942029,33.33333333],
'Nord-ovest':[30,70,0.000386698,0.033333333,4.54,0.125,0,60,31.81818182],
'Sud_Isole': [20,60,0.000833333,0.06,4,0.06,0,60,33.33333333]
})
#mapping delle variabili
variable_mapping = {
'fatturato': 'fatturato',
'iniziativeConsumo': 'n_iniz_cons_sosteni',
'oreFormazione': 'tot_ore_formazione',
'percentualeDipendenti': 'perc_donne_dip',
'spesaFornitori': '%_spesa_forn_loc',
'percentualeDonne': 'perc_dip',
'energiaElettrica': 'consumo_energia_kw',
'emissioniGHG': 'tons_ghg',
'consumoAcqua': 'consumo_acqua_m3',
'donneGovernance': 'perc_donne_gov',
'region': 'region',
'numeroDipendenti': 'n_tot_dip',
"dataInizio": "dataInizio",
"dataFine": "dataFine",
"testo":"testo"
}
transformed_json = {variable_mapping[key]: value for key, value in input_json.items()}
# Rimozione delle chiavi non desiderate
transformed_json.pop('region', None)
transformed_json.pop('dataInizio', None)
transformed_json.pop('dataFine', None)
transformed_json.pop('testo', None)
#costruzione dataframe
df = pd.DataFrame([{k: float(v) for k, v in transformed_json.items()}])
print(df)
#costruzione indicatori
df['ini_fatt'] = df['n_iniz_cons_sosteni']
df['form_fatt'] = df['tot_ore_formazione'] / df['n_tot_dip']
df['en_fatt'] = df['consumo_energia_kw'] / df['fatturato']
df['ghg_fatt'] = df['tons_ghg'] / df['fatturato']
df['acqu_fatt'] = df['consumo_acqua_m3'] / df['fatturato']
#controllo dati
for index, row in percentili.iterrows():
var = row['variabile']
if var in df.columns:
df[var] = np.clip(df[var], row['percentile_0.05'], row['percentile_0.95'])
#inizio costruzione
percentile_value = percentili.loc[percentili['variabile'] == 'ini_fatt', 'percentile_0.95'].iloc[0]
df['ini_fatt2'] = (df['ini_fatt'] / percentile_value) * 100
#variabili_effetto_positivo=['form_fatt','perc_donne_dip','perc_dip','perc_donne_gov','%_spesa_forn_loc']
variabili_effetto_positivo=['form_fatt','perc_dip','%_spesa_forn_loc']
variabili_effetto_positivo_donne=['perc_donne_dip','perc_donne_gov']
variabili_effetto_negativo=['en_fatt','ghg_fatt','acqu_fatt']
reg=input_json['region']
regione=reg
for d in variabili_effetto_positivo:
#print(d)
percentile_05=percentili.loc[percentili['variabile'] == d, 'percentile_0.05'].iloc[0]
percentile_95=percentili.loc[percentili['variabile'] == d, 'percentile_0.95'].iloc[0]
mediana=mediane.loc[percentili['variabile'] == d, regione].iloc[0]
df[d+'2'] = np.where(
df[d] <= mediana,
((df[d] - percentile_05) / (mediana - percentile_05)) * 50,
50+((df[d] - mediana) / (percentile_95 - mediana)) * 50
)
for d in variabili_effetto_positivo_donne:
percentile_05=percentili.loc[percentili['variabile'] == d, 'percentile_0.05'].iloc[0]
percentile_95=percentili.loc[percentili['variabile'] == d, 'percentile_0.95'].iloc[0]
# Apply the logic to compute the new column d+'2'
df[d+'2'] = np.where(
df[d] <= 50,
((df[d] - percentile_05) / (50 - percentile_05)) * 100,
((percentile_95 - df[d]) / (percentile_95 - 50)) * 100
)
for d in variabili_effetto_negativo:
#print(d)
percentile_05=percentili.loc[percentili['variabile'] == d, 'percentile_0.05'].iloc[0]
percentile_95=percentili.loc[percentili['variabile'] == d, 'percentile_0.95'].iloc[0]
mediana=mediane.loc[percentili['variabile'] == d, regione].iloc[0]
df[d+'2'] = np.where(
df[d] > mediana,
((percentile_95 - df[d]) / (percentile_95 -mediana)) * 50,
((mediana - df[d]) / (mediana - percentile_05)) * 50 + 50
)
# Calcolo dello score ponderato
weights = {'ini_fatt2': 3.778, 'form_fatt2': 4.488, 'perc_donne_dip2': 4.394, 'perc_dip2': 4.394, 'en_fatt2': 4.534, 'ghg_fatt2': 4.534, 'acqu_fatt2': 4.563, 'perc_donne_gov2':4.709}
total_weight = sum(weights.values())
var=['ini_fatt2','form_fatt2', 'perc_donne_dip2', 'perc_dip2', 'en_fatt2', 'ghg_fatt2', 'acqu_fatt2', 'perc_donne_gov2']
#score1
score=0
for v in var:
score=score+df[v].iloc[0]*weights[v]
df['score']=score/total_weight
output1=df['score'].iloc[0]
#score2
var211=['form_fatt2', 'perc_donne_dip2', 'perc_dip2', 'en_fatt2', 'ghg_fatt2', 'acqu_fatt2']
var2=['ini_fatt2','%_spesa_forn_loc2', 'perc_donne_gov2','score211']
#score1
score211=0
total_weight211=0
for v in var211:
score211=score211+df[v].iloc[0]*weights[v]
total_weight211=total_weight211+weights[v]
df['score211']=score211/total_weight211
weights2 = {'ini_fatt2': 4.823, 'perc_donne_gov2':4.709,'score211':4.583, '%_spesa_forn_loc2':4.665}
score2=0
total_weight2=0
for v in var2:
score2=score2+df[v].iloc[0]*weights2[v]
total_weight2=total_weight2+weights2[v]
df['score2']=score2/total_weight2
output2=df['score2'].iloc[0]
print(output1,output2) # Output the data received for debugging
dati=[]
w='ok'
d={}
d['score1']=str(output1)
d['score2']=str(output2)
d['score_ambito_1']=df['ini_fatt2'].iloc[0]
d['score_ambito_2']=df['score211'].iloc[0]
d['score_ambito_3']=df['%_spesa_forn_loc2'].iloc[0]
d['score_ambito_4']=df['perc_donne_gov2'].iloc[0]
varExp=['form_fatt2', 'perc_donne_dip2', 'perc_dip2', 'en_fatt2', 'ghg_fatt2', 'acqu_fatt2','ini_fatt2','%_spesa_forn_loc2', 'perc_donne_gov2']
for v in varExp:
d[v]=df[v].iloc[0]
dati.append(d)
output=dati
response = jsonify(output)
return response
@app.route('/bot',methods=['POST','GET'])
@cross_origin()
def bot():
dati=[]
input_json = request.get_json()
domanda=input_json['domanda']
thread_id=input_json['thread']
if thread_id=='nn':
thread = client.beta.threads.create()
thread_id=thread.id
messaggio = client.beta.threads.messages.create(
thread_id = thread_id,
role = "user",
content = domanda
)
run = client.beta.threads.runs.create(thread_id = thread_id, assistant_id = my_assistant.id)
print(f"[] Run Created : {run.id}")
while run.status != 'completed':
run = client.beta.threads.runs.retrieve(
thread_id=thread_id,
run_id=run.id
)
print(run.status)
time.sleep(5)
# ottengo tutti i messaggi nel thread
message_responses = client.beta.threads.messages.list(thread_id = thread_id)
latest_messages = message_responses.data[0]
text = latest_messages.content[0].text.value
print(text)
d={}
d['thread']=thread_id
d['risposta']=text
dati.append(d)
output=dati
response = jsonify(output)
return response
@app.route('/upload', methods=['POST'])
@cross_origin()
def upload_audio():
w='ok'
dati=[]
excel_file = request.files['file']
print('nome',excel_file.filename)
df = pd.read_excel(excel_file, sheet_name="Test set", usecols=range(2,1559))
Xts = df.to_numpy()
with open('plsdamod.pkl', 'rb') as f:
plscontents = pickle.load(f)
bval = plscontents['b']
mxval = plscontents['mx']
myval = plscontents['my']
ymval = plscontents['ym']
ypthrval = plscontents['ypthr']
Xd1ts = savgol_filter(Xts, 11, 2, 1)
Xd1ts_shape = np.shape(Xd1ts)
mxval_rep = np.resize(mxval, Xd1ts_shape)
Xd1ts_mc = Xd1ts - mxval_rep
Yts_predc=np.dot(Xd1ts_mc,bval)
Yts_pred=Yts_predc+myval
out=[]
clts_pred = np.zeros_like(Yts_pred)
for j in range(Yts_pred.shape[0]):
if Yts_pred[j] > ypthrval:
clts_pred[j, 0] = 1
elif Yts_pred[j] < ypthrval:
clts_pred[j, 0] = 0
elif Yts_pred[j] == ypthrval:
clts_pred[j, 0] = np.nan
out.append(clts_pred[j, 0])
# Sposto la colonna 'classe' all'inizio del dataframe
df['classe']=out
cols = df.columns.tolist()
cols = [cols[-1]] + cols[:-1]
df = df[cols]
#np.savetxt("result.csv",clts_pred.astype(int), fmt='%i', delimiter=',')
df.to_csv('result.csv', index=False)
w='messaggio andato!'
print(w)
d={}
d['text']=str(w)
d['risposta']=out
dati.append(d)
risposta = jsonify(dati)
return risposta
@app.route('/files/<filename>')
@cross_origin()
def serve_file(filename):
directory_path = './'
return send_from_directory(directory_path, filename)
@app.route('/get_file/<filename>', methods=['GET'])
@cross_origin()
def get_file(filename):
file_path = os.path.join('./', filename)
if os.path.exists(file_path) and filename.endswith('.txt'):
return send_file(file_path, as_attachment=True)
else:
abort(404)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=7860)