modelbuilderhq commited on
Commit
b837a03
·
verified ·
1 Parent(s): f2beac3

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. server/app.py +88 -50
  2. validate-submission.sh +164 -185
server/app.py CHANGED
@@ -1,50 +1,88 @@
1
- from fastapi import FastAPI
2
-
3
- from env import Action, PharmaVigilanceEnv
4
-
5
-
6
- app = FastAPI()
7
- env = PharmaVigilanceEnv()
8
-
9
-
10
- @app.post("/reset")
11
- def reset(body: dict = {}):
12
- task_id = body.get("task_id", "known_signal_easy")
13
- obs = env.reset(task_id)
14
- return obs.model_dump()
15
-
16
-
17
- @app.post("/step")
18
- def step(action: Action):
19
- obs, reward, done, info = env.step(action)
20
- return {
21
- "observation": obs.model_dump(),
22
- "reward": reward.model_dump(),
23
- "done": done,
24
- "info": info,
25
- }
26
-
27
-
28
- @app.get("/state")
29
- def state():
30
- return env.state()
31
-
32
-
33
- @app.get("/tasks")
34
- def list_tasks():
35
- return {"tasks": ["known_signal_easy", "cluster_signal_medium", "confounded_hard"]}
36
-
37
-
38
- @app.get("/health")
39
- def health():
40
- return {"status": "ok"}
41
-
42
-
43
- def main(host: str = "0.0.0.0", port: int = 7860) -> None:
44
- import uvicorn
45
-
46
- uvicorn.run(app, host=host, port=port)
47
-
48
-
49
- if __name__ == "__main__":
50
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from openenv.core.env_server import create_web_interface_app
3
+
4
+ from env import PharmaVigilanceEnv
5
+ from models import PharmaAction, PharmaObservation
6
+
7
+
8
+ TASK_IDS = ["known_signal_easy", "cluster_signal_medium", "confounded_hard"]
9
+
10
+
11
+ class OpenEnvPharmaAdapter:
12
+ """
13
+ Thin adapter that exposes the local environment through the interface
14
+ expected by OpenEnv's HTTP server and web playground helpers.
15
+ """
16
+
17
+ def __init__(self) -> None:
18
+ self._env = PharmaVigilanceEnv()
19
+ self._last_state: dict = {
20
+ "episode_id": None,
21
+ "step_count": 0,
22
+ }
23
+
24
+ def reset(self, task_id: str = "known_signal_easy") -> PharmaObservation:
25
+ observation = self._env.reset(task_id=task_id)
26
+ self._last_state = {
27
+ "episode_id": task_id,
28
+ "step_count": 0,
29
+ }
30
+ return PharmaObservation(
31
+ task_id=observation.task_id,
32
+ reports=observation.reports,
33
+ drug_interaction_db=observation.drug_interaction_db,
34
+ step_number=observation.step_number,
35
+ max_steps=observation.max_steps,
36
+ feedback=observation.feedback,
37
+ reward=0.0,
38
+ done=False,
39
+ metadata={"difficulty": self._env.current_task.difficulty if self._env.current_task else None},
40
+ )
41
+
42
+ def step(self, action: PharmaAction) -> PharmaObservation:
43
+ observation, reward, done, info = self._env.step(action)
44
+ self._last_state = {
45
+ "episode_id": observation.task_id,
46
+ "step_count": observation.step_number,
47
+ }
48
+ return PharmaObservation(
49
+ task_id=observation.task_id,
50
+ reports=observation.reports,
51
+ drug_interaction_db=observation.drug_interaction_db,
52
+ step_number=observation.step_number,
53
+ max_steps=observation.max_steps,
54
+ feedback=observation.feedback,
55
+ reward=reward.total,
56
+ done=done,
57
+ metadata=info,
58
+ )
59
+
60
+ @property
61
+ def state(self) -> dict:
62
+ return self._last_state
63
+
64
+ def close(self) -> None:
65
+ return None
66
+
67
+
68
+ app: FastAPI = create_web_interface_app(
69
+ OpenEnvPharmaAdapter,
70
+ PharmaAction,
71
+ PharmaObservation,
72
+ env_name="pharma_vigilance_env",
73
+ )
74
+
75
+
76
+ @app.get("/tasks")
77
+ def list_tasks():
78
+ return {"tasks": TASK_IDS}
79
+
80
+
81
+ def main(host: str = "0.0.0.0", port: int = 7860) -> None:
82
+ import uvicorn
83
+
84
+ uvicorn.run(app, host=host, port=port)
85
+
86
+
87
+ if __name__ == "__main__":
88
+ main()
validate-submission.sh CHANGED
@@ -1,185 +1,164 @@
1
- #!/usr/bin/env bash
2
- #
3
- # validate-submission.sh — OpenEnv Submission Validator
4
- #
5
- # Checks that your HF Space is live, Docker image builds, and openenv validate passes.
6
- #
7
- # Prerequisites:
8
- # - Docker: https://docs.docker.com/get-docker/
9
- # - openenv-core: pip install openenv-core
10
- # - curl (usually pre-installed)
11
- #
12
- # Run:
13
- # curl -fsSL https://raw.githubusercontent.com/<owner>/<repo>/main/scripts/validate-submission.sh | bash -s -- <ping_url> [repo_dir]
14
- #
15
- # Or download and run locally:
16
- # chmod +x validate-submission.sh
17
- # ./validate-submission.sh <ping_url> [repo_dir]
18
- #
19
- # Arguments:
20
- # ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)
21
- # repo_dir Path to your repo (default: current directory)
22
- #
23
- # Examples:
24
- # ./validate-submission.sh https://my-team.hf.space
25
- # ./validate-submission.sh https://my-team.hf.space ./my-repo
26
- #
27
-
28
- set -uo pipefail
29
-
30
- DOCKER_BUILD_TIMEOUT=600
31
- if [ -t 1 ]; then
32
- RED='\033[0;31m'
33
- GREEN='\033[0;32m'
34
- YELLOW='\033[1;33m'
35
- BOLD='\033[1m'
36
- NC='\033[0m'
37
- else
38
- RED='' GREEN='' YELLOW='' BOLD='' NC=''
39
- fi
40
-
41
- run_with_timeout() {
42
- local secs="$1"; shift
43
- if command -v timeout &>/dev/null; then
44
- timeout "$secs" "$@"
45
- elif command -v gtimeout &>/dev/null; then
46
- gtimeout "$secs" "$@"
47
- else
48
- "$@" &
49
- local pid=$!
50
- ( sleep "$secs" && kill "$pid" 2>/dev/null ) &
51
- local watcher=$!
52
- wait "$pid" 2>/dev/null
53
- local rc=$?
54
- kill "$watcher" 2>/dev/null
55
- wait "$watcher" 2>/dev/null
56
- return $rc
57
- fi
58
- }
59
-
60
- portable_mktemp() {
61
- local prefix="${1:-validate}"
62
- mktemp "${TMPDIR:-/tmp}/${prefix}-XXXXXX" 2>/dev/null || mktemp
63
- }
64
-
65
- CLEANUP_FILES=()
66
- cleanup() { rm -f "${CLEANUP_FILES[@]+"${CLEANUP_FILES[@]}"}"; }
67
- trap cleanup EXIT
68
-
69
- PING_URL="${1:-}"
70
- REPO_DIR="${2:-.}"
71
-
72
- if [ -z "$PING_URL" ]; then
73
- printf "Usage: %s <ping_url> [repo_dir]\n" "$0"
74
- printf "\n"
75
- printf " ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)\n"
76
- printf " repo_dir Path to your repo (default: current directory)\n"
77
- exit 1
78
- fi
79
-
80
- if ! REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"; then
81
- printf "Error: directory '%s' not found\n" "${2:-.}"
82
- exit 1
83
- fi
84
- PING_URL="${PING_URL%/}"
85
- export PING_URL
86
- PASS=0
87
-
88
- log() { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }
89
- pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }
90
- fail() { log "${RED}FAILED${NC} -- $1"; }
91
- hint() { printf " ${YELLOW}Hint:${NC} %b\n" "$1"; }
92
- stop_at() {
93
- printf "\n"
94
- printf "${RED}${BOLD}Validation stopped at %s.${NC} Fix the above before continuing.\n" "$1"
95
- exit 1
96
- }
97
-
98
- printf "\n"
99
- printf "${BOLD}========================================${NC}\n"
100
- printf "${BOLD} OpenEnv Submission Validator${NC}\n"
101
- printf "${BOLD}========================================${NC}\n"
102
- log "Repo: $REPO_DIR"
103
- log "Ping URL: $PING_URL"
104
- printf "\n"
105
-
106
- log "${BOLD}Step 1/3: Pinging HF Space${NC} ($PING_URL/reset) ..."
107
-
108
- CURL_OUTPUT=$(portable_mktemp "validate-curl")
109
- CLEANUP_FILES+=("$CURL_OUTPUT")
110
- HTTP_CODE=$(curl -s -o "$CURL_OUTPUT" -w "%{http_code}" -X POST \
111
- -H "Content-Type: application/json" -d '{}' \
112
- "$PING_URL/reset" --max-time 30 2>"$CURL_OUTPUT" || printf "000")
113
-
114
- if [ "$HTTP_CODE" = "200" ]; then
115
- pass "HF Space is live and responds to /reset"
116
- elif [ "$HTTP_CODE" = "000" ]; then
117
- fail "HF Space not reachable (connection failed or timed out)"
118
- hint "Check your network connection and that the Space is running."
119
- hint "Try: curl -s -o /dev/null -w '%%{http_code}' -X POST $PING_URL/reset"
120
- stop_at "Step 1"
121
- else
122
- fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"
123
- hint "Make sure your Space is running and the URL is correct."
124
- hint "Try opening $PING_URL in your browser first."
125
- stop_at "Step 1"
126
- fi
127
-
128
- log "${BOLD}Step 2/3: Running docker build${NC} ..."
129
-
130
- if ! command -v docker &>/dev/null; then
131
- fail "docker command not found"
132
- hint "Install Docker: https://docs.docker.com/get-docker/"
133
- stop_at "Step 2"
134
- fi
135
-
136
- if [ -f "$REPO_DIR/Dockerfile" ]; then
137
- DOCKER_CONTEXT="$REPO_DIR"
138
- elif [ -f "$REPO_DIR/server/Dockerfile" ]; then
139
- DOCKER_CONTEXT="$REPO_DIR/server"
140
- else
141
- fail "No Dockerfile found in repo root or server/ directory"
142
- stop_at "Step 2"
143
- fi
144
-
145
- log " Found Dockerfile in $DOCKER_CONTEXT"
146
-
147
- BUILD_OK=false
148
- BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true
149
-
150
- if [ "$BUILD_OK" = true ]; then
151
- pass "Docker build succeeded"
152
- else
153
- fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"
154
- printf "%s\n" "$BUILD_OUTPUT" | tail -20
155
- stop_at "Step 2"
156
- fi
157
-
158
- log "${BOLD}Step 3/3: Running openenv validate${NC} ..."
159
-
160
- if ! command -v openenv &>/dev/null; then
161
- fail "openenv command not found"
162
- hint "Install it: pip install openenv-core"
163
- stop_at "Step 3"
164
- fi
165
-
166
- VALIDATE_OK=false
167
- VALIDATE_OUTPUT=$(cd "$REPO_DIR" && openenv validate 2>&1) && VALIDATE_OK=true
168
-
169
- if [ "$VALIDATE_OK" = true ]; then
170
- pass "openenv validate passed"
171
- [ -n "$VALIDATE_OUTPUT" ] && log " $VALIDATE_OUTPUT"
172
- else
173
- fail "openenv validate failed"
174
- printf "%s\n" "$VALIDATE_OUTPUT"
175
- stop_at "Step 3"
176
- fi
177
-
178
- printf "\n"
179
- printf "${BOLD}========================================${NC}\n"
180
- printf "${GREEN}${BOLD} All 3/3 checks passed!${NC}\n"
181
- printf "${GREEN}${BOLD} Your submission is ready to submit.${NC}\n"
182
- printf "${BOLD}========================================${NC}\n"
183
- printf "\n"
184
-
185
- exit 0
 
