Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,11 +43,10 @@ DATABASE_URI = f"sqlite:///{os.path.join(BASE_DIR, 'data', 'mydb.db')}"
|
|
43 |
print("DATABASE URI:", DATABASE_URI)
|
44 |
|
45 |
# API Keys from .env file
|
46 |
-
import os
|
47 |
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
48 |
os.environ["MISTRAL_API_KEY"] = os.getenv("MISTRAL_API_KEY")
|
49 |
|
50 |
-
# Global
|
51 |
agent_app = None
|
52 |
abs_file_path = None
|
53 |
db_path = None
|
@@ -56,7 +55,7 @@ db_path = None
|
|
56 |
# create_agent_app: Given a database path, initialize the agent workflow.
|
57 |
# =============================================================================
|
58 |
def create_agent_app(db_path: str):
|
59 |
-
# Use ChatGroq as our LLM here;
|
60 |
from langchain_groq import ChatGroq
|
61 |
llm = ChatGroq(model="llama3-70b-8192")
|
62 |
|
@@ -87,7 +86,7 @@ def create_agent_app(db_path: str):
|
|
87 |
messages: Annotated[list[AnyMessage], add_messages]
|
88 |
|
89 |
# -------------------------------------------------------------------------
|
90 |
-
# Set up prompt templates for query checking and
|
91 |
# -------------------------------------------------------------------------
|
92 |
query_check_system = (
|
93 |
"You are a SQL expert with a strong attention to detail.\n"
|
@@ -245,7 +244,6 @@ def create_app():
|
|
245 |
if not abs_file_path:
|
246 |
socketio.emit("log", {"message": "[ERROR]: No DB file uploaded."})
|
247 |
socketio.emit("final", {"message": "No database available. Please upload one and try again."})
|
248 |
-
flash("No database available. Please upload one and try again.", "error")
|
249 |
return
|
250 |
try:
|
251 |
if agent_app is None:
|
@@ -306,7 +304,7 @@ def create_app():
|
|
306 |
print("Saving file to:", db_path)
|
307 |
file.save(db_path)
|
308 |
abs_file_path = os.path.abspath(db_path)
|
309 |
-
agent_app = None # Reset the agent
|
310 |
print(f"[INFO]: File '{filename}' uploaded. Agent will be initialized on first query.")
|
311 |
socketio.emit("log", {"message": f"[INFO]: Database file '{filename}' uploaded."})
|
312 |
flash(f"Database file '{filename}' uploaded successfully.", "info")
|
|
|
43 |
print("DATABASE URI:", DATABASE_URI)
|
44 |
|
45 |
# API Keys from .env file
|
|
|
46 |
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
47 |
os.environ["MISTRAL_API_KEY"] = os.getenv("MISTRAL_API_KEY")
|
48 |
|
49 |
+
# Global state: dynamic agent and DB file path
|
50 |
agent_app = None
|
51 |
abs_file_path = None
|
52 |
db_path = None
|
|
|
55 |
# create_agent_app: Given a database path, initialize the agent workflow.
|
56 |
# =============================================================================
|
57 |
def create_agent_app(db_path: str):
|
58 |
+
# Use ChatGroq as our LLM here; swap to ChatMistralAI if preferred.
|
59 |
from langchain_groq import ChatGroq
|
60 |
llm = ChatGroq(model="llama3-70b-8192")
|
61 |
|
|
|
86 |
messages: Annotated[list[AnyMessage], add_messages]
|
87 |
|
88 |
# -------------------------------------------------------------------------
|
89 |
+
# Set up prompt templates for query checking and generation.
|
90 |
# -------------------------------------------------------------------------
|
91 |
query_check_system = (
|
92 |
"You are a SQL expert with a strong attention to detail.\n"
|
|
|
244 |
if not abs_file_path:
|
245 |
socketio.emit("log", {"message": "[ERROR]: No DB file uploaded."})
|
246 |
socketio.emit("final", {"message": "No database available. Please upload one and try again."})
|
|
|
247 |
return
|
248 |
try:
|
249 |
if agent_app is None:
|
|
|
304 |
print("Saving file to:", db_path)
|
305 |
file.save(db_path)
|
306 |
abs_file_path = os.path.abspath(db_path)
|
307 |
+
agent_app = None # Reset the agent to be lazily reinitialized on next query.
|
308 |
print(f"[INFO]: File '{filename}' uploaded. Agent will be initialized on first query.")
|
309 |
socketio.emit("log", {"message": f"[INFO]: Database file '{filename}' uploaded."})
|
310 |
flash(f"Database file '{filename}' uploaded successfully.", "info")
|