Update app.py
Browse files
app.py
CHANGED
|
@@ -84,7 +84,6 @@ def handle_csv_upload(file):
|
|
| 84 |
db = SQLDatabase(engine=engine)
|
| 85 |
logging.info("[UPLOAD] Novo banco carregado e DB atualizado.")
|
| 86 |
|
| 87 |
-
# Recria o agent com o novo DB
|
| 88 |
sql_agent = create_sql_agent(
|
| 89 |
ChatOpenAI(model="gpt-4o-mini", temperature=0),
|
| 90 |
db=db,
|
|
@@ -95,7 +94,12 @@ def handle_csv_upload(file):
|
|
| 95 |
)
|
| 96 |
|
| 97 |
logging.info("[UPLOAD] Novo banco carregado e agente recriado. Cache limpo.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
return "✅ CSV carregado com sucesso!"
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
logging.error(f"[ERRO] Falha ao processar novo CSV: {e}")
|
| 101 |
return f"❌ Erro ao processar CSV: {e}"
|
|
@@ -118,19 +122,21 @@ def reset_app():
|
|
| 118 |
query_cache.clear()
|
| 119 |
history_log.clear()
|
| 120 |
recent_history.clear()
|
|
|
|
| 121 |
return "🔄 Sistema resetado para o estado inicial."
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
return f"❌ Erro ao resetar: {e}"
|
| 124 |
|
| 125 |
def export_history_json():
|
| 126 |
path = "history_log.json"
|
| 127 |
pd.DataFrame(history_log).to_json(path, orient="records", indent=2)
|
| 128 |
-
return path
|
| 129 |
|
| 130 |
def export_history_csv():
|
| 131 |
path = "history_log.csv"
|
| 132 |
-
pd.DataFrame(history_log).to_csv(path, index=False)
|
| 133 |
-
return path
|
| 134 |
|
| 135 |
def generate_initial_context(db_sample):
|
| 136 |
return (
|
|
@@ -241,12 +247,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 241 |
with gr.Column(scale=1):
|
| 242 |
gr.Markdown("## ⚙️ Configurações")
|
| 243 |
model_selector = gr.Dropdown(list(LLAMA_MODELS.keys()), label="Modelo LLM", value="LLaMA 70B")
|
| 244 |
-
csv_file = gr.File(label="📂 Enviar novo CSV", file_types=[".csv"])
|
| 245 |
upload_feedback = gr.Markdown()
|
| 246 |
reset_btn = gr.Button("🔄 Resetar")
|
| 247 |
export_json = gr.Button("📤 Exportar JSON")
|
| 248 |
export_csv = gr.Button("📤 Exportar CSV")
|
| 249 |
-
download_output = gr.File()
|
| 250 |
|
| 251 |
with gr.Column(scale=4):
|
| 252 |
gr.Markdown("# 🧠 Anomalia Agent")
|
|
@@ -265,8 +271,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 265 |
btn.click(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
| 266 |
history_btn.click(toggle_history, outputs=history_output)
|
| 267 |
|
| 268 |
-
csv_file.change(handle_csv_upload, inputs=csv_file, outputs=upload_feedback)
|
| 269 |
-
reset_btn.click(reset_app, outputs=upload_feedback)
|
| 270 |
export_json.click(export_history_json, outputs=download_output)
|
| 271 |
export_csv.click(export_history_csv, outputs=download_output)
|
| 272 |
|
|
|
|
| 84 |
db = SQLDatabase(engine=engine)
|
| 85 |
logging.info("[UPLOAD] Novo banco carregado e DB atualizado.")
|
| 86 |
|
|
|
|
| 87 |
sql_agent = create_sql_agent(
|
| 88 |
ChatOpenAI(model="gpt-4o-mini", temperature=0),
|
| 89 |
db=db,
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
logging.info("[UPLOAD] Novo banco carregado e agente recriado. Cache limpo.")
|
| 97 |
+
query_cache.clear()
|
| 98 |
+
history_log.clear()
|
| 99 |
+
recent_history.clear()
|
| 100 |
+
|
| 101 |
return "✅ CSV carregado com sucesso!"
|
| 102 |
+
|
| 103 |
except Exception as e:
|
| 104 |
logging.error(f"[ERRO] Falha ao processar novo CSV: {e}")
|
| 105 |
return f"❌ Erro ao processar CSV: {e}"
|
|
|
|
| 122 |
query_cache.clear()
|
| 123 |
history_log.clear()
|
| 124 |
recent_history.clear()
|
| 125 |
+
|
| 126 |
return "🔄 Sistema resetado para o estado inicial."
|
| 127 |
+
|
| 128 |
except Exception as e:
|
| 129 |
return f"❌ Erro ao resetar: {e}"
|
| 130 |
|
| 131 |
def export_history_json():
|
| 132 |
path = "history_log.json"
|
| 133 |
pd.DataFrame(history_log).to_json(path, orient="records", indent=2)
|
| 134 |
+
return gr.File.update(value=path, visible=True)
|
| 135 |
|
| 136 |
def export_history_csv():
|
| 137 |
path = "history_log.csv"
|
| 138 |
+
pd.DataFrame(history_log).to_csv(path, index=False, sep=";")
|
| 139 |
+
return gr.File.update(value=path, visible=True)
|
| 140 |
|
| 141 |
def generate_initial_context(db_sample):
|
| 142 |
return (
|
|
|
|
| 247 |
with gr.Column(scale=1):
|
| 248 |
gr.Markdown("## ⚙️ Configurações")
|
| 249 |
model_selector = gr.Dropdown(list(LLAMA_MODELS.keys()), label="Modelo LLM", value="LLaMA 70B")
|
| 250 |
+
csv_file = gr.File(label="📂 Enviar novo CSV", file_types=[".csv"], file_types_strict=True)
|
| 251 |
upload_feedback = gr.Markdown()
|
| 252 |
reset_btn = gr.Button("🔄 Resetar")
|
| 253 |
export_json = gr.Button("📤 Exportar JSON")
|
| 254 |
export_csv = gr.Button("📤 Exportar CSV")
|
| 255 |
+
download_output = gr.File(visible=False)
|
| 256 |
|
| 257 |
with gr.Column(scale=4):
|
| 258 |
gr.Markdown("# 🧠 Anomalia Agent")
|
|
|
|
| 271 |
btn.click(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
| 272 |
history_btn.click(toggle_history, outputs=history_output)
|
| 273 |
|
| 274 |
+
csv_file.change(lambda f: (handle_csv_upload(f), gr.update(value=None), []), inputs=csv_file, outputs=[upload_feedback, csv_file, chatbot])
|
| 275 |
+
reset_btn.click(lambda: (reset_app(), gr.update(value=None), []), outputs=[upload_feedback, csv_file, chatbot])
|
| 276 |
export_json.click(export_history_json, outputs=download_output)
|
| 277 |
export_csv.click(export_history_csv, outputs=download_output)
|
| 278 |
|