WebashalarForML commited on
Commit
d7bfcac
·
verified ·
1 Parent(s): 2a52435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -8
app.py CHANGED
@@ -131,29 +131,88 @@ def create_agent_app(db_path: str):
131
 
132
  return workflow.compile()
133
 
134
- @flask_app.route("/", methods=["GET"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  def index():
136
  return render_template("index.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  @flask_app.route("/upload", methods=["GET", "POST"])
139
  def upload():
140
- global abs_file_path, agent_app
141
  try:
142
  if request.method == "POST":
143
  file = request.files.get("file")
144
  if not file:
 
145
  return "No file uploaded", 400
146
-
147
  filename = secure_filename(file.filename)
148
  if filename.endswith('.db'):
149
- save_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], "uploaded.db")
150
- file.save(save_path)
151
- abs_file_path = os.path.abspath(save_path)
152
- agent_app = None # Reset agent; reinitialize on next query.
153
- socketio.emit("log", {"message": f"Database '{filename}' uploaded."})
 
154
  return redirect(url_for("index"))
155
  return render_template("upload.html")
156
  except Exception as e:
 
157
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
158
  return render_template("upload.html")
159
 
@@ -165,6 +224,7 @@ def handle_user_input(data):
165
  return
166
  run_agent(prompt)
167
 
 
168
  def run_agent(prompt):
169
  global agent_app, abs_file_path
170
  if not abs_file_path:
@@ -185,6 +245,7 @@ def run_agent(prompt):
185
  except Exception as e:
186
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
187
  socketio.emit("final", {"message": "Generation failed."})
 
188
 
189
  # Expose the Flask app as "app" for Gunicorn
190
  app = flask_app
 
131
 
132
  return workflow.compile()
133
 
134
+ @flask_app.route("/files/<path:filename>")
135
+ def uploaded_file(filename):
136
+ return send_from_directory(flask_app.config['UPLOAD_FOLDER'], filename)
137
+ # -------------------------------------------------------------------------
138
+ # Helper: run_agent runs the agent with the given prompt.
139
+ # -------------------------------------------------------------------------
140
+
141
+ def run_agent(prompt, socketio):
142
+ global agent_app, abs_file_path, db_path
143
+ if not abs_file_path:
144
+ socketio.emit("log", {"message": "[ERROR]: No DB file uploaded."})
145
+ socketio.emit("final", {"message": "No database available. Please upload one and try again."})
146
+ return
147
+ try:
148
+ # Lazy agent initialization: use the previously uploaded DB.
149
+ if agent_app is None:
150
+ print("[INFO]: Initializing agent for the first time...")
151
+ agent_app = create_agent_app(abs_file_path)
152
+ socketio.emit("log", {"message": "[INFO]: Agent initialized."})
153
+ query = {"messages": [("user", prompt)]}
154
+ result = agent_app.invoke(query)
155
+ try:
156
+ result = result["messages"][-1].tool_calls[0]["args"]["final_answer"]
157
+ except Exception:
158
+ result = "Query failed or no valid answer found."
159
+ print("final_answer------>", result)
160
+ socketio.emit("final", {"message": result})
161
+ except Exception as e:
162
+ print(f"[ERROR]: {str(e)}")
163
+ socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
164
+ socketio.emit("final", {"message": "Generation failed."})
165
+ # -------------------------------------------------------------------------
166
+ # Route: index page.
167
+ # -------------------------------------------------------------------------
168
+
169
+ @flask_app.route("/")
170
  def index():
171
  return render_template("index.html")
172
+ # -------------------------------------------------------------------------
173
+ # Route: generate (POST) – receives a prompt and runs the agent.
174
+ # -------------------------------------------------------------------------
175
+
176
+ @flask_app.route("/generate", methods=["POST"])
177
+ def generate():
178
+ try:
179
+ socketio.emit("log", {"message": "[STEP]: Entering query_gen..."})
180
+ data = request.json
181
+ prompt = data.get("prompt", "")
182
+ socketio.emit("log", {"message": f"[INFO]: Received prompt: {prompt}"})
183
+ thread = threading.Thread(target=run_agent, args=(prompt, socketio))
184
+ socketio.emit("log", {"message": f"[INFO]: Starting thread: {thread}"})
185
+ thread.start()
186
+ return "OK", 200
187
+ except Exception as e:
188
+ print(f"[ERROR]: {str(e)}")
189
+ socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
190
+ return "ERROR", 500
191
+ # -------------------------------------------------------------------------
192
+ # Route: upload (GET/POST) – handles uploading the SQLite DB file.
193
+ # -------------------------------------------------------------------------
194
 
195
  @flask_app.route("/upload", methods=["GET", "POST"])
196
  def upload():
197
+ global abs_file_path, agent_app, db_path
198
  try:
199
  if request.method == "POST":
200
  file = request.files.get("file")
201
  if not file:
202
+ print("No file uploaded")
203
  return "No file uploaded", 400
 
204
  filename = secure_filename(file.filename)
205
  if filename.endswith('.db'):
206
+ db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], "uploaded.db")
207
+ print("Saving file to:", db_path)
208
+ file.save(db_path)
209
+ abs_file_path = os.path.abspath(db_path) # Save it here; agent init will occur on first query.
210
+ print(f"[INFO]: File '{filename}' uploaded. Agent will be initialized on first query.")
211
+ socketio.emit("log", {"message": f"[INFO]: Database file '{filename}' uploaded."})
212
  return redirect(url_for("index"))
213
  return render_template("upload.html")
214
  except Exception as e:
215
+ print(f"[ERROR]: {str(e)}")
216
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
217
  return render_template("upload.html")
218
 
 
224
  return
225
  run_agent(prompt)
226
 
227
+ '''
228
  def run_agent(prompt):
229
  global agent_app, abs_file_path
230
  if not abs_file_path:
 
245
  except Exception as e:
246
  socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
247
  socketio.emit("final", {"message": "Generation failed."})
248
+ '''
249
 
250
  # Expose the Flask app as "app" for Gunicorn
251
  app = flask_app