File size: 12,126 Bytes
128f5d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
---
title: BirdScope AI - MCP Multi-Agent System
emoji: πŸ¦…
colorFrom: green
colorTo: blue
sdk: gradio
python_version: 3.11
app_file: app.py
pinned: false
---

# πŸ¦… BirdScope AI - Multi-Agent Bird Identification System

**AI-powered bird identification with specialized MCP agents**

Built for the [MCP 1st Birthday Hackathon](https://huggingface.co/MCP-1st-Birthday)

---

## 🎯 Overview

BirdScope AI is a production-ready multi-agent system that combines **Modal GPU classification** with **Nuthatch species database** to provide comprehensive bird identification and exploration. Users can upload photos, search species, explore taxonomic families, and access rich multimedia content (images, audio recordings, conservation data).

**Two Agent Modes:**
1. **Specialized Subagents (3 Specialists)** - Router orchestrates image identifier, species explorer, and taxonomy specialist
2. **Audio Finder Agent** - Specialized agent for discovering bird audio recordings

---

## ✨ Features

- πŸ” **Image Classification**: Upload bird photos for instant GPU-powered identification
- πŸ“Έ **Reference Images**: High-quality Unsplash photos for each species
- 🎡 **Audio Recordings**: Bird calls and songs from xeno-canto.org
- 🌍 **Conservation Data**: IUCN status and taxonomic information
- 🧠 **Multi-Agent Architecture**: Specialized agents with focused tool subsets
- πŸ”„ **Dual Streaming**: Separate outputs for chat responses and tool execution logs
- πŸ€– **Multi-Provider**: OpenAI (GPT-4), Anthropic (Claude), HuggingFace (Qwen)

---

## πŸš€ Quick Start (For Users)

### Option 1: OpenAI (Recommended)
1. Get your OpenAI API key from [platform.openai.com/api-keys](https://platform.openai.com/api-keys)
2. Select **OpenAI** as provider in the sidebar
3. Enter your API key
4. Model used: `gpt-4o-mini`

### Option 2: Anthropic (Claude)
1. Get your Anthropic API key from [console.anthropic.com/settings/keys](https://console.anthropic.com/settings/keys)
2. Select **Anthropic** as provider
3. Enter your API key
4. Model used: `claude-sonnet-4-5`

### Option 3: HuggingFace
⚠️ **Note**: HuggingFace Inference API has limited function calling support. OpenAI or Anthropic recommended for full functionality.

---

## πŸ› οΈ Environment Setup (For Developers)

### Prerequisites

- Python 3.11+
- Modal account (for GPU classifier)
- Nuthatch API key
- LLM API key (OpenAI, Anthropic, or HuggingFace)

---

### 🏠 Local Development Setup

#### Step 1: Clone and Install

```bash
cd ~/Desktop/hackathon/hackathon_draft

# Create virtual environment
python3.11 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt
```

#### Step 2: Configure Environment Variables

Create a `.env` file from the example:

```bash
cp .env.example .env
```

Edit `.env` with your API keys:

```bash
# ================================================
# REQUIRED: Modal Bird Classifier (GPU)
# ================================================
MODAL_MCP_URL=https://your-modal-app--mcp-server.modal.run/mcp
BIRD_CLASSIFIER_API_KEY=your-modal-api-key-here

# ================================================
# REQUIRED: Nuthatch Species Database
# ================================================
NUTHATCH_API_KEY=your-nuthatch-api-key-here
NUTHATCH_BASE_URL=https://nuthatch.lastelm.software/v2  # Default, can omit

# Nuthatch Transport Mode (STDIO or HTTP)
NUTHATCH_USE_STDIO=true  # Recommended for local development

# Only needed if NUTHATCH_USE_STDIO=false:
# NUTHATCH_MCP_URL=http://localhost:8001/mcp
# NUTHATCH_MCP_AUTH_KEY=your-auth-key-here

# ================================================
# LLM Provider (Choose ONE)
# ================================================
# OpenAI (Recommended)
OPENAI_API_KEY=sk-your-openai-key-here
DEFAULT_OPENAI_MODEL=gpt-4o-mini
OPENAI_TEMPERATURE=0.0

# OR Anthropic
# ANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
# DEFAULT_ANTHROPIC_MODEL=claude-sonnet-4-5-20250929
# ANTHROPIC_TEMPERATURE=0.0

# OR HuggingFace (Limited function calling support)
# HF_API_KEY=hf_your-huggingface-token-here
# DEFAULT_HF_MODEL=Qwen/Qwen2.5-Coder-32B-Instruct
# HF_TEMPERATURE=0.1
```

#### Step 3: Understanding Nuthatch Transport Modes

**STDIO Mode (Recommended for Local):**
- Nuthatch MCP server runs as subprocess
- Automatically started by the app
- No separate server process needed
- Set `NUTHATCH_USE_STDIO=true`

**HTTP Mode (Alternative for Local):**
- Nuthatch MCP server runs as separate HTTP server
- Useful for debugging or multiple clients
- Requires running server in separate terminal

To use HTTP mode:

```bash
# Terminal 1: Run Nuthatch MCP server
python nuthatch_tools.py --http --port 8001

# Terminal 2: Run the app
# Set in .env:
# NUTHATCH_USE_STDIO=false
# NUTHATCH_MCP_URL=http://localhost:8001/mcp
python app.py
```

#### Step 4: Run the App

```bash
# With STDIO mode (default, easiest):
python app.py

# Or using Gradio CLI:
gradio app.py
```

App will be available at: `http://127.0.0.1:7860`

---

### ☁️ HuggingFace Spaces Deployment

#### Step 1: Create a New Space

1. Go to [huggingface.co/new-space](https://huggingface.co/new-space)
2. Choose:
   - **SDK**: Gradio
   - **Hardware**: CPU Basic (free) or CPU Upgrade (faster)
   - **Visibility**: Public or Private

#### Step 2: Upload Your Code

**Option A: Using `upload_to_space.py` (Recommended)**

```bash
# 1. Install HuggingFace CLI
pip install huggingface_hub

# 2. Login
huggingface-cli login

# 3. Update upload_to_space.py with your Space name
# Edit line with repo_id:
# repo_id="YOUR-USERNAME/YOUR-SPACE-NAME"

# 4. Upload
python upload_to_space.py
```

**Option B: Using Git**

```bash
git remote add hf-space https://huggingface.co/spaces/YOUR-USERNAME/YOUR-SPACE-NAME
git push hf-space main
```

#### Step 3: Configure Secrets in HuggingFace Spaces

⚠️ **CRITICAL**: Spaces use **Secrets**, not `.env` files!

Go to your Space β†’ **Settings** β†’ **Variables and secrets**

**Add these secrets:**

```bash
# REQUIRED: Modal Bird Classifier
MODAL_MCP_URL = https://your-modal-app--mcp-server.modal.run/mcp
BIRD_CLASSIFIER_API_KEY = your-modal-api-key-here

# REQUIRED: Nuthatch Species Database
NUTHATCH_API_KEY = your-nuthatch-api-key-here
NUTHATCH_BASE_URL = https://nuthatch.lastelm.software/v2  # Optional
NUTHATCH_USE_STDIO = true  # MUST be "true" for Spaces

# OPTIONAL: Backend-provided LLM keys (users can provide their own)
# Only add if you want to provide default keys:
# OPENAI_API_KEY = sk-your-key-here
# ANTHROPIC_API_KEY = sk-ant-your-key-here
```

**Important Notes:**
- βœ… **ALWAYS** use `NUTHATCH_USE_STDIO=true` on Spaces (subprocess mode)
- βœ… HTTP mode not supported on Spaces (port binding restrictions)
- βœ… Users can provide their own LLM keys via the UI
- βœ… Environment variables from Spaces **do not** auto-inherit to subprocesses
  - The app explicitly passes `NUTHATCH_API_KEY` and `NUTHATCH_BASE_URL` to the subprocess (see `mcp_clients.py`)

#### Step 4: Verify Deployment

1. Wait for Space to build (2-5 minutes)
2. Check **Logs** tab for errors
3. Try the app - upload a bird photo or ask about species

---

## πŸ“ Project Structure

```
hackathon_draft/
β”œβ”€β”€ app.py                      # Main Gradio app
β”œβ”€β”€ upload_to_space.py          # HF Spaces upload script
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ .env.example                # Environment template
β”œβ”€β”€ langgraph_agent/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ agents.py               # Agent factory (single/multi-agent)
β”‚   β”œβ”€β”€ config.py               # Configuration loader
β”‚   β”œβ”€β”€ mcp_clients.py          # MCP client setup
β”‚   β”œβ”€β”€ subagent_config.py      # Agent mode definitions
β”‚   β”œβ”€β”€ prompts.py              # System prompts
β”‚   └── structured_output.py    # Response formatting
β”œβ”€β”€ nuthatch_tools.py           # Nuthatch MCP server
└── agent_cache.py              # Session-based agent caching
```

---

## πŸ—οΈ Architecture

### MCP Servers

**1. Modal Bird Classifier (GPU)**
- Hosted on Modal (serverless GPU)
- ResNet50 trained on 555 bird species
- Tools: `classify_from_url`, `classify_from_base64`
- Transport: Streamable HTTP

**2. Nuthatch Species Database**
- Species reference API (1000+ birds)
- Tools: `search_birds`, `get_bird_info`, `get_bird_images`, `get_bird_audio`, `search_by_family`, `filter_by_status`, `get_all_families`
- Transport: **STDIO** (subprocess on Spaces), STDIO or HTTP (local)
- Data sources: Unsplash (images), xeno-canto (audio)

### Agent Modes

**Mode 1: Specialized Subagents (3 Specialists)**
- **Router** orchestrates 3 specialized agents:
  1. **Image Identifier**: classify images, show reference photos
  2. **Species Explorer**: search by name, provide multimedia
  3. **Taxonomy Specialist**: conservation status, family search
- Each specialist has focused tool subset

**Mode 2: Audio Finder Agent**
- Single specialized agent for finding bird audio
- Tools: `search_birds`, `get_bird_info`, `get_bird_audio`
- Optimized workflow for xeno-canto recordings

### Tech Stack

- **Frontend**: Gradio 6.0 with custom CSS (cloud/sky theme)
- **Agent Framework**: LangGraph with streaming
- **MCP Integration**: FastMCP client library
- **LLM Support**: OpenAI, Anthropic, HuggingFace
- **Session Management**: In-memory agent caching
- **Output Parsing**: LlamaIndex Pydantic + regex (optimized)

---

## 🎨 Special Features

### Dual Streaming Output
- **Chat Panel**: LLM responses with markdown rendering
- **Tool Log Panel**: Real-time tool execution traces (inputs/outputs)

### Dynamic Examples
- Examples change based on selected agent mode
- Photo examples always visible
- Text examples adapt to Audio Finder vs Multi-Agent

### Structured Output
- Automatic image/audio URL extraction
- Markdown formatting for media
- xeno-canto audio links (browser-friendly)

---

## πŸ“ API Key Sources

| Service | Get Key From | Purpose |
|---------|-------------|---------|
| **Modal** | [modal.com](https://modal.com) | GPU bird classifier |
| **Nuthatch** | [nuthatch.lastelm.software](https://nuthatch.lastelm.software) | Species database |
| **OpenAI** | [platform.openai.com/api-keys](https://platform.openai.com/api-keys) | LLM (recommended) |
| **Anthropic** | [console.anthropic.com/settings/keys](https://console.anthropic.com/settings/keys) | LLM (Claude) |
| **HuggingFace** | [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) | LLM (limited support) |

---

## πŸ› Troubleshooting

### Space stuck on "Building"
- Check **Logs** tab for errors
- Verify all required secrets are set
- Try Factory Reboot (Settings β†’ Factory Reboot)

### "Invalid API key" errors
- Ensure secrets are set correctly (no quotes needed)
- Check secret names match exactly (case-sensitive)

### HuggingFace provider fails with "function calling not support"
- HuggingFace Inference API has limited tool calling
- Use OpenAI or Anthropic instead

### Nuthatch server not starting (local)
- Check `NUTHATCH_API_KEY` is set in `.env`
- Verify API key is valid
- Try STDIO mode: `NUTHATCH_USE_STDIO=true`

### Audio links broken
- Check AUDIO_FINDER_PROMPT is working
- Verify xeno-canto URLs include `/download`
- Check structured output parsing logs

---

## πŸ“š Documentation

For detailed implementation docs, see:
- `project_docs/implementation/phase_5_final.md` - Complete agent architecture
- `project_docs/commands_guide/git_spaces_cheatsheet.md` - Deployment guide

---

## πŸ† Credits

- **Bird Species Data**: [Nuthatch API](https://nuthatch.lastelm.software) by Last Elm Software
- **Bird Audio**: [xeno-canto.org](https://xeno-canto.org) - Community bird recordings
- **Reference Images**: [Unsplash](https://unsplash.com) + curated collections
- **MCP Protocol**: [Anthropic Model Context Protocol](https://github.com/anthropics/mcp)
- **Hackathon**: [HuggingFace MCP-1st-Birthday](https://huggingface.co/MCP-1st-Birthday)

---

## πŸ“„ License

MIT License - Built for educational and research purposes