Commit
Β·
4a4198e
1
Parent(s):
7ac8db1
root endpoint updated for websockets route
Browse files
app.py
CHANGED
|
@@ -973,36 +973,8 @@ async def log_requests(request: Request, call_next):
|
|
| 973 |
print(f"π€ {response.status_code} {request.url.path} [rid={rid}]")
|
| 974 |
return response
|
| 975 |
|
| 976 |
-
@app.get("/ping")
|
| 977 |
-
def ping():
|
| 978 |
-
return {"ok": True}
|
| 979 |
|
| 980 |
-
|
| 981 |
-
def read_root():
|
| 982 |
-
"""Root endpoint that explains what this API does"""
|
| 983 |
-
html_content = """
|
| 984 |
-
<!DOCTYPE html>
|
| 985 |
-
<html>
|
| 986 |
-
<head><title>MagentaRT Research API</title></head>
|
| 987 |
-
<body style="font-family: Arial; max-width: 800px; margin: 50px auto; padding: 20px;">
|
| 988 |
-
<h1>π΅ MagentaRT Research API</h1>
|
| 989 |
-
<p><strong>Purpose:</strong> AI music generation for iOS app research using Google's MagentaRT</p>
|
| 990 |
-
<h2>Available Endpoints:</h2>
|
| 991 |
-
<ul>
|
| 992 |
-
<li><code>POST /generate</code> - Generate 4-8 bars of music</li>
|
| 993 |
-
<li><code>POST /jam/start</code> - Start continuous jamming</li>
|
| 994 |
-
<li><code>GET /jam/next</code> - Get next chunk</li>
|
| 995 |
-
<li><code>POST /jam/consume</code> - confirm a chunk as consumed</li>
|
| 996 |
-
<li><code>POST /jam/stop</code> - End session</li>
|
| 997 |
-
<li><code>GET /docs</code> - API documentation</li>
|
| 998 |
-
</ul>
|
| 999 |
-
<p><strong>Research Only:</strong> Experimental implementation for iOS app development.</p>
|
| 1000 |
-
<p><strong>Licensing:</strong> Uses MagentaRT (Apache 2.0 + CC-BY 4.0). Users responsible for outputs.</p>
|
| 1001 |
-
<p>Visit <a href="/docs">/docs</a> for detailed API documentation.</p>
|
| 1002 |
-
</body>
|
| 1003 |
-
</html>
|
| 1004 |
-
"""
|
| 1005 |
-
return Response(content=html_content, media_type="text/html")
|
| 1006 |
|
| 1007 |
|
| 1008 |
# ----------------------------
|
|
@@ -1320,4 +1292,85 @@ async def ws_jam(websocket: WebSocket):
|
|
| 1320 |
if websocket.client_state != WebSocketState.DISCONNECTED:
|
| 1321 |
await websocket.close()
|
| 1322 |
except Exception:
|
| 1323 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 973 |
print(f"π€ {response.status_code} {request.url.path} [rid={rid}]")
|
| 974 |
return response
|
| 975 |
|
|
|
|
|
|
|
|
|
|
| 976 |
|
| 977 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 978 |
|
| 979 |
|
| 980 |
# ----------------------------
|
|
|
|
| 1292 |
if websocket.client_state != WebSocketState.DISCONNECTED:
|
| 1293 |
await websocket.close()
|
| 1294 |
except Exception:
|
| 1295 |
+
pass
|
| 1296 |
+
|
| 1297 |
+
|
| 1298 |
+
@app.get("/ping")
|
| 1299 |
+
def ping():
|
| 1300 |
+
return {"ok": True}
|
| 1301 |
+
|
| 1302 |
+
@app.get("/", response_class=Response)
|
| 1303 |
+
def read_root():
|
| 1304 |
+
"""Root endpoint that explains what this API does"""
|
| 1305 |
+
html_content = """
|
| 1306 |
+
<!DOCTYPE html>
|
| 1307 |
+
<html>
|
| 1308 |
+
<head>
|
| 1309 |
+
<meta charset="utf-8">
|
| 1310 |
+
<title>MagentaRT Research API</title>
|
| 1311 |
+
<style>
|
| 1312 |
+
body { font-family: Arial, sans-serif; max-width: 860px; margin: 48px auto; padding: 0 20px; color:#111; }
|
| 1313 |
+
code, pre { background:#f6f8fa; border:1px solid #eaecef; border-radius:6px; padding:2px 6px; }
|
| 1314 |
+
pre { padding:12px; overflow:auto; }
|
| 1315 |
+
.muted { color:#555; }
|
| 1316 |
+
ul { line-height: 1.8; }
|
| 1317 |
+
</style>
|
| 1318 |
+
</head>
|
| 1319 |
+
<body>
|
| 1320 |
+
<h1>π΅ MagentaRT Research API</h1>
|
| 1321 |
+
<p class="muted"><strong>Purpose:</strong> AI music generation for iOS/web app research using Google's MagentaRT.</p>
|
| 1322 |
+
|
| 1323 |
+
<h2>Available Endpoints</h2>
|
| 1324 |
+
<ul>
|
| 1325 |
+
<li><code>POST /generate</code> β Generate 4β8 bars of music (HTTP, bar-aligned)</li>
|
| 1326 |
+
<li><code>POST /jam/start</code> β Start continuous jamming (HTTP)</li>
|
| 1327 |
+
<li><code>GET /jam/next</code> β Get next chunk (HTTP)</li>
|
| 1328 |
+
<li><code>POST /jam/consume</code> β Confirm a chunk as consumed (HTTP)</li>
|
| 1329 |
+
<li><code>POST /jam/stop</code> β End session (HTTP)</li>
|
| 1330 |
+
<li><code>WEBSOCKET /ws/jam</code> β Realtime streaming (<code>mode="rt"</code>)</li>
|
| 1331 |
+
<li><code>GET /docs</code> β API documentation (Gradio)</li>
|
| 1332 |
+
</ul>
|
| 1333 |
+
|
| 1334 |
+
<h2>WebSocket Quick Start (rt mode)</h2>
|
| 1335 |
+
<p>Connect to <code>wss://<your-space>/ws/jam</code> and send:</p>
|
| 1336 |
+
<pre>{
|
| 1337 |
+
"type": "start",
|
| 1338 |
+
"mode": "rt",
|
| 1339 |
+
"binary_audio": false,
|
| 1340 |
+
"params": {
|
| 1341 |
+
"styles": "warmup",
|
| 1342 |
+
"temperature": 1.1,
|
| 1343 |
+
"topk": 40,
|
| 1344 |
+
"guidance_weight": 1.1,
|
| 1345 |
+
"pace": "realtime", // or "asap" to bootstrap quickly
|
| 1346 |
+
"max_decode_frames": 50 // default ~2.0s; try 36β45 on smaller GPUs
|
| 1347 |
+
}
|
| 1348 |
+
}</pre>
|
| 1349 |
+
<p>Update parameters live:</p>
|
| 1350 |
+
<pre>{
|
| 1351 |
+
"type": "update",
|
| 1352 |
+
"styles": "jazz, hiphop",
|
| 1353 |
+
"style_weights": "1.0,0.8",
|
| 1354 |
+
"temperature": 1.2,
|
| 1355 |
+
"topk": 64,
|
| 1356 |
+
"guidance_weight": 1.0,
|
| 1357 |
+
"pace": "realtime",
|
| 1358 |
+
"max_decode_frames": 40
|
| 1359 |
+
}</pre>
|
| 1360 |
+
<p>Stop:</p>
|
| 1361 |
+
<pre>{"type":"stop"}</pre>
|
| 1362 |
+
|
| 1363 |
+
<h2>Notes</h2>
|
| 1364 |
+
<ul>
|
| 1365 |
+
<li>Audio: 48 kHz stereo, ~2.0 s chunks by default with ~40 ms crossfade.</li>
|
| 1366 |
+
<li>L40S 48GB: faster than realtime β prefer <code>pace: "realtime"</code>.</li>
|
| 1367 |
+
<li>L4 24GB: slightly under realtime even with pre-roll and tuning.</li>
|
| 1368 |
+
<li>For sustained realtime, target ~40 GB VRAM per active stream (e.g., A100 40GB or β35β40 GB MIG slice).</li>
|
| 1369 |
+
</ul>
|
| 1370 |
+
|
| 1371 |
+
<p class="muted"><strong>Licensing:</strong> Uses MagentaRT (Apache 2.0 + CC-BY 4.0). Users are responsible for outputs.</p>
|
| 1372 |
+
<p>See <a href="/docs">/docs</a> for full API details and client examples.</p>
|
| 1373 |
+
</body>
|
| 1374 |
+
</html>
|
| 1375 |
+
"""
|
| 1376 |
+
return Response(content=html_content, media_type="text/html")
|