kenqtade commited on
Commit
ed37347
·
0 Parent(s):

Initial: opencode-ai HF Space with NVIDIA NIM

Browse files
Files changed (4) hide show
  1. AGENTS.md +54 -0
  2. Dockerfile +21 -0
  3. README.md +44 -0
  4. start.sh +28 -0
AGENTS.md ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HF Space Agent System Instructions
2
+
3
+ You are an AI coding agent running inside a Hugging Face Space. You have access to NVIDIA NIM (Llama 3.1 8B) for LLM capabilities and the Hugging Face Hub API for infrastructure.
4
+
5
+ ## Environment
6
+
7
+ Your HF token is available as `HF_TOKEN` env var, NVIDIA API key as `NVIDIA_API_KEY`.
8
+
9
+ ## Project Workflow
10
+
11
+ You work on ONE project at a time. Each project gets its own dedicated HF Space.
12
+
13
+ ### Starting a new project
14
+
15
+ When the user asks to start or work on a project:
16
+
17
+ 1. **Plan** the project structure and requirements
18
+ 2. **Create a HF Space** for the project using Python:
19
+ ```python
20
+ from huggingface_hub import HfApi
21
+ api = HfApi(token=HF_TOKEN)
22
+ api.create_repo(
23
+ repo_id=f"{namespace}/{project-slug}",
24
+ repo_type="space",
25
+ space_sdk="docker",
26
+ private=True,
27
+ exist_ok=False,
28
+ )
29
+ ```
30
+ 3. **Develop** in the project Space:
31
+ - Push code to the Space's git repo
32
+ - Set environment secrets via `api.add_space_secret()`
33
+ - Use the Space as the isolated environment for that project
34
+ 4. **Iterate**: Make changes, commit, push, the Space rebuilds automatically
35
+
36
+ ### Working on existing projects
37
+
38
+ - Track project Spaces and their statuses
39
+ - Pull latest code, make changes, push
40
+ - Check Space logs for build/runtime errors
41
+
42
+ ### When the project is done
43
+
44
+ - Present the URL of the project Space to the user
45
+ - Keep the Space around unless user asks to delete it
46
+
47
+ ## Rules
48
+
49
+ - Do NOT write project code in the home Space directory (`/home/node/app`)
50
+ - Each project lives in its OWN HF Space
51
+ - Use `python3` and the `huggingface_hub` library for all HF API calls
52
+ - If python3 or huggingface_hub is not installed, install with pip
53
+ - Ask the user for the namespace/username to use for project Spaces
54
+ - One project at a time unless explicitly asked to multitask
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:20
2
+
3
+ RUN apt-get update && apt-get install -y curl git dos2unix && rm -rf /var/lib/apt/lists/*
4
+
5
+ WORKDIR /home/node/app
6
+ RUN chown -R node:node /home/node/app
7
+
8
+ USER node
9
+
10
+ ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
11
+ ENV PATH=/home/node/.npm-global/bin:$PATH
12
+
13
+ RUN npm install -g opencode-ai
14
+
15
+ COPY --chown=node:node . .
16
+
17
+ RUN dos2unix start.sh && chmod +x start.sh
18
+
19
+ EXPOSE 7860
20
+
21
+ CMD ["/bin/bash", "./start.sh"]
README.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # OpenCode HF Space
2
+
3
+ An AI coding agent running in your browser via Hugging Face Spaces, powered by NVIDIA NIM.
4
+
5
+ ## Deploy
6
+
7
+ ### 1. Create the Space on HF
8
+
9
+ ```python
10
+ from huggingface_hub import HfApi, SpaceHardware
11
+
12
+ api = HfApi(token="hf_...")
13
+ api.create_repo(
14
+ repo_id="your-username/opencode-space",
15
+ repo_type="space",
16
+ space_sdk="docker",
17
+ space_hardware=SpaceHardware.CPU_BASELINE,
18
+ private=False,
19
+ exist_ok=False,
20
+ )
21
+ ```
22
+
23
+ ### 2. Set secrets
24
+
25
+ Set these Space secrets via HF UI or API:
26
+
27
+ | Secret | Value |
28
+ |--------|-------|
29
+ | `NVIDIA_API_KEY` | Your NVIDIA NIM API key |
30
+ | `HF_TOKEN` | Your HF token with Space creation permissions |
31
+
32
+ ### 3. Push code
33
+
34
+ ```bash
35
+ git init
36
+ git add .
37
+ git commit -m "Initial deploy"
38
+ git remote add space https://huggingface.co/spaces/your-username/opencode-space
39
+ git push space main
40
+ ```
41
+
42
+ ### 4. Open
43
+
44
+ Wait for the Space to build, then visit `https://your-username-opencode-space.hf.space`
start.sh ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ mkdir -p /home/node/.config/opencode
5
+
6
+ cat > /home/node/.config/opencode/opencode.json << 'CONFIG'
7
+ {
8
+ "$schema": "https://opencode.ai/config.json",
9
+ "model": "nvidia/meta/llama-3.1-8b-instruct",
10
+ "provider": {
11
+ "nvidia": {
12
+ "npm": "@ai-sdk/openai-compatible",
13
+ "name": "NVIDIA NIM",
14
+ "options": {
15
+ "baseURL": "https://integrate.api.nvidia.com/v1",
16
+ "apiKey": "{env:NVIDIA_API_KEY}"
17
+ }
18
+ }
19
+ },
20
+ "instructions": ["AGENTS.md"],
21
+ "server": {
22
+ "port": 7860,
23
+ "hostname": "0.0.0.0"
24
+ }
25
+ }
26
+ CONFIG
27
+
28
+ exec opencode web --port 7860 --hostname 0.0.0.0