134 lines
5.5 KiB
Python
134 lines
5.5 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_cors import CORS
|
|
from main import (
|
|
chat_with_gpt, parse_user_input, get_total_by_cunit, get_filtered_data,
|
|
get_price_comparison, compare_current_vs_previous_year, get_top_consumers,
|
|
compare_kwh_current_vs_previous_year, get_invoices_by_month_year,
|
|
get_invoices_from_inactive_units, get_total_kwh_by_building_type, get_data
|
|
)
|
|
import re
|
|
from datetime import datetime
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
month_map = {
|
|
"janeiro": 1, "fevereiro": 2, "março": 3, "abril": 4, "maio": 5, "junho": 6,
|
|
"julho": 7, "agosto": 8, "setembro": 9, "outubro": 10, "novembro": 11, "dezembro": 12
|
|
}
|
|
|
|
@app.route('/api/chat', methods=['POST'])
|
|
def chat():
|
|
data = request.json
|
|
user_input = data.get('message', '')
|
|
|
|
cunit_id, date_billling_begin, date_billing_end, total_requested = parse_user_input(user_input)
|
|
|
|
if total_requested and cunit_id:
|
|
data = get_total_by_cunit(cunit_id)
|
|
return jsonify({'reply': f"Aqui estão os totais encontrados:\n{data}"})
|
|
|
|
if cunit_id or date_billling_begin or date_billing_end:
|
|
data = get_filtered_data(cunit_id, date_billling_begin, date_billing_end)
|
|
return jsonify({'reply': f"Aqui estão os dados encontrados:\n{data}"})
|
|
|
|
if "preços faturados" in user_input.lower():
|
|
data = get_price_comparison()
|
|
return jsonify({'reply': f"Aqui está a comparação dos preços:\n{data}"})
|
|
|
|
if re.search(r"mês atual.*igual período.*ano anterior", user_input.lower()):
|
|
data = compare_current_vs_previous_year()
|
|
return jsonify({'reply': data})
|
|
|
|
if re.search(r"mês.*igual período.*ano anterior", user_input.lower()):
|
|
match = re.search(
|
|
r"(?:mês\s+de\s+([a-zç]+|\d{1,2}))(?:\s+do\s+ano\s+(\d{4}))?",
|
|
user_input.lower()
|
|
)
|
|
if match:
|
|
mes_input = match.group(1).strip().lower()
|
|
ano = int(match.group(2)) if match.group(2) else datetime.now().year
|
|
if mes_input.isdigit():
|
|
mes = int(mes_input)
|
|
else:
|
|
mes = month_map.get(mes_input)
|
|
if not mes:
|
|
return jsonify({'reply': "Mês não reconhecido. Tenta novamente."})
|
|
else:
|
|
mes = datetime.now().month
|
|
ano = datetime.now().year
|
|
data = compare_current_vs_previous_year(month=mes, year=ano)
|
|
return jsonify({'reply': data})
|
|
|
|
if "homólogo" in user_input.lower():
|
|
match = re.search(r"homólogo.*?(\d{4})", user_input.lower())
|
|
ano = int(match.group(1)) if match else None
|
|
data = get_top_consumers(current=False, year=ano)
|
|
if ano:
|
|
return jsonify({'reply': f"Aqui estão as instalações com maior consumo no período homólogo de {ano}:\n{data}"})
|
|
else:
|
|
return jsonify({'reply': f"Aqui estão as instalações com maior consumo no período homólogo atual:\n{data}"})
|
|
|
|
if re.search(r"total de kwh.*mês.*ano anterior", user_input.lower()):
|
|
match = re.search(
|
|
r"(?:mês\s+de\s+([a-zç]+|\d{1,2}))(?:\s+do\s+ano\s+(\d{4}))?",
|
|
user_input.lower()
|
|
)
|
|
if match:
|
|
mes_input = match.group(1).strip().lower()
|
|
ano = int(match.group(2)) if match.group(2) else datetime.now().year
|
|
if mes_input.isdigit():
|
|
mes = int(mes_input)
|
|
else:
|
|
mes = month_map.get(mes_input)
|
|
if not mes:
|
|
return jsonify({'reply': "Mês não reconhecido. Tenta novamente."})
|
|
else:
|
|
mes = datetime.now().month
|
|
ano = datetime.now().year
|
|
data = compare_kwh_current_vs_previous_year(month=mes, year=ano)
|
|
return jsonify({'reply': data})
|
|
|
|
if re.search(r"quantas faturas.*mês", user_input.lower()):
|
|
match = re.search(
|
|
r"(?:mês\s+de\s+([a-zç]+|\d{1,2}))(?:\s+do\s+ano\s+(\d{4}))?",
|
|
user_input.lower()
|
|
)
|
|
if match:
|
|
mes_input = match.group(1).strip().lower()
|
|
ano = int(match.group(2)) if match.group(2) else datetime.now().year
|
|
if mes_input.isdigit():
|
|
mes = int(mes_input)
|
|
else:
|
|
mes = month_map.get(mes_input)
|
|
if not mes:
|
|
return jsonify({'reply': "Mês não reconhecido. Tenta novamente."})
|
|
else:
|
|
mes = datetime.now().month
|
|
ano = datetime.now().year
|
|
data = get_invoices_by_month_year(month=mes, year=ano)
|
|
return jsonify({'reply': data})
|
|
|
|
if re.search(r"faturas.*instalações.*inativas", user_input.lower()):
|
|
data = get_invoices_from_inactive_units()
|
|
return jsonify({'reply': data})
|
|
|
|
if re.search(r"total de kwh.*tipo de edifícios", user_input.lower()):
|
|
match = re.search(r"tipo de edifícios\s+([a-zçãõáéíóúâêîôûäëïöü\s]+)", user_input.lower())
|
|
building_type = match.group(1).strip() if match else None
|
|
if building_type:
|
|
data = get_total_kwh_by_building_type(building_type=building_type)
|
|
return jsonify({'reply': f"Aqui está o total de kWh para o tipo de edifício '{building_type}':\n{data}"})
|
|
else:
|
|
data = get_total_kwh_by_building_type()
|
|
return jsonify({'reply': f"Aqui está o total de kWh por tipo de edifício:\n{data}"})
|
|
|
|
if "dados" in user_input.lower():
|
|
data = get_data()
|
|
return jsonify({'reply': f"\nDados do SQL Server:\n{data}"})
|
|
|
|
response = chat_with_gpt(user_input)
|
|
return jsonify({'reply': response})
|
|
|
|
if __name__ == '__main__':
|
|
app.run(port=3000, debug=True) |