Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -250,40 +250,47 @@ def create_app():
|
|
| 250 |
|
| 251 |
@flask_app.route("/generate", methods=["POST"])
|
| 252 |
def generate():
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
@flask_app.route("/upload", methods=["POST", "GET"])
|
| 263 |
def upload():
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
return flask_app, socketio
|
| 289 |
|
|
|
|
| 250 |
|
| 251 |
@flask_app.route("/generate", methods=["POST"])
|
| 252 |
def generate():
|
| 253 |
+
try:
|
| 254 |
+
socketio.emit("log", {"message": "[STEP]: Entering query_gen..."})
|
| 255 |
+
data = request.json
|
| 256 |
+
prompt = data.get("prompt", "")
|
| 257 |
+
socketio.emit("log", {"message": f"[INFO]: Received prompt: {prompt}\n"})
|
| 258 |
+
# Run the agent in a separate thread
|
| 259 |
+
thread = threading.Thread(target=run_agent, args=(prompt, socketio))
|
| 260 |
+
thread.start()
|
| 261 |
+
return "OK", 200
|
| 262 |
+
except Exception as e:
|
| 263 |
+
socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
|
| 264 |
|
| 265 |
@flask_app.route("/upload", methods=["POST", "GET"])
|
| 266 |
def upload():
|
| 267 |
+
try:
|
| 268 |
+
if request.method == 'POST':
|
| 269 |
+
file = request.files.get('file')
|
| 270 |
+
if not file:
|
| 271 |
+
print("No file uploaded")
|
| 272 |
+
return "No file uploaded", 400
|
| 273 |
+
if file and file.filename.endswith('.db'):
|
| 274 |
+
# Use flask_app.config instead of app.config
|
| 275 |
+
db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], 'uploaded.db')
|
| 276 |
+
print("file uploaded")
|
| 277 |
+
file.save(db_path)
|
| 278 |
+
|
| 279 |
+
# Convert the file path to an absolute path and reinitialize the agent_app
|
| 280 |
+
abs_file_path = os.path.abspath(db_path)
|
| 281 |
+
global agent_app
|
| 282 |
+
agent_app = create_agent_app(abs_file_path)
|
| 283 |
+
|
| 284 |
+
# Use an f-string properly to log the uploaded file name
|
| 285 |
+
print(f"[INFO_PRINT]: Database file '{file.filename}' uploaded and loaded.")
|
| 286 |
+
socketio.emit("log", {"message": f"[INFO]: Database file '{file.filename}' uploaded and loaded."})
|
| 287 |
+
print("Received file:", file.filename)
|
| 288 |
+
return redirect(url_for("index")) # Go back to index page
|
| 289 |
+
# For GET requests, simply render the upload form.
|
| 290 |
+
return render_template("upload.html")
|
| 291 |
+
except Exception as e:
|
| 292 |
+
socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
|
| 293 |
+
return render_template("upload.html")
|
| 294 |
|
| 295 |
return flask_app, socketio
|
| 296 |
|