-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
249 lines (205 loc) · 7.86 KB
/
app.py
File metadata and controls
249 lines (205 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
Web application for ModelSync visualization
"""
from fastapi import FastAPI, Request, Form, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pathlib import Path
from typing import Dict, List, Optional, Any
import json
from datetime import datetime
# Import ModelSync components
from modelsync.experiments.branching import ExperimentManager
from modelsync.storage.model_storage import ModelStorage
from modelsync.storage.dataset_storage import DatasetStorage
from modelsync.deployment.continuous_deploy import DeploymentManager
from modelsync.collaboration.audit import CollaborationManager
app = FastAPI(title="ModelSync Web Interface", version="1.0.0")
# Setup templates and static files
templates = Jinja2Templates(directory="modelsync/web/templates")
app.mount("/static", StaticFiles(directory="modelsync/web/static"), name="static")
# Initialize managers
experiment_manager = ExperimentManager()
model_storage = ModelStorage()
dataset_storage = DatasetStorage()
deployment_manager = DeploymentManager()
collaboration_manager = CollaborationManager()
@app.get("/", response_class=HTMLResponse)
async def dashboard(request: Request):
"""Main dashboard"""
# Get summary data
branches = experiment_manager.list_branches()
models = model_storage.list_models()
datasets = dataset_storage.list_datasets()
deployments = deployment_manager.get_deployments()
# Get recent activity
recent_activity = []
for branch in branches[:5]: # Last 5 branches
branch_obj = experiment_manager.get_branch(branch)
if branch_obj:
experiments = branch_obj.get_experiments()
for exp in experiments[:3]: # Last 3 experiments per branch
recent_activity.append({
"type": "experiment",
"branch": branch,
"name": exp["name"],
"timestamp": exp["created_at"],
"metrics": exp.get("metrics", {})
})
# Sort by timestamp
recent_activity.sort(key=lambda x: x["timestamp"], reverse=True)
context = {
"request": request,
"branches_count": len(branches),
"models_count": len(models),
"datasets_count": len(datasets),
"deployments_count": len(deployments),
"recent_activity": recent_activity[:10]
}
return templates.TemplateResponse("dashboard.html", context)
@app.get("/experiments", response_class=HTMLResponse)
async def experiments_page(request: Request):
"""Experiments page"""
branches = experiment_manager.list_branches()
# Get detailed branch information
branch_data = []
for branch_name in branches:
branch = experiment_manager.get_branch(branch_name)
if branch:
experiments = branch.get_experiments()
metrics_summary = branch.get_metrics_summary()
branch_data.append({
"name": branch_name,
"experiments_count": len(experiments),
"metrics_summary": metrics_summary,
"best_experiment": branch.get_best_experiment("accuracy") if experiments else None
})
context = {
"request": request,
"branches": branch_data
}
return templates.TemplateResponse("experiments.html", context)
@app.get("/experiments/{branch_name}", response_class=HTMLResponse)
async def branch_detail(request: Request, branch_name: str):
"""Branch detail page"""
branch = experiment_manager.get_branch(branch_name)
if not branch:
raise HTTPException(status_code=404, detail="Branch not found")
experiments = branch.get_experiments()
metrics_summary = branch.get_metrics_summary()
context = {
"request": request,
"branch": {
"name": branch_name,
"experiments": experiments,
"metrics_summary": metrics_summary
}
}
return templates.TemplateResponse("branch_detail.html", context)
@app.get("/models", response_class=HTMLResponse)
async def models_page(request: Request):
"""Models page"""
models = model_storage.list_models()
# Group models by framework
frameworks = {}
for model in models:
framework = model.get("framework", "unknown")
if framework not in frameworks:
frameworks[framework] = []
frameworks[framework].append(model)
context = {
"request": request,
"models": models,
"frameworks": frameworks
}
return templates.TemplateResponse("models.html", context)
@app.get("/datasets", response_class=HTMLResponse)
async def datasets_page(request: Request):
"""Datasets page"""
datasets = dataset_storage.list_datasets()
# Calculate total size
total_size = sum(dataset.get("size", 0) for dataset in datasets)
context = {
"request": request,
"datasets": datasets,
"total_size": total_size
}
return templates.TemplateResponse("datasets.html", context)
@app.get("/deployments", response_class=HTMLResponse)
async def deployments_page(request: Request):
"""Deployments page"""
rules = deployment_manager.list_deployment_rules()
deployments = deployment_manager.get_deployments()
# Group deployments by status
status_groups = {}
for deployment in deployments:
status = deployment.get("status", "unknown")
if status not in status_groups:
status_groups[status] = []
status_groups[status].append(deployment)
context = {
"request": request,
"rules": rules,
"deployments": deployments,
"status_groups": status_groups
}
return templates.TemplateResponse("deployments.html", context)
@app.get("/api/experiments")
async def api_experiments():
"""API endpoint for experiments data"""
branches = experiment_manager.list_branches()
data = []
for branch_name in branches:
branch = experiment_manager.get_branch(branch_name)
if branch:
experiments = branch.get_experiments()
data.append({
"name": branch_name,
"experiments": experiments,
"count": len(experiments)
})
return {"branches": data}
@app.get("/api/metrics/{branch_name}")
async def api_branch_metrics(branch_name: str):
"""API endpoint for branch metrics"""
branch = experiment_manager.get_branch(branch_name)
if not branch:
raise HTTPException(status_code=404, detail="Branch not found")
experiments = branch.get_experiments()
metrics_summary = branch.get_metrics_summary()
return {
"branch": branch_name,
"experiments": experiments,
"metrics_summary": metrics_summary
}
@app.get("/api/comparison")
async def api_comparison(branches: str = None, metric: str = "accuracy"):
"""API endpoint for branch comparison"""
if not branches:
return {"error": "No branches specified"}
branch_list = branches.split(",")
comparison = experiment_manager.compare_branches(branch_list, metric)
return comparison
@app.post("/api/deploy")
async def api_deploy(
branch: str = Form(...),
model_id: str = Form(...),
metrics: str = Form(...)
):
"""API endpoint to trigger deployment"""
try:
metrics_dict = json.loads(metrics)
triggered_rules = deployment_manager.check_deployment_rules(
branch, metrics_dict, model_id
)
return {
"status": "success",
"triggered_rules": len(triggered_rules),
"rules": triggered_rules
}
except Exception as e:
return {"status": "error", "message": str(e)}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)