Update app.py
Browse files
app.py
CHANGED
@@ -214,10 +214,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
214 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
215 |
|
216 |
def handle_csv_upload(file):
|
217 |
-
global engine, db
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
csv_file.change(handle_csv_upload, inputs=csv_file, outputs=csv_file)
|
223 |
|
|
|
214 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
215 |
|
216 |
def handle_csv_upload(file):
|
217 |
+
global engine, db, sql_agent
|
218 |
+
|
219 |
+
try:
|
220 |
+
engine = load_uploaded_csv_and_create_db(file)
|
221 |
+
if engine is not None:
|
222 |
+
db = SQLDatabase(engine=engine)
|
223 |
+
sql_agent = create_sql_agent(
|
224 |
+
ChatOpenAI(model="gpt-4o-mini", temperature=0),
|
225 |
+
db=db,
|
226 |
+
agent_type="openai-tools",
|
227 |
+
verbose=True,
|
228 |
+
max_iterations=40,
|
229 |
+
return_intermediate_steps=True
|
230 |
+
)
|
231 |
+
print("[UPLOAD] Banco e agente SQL atualizados com sucesso.")
|
232 |
+
except Exception as e:
|
233 |
+
print(f"[ERRO] Falha ao processar novo CSV: {e}")
|
234 |
|
235 |
csv_file.change(handle_csv_upload, inputs=csv_file, outputs=csv_file)
|
236 |
|