Update server/app.py
Browse files- server/app.py +85 -5
server/app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import uvicorn
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
import sys
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Force the root directory into the python path
|
| 7 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
@@ -14,9 +16,87 @@ import env
|
|
| 14 |
app = FastAPI(title="SQLite Rescue Environment API")
|
| 15 |
env_instance = env.DatabaseRescueEnv()
|
| 16 |
|
| 17 |
-
@app.get("/")
|
| 18 |
-
def health_check():
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.post("/reset")
|
| 22 |
def reset_env(task_name: str = "easy_data_cleaning"):
|
|
@@ -27,4 +107,4 @@ def main():
|
|
| 27 |
uvicorn.run(app, host="0.0.0.0", port=7860) # Port 7860 is the HF default
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
| 30 |
-
main()
|
|
|
|
| 1 |
import uvicorn
|
|
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
+
import sqlite3
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.responses import HTMLResponse
|
| 7 |
|
| 8 |
# Force the root directory into the python path
|
| 9 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 16 |
app = FastAPI(title="SQLite Rescue Environment API")
|
| 17 |
env_instance = env.DatabaseRescueEnv()
|
| 18 |
|
| 19 |
+
# @app.get("/")
|
| 20 |
+
# def health_check():
|
| 21 |
+
# return {"status": "ok", "environment": "sqlite-rescue-env"}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@app.get("/", response_class=HTMLResponse)
|
| 25 |
+
def serve_dashboard():
|
| 26 |
+
"""Generates a live HTML view of the current database."""
|
| 27 |
+
|
| 28 |
+
# IMPORTANT: Change "workspace.db" if your environment uses a different filename!
|
| 29 |
+
db_path = "workspace.db"
|
| 30 |
+
|
| 31 |
+
html_content = """
|
| 32 |
+
<!DOCTYPE html>
|
| 33 |
+
<html>
|
| 34 |
+
<head>
|
| 35 |
+
<title>🗄️ SQLite Rescue - Live View</title>
|
| 36 |
+
<style>
|
| 37 |
+
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f4f9; color: #333; padding: 2rem; max-width: 1200px; margin: 0 auto; }
|
| 38 |
+
h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }
|
| 39 |
+
.table-container { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); margin-bottom: 2rem; }
|
| 40 |
+
h2 { color: #e74c3c; margin-top: 0; }
|
| 41 |
+
table { width: 100%; border-collapse: collapse; margin-top: 10px; }
|
| 42 |
+
th, td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }
|
| 43 |
+
th { background-color: #f8f9fa; font-weight: 600; color: #2c3e50; }
|
| 44 |
+
tr:hover { background-color: #f1f2f6; }
|
| 45 |
+
.error { color: red; font-weight: bold; }
|
| 46 |
+
</style>
|
| 47 |
+
</head>
|
| 48 |
+
<body>
|
| 49 |
+
<h1>🗄️ SQLite Rescue Environment - Live View</h1>
|
| 50 |
+
<p>This dashboard shows the real-time state of the agent's workspace database. Refresh the page to see changes after an agent takes an action.</p>
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
if not os.path.exists(db_path):
|
| 54 |
+
html_content += f"<p class='error'>Waiting for environment to initialize... ({db_path} not found).</p>"
|
| 55 |
+
else:
|
| 56 |
+
try:
|
| 57 |
+
with sqlite3.connect(db_path) as conn:
|
| 58 |
+
c = conn.cursor()
|
| 59 |
+
# Find all tables and views in the database
|
| 60 |
+
c.execute("SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%';")
|
| 61 |
+
db_objects = c.fetchall()
|
| 62 |
+
|
| 63 |
+
if not db_objects:
|
| 64 |
+
html_content += "<p>Database is empty.</p>"
|
| 65 |
+
|
| 66 |
+
for obj_name, obj_type in db_objects:
|
| 67 |
+
html_content += f"<div class='table-container'>"
|
| 68 |
+
html_content += f"<h2>{obj_name} ({obj_type})</h2>"
|
| 69 |
+
|
| 70 |
+
# Fetch all rows for the table
|
| 71 |
+
c.execute(f"SELECT * FROM {obj_name} LIMIT 50")
|
| 72 |
+
rows = c.fetchall()
|
| 73 |
+
|
| 74 |
+
if not rows:
|
| 75 |
+
html_content += "<p><em>No data</em></p></div>"
|
| 76 |
+
continue
|
| 77 |
+
|
| 78 |
+
# Fetch column names
|
| 79 |
+
cols = [description[0] for description in c.description]
|
| 80 |
+
|
| 81 |
+
# Build HTML Table
|
| 82 |
+
html_content += "<table><thead><tr>"
|
| 83 |
+
for col in cols:
|
| 84 |
+
html_content += f"<th>{col}</th>"
|
| 85 |
+
html_content += "</tr></thead><tbody>"
|
| 86 |
+
|
| 87 |
+
for row in rows:
|
| 88 |
+
html_content += "<tr>"
|
| 89 |
+
for val in row:
|
| 90 |
+
html_content += f"<td>{str(val)}</td>"
|
| 91 |
+
html_content += "</tr>"
|
| 92 |
+
|
| 93 |
+
html_content += "</tbody></table></div>"
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
html_content += f"<p class='error'>Error reading database: {str(e)}</p>"
|
| 97 |
+
|
| 98 |
+
html_content += "</body></html>"
|
| 99 |
+
return HTMLResponse(content=html_content, status_code=200)
|
| 100 |
|
| 101 |
@app.post("/reset")
|
| 102 |
def reset_env(task_name: str = "easy_data_cleaning"):
|
|
|
|
| 107 |
uvicorn.run(app, host="0.0.0.0", port=7860) # Port 7860 is the HF default
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
main()
|