aaloksan commited on
Commit
5fe4705
·
1 Parent(s): e055579

feat: added dockerfile with missing fixes

Browse files
Files changed (5) hide show
  1. Dockerfile +16 -0
  2. README.md +11 -0
  3. app.py +5 -6
  4. env_server.py +2 -2
  5. scripts/validate-submission.sh +170 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ ENV PYTHONDONTWRITEBYTECODE=1 \
4
+ PYTHONUNBUFFERED=1 \
5
+ PIP_NO_CACHE_DIR=1
6
+
7
+ WORKDIR /app
8
+
9
+ COPY requirements.txt ./
10
+ RUN pip install --upgrade pip && pip install -r requirements.txt
11
+
12
+ COPY . .
13
+
14
+ EXPOSE 7860
15
+
16
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -20,6 +20,8 @@ pip install -r requirements.txt
20
  python app.py
21
  ```
22
 
 
 
23
  ## Hugging Face Space setup
24
 
25
  Set the OpenAI key in Space **Settings → Variables and secrets** as:
@@ -30,3 +32,12 @@ Optional:
30
 
31
  - `MODEL_NAME` (default: `gpt-4`)
32
  - `API_BASE_URL` (default: `https://api.openai.com/v1`)
 
 
 
 
 
 
 
 
 
 
20
  python app.py
21
  ```
22
 
23
+ OpenEnv API is served at `/reset`, `/step`, `/state`, and the Gradio UI is at `/ui`.
24
+
25
  ## Hugging Face Space setup
26
 
27
  Set the OpenAI key in Space **Settings → Variables and secrets** as:
 
32
 
33
  - `MODEL_NAME` (default: `gpt-4`)
34
  - `API_BASE_URL` (default: `https://api.openai.com/v1`)
35
+
36
+ ## Submission validation
37
+
38
+ Use the validator script with the **runtime URL**:
39
+
40
+ ```bash
41
+ chmod +x scripts/validate-submission.sh
42
+ ./scripts/validate-submission.sh https://aaloksan-kernel.hf.space .
43
+ ```
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import os
2
  from dotenv import load_dotenv
3
  from typing import Iterator, Tuple
4
- from env_server import KernelOptimization_env, TASKS
5
  from openai import OpenAI
6
  from models import Action
7
  import gradio as gr
8
  import traceback
 
9
 
10
  load_dotenv()
11
 
@@ -81,10 +82,8 @@ with gr.Blocks(title="CUDA Kernel Optimizer") as demo:
81
  task.change(task_baseline_code, inputs=[task], outputs=[kernel_input])
82
  run.click(ui, inputs=[task, kernel_input, steps, key], outputs=[logs, code])
83
 
 
 
84
 
85
  if __name__ == "__main__":
86
- demo.launch(
87
- show_error=True,
88
- server_name="0.0.0.0",
89
- server_port=int(os.getenv("PORT", "7860")),
90
- )
 
1
  import os
2
  from dotenv import load_dotenv
3
  from typing import Iterator, Tuple
4
+ from env_server import KernelOptimization_env, TASKS, app as openenv_app
5
  from openai import OpenAI
6
  from models import Action
7
  import gradio as gr
8
  import traceback
9
+ import uvicorn
10
 
11
  load_dotenv()
12
 
 
82
  task.change(task_baseline_code, inputs=[task], outputs=[kernel_input])
83
  run.click(ui, inputs=[task, kernel_input, steps, key], outputs=[logs, code])
84
 
85
+ app = gr.mount_gradio_app(openenv_app, demo, path="/ui")
86
+
87
 
88
  if __name__ == "__main__":
89
+ uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", "7860")))
 
 
 
 
env_server.py CHANGED
@@ -202,8 +202,8 @@ def health_check():
202
  return {"status": "healthy", "service": "kernel-optimization-openenv"}
203
 
204
  @app.post("/reset")
