Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -229,31 +229,34 @@ def create_app():
|
|
| 229 |
# Helper: run_agent runs the agent with the given prompt.
|
| 230 |
# -------------------------------------------------------------------------
|
| 231 |
def run_agent(prompt, socketio):
|
| 232 |
-
global agent_app
|
| 233 |
-
if
|
| 234 |
-
socketio.emit("log", {"message": "[ERROR]: No
|
| 235 |
-
|
| 236 |
-
socketio.emit("final", {"message": "No database available. Upload one and try again."})
|
| 237 |
return
|
|
|
|
| 238 |
try:
|
| 239 |
-
|
| 240 |
-
agent_app
|
| 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 |
-
|
| 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 |
|
|
|
|
| 257 |
# -------------------------------------------------------------------------
|
| 258 |
# Route: index page
|
| 259 |
# -------------------------------------------------------------------------
|
|
@@ -286,28 +289,23 @@ def create_app():
|
|
| 286 |
# -------------------------------------------------------------------------
|
| 287 |
@flask_app.route("/upload", methods=["GET", "POST"])
|
| 288 |
def upload():
|
| 289 |
-
global abs_file_path,
|
| 290 |
try:
|
| 291 |
if request.method == "POST":
|
| 292 |
file = request.files.get("file")
|
| 293 |
if not file:
|
| 294 |
-
print("No file uploaded")
|
| 295 |
return "No file uploaded", 400
|
| 296 |
-
# Secure the filename to avoid path traversal issues.
|
| 297 |
filename = secure_filename(file.filename)
|
| 298 |
if filename.endswith('.db'):
|
| 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 |
-
|
| 303 |
-
|
| 304 |
-
|
| 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 |
|
|
|
|
| 229 |
# Helper: run_agent runs the agent with the given prompt.
|
| 230 |
# -------------------------------------------------------------------------
|
| 231 |
def run_agent(prompt, socketio):
|
| 232 |
+
global agent_app, abs_file_path
|
| 233 |
+
if not abs_file_path:
|
| 234 |
+
socketio.emit("log", {"message": "[ERROR]: No DB file uploaded."})
|
| 235 |
+
socketio.emit("final", {"message": "No database available. Please upload one and try again."})
|
|
|
|
| 236 |
return
|
| 237 |
+
|
| 238 |
try:
|
| 239 |
+
# Lazy agent init
|
| 240 |
+
if agent_app is None:
|
| 241 |
+
print("[INFO]: Initializing agent for the first time...")
|
| 242 |
+
agent_app = create_agent_app(abs_file_path)
|
| 243 |
+
socketio.emit("log", {"message": "[INFO]: Agent initialized."})
|
| 244 |
+
|
| 245 |
query = {"messages": [("user", prompt)]}
|
| 246 |
result = agent_app.invoke(query)
|
| 247 |
try:
|
| 248 |
result = result["messages"][-1].tool_calls[0]["args"]["final_answer"]
|
| 249 |
except Exception:
|
| 250 |
result = "Query failed or no valid answer found."
|
| 251 |
+
|
| 252 |
print("final_answer------>", result)
|
| 253 |
socketio.emit("final", {"message": result})
|
| 254 |
except Exception as e:
|
| 255 |
print(f"[ERROR]: {str(e)}")
|
|
|
|
| 256 |
socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
|
| 257 |
socketio.emit("final", {"message": "Generation failed."})
|
| 258 |
|
| 259 |
+
|
| 260 |
# -------------------------------------------------------------------------
|
| 261 |
# Route: index page
|
| 262 |
# -------------------------------------------------------------------------
|
|
|
|
| 289 |
# -------------------------------------------------------------------------
|
| 290 |
@flask_app.route("/upload", methods=["GET", "POST"])
|
| 291 |
def upload():
|
| 292 |
+
global abs_file_path, db_path
|
| 293 |
try:
|
| 294 |
if request.method == "POST":
|
| 295 |
file = request.files.get("file")
|
| 296 |
if not file:
|
|
|
|
| 297 |
return "No file uploaded", 400
|
|
|
|
| 298 |
filename = secure_filename(file.filename)
|
| 299 |
if filename.endswith('.db'):
|
| 300 |
db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], "uploaded.db")
|
|
|
|
| 301 |
file.save(db_path)
|
| 302 |
+
abs_file_path = os.path.abspath(db_path) # Save it here, don't create agent
|
| 303 |
+
print(f"[INFO]: File '{filename}' uploaded. Agent will be initialized on first query.")
|
| 304 |
+
socketio.emit("log", {"message": f"[INFO]: Database file '{filename}' uploaded."})
|
|
|
|
| 305 |
return redirect(url_for("index"))
|
| 306 |
return render_template("upload.html")
|
| 307 |
except Exception as e:
|
| 308 |
print(f"[ERROR]: {str(e)}")
|
|
|
|
| 309 |
socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
|
| 310 |
return render_template("upload.html")
|
| 311 |
|