Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -219,21 +219,24 @@ def create_agent_app(db_path: str):
|
|
| 219 |
# This function sets up the Flask application, SocketIO, routes, and initializes
|
| 220 |
# the global agent_app using the default DATABASE_URI. It returns the Flask app.
|
| 221 |
###############################################################################
|
|
|
|
| 222 |
def create_app():
|
| 223 |
-
|
|
|
|
| 224 |
socketio = SocketIO(flask_app, cors_allowed_origins="*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
-
#
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
|
| 231 |
-
flask_app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 232 |
-
agent_app = None
|
| 233 |
-
# Create a global agent_app using the default DATABASE_URI
|
| 234 |
-
###############################################################################
|
| 235 |
# Helper function to run the agent; uses the global agent_app.
|
| 236 |
-
###############################################################################
|
| 237 |
def run_agent(prompt, socketio):
|
| 238 |
global agent_app
|
| 239 |
if agent_app is None:
|
|
@@ -281,20 +284,20 @@ def create_app():
|
|
| 281 |
print("No file uploaded")
|
| 282 |
return "No file uploaded", 400
|
| 283 |
if file and file.filename.endswith('.db'):
|
| 284 |
-
#
|
| 285 |
db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], 'uploaded.db')
|
| 286 |
print("Saving file to:", db_path)
|
| 287 |
file.save(db_path)
|
| 288 |
|
| 289 |
-
#
|
| 290 |
abs_file_path = os.path.abspath(db_path)
|
| 291 |
global agent_app
|
| 292 |
agent_app = create_agent_app(abs_file_path)
|
| 293 |
|
| 294 |
print(f"[INFO_PRINT]: Database file '{file.filename}' uploaded and loaded.")
|
| 295 |
socketio.emit("log", {"message": f"[INFO]: Database file '{file.filename}' uploaded and loaded."})
|
| 296 |
-
return redirect(url_for("index"))
|
| 297 |
-
# For GET
|
| 298 |
return render_template("upload.html")
|
| 299 |
except Exception as e:
|
| 300 |
socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
|
|
@@ -302,7 +305,7 @@ def create_app():
|
|
| 302 |
|
| 303 |
return flask_app, socketio
|
| 304 |
|
| 305 |
-
# Create the app
|
| 306 |
app, socketio_instance = create_app()
|
| 307 |
|
| 308 |
if __name__ == "__main__":
|
|
|
|
| 219 |
# This function sets up the Flask application, SocketIO, routes, and initializes
|
| 220 |
# the global agent_app using the default DATABASE_URI. It returns the Flask app.
|
| 221 |
###############################################################################
|
| 222 |
+
# --- Application Factory ---
|
| 223 |
def create_app():
|
| 224 |
+
# Option: configure static files from uploads folder as well.
|
| 225 |
+
flask_app = Flask(__name__, static_url_path='/uploads', static_folder='uploads')
|
| 226 |
socketio = SocketIO(flask_app, cors_allowed_origins="*")
|
| 227 |
+
|
| 228 |
+
# Set up uploads folder
|
| 229 |
+
UPLOAD_FOLDER_LOCAL = os.path.join(os.getcwd(), "uploads")
|
| 230 |
+
if not os.path.exists(UPLOAD_FOLDER_LOCAL):
|
| 231 |
+
os.makedirs(UPLOAD_FOLDER_LOCAL)
|
| 232 |
+
flask_app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER_LOCAL
|
| 233 |
|
| 234 |
+
# Static route: option if you want a custom route to serve files:
|
| 235 |
+
@flask_app.route("/files/<path:filename>")
|
| 236 |
+
def uploaded_file(filename):
|
| 237 |
+
return send_from_directory(flask_app.config['UPLOAD_FOLDER'], filename)
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
# Helper function to run the agent; uses the global agent_app.
|
|
|
|
| 240 |
def run_agent(prompt, socketio):
|
| 241 |
global agent_app
|
| 242 |
if agent_app is None:
|
|
|
|
| 284 |
print("No file uploaded")
|
| 285 |
return "No file uploaded", 400
|
| 286 |
if file and file.filename.endswith('.db'):
|
| 287 |
+
# Save file using flask_app.config
|
| 288 |
db_path = os.path.join(flask_app.config['UPLOAD_FOLDER'], 'uploaded.db')
|
| 289 |
print("Saving file to:", db_path)
|
| 290 |
file.save(db_path)
|
| 291 |
|
| 292 |
+
# Reinitialize the agent_app with the new database file
|
| 293 |
abs_file_path = os.path.abspath(db_path)
|
| 294 |
global agent_app
|
| 295 |
agent_app = create_agent_app(abs_file_path)
|
| 296 |
|
| 297 |
print(f"[INFO_PRINT]: Database file '{file.filename}' uploaded and loaded.")
|
| 298 |
socketio.emit("log", {"message": f"[INFO]: Database file '{file.filename}' uploaded and loaded."})
|
| 299 |
+
return redirect(url_for("index"))
|
| 300 |
+
# For GET, render upload form:
|
| 301 |
return render_template("upload.html")
|
| 302 |
except Exception as e:
|
| 303 |
socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
|
|
|
|
| 305 |
|
| 306 |
return flask_app, socketio
|
| 307 |
|
| 308 |
+
# Create the app for Gunicorn compatibility.
|
| 309 |
app, socketio_instance = create_app()
|
| 310 |
|
| 311 |
if __name__ == "__main__":
|