205
- def reset(request: ResetRequest = ResetRequest()):
206
- return env.reset(task_id=request.task_id)
207
 
208
 
209
  @app.post("/step")
 
202
  return {"status": "healthy", "service": "kernel-optimization-openenv"}
203
 
204
  @app.post("/reset")
205
+ def reset(request: ResetRequest | None = None):
206
+ return env.reset(task_id=request.task_id if request else None)
207
 
208
 
209
  @app.post("/step")
scripts/validate-submission.sh ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ #
3
+ # validate-submission.sh — OpenEnv Submission Validator
4
+ #
5
+ # Usage:
6
+ # ./scripts/validate-submission.sh <ping_url> [repo_dir]
7
+ #
8
+ # Example ping_url:
9
+ # https://your-space-name.hf.space
10
+
11
+ set -uo pipefail
12
+
13
+ DOCKER_BUILD_TIMEOUT=600
14
+ if [ -t 1 ]; then
15
+ RED='\033[0;31m'
16
+ GREEN='\033[0;32m'
17
+ YELLOW='\033[1;33m'
18
+ BOLD='\033[1m'
19
+ NC='\033[0m'
20
+ else
21
+ RED='' GREEN='' YELLOW='' BOLD='' NC=''
22
+ fi
23
+
24
+ run_with_timeout() {
25
+ local secs="$1"; shift
26
+ if command -v timeout &>/dev/null; then
27
+ timeout "$secs" "$@"
28
+ elif command -v gtimeout &>/dev/null; then
29
+ gtimeout "$secs" "$@"
30
+ else
31
+ "$@" &
32
+ local pid=$!
33
+ ( sleep "$secs" && kill "$pid" 2>/dev/null ) &
34
+ local watcher=$!
35
+ wait "$pid" 2>/dev/null
36
+ local rc=$?
37
+ kill "$watcher" 2>/dev/null
38
+ wait "$watcher" 2>/dev/null
39
+ return $rc
40
+ fi
41
+ }
42
+
43
+ portable_mktemp() {
44
+ local prefix="${1:-validate}"
45
+ mktemp "${TMPDIR:-/tmp}/${prefix}-XXXXXX" 2>/dev/null || mktemp
46
+ }
47
+
48
+ CLEANUP_FILES=()
49
+ cleanup() { rm -f "${CLEANUP_FILES[@]+"${CLEANUP_FILES[@]}"}"; }
50
+ trap cleanup EXIT
51
+
52
+ PING_URL="${1:-}"
53
+ REPO_DIR="${2:-.}"
54
+
55
+ if [ -z "$PING_URL" ]; then
56
+ printf "Usage: %s <ping_url> [repo_dir]\n" "$0"
57
+ exit 1
58
+ fi
59
+
60
+ if ! REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"; then
61
+ printf "Error: directory '%s' not found\n" "${2:-.}"
62
+ exit 1
63
+ fi
64
+
65
+ PING_URL="${PING_URL%/}"
66
+ PASS=0
67
+
68
+ log() { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }
69
+ pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }
70
+ fail() { log "${RED}FAILED${NC} -- $1"; }
71
+ hint() { printf " ${YELLOW}Hint:${NC} %b\n" "$1"; }
72
+ stop_at() {
73
+ printf "\n"
74
+ printf "${RED}${BOLD}Validation stopped at %s.${NC} Fix the above before continuing.\n" "$1"
75
+ exit 1
76
+ }
77
+
78
+ printf "\n"
79
+ printf "${BOLD}========================================${NC}\n"
80
+ printf "${BOLD} OpenEnv Submission Validator${NC}\n"
81
+ printf "${BOLD}========================================${NC}\n"
82
+ log "Repo: $REPO_DIR"
83
+ log "Ping URL: $PING_URL"
84
+ printf "\n"
85
+
86
+ log "${BOLD}Step 1/3: Pinging HF Space${NC} ($PING_URL/reset) ..."
87
+
88
+ CURL_OUTPUT=$(portable_mktemp "validate-curl")
89
+ CLEANUP_FILES+=("$CURL_OUTPUT")
90
+ HTTP_CODE=$(curl -s -o "$CURL_OUTPUT" -w "%{http_code}" -X POST \
91
+ -H "Content-Type: application/json" -d '{}' \
92
+ "$PING_URL/reset" --max-time 30 2>"$CURL_OUTPUT" || printf "000")
93
+
94
+ if [ "$HTTP_CODE" = "200" ]; then
95
+ pass "HF Space is live and responds to /reset"
96
+ elif [ "$HTTP_CODE" = "000" ]; then
97
+ fail "HF Space not reachable (connection failed or timed out)"
98
+ hint "Check your network connection and that the Space is running."
99
+ stop_at "Step 1"
100
+ else
101
+ fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"
102
+ hint "Use the runtime URL (https://<space>.hf.space), not huggingface.co/spaces/<owner>/<space>."
103
+ stop_at "Step 1"
104
+ fi
105
+
106
+ log "${BOLD}Step 2/3: Running docker build${NC} ..."
107
+
108
+ if ! command -v docker &>/dev/null; then
109
+ fail "docker command not found"
110
+ hint "Install Docker: https://docs.docker.com/get-docker/"
111
+ stop_at "Step 2"
112
+ fi
113
+
114
+ if [ -f "$REPO_DIR/Dockerfile" ]; then
115
+ DOCKER_CONTEXT="$REPO_DIR"
116
+ elif [ -f "$REPO_DIR/server/Dockerfile" ]; then
117
+ DOCKER_CONTEXT="$REPO_DIR/server"
118
+ else
119
+ fail "No Dockerfile found in repo root or server/ directory"
120
+ stop_at "Step 2"
121
+ fi
122
+
123
+ log " Found Dockerfile in $DOCKER_CONTEXT"
124
+
125
+ BUILD_OK=false
126
+ BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true
127
+
128
+ if [ "$BUILD_OK" = true ]; then
129
+ pass "Docker build succeeded"
130
+ else
131
+ fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"
132
+ printf "%s\n" "$BUILD_OUTPUT" | tail -20
133
+ stop_at "Step 2"
134
+ fi
135
+
136
+ log "${BOLD}Step 3/3: Running openenv validate${NC} ..."
137
+
138
+ OPENENV_CMD=""
139
+ if command -v openenv &>/dev/null; then
140
+ OPENENV_CMD="openenv"
141
+ elif [ -x "$REPO_DIR/venv/bin/openenv" ]; then
142
+ OPENENV_CMD="$REPO_DIR/venv/bin/openenv"
143
+ fi
144
+
145
+ if [ -z "$OPENENV_CMD" ]; then
146
+ fail "openenv command not found"
147
+ hint "Install it: pip install openenv-core"
148
+ stop_at "Step 3"
149
+ fi
150
+
151
+ VALIDATE_OK=false
152
+ VALIDATE_OUTPUT=$(cd "$REPO_DIR" && "$OPENENV_CMD" validate 2>&1) && VALIDATE_OK=true
153
+
154
+ if [ "$VALIDATE_OK" = true ]; then
155
+ pass "openenv validate passed"
156
+ [ -n "$VALIDATE_OUTPUT" ] && log " $VALIDATE_OUTPUT"
157
+ else
158
+ fail "openenv validate failed"
159
+ printf "%s\n" "$VALIDATE_OUTPUT"
160
+ stop_at "Step 3"
161
+ fi
162
+
163
+ printf "\n"
164
+ printf "${BOLD}========================================${NC}\n"
165
+ printf "${GREEN}${BOLD} All 3/3 checks passed!${NC}\n"
166
+ printf "${GREEN}${BOLD} Your submission is ready to submit.${NC}\n"
167
+ printf "${BOLD}========================================${NC}\n"
168
+ printf "\n"
169
+
170
+ exit 0