WebashalarForML commited on
Commit
7864980
·
verified ·
1 Parent(s): e4cb1f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -47,6 +47,7 @@ os.environ["MISTRAL_API_KEY"] = MISTRAL_API_KEY
47
  # Global variables for dynamic agent and DB file path; initially None.
48
  agent_app = None
49
  abs_file_path = None
 
50
 
51
  # =============================================================================
52
  # create_agent_app: Given a database path, initialize the agent workflow.
@@ -129,11 +130,12 @@ def create_agent_app(db_path: str):
129
  DATABASE_URI = abs_db_path_local
130
  db_uri = f"sqlite:///{abs_db_path_local}"
131
  print("db_uri", db_uri)
132
-
133
  # Create SQLDatabase connection using langchain utility.
134
  from langchain_community.utilities import SQLDatabase
135
  db_instance = SQLDatabase.from_uri(db_uri)
136
  print("db_instance----->", db_instance)
 
137
 
138
  # Create SQL toolkit.
139
  from langchain_community.agent_toolkits import SQLDatabaseToolkit
@@ -230,19 +232,25 @@ def create_app():
230
  global agent_app
231
  if agent_app is None:
232
  socketio.emit("log", {"message": "[ERROR]: No database has been uploaded. Upload a database file first."})
 
233
  socketio.emit("final", {"message": "No database available. Upload one and try again."})
234
  return
235
  try:
 
 
 
236
  query = {"messages": [("user", prompt)]}
237
  result = agent_app.invoke(query)
238
  try:
239
  result = result["messages"][-1].tool_calls[0]["args"]["final_answer"]
240
  except Exception:
241
  result = "Query failed or no valid answer found."
 
242
  print("final_answer------>", result)
243
  socketio.emit("final", {"message": result})
244
  except Exception as e:
245
  print(f"[ERROR]: {str(e)}")
 
246
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
247
  socketio.emit("final", {"message": "Generation failed."})
248
 
@@ -269,6 +277,7 @@ def create_app():
269
  return "OK", 200
270
  except Exception as e:
271
  print(f"[ERROR]: {str(e)}")
 
272
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
273
  return "ERROR", 500
274
 
@@ -277,7 +286,7 @@ def create_app():
277
  # -------------------------------------------------------------------------
278
  @flask_app.route("/upload", methods=["GET", "POST"])
279
  def upload():
280
- global abs_file_path, agent_app
281
  try:
282
  if request.method == "POST":
283
  file = request.files.get("file")
@@ -290,14 +299,15 @@ def create_app():
290
  db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], "uploaded.db")
291
  print("Saving file to:", db_path)
292
  file.save(db_path)
293
- abs_file_path = os.path.abspath(db_path)
294
- agent_app = create_agent_app(abs_file_path)
295
  print(f"[INFO]: Database file '{filename}' uploaded and loaded.")
296
  socketio.emit("log", {"message": f"[INFO]: Database file '{filename}' uploaded and loaded."})
297
  return redirect(url_for("index"))
298
  return render_template("upload.html")
299
  except Exception as e:
300
  print(f"[ERROR]: {str(e)}")
 
301
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
302
  return render_template("upload.html")
303
 
 
47
  # Global variables for dynamic agent and DB file path; initially None.
48
  agent_app = None
49
  abs_file_path = None
50
+ db_path = None
51
 
52
  # =============================================================================
53
  # create_agent_app: Given a database path, initialize the agent workflow.
 
130
  DATABASE_URI = abs_db_path_local
131
  db_uri = f"sqlite:///{abs_db_path_local}"
132
  print("db_uri", db_uri)
133
+ flash(f"db_uri:{db_uri}", "warning")
134
  # Create SQLDatabase connection using langchain utility.
135
  from langchain_community.utilities import SQLDatabase
136
  db_instance = SQLDatabase.from_uri(db_uri)
137
  print("db_instance----->", db_instance)
138
+ flash(f"db_instance:{db_instance}", "warning")
139
 
140
  # Create SQL toolkit.
141
  from langchain_community.agent_toolkits import SQLDatabaseToolkit
 
232
  global agent_app
233
  if agent_app is None:
234
  socketio.emit("log", {"message": "[ERROR]: No database has been uploaded. Upload a database file first."})
235
+ flash(f"[ERROR]: No database has been uploaded. Upload a database file first.", "error")
236
  socketio.emit("final", {"message": "No database available. Upload one and try again."})
237
  return
238
  try:
239
+ abs_file_path = os.path.abspath(db_path)
240
+ agent_app = create_agent_app(abs_file_path)
241
+
242
  query = {"messages": [("user", prompt)]}
243
  result = agent_app.invoke(query)
244
  try:
245
  result = result["messages"][-1].tool_calls[0]["args"]["final_answer"]
246
  except Exception:
247
  result = "Query failed or no valid answer found."
248
+ flash(f"[ERROR]: Query failed or no valid answer found.", "error")
249
  print("final_answer------>", result)
250
  socketio.emit("final", {"message": result})
251
  except Exception as e:
252
  print(f"[ERROR]: {str(e)}")
253
+ flash(f"[ERROR]: {str(e)}", "error")
254
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
255
  socketio.emit("final", {"message": "Generation failed."})
256
 
 
277
  return "OK", 200
278
  except Exception as e:
279
  print(f"[ERROR]: {str(e)}")
280
+ flash(f"[ERROR]: {str(e)}", "error")
281
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
282
  return "ERROR", 500
283
 
 
286
  # -------------------------------------------------------------------------
287
  @flask_app.route("/upload", methods=["GET", "POST"])
288
  def upload():
289
+ global abs_file_path, agent_app, db_path
290
  try:
291
  if request.method == "POST":
292
  file = request.files.get("file")
 
299
  db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], "uploaded.db")
300
  print("Saving file to:", db_path)
301
  file.save(db_path)
302
+ #abs_file_path = os.path.abspath(db_path)
303
+ #agent_app = create_agent_app(abs_file_path)
304
  print(f"[INFO]: Database file '{filename}' uploaded and loaded.")
305
  socketio.emit("log", {"message": f"[INFO]: Database file '{filename}' uploaded and loaded."})
306
  return redirect(url_for("index"))
307
  return render_template("upload.html")
308
  except Exception as e:
309
  print(f"[ERROR]: {str(e)}")
310
+ flash(f"[ERROR]: {str(e)}", "error")
311
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
312
  return render_template("upload.html")
313