Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,9 @@ import logging
|
|
12 |
|
13 |
load_dotenv()
|
14 |
|
|
|
|
|
|
|
15 |
CSV_FILE_PATH = "anomalia_vendas.csv"
|
16 |
SQL_DB_PATH = "data.db"
|
17 |
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
@@ -56,6 +59,23 @@ def create_or_load_sql_database(csv_path, sql_db_path):
|
|
56 |
print("Banco de dados SQL criado com sucesso!")
|
57 |
return engine
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
engine = create_or_load_sql_database(CSV_FILE_PATH, SQL_DB_PATH)
|
60 |
db = SQLDatabase(engine=engine)
|
61 |
|
@@ -177,6 +197,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
177 |
label="Escolha o Modelo LLM para gerar a query SQL",
|
178 |
value="LLaMA 70B"
|
179 |
)
|
|
|
180 |
|
181 |
with gr.Column(scale=4):
|
182 |
chatbot = gr.Chatbot(height=600)
|
@@ -195,5 +216,21 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
195 |
history_output = gr.JSON()
|
196 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
if __name__ == "__main__":
|
199 |
demo.launch(share=False)
|
|
|
12 |
|
13 |
load_dotenv()
|
14 |
|
15 |
+
UPLOAD_DIR = "uploaded_data"
|
16 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
17 |
+
|
18 |
CSV_FILE_PATH = "anomalia_vendas.csv"
|
19 |
SQL_DB_PATH = "data.db"
|
20 |
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
|
|
59 |
print("Banco de dados SQL criado com sucesso!")
|
60 |
return engine
|
61 |
|
62 |
+
def load_uploaded_csv_and_create_db(uploaded_file):
|
63 |
+
if uploaded_file is None:
|
64 |
+
return None
|
65 |
+
|
66 |
+
saved_path = os.path.join(UPLOAD_DIR, "tabela.csv")
|
67 |
+
|
68 |
+
with open(saved_path, "wb") as f:
|
69 |
+
f.write(uploaded_file.read())
|
70 |
+
|
71 |
+
print(f"[UPLOAD] Novo CSV salvo em: {saved_path}")
|
72 |
+
|
73 |
+
engine = create_engine(f"sqlite:///{SQL_DB_PATH}")
|
74 |
+
df = pd.read_csv(saved_path, sep=";", on_bad_lines="skip")
|
75 |
+
df.to_sql("anomalia_vendas", engine, index=False, if_exists="replace")
|
76 |
+
print("Banco recriado com base no novo CSV.")
|
77 |
+
return engine
|
78 |
+
|
79 |
engine = create_or_load_sql_database(CSV_FILE_PATH, SQL_DB_PATH)
|
80 |
db = SQLDatabase(engine=engine)
|
81 |
|
|
|
197 |
label="Escolha o Modelo LLM para gerar a query SQL",
|
198 |
value="LLaMA 70B"
|
199 |
)
|
200 |
+
csv_file = gr.File(label="📂 Enviar novo CSV", file_types=[".csv"])
|
201 |
|
202 |
with gr.Column(scale=4):
|
203 |
chatbot = gr.Chatbot(height=600)
|
|
|
216 |
history_output = gr.JSON()
|
217 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
218 |
|
219 |
+
def handle_csv_upload(file):
|
220 |
+
global engine, db, sql_agent
|
221 |
+
engine = load_uploaded_csv_and_create_db(file)
|
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 |
+
return gr.update(value=None)
|
232 |
+
|
233 |
+
csv_file.change(handle_csv_upload, inputs=csv_file, outputs=csv_file)
|
234 |
+
|
235 |
if __name__ == "__main__":
|
236 |
demo.launch(share=False)
|