1
+ #!/usr/bin/env bash
2
+ #
3
+ # validate-submission.sh — OpenEnv Submission Validator
4
+ #
5
+ # Checks that your HF Space is live, Docker image builds, and openenv validate passes.
6
+
7
+ set -uo pipefail
8
+
9
+ DOCKER_BUILD_TIMEOUT=600
10
+ if [ -t 1 ]; then
11
+ RED='\033[0;31m'
12
+ GREEN='\033[0;32m'
13
+ YELLOW='\033[1;33m'
14
+ BOLD='\033[1m'
15
+ NC='\033[0m'
16
+ else
17
+ RED='' GREEN='' YELLOW='' BOLD='' NC=''
18
+ fi
19
+
20
+ run_with_timeout() {
21
+ local secs="$1"; shift
22
+ if command -v timeout &>/dev/null; then
23
+ timeout "$secs" "$@"
24
+ elif command -v gtimeout &>/dev/null; then
25
+ gtimeout "$secs" "$@"
26
+ else
27
+ "$@" &
28
+ local pid=$!
29
+ ( sleep "$secs" && kill "$pid" 2>/dev/null ) &
30
+ local watcher=$!
31
+ wait "$pid" 2>/dev/null
32
+ local rc=$?
33
+ kill "$watcher" 2>/dev/null
34
+ wait "$watcher" 2>/dev/null
35
+ return $rc
36
+ fi
37
+ }
38
+
39
+ portable_mktemp() {
40
+ local prefix="${1:-validate}"
41
+ mktemp "${TMPDIR:-/tmp}/${prefix}-XXXXXX" 2>/dev/null || mktemp
42
+ }
43
+
44
+ CLEANUP_FILES=()
45
+ cleanup() { rm -f "${CLEANUP_FILES[@]+"${CLEANUP_FILES[@]}"}"; }
46
+ trap cleanup EXIT
47
+
48
+ PING_URL="${1:-}"
49
+ REPO_DIR="${2:-.}"
50
+
51
+ if [ -z "$PING_URL" ]; then
52
+ printf "Usage: %s <ping_url> [repo_dir]\n" "$0"
53
+ printf "\n"
54
+ printf " ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)\n"
55
+ printf " repo_dir Path to your repo (default: current directory)\n"
56
+ exit 1
57
+ fi
58
+
59
+ if ! REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"; then
60
+ printf "Error: directory '%s' not found\n" "${2:-.}"
61
+ exit 1
62
+ fi
63
+ PING_URL="${PING_URL%/}"
64
+ export PING_URL
65
+ PASS=0
66
+
67
+ log() { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }
68
+ pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }
69
+ fail() { log "${RED}FAILED${NC} -- $1"; }
70
+ hint() { printf " ${YELLOW}Hint:${NC} %b\n" "$1"; }
71
+ stop_at() {
72
+ printf "\n"
73
+ printf "${RED}${BOLD}Validation stopped at %s.${NC} Fix the above before continuing.\n" "$1"
74
+ exit 1
75
+ }
76
+
77
+ printf "\n"
78
+ printf "${BOLD}========================================${NC}\n"
79
+ printf "${BOLD} OpenEnv Submission Validator${NC}\n"
80
+ printf "${BOLD}========================================${NC}\n"
81
+ log "Repo: $REPO_DIR"
82
+ log "Ping URL: $PING_URL"
83
+ printf "\n"
84
+
85
+ log "${BOLD}Step 1/3: Pinging HF Space${NC} ($PING_URL/reset) ..."
86
+
87
+ CURL_OUTPUT=$(portable_mktemp "validate-curl")
88
+ CLEANUP_FILES+=("$CURL_OUTPUT")
89
+ HTTP_CODE=$(curl -s -o "$CURL_OUTPUT" -w "%{http_code}" -X POST \
90
+ -H "Content-Type: application/json" -d '{}' \
91
+ "$PING_URL/reset" --max-time 30 2>"$CURL_OUTPUT" || printf "000")
92
+
93
+ if [ "$HTTP_CODE" = "200" ]; then
94
+ pass "HF Space is live and responds to /reset"
95
+ elif [ "$HTTP_CODE" = "000" ]; then
96
+ fail "HF Space not reachable (connection failed or timed out)"
97
+ hint "Check your network connection and that the Space is running."
98
+ hint "Try: curl -s -o /dev/null -w '%%{http_code}' -X POST $PING_URL/reset"
99
+ stop_at "Step 1"
100
+ else
101
+ fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"
102
+ hint "Make sure your Space is running and the URL is correct."
103
+ hint "Try opening $PING_URL in your browser first."
104
+ stop_at "Step 1"
105
+ fi
106
+
107
+ log "${BOLD}Step 2/3: Running docker build${NC} ..."
108
+
109
+ if ! command -v docker &>/dev/null; then
110
+ fail "docker command not found"
111
+ hint "Install Docker: https://docs.docker.com/get-docker/"
112
+ stop_at "Step 2"
113
+ fi
114
+
115
+ if [ -f "$REPO_DIR/Dockerfile" ]; then
116
+ DOCKER_CONTEXT="$REPO_DIR"
117
+ elif [ -f "$REPO_DIR/server/Dockerfile" ]; then
118
+ DOCKER_CONTEXT="$REPO_DIR/server"
119
+ else
120
+ fail "No Dockerfile found in repo root or server/ directory"
121
+ stop_at "Step 2"
122
+ fi
123
+
124
+ log " Found Dockerfile in $DOCKER_CONTEXT"
125
+
126
+ BUILD_OK=false
127
+ BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true
128
+
129
+ if [ "$BUILD_OK" = true ]; then
130
+ pass "Docker build succeeded"
131
+ else
132
+ fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"
133
+ printf "%s\n" "$BUILD_OUTPUT" | tail -20
134
+ stop_at "Step 2"
135
+ fi
136
+
137
+ log "${BOLD}Step 3/3: Running openenv validate${NC} ..."
138
+
139
+ if ! command -v openenv &>/dev/null; then
140
+ fail "openenv command not found"
141
+ hint "Install it: pip install openenv-core"
142
+ stop_at "Step 3"
143
+ fi
144
+
145
+ VALIDATE_OK=false
146
+ VALIDATE_OUTPUT=$(cd "$REPO_DIR" && openenv validate 2>&1) && VALIDATE_OK=true
147
+
148
+ if [ "$VALIDATE_OK" = true ]; then
149
+ pass "openenv validate passed"
150
+ [ -n "$VALIDATE_OUTPUT" ] && log " $VALIDATE_OUTPUT"
151
+ else
152
+ fail "openenv validate failed"
153
+ printf "%s\n" "$VALIDATE_OUTPUT"
154
+ stop_at "Step 3"
155
+ fi
156
+
157
+ printf "\n"
158
+ printf "${BOLD}========================================${NC}\n"
159
+ printf "${GREEN}${BOLD} All 3/3 checks passed!${NC}\n"
160
+ printf "${GREEN}${BOLD} Your submission is ready to submit.${NC}\n"
161
+ printf "${BOLD}========================================${NC}\n"
162
+ printf "\n"
163
+
164
+ exit 0