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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# asa-api (Hugging Face Space)
|
| 2 |
+
|
| 3 |
+
`asa-api` is a Docker-based API + GUI wrapper around [`asa::run_task()`](https://github.com/cjerzak/asa-software) designed for deployment as a Hugging Face Space.
|
| 4 |
+
|
| 5 |
+
It uses:
|
| 6 |
+
- `R` + `plumber` for HTTP endpoints
|
| 7 |
+
- The `asa` package for orchestration (`run_task`, `run_task_batch`)
|
| 8 |
+
- A lightweight password-protected browser GUI at `/`
|
| 9 |
+
|
| 10 |
+
## What This Space Exposes
|
| 11 |
+
|
| 12 |
+
1. `GET /healthz`
|
| 13 |
+
2. `POST /v1/run` for a single prompt
|
| 14 |
+
3. `POST /v1/batch` for many prompts
|
| 15 |
+
4. `GET /` GUI page
|
| 16 |
+
5. `POST /gui/query` used by the GUI
|
| 17 |
+
|
| 18 |
+
## Security Model
|
| 19 |
+
|
| 20 |
+
- API key auth is optional:
|
| 21 |
+
- If `ASA_API_KEY` is set, `/v1/*` requires:
|
| 22 |
+
- `Authorization: Bearer <key>` or
|
| 23 |
+
- `x-api-key: <key>`
|
| 24 |
+
- If `ASA_API_KEY` is not set, `/v1/*` is public.
|
| 25 |
+
- GUI auth is password-based:
|
| 26 |
+
- `/gui/query` checks `GUI_PASSWORD`
|
| 27 |
+
|
| 28 |
+
## Required / Recommended HF Space Secrets
|
| 29 |
+
|
| 30 |
+
Set these in the Hugging Face Space (`Settings` -> `Variables and secrets`):
|
| 31 |
+
|
| 32 |
+
- `OPENAI_API_KEY` (or the provider key you use)
|
| 33 |
+
- `GUI_PASSWORD`
|
| 34 |
+
|
| 35 |
+
Optional secrets / vars:
|
| 36 |
+
|
| 37 |
+
- `ASA_API_KEY` (enables API token auth for `/v1/*`)
|
| 38 |
+
- `ASA_DEFAULT_BACKEND` (example: `openai`, `groq`, `anthropic`, `gemini`, `openrouter`)
|
| 39 |
+
- `ASA_DEFAULT_MODEL` (example: `gpt-4.1-mini`)
|
| 40 |
+
- `ASA_CONDA_ENV` (default: `asa_env`)
|
| 41 |
+
- `ASA_USE_BROWSER_DEFAULT` (default: `false`, recommended for Space stability)
|
| 42 |
+
- `CORS_ALLOW_ORIGIN` (default: `*`)
|
| 43 |
+
|
| 44 |
+
Provider-specific keys supported by `asa` include:
|
| 45 |
+
|
| 46 |
+
- `OPENAI_API_KEY`
|
| 47 |
+
- `GROQ_API_KEY`
|
| 48 |
+
- `ANTHROPIC_API_KEY`
|
| 49 |
+
- `GOOGLE_API_KEY` (or `GEMINI_API_KEY`)
|
| 50 |
+
- `OPENROUTER_API_KEY`
|
| 51 |
+
- Bedrock credentials if using `bedrock`
|
| 52 |
+
|
| 53 |
+
## API Usage
|
| 54 |
+
|
| 55 |
+
### 1) Health check
|
| 56 |
+
|
| 57 |
+
```bash
|
| 58 |
+
curl -s https://<your-space>.hf.space/healthz
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### 2) Single query
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
curl -s https://<your-space>.hf.space/v1/run \
|
| 65 |
+
-H "Content-Type: application/json" \
|
| 66 |
+
-H "Authorization: Bearer $ASA_API_KEY" \
|
| 67 |
+
-d '{
|
| 68 |
+
"prompt": "What is the population of Tokyo?",
|
| 69 |
+
"config": {
|
| 70 |
+
"backend": "openai",
|
| 71 |
+
"model": "gpt-4.1-mini"
|
| 72 |
+
},
|
| 73 |
+
"run": {
|
| 74 |
+
"output_format": "text",
|
| 75 |
+
"recursion_limit": 20
|
| 76 |
+
}
|
| 77 |
+
}'
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### 3) Structured JSON output
|
| 81 |
+
|
| 82 |
+
```bash
|
| 83 |
+
curl -s https://<your-space>.hf.space/v1/run \
|
| 84 |
+
-H "Content-Type: application/json" \
|
| 85 |
+
-d '{
|
| 86 |
+
"prompt": "Find Marie Curie birth year and nationality. Return JSON.",
|
| 87 |
+
"run": {
|
| 88 |
+
"output_format": "json"
|
| 89 |
+
}
|
| 90 |
+
}'
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### 4) Batch query
|
| 94 |
+
|
| 95 |
+
```bash
|
| 96 |
+
curl -s https://<your-space>.hf.space/v1/batch \
|
| 97 |
+
-H "Content-Type: application/json" \
|
| 98 |
+
-d '{
|
| 99 |
+
"prompts": [
|
| 100 |
+
"What is the capital of France?",
|
| 101 |
+
"What is the capital of Japan?"
|
| 102 |
+
],
|
| 103 |
+
"parallel": false,
|
| 104 |
+
"run": {
|
| 105 |
+
"output_format": "text"
|
| 106 |
+
}
|
| 107 |
+
}'
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
## Request Shape
|
| 111 |
+
|
| 112 |
+
### `POST /v1/run`
|
| 113 |
+
|
| 114 |
+
```json
|
| 115 |
+
{
|
| 116 |
+
"prompt": "string, required",
|
| 117 |
+
"config": {
|
| 118 |
+
"...": "any asa::asa_config argument"
|
| 119 |
+
},
|
| 120 |
+
"run": {
|
| 121 |
+
"...": "any asa::run_task argument except prompt/config/agent"
|
| 122 |
+
},
|
| 123 |
+
"include_raw_output": false,
|
| 124 |
+
"include_trace_json": false
|
| 125 |
+
}
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
### `POST /v1/batch`
|
| 129 |
+
|
| 130 |
+
```json
|
| 131 |
+
{
|
| 132 |
+
"prompts": ["required", "array", "of strings"],
|
| 133 |
+
"config": {
|
| 134 |
+
"...": "any asa::asa_config argument"
|
| 135 |
+
},
|
| 136 |
+
"run": {
|
| 137 |
+
"...": "run_task-compatible arguments shared across the batch"
|
| 138 |
+
},
|
| 139 |
+
"batch": {
|
| 140 |
+
"...": "any asa::run_task_batch argument except prompts/config/agent"
|
| 141 |
+
},
|
| 142 |
+
"parallel": false,
|
| 143 |
+
"include_raw_output": false,
|
| 144 |
+
"include_trace_json": false
|
| 145 |
+
}
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
## Future Compatibility Strategy
|
| 149 |
+
|
| 150 |
+
The adapter is intentionally resilient to `asa` changes:
|
| 151 |
+
|
| 152 |
+
- It dynamically inspects `formals(asa::asa_config)`, `formals(asa::run_task)`, and `formals(asa::run_task_batch)`.
|
| 153 |
+
- It only forwards request keys that exist in current function signatures.
|
| 154 |
+
- This avoids hard crashes when upstream adds/removes optional args.
|
| 155 |
+
|
| 156 |
+
## GUI
|
| 157 |
+
|
| 158 |
+
- Open `/` in your browser.
|
| 159 |
+
- Enter `GUI_PASSWORD`.
|
| 160 |
+
- Enter prompt and choose output format.
|
| 161 |
+
- Submit (Ctrl/Cmd + Enter is supported).
|
| 162 |
+
|
| 163 |
+
## Docker Build and Runtime
|
| 164 |
+
|
| 165 |
+
The `Dockerfile` clones `asa-software` during build and installs `asa` from source.
|
| 166 |
+
|
| 167 |
+
Build args:
|
| 168 |
+
|
| 169 |
+
- `ASA_SOFTWARE_REPO` (default: `https://github.com/cjerzak/asa-software`)
|
| 170 |
+
- `ASA_SOFTWARE_REF` (default: `main`)
|
| 171 |
+
|
| 172 |
+
Local build:
|
| 173 |
+
|
| 174 |
+
```bash
|
| 175 |
+
docker build -t asa-api .
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
Local run:
|
| 179 |
+
|
| 180 |
+
```bash
|
| 181 |
+
docker run --rm -p 7860:7860 \
|
| 182 |
+
-e OPENAI_API_KEY=... \
|
| 183 |
+
-e GUI_PASSWORD=XXX \
|
| 184 |
+
-e ASA_API_KEY=optional_token \
|
| 185 |
+
asa-api
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
Then open:
|
| 189 |
+
- `http://localhost:7860/healthz`
|
| 190 |
+
- `http://localhost:7860/`
|
| 191 |
+
|
| 192 |
+
## Notes
|
| 193 |
+
|
| 194 |
+
- Browser/Selenium tier is disabled by default (`use_browser = FALSE`) for better reliability in Space containers.
|
| 195 |
+
- If you want browser tier, set `config.use_browser = true` explicitly per request and ensure supporting binaries are installed.
|