File size: 8,685 Bytes
83d677f
 
 
 
 
 
 
 
 
 
 
c2b7a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d677f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53def98
83d677f
 
 
 
0aec951
 
83d677f
0aec951
83d677f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b7a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53def98
c2b7a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53def98
c2b7a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
title: Northwestern CS Kiosk API
emoji: πŸŽ™οΈ
colorFrom: blue
colorTo: indigo
sdk: docker
sdk_version: "latest"
app_file: Dockerfile
pinned: false
---

# Northwestern CS Kiosk API

REST API backend for the Northwestern CS Department Kiosk. This is a stripped-down version optimized for integration with external systems (e.g., speech-to-text/text-to-speech).

## Quick Start

### 1. Install Dependencies

```bash
pip install -r requirements.txt
```

### 2. Configure Environment

```bash
cp .env.example .env
# Edit .env and add your API key
```

### 3. Run the Server

```bash
python -m backend.main
```

The API will be available at `http://0.0.0.0:8000`

---

## Deploy to Hugging Face Spaces

Deploy this API as a public endpoint so your manager (or STT/TTS systems) can send requests from anywhere.

### 1. Create a new Space

1. Go to [huggingface.co/spaces](https://huggingface.co/spaces)
2. Click **Create new Space**
3. Choose **Docker** SDK, **Blank** template
4. Name it (e.g. `monish563/NU-Kiosk-API`)
5. Create, then push this `kiosk-api` folder to the Space repo

### 2. Add secrets (Settings β†’ Variables and secrets β†’ Secrets Private)

| Secret | Required | Description |
|--------|----------|-------------|
| `ANTHROPIC_API_KEY` | **Yes*** | Anthropic API key (starts with `sk-ant-api03-...`) |
| `KIOSK_LLM_PROVIDER` | No | Default: `anthropic` |
| `KIOSK_LLM_MODEL` | No | Default: `claude-haiku-4-5` |
| `KIOSK_LLM_SYSTEM_PROMPT` | No | Custom system prompt for the receptionist |
| `KIOSK_LLM_STYLE` | No | Style guidelines for TTS-friendly responses |
| `OPENAI_API_KEY` | No | If using `provider: "openai"` |
| `GEMINI_API_KEY` | No | If using `provider: "gemini"` |
| `KIOSK_HF_DATASET_REPO` | No | HF dataset for persistence (e.g. `monish563/kiosk-api-metrics`) |
| `KIOSK_HF_TOKEN` | No* | HF token with write access (required if dataset repo is set) |

*At least one LLM API key is required. `KIOSK_HF_TOKEN` is required if `KIOSK_HF_DATASET_REPO` is set.

### 3. Endpoint URL for your manager

Once the Space is built and running, the base URL will be:

```
https://<your-username>-<space-name>.hf.space
```

**Main endpoint (for STT β†’ TTS flow):**

```
POST https://<your-username>-<space-name>.hf.space/api/query
Content-Type: application/json

{"question": "Where is Professor Hammond's office?"}
```

**Response:** `{"answer": "...", ...}` β€” send `answer` to your TTS system.

---

## API Reference

### Health Check

```
GET /
```

**Response:**
```json
{
  "status": "ok",
  "service": "Northwestern CS Kiosk API"
}
```

---

### Query (Main Endpoint)

```
POST /api/query
```

This is the primary endpoint for speech integration.

**Request Body:**
```json
{
  "question": "Where is Professor Hammond's office?",
  "session_id": "optional-session-id",
  "provider": "anthropic"
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `question` | string | **Yes** | The user's question (from speech-to-text) |
| `session_id` | string | No | Session ID for conversation continuity (default: "default") |
| `provider` | string | No | LLM provider: `anthropic`, `openai`, `gemini` |

**Response:**
```json
{
  "session_id": "default",
  "session_title": "Chat – Jan 23, 10:30 AM",
  "question": "Where is Professor Hammond's office?",
  "answer": "Professor Kristian Hammond's office is located in Mudd 3225.",
  "blueprint": "location",
  "facts": [...],
  "notes": [],
  "usage": {
    "provider": "anthropic",
    "model": "claude-haiku-4-5",
    "tokens": 512
  },
  "action": {
    "type": "lookup_location",
    "arguments": { "name": "Kristian Hammond" }
  }
}
```

**Key Fields:**
- `answer` - The response text (send to text-to-speech)
- `question` - Echo of the input question
- `blueprint` - Which tool was used internally
- `facts` - Structured data retrieved
- `usage` - Token/model metadata

---

### List Providers

```
GET /api/providers
```

Returns available LLM providers and their configuration status.

**Response:**
```json
{
  "providers": {
    "claude": {
      "name": "Claude",
      "configured": true,
      "default_model": "claude-haiku-4-5"
    },
    "gpt": {
      "name": "GPT",
      "configured": false,
      "note": "Set OPENAI_API_KEY before using this provider."
    }
  },
  "default_provider": "claude"
}
```

---

### Get History

```
GET /api/history?session_id=default
```

Returns conversation history for a session.

**Response:**
```json
{
  "session_id": "default",
  "title": "Chat – Jan 23, 10:30 AM",
  "history": [
    {
      "timestamp": 1706012345.123,
      "question": "Who is Kristian Hammond?",
      "answer": "Professor Kristian Hammond is...",
      "blueprint": "person_lookup"
    }
  ]
}
```

---

### List Sessions

```
GET /api/sessions
```

Returns all conversation sessions.

**Response:**
```json
{
  "sessions": [
    {
      "session_id": "default",
      "title": "Chat – Jan 23, 10:30 AM",
      "created_at": 1706012345.123,
      "updated_at": 1706012400.456
    }
  ]
}
```

---

## Integration Example

### cURL

```bash
curl -X POST "http://localhost:8000/api/query" \
  -H "Content-Type: application/json" \
  -d '{"question": "Where is Professor Hammond?"}'
```

### Python

```python
import requests

response = requests.post(
    "http://localhost:8000/api/query",
    json={"question": "Where is Professor Hammond?"}
)
data = response.json()
answer = data["answer"]  # Send this to text-to-speech
```

### JavaScript

```javascript
const response = await fetch("http://localhost:8000/api/query", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ question: "Where is Professor Hammond?" })
});
const data = await response.json();
const answer = data.answer;  // Send this to text-to-speech
```

---

## Speech Integration Flow

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Microphone β”‚ ──▢ β”‚   STT API   β”‚ ──▢ β”‚ Kiosk API   β”‚ ──▢ β”‚   TTS API   β”‚
β”‚             β”‚     β”‚ (Speech to  β”‚     β”‚ /api/query  β”‚     β”‚ (Text to    β”‚
β”‚             β”‚     β”‚   Text)     β”‚     β”‚             β”‚     β”‚  Speech)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚                   β”‚                   β”‚
                           β–Ό                   β–Ό                   β–Ό
                      "Where is           {"answer":          [Audio]
                       Prof X?"           "Prof X is           πŸ”Š
                                          in Mudd..."}
```

---

## Available Query Types

| Query Type | Example Questions |
|------------|-------------------|
| Person lookup | "Who is Kristian Hammond?", "Tell me about Katie Winters" |
| Location | "Where is Professor X's office?", "Where does student Y sit?" |
| Research topics | "Who researches AI?", "Faculty working on machine learning?" |
| Advisors | "Who advises student X?", "Who does Prof Y advise?" |
| Centers | "Who leads the Center for Deep Learning?" |
| Staff support | "Who handles reimbursements?", "Academic advising contact?" |
| Office hours | "When are CS 211 office hours?" |
| Events | "Any upcoming AI events?" |

---

## Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `ANTHROPIC_API_KEY` | Yes* | - | Anthropic API key |
| `OPENAI_API_KEY` | Yes* | - | OpenAI API key |
| `GEMINI_API_KEY` | Yes* | - | Google Gemini API key |
| `KIOSK_LLM_PROVIDER` | No | `anthropic` | Default LLM provider |
| `KIOSK_HOST` | No | `0.0.0.0` | Server host |
| `KIOSK_PORT` | No | `8000` | Server port |
| `KIOSK_LLM_TIMEOUT` | No | `60` | LLM timeout (seconds) |

*At least one API key is required.

---

## Project Structure

```
kiosk-api/
β”œβ”€β”€ Archive/              # Data files (CSV)
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ data/             # Data loading utilities
β”‚   β”œβ”€β”€ mcp/              # LLM planner & tool execution
β”‚   β”œβ”€β”€ providers/        # LLM provider implementations
β”‚   β”œβ”€β”€ tools/            # Query blueprints
β”‚   β”œβ”€β”€ main.py           # FastAPI application
β”‚   └── responders.py     # Response generation
β”œβ”€β”€ .env.example          # Environment template
β”œβ”€β”€ requirements.txt      # Python dependencies
└── README.md             # This file
```