Update app.py
Browse files
app.py
CHANGED
|
@@ -74,10 +74,22 @@ def load_uploaded_csv_and_create_db(uploaded_file):
|
|
| 74 |
return engine
|
| 75 |
|
| 76 |
engine = create_or_load_sql_database(CSV_FILE_PATH, SQL_DB_PATH)
|
| 77 |
-
db = SQLDatabase(engine=engine)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def generate_initial_context(db_sample):
|
| 83 |
return (
|
|
@@ -214,23 +226,13 @@ 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
|
| 218 |
-
|
| 219 |
try:
|
| 220 |
engine = load_uploaded_csv_and_create_db(file)
|
| 221 |
if engine is not None:
|
| 222 |
-
|
| 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 |
|
|
|
|
| 74 |
return engine
|
| 75 |
|
| 76 |
engine = create_or_load_sql_database(CSV_FILE_PATH, SQL_DB_PATH)
|
|
|
|
| 77 |
|
| 78 |
+
def refresh_sql_agent():
|
| 79 |
+
global db, sql_agent
|
| 80 |
+
db = SQLDatabase(engine=engine)
|
| 81 |
+
sql_agent = create_sql_agent(
|
| 82 |
+
ChatOpenAI(model="gpt-4o-mini", temperature=0),
|
| 83 |
+
db=db,
|
| 84 |
+
agent_type="openai-tools",
|
| 85 |
+
verbose=True,
|
| 86 |
+
max_iterations=40,
|
| 87 |
+
return_intermediate_steps=True
|
| 88 |
+
)
|
| 89 |
+
print("[SQL_AGENT] Atualizado com novo banco de dados.")
|
| 90 |
+
|
| 91 |
+
refresh_sql_agent()
|
| 92 |
+
|
| 93 |
|
| 94 |
def generate_initial_context(db_sample):
|
| 95 |
return (
|
|
|
|
| 226 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
| 227 |
|
| 228 |
def handle_csv_upload(file):
|
| 229 |
+
global engine
|
|
|
|
| 230 |
try:
|
| 231 |
engine = load_uploaded_csv_and_create_db(file)
|
| 232 |
if engine is not None:
|
| 233 |
+
refresh_sql_agent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
except Exception as e:
|
| 235 |
+
print(f"[ERRO] Falha ao processar novo CSV: {e}
|
| 236 |
|
| 237 |
csv_file.change(handle_csv_upload, inputs=csv_file, outputs=csv_file)
|
| 238 |
|