Spaces:
Sleeping
Sleeping
Upload codedark/server/app.py with huggingface_hub
Browse files- codedark/server/app.py +92 -0
codedark/server/app.py
CHANGED
|
@@ -12,6 +12,7 @@ from typing import Optional
|
|
| 12 |
import uvicorn
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 15 |
|
| 16 |
from ..models import (
|
| 17 |
CodeDarkAction,
|
|
@@ -69,6 +70,97 @@ app.add_middleware(
|
|
| 69 |
)
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
@app.get("/health", response_model=HealthResponse)
|
| 73 |
async def health():
|
| 74 |
"""Health check endpoint."""
|
|
|
|
| 12 |
import uvicorn
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
+
from fastapi.responses import HTMLResponse
|
| 16 |
|
| 17 |
from ..models import (
|
| 18 |
CodeDarkAction,
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
|
| 73 |
+
@app.get("/", response_class=HTMLResponse)
|
| 74 |
+
async def root():
|
| 75 |
+
"""Landing page with API documentation."""
|
| 76 |
+
env = get_env()
|
| 77 |
+
return f"""
|
| 78 |
+
<!DOCTYPE html>
|
| 79 |
+
<html>
|
| 80 |
+
<head>
|
| 81 |
+
<title>CodeDark Environment</title>
|
| 82 |
+
<style>
|
| 83 |
+
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 84 |
+
max-width: 800px; margin: 50px auto; padding: 20px; background: #0d1117; color: #c9d1d9; }}
|
| 85 |
+
h1 {{ color: #f0883e; }}
|
| 86 |
+
h2 {{ color: #8b949e; border-bottom: 1px solid #30363d; padding-bottom: 8px; }}
|
| 87 |
+
code {{ background: #161b22; padding: 2px 6px; border-radius: 4px; color: #7ee787; }}
|
| 88 |
+
pre {{ background: #161b22; padding: 16px; border-radius: 8px; overflow-x: auto; }}
|
| 89 |
+
.endpoint {{ background: #21262d; padding: 12px; margin: 8px 0; border-radius: 6px; }}
|
| 90 |
+
.method {{ color: #7ee787; font-weight: bold; }}
|
| 91 |
+
.path {{ color: #79c0ff; }}
|
| 92 |
+
a {{ color: #58a6ff; }}
|
| 93 |
+
.stats {{ display: flex; gap: 20px; margin: 20px 0; }}
|
| 94 |
+
.stat {{ background: #21262d; padding: 16px; border-radius: 8px; text-align: center; }}
|
| 95 |
+
.stat-value {{ font-size: 24px; color: #f0883e; font-weight: bold; }}
|
| 96 |
+
.stat-label {{ color: #8b949e; font-size: 12px; }}
|
| 97 |
+
</style>
|
| 98 |
+
</head>
|
| 99 |
+
<body>
|
| 100 |
+
<h1>CodeDark Environment Server</h1>
|
| 101 |
+
<p>OpenEnv-compatible multi-turn data analytics environment for RL agent training.</p>
|
| 102 |
+
|
| 103 |
+
<div class="stats">
|
| 104 |
+
<div class="stat"><div class="stat-value">{len(env.tasks)}</div><div class="stat-label">Tasks</div></div>
|
| 105 |
+
<div class="stat"><div class="stat-value">750K</div><div class="stat-label">Bank Rows</div></div>
|
| 106 |
+
<div class="stat"><div class="stat-value">500K</div><div class="stat-label">Road Rows</div></div>
|
| 107 |
+
<div class="stat"><div class="stat-value">5</div><div class="stat-label">Tools</div></div>
|
| 108 |
+
</div>
|
| 109 |
+
|
| 110 |
+
<h2>API Endpoints</h2>
|
| 111 |
+
<div class="endpoint"><span class="method">GET</span> <span class="path">/health</span> - Health check</div>
|
| 112 |
+
<div class="endpoint"><span class="method">POST</span> <span class="path">/reset</span> - Reset for new episode</div>
|
| 113 |
+
<div class="endpoint"><span class="method">POST</span> <span class="path">/step</span> - Execute action</div>
|
| 114 |
+
<div class="endpoint"><span class="method">GET</span> <span class="path">/state</span> - Current state</div>
|
| 115 |
+
<div class="endpoint"><span class="method">GET</span> <span class="path">/metadata</span> - Environment info</div>
|
| 116 |
+
<div class="endpoint"><span class="method">GET</span> <span class="path">/schema</span> - Type schemas</div>
|
| 117 |
+
<div class="endpoint"><span class="method">GET</span> <span class="path">/docs</span> - Interactive API docs</div>
|
| 118 |
+
|
| 119 |
+
<h2>Quick Start</h2>
|
| 120 |
+
<pre>
|
| 121 |
+
import requests
|
| 122 |
+
|
| 123 |
+
BASE = "https://albert-einstein-09-codedark.hf.space"
|
| 124 |
+
|
| 125 |
+
# Reset for new task
|
| 126 |
+
obs = requests.post(f"{{BASE}}/reset").json()
|
| 127 |
+
print(f"Task: {{obs['question']}}")
|
| 128 |
+
|
| 129 |
+
# Run Python code
|
| 130 |
+
obs = requests.post(f"{{BASE}}/step", json={{
|
| 131 |
+
"tool": "run_python",
|
| 132 |
+
"args": "<code>result = df.shape</code>"
|
| 133 |
+
}}).json()
|
| 134 |
+
print(f"Result: {{obs['stdout']}}")
|
| 135 |
+
|
| 136 |
+
# Submit answer
|
| 137 |
+
obs = requests.post(f"{{BASE}}/step", json={{
|
| 138 |
+
"tool": "submit_answer",
|
| 139 |
+
"args": "<answer>42.5</answer>"
|
| 140 |
+
}}).json()
|
| 141 |
+
print(f"Reward: {{obs['reward']}}")
|
| 142 |
+
</pre>
|
| 143 |
+
|
| 144 |
+
<h2>Tools</h2>
|
| 145 |
+
<ul>
|
| 146 |
+
<li><code>run_python</code> - Execute Python/pandas code</li>
|
| 147 |
+
<li><code>read_notes</code> - Read saved notes</li>
|
| 148 |
+
<li><code>save_note</code> - Save note for later</li>
|
| 149 |
+
<li><code>clarify</code> - Ask clarifying question</li>
|
| 150 |
+
<li><code>submit_answer</code> - Submit final answer</li>
|
| 151 |
+
</ul>
|
| 152 |
+
|
| 153 |
+
<h2>Links</h2>
|
| 154 |
+
<p>
|
| 155 |
+
<a href="/docs">Interactive API Docs</a> |
|
| 156 |
+
<a href="https://github.com/vj-09/codeblue-env">GitHub</a> |
|
| 157 |
+
<a href="https://www.analytics-rl.com">Leaderboard</a>
|
| 158 |
+
</p>
|
| 159 |
+
</body>
|
| 160 |
+
</html>
|
| 161 |
+
"""
|
| 162 |
+
|
| 163 |
+
|
| 164 |
@app.get("/health", response_model=HealthResponse)
|
| 165 |
async def health():
|
| 166 |
"""Health check endpoint."""
|