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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Deploying GAIA on Hugging Face Spaces

This guide provides step-by-step instructions for deploying the GAIA agent on Hugging Face Spaces, making it accessible as a web application to users worldwide.

## Why Deploy on Hugging Face Spaces?

Hugging Face Spaces offers several advantages for deploying GAIA:

1. **Free Hosting**: Basic deployment is free with reasonable usage limits
2. **Easy Sharing**: Public URL that can be shared with anyone
3. **Version Control**: Built-in Git integration
4. **Secrets Management**: Secure storage for API keys
5. **Community**: Integration with the broader AI community
6. **Customization**: Support for custom domains and branding

## Prerequisites

Before deploying to Hugging Face Spaces, you'll need:

1. **Hugging Face Account**: Create an account at [huggingface.co](https://huggingface.co/join)
2. **API Keys**: Gather all necessary API keys (OpenAI, Serper, etc.)
3. **GAIA Repository**: A local copy of the GAIA repository
4. **Git**: For pushing your code to Hugging Face

## Deployment Steps

### Step 1: Prepare Your Repository

1. Clone the GAIA repository if you haven't already:

```bash
git clone https://github.com/your-organization/gaia.git
cd gaia
```

2. Create a specific `requirements.txt` file for Hugging Face deployment:

```bash
# Copy the main requirements
cp requirements.txt requirements-hf.txt

# Edit to add Gradio (if not already included)
echo "gradio>=4.0.0" >> requirements-hf.txt

# Remove any development-specific packages
# Edit requirements-hf.txt to remove unnecessary packages
```

3. Create a Hugging Face-specific `app.py` or modify the existing one:

```python
import os
import gradio as gr
from src.gaia.agent import GaiaAgent
from src.gaia.config import Configuration

# Initialize configuration
config = Configuration()

# Set default values for Hugging Face deployment
config.set("demo_mode", True)
config.set("models.default", "gpt-3.5-turbo")  # Use cheaper model by default

# Initialize the agent
agent = GaiaAgent(config=config)

# Define the Gradio interface
def process_query(query, history):
    try:
        response = agent.run(query)
        return response
    except Exception as e:
        return f"Error: {str(e)}"

# Create the gradio app
demo = gr.ChatInterface(
    fn=process_query,
    title="GAIA - Grounded AI Alignment Agent",
    description="Ask any question and GAIA will search for information to provide a grounded answer.",
    examples=[
        "What is quantum computing?",
        "Explain the theory of relativity in simple terms.",
        "What are the latest developments in AI safety?"
    ],
    theme="huggingface"
)

# Launch the app
if __name__ == "__main__":
    demo.launch()
```

### Step 2: Create a Space on Hugging Face

1. Log in to Hugging Face and go to [huggingface.co/spaces](https://huggingface.co/spaces)
2. Click on "Create new Space"
3. Fill in the details:
   - **Owner**: Your username or organization
   - **Space name**: Choose a unique name (e.g., "gaia-agent")
   - **License**: Choose an appropriate license (e.g., MIT)
   - **SDK**: Choose "Gradio"
   - **Space hardware**: Start with "CPU basic" (free tier)
   - **Make this Space private**: Optional, if you want to restrict access

4. Click "Create Space"

### Step 3: Configure the Repository

1. In your local GAIA directory, add the Hugging Face Space as a remote:

```bash
git remote add space https://huggingface.co/spaces/your-username/gaia-agent
```

2. Create a `.gitignore` file to exclude unnecessary files:

```bash
cat > .gitignore << EOF
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Virtual Environment
venv/
ENV/
env/

# Local configuration
.env
config.json

# Logs
logs/
*.log

# Results
results/

# IDE files
.idea/
.vscode/
*.swp
*.swo
EOF
```

3. Create a `README.md` file for your Space:

```bash
cat > README.md << EOF
# GAIA Agent

GAIA (Grounded AI Alignment) is an AI agent designed to answer questions with grounded, factual information.

## Features

- Web search capabilities using multiple providers
- Academic research integration
- Reasoning tools for complex problems
- Memory system for context-aware responses

## Usage

Simply type your question in the input box and click "Submit" to get a response from GAIA.

## Examples

- "What is quantum computing?"
- "Explain the theory of relativity in simple terms."
- "What are the latest developments in AI safety?"

## About

GAIA is built with Python using LangChain, LangGraph, and GPT-4. It leverages various APIs to provide accurate and up-to-date information.
EOF
```

### Step 4: Create a Requirements File for Hugging Face

Create or modify the `requirements.txt` file to include only the necessary packages:

```bash
cat > requirements.txt << EOF
gradio>=4.0.0
langchain>=0.0.267
langgraph>=0.0.15
openai>=1.1.1
tiktoken>=0.5.1
supabase>=2.0.3
requests>=2.31.0
python-dotenv>=1.0.0
EOF
```

### Step 5: Set Up Environment Variables

In your Hugging Face Space:

1. Go to the Settings tab of your Space
2. Scroll down to the "Repository secrets" section
3. Add your API keys and configuration as secrets:
   - `OPENAI_API_KEY`: Your OpenAI API key
   - `SERPER_API_KEY`: Your Serper API key (if used)
   - `PERPLEXITY_API_KEY`: Your Perplexity API key (if used)
   - Any other necessary API keys or configuration values

### Step 6: Push Your Code to Hugging Face

Commit and push your code to the Hugging Face Space:

```bash
# Add your files
git add app.py requirements.txt README.md .gitignore src/

# Commit the changes
git commit -m "Initial GAIA deployment on Hugging Face"

# Push to Hugging Face
git push space main
```

After pushing, Hugging Face will automatically build and deploy your application. This may take a few minutes.

### Step 7: Test Your Deployment

1. Once the build is complete, navigate to your Space's URL: `https://huggingface.co/spaces/your-username/gaia-agent`
2. Test the application by asking a few questions
3. Check the Space logs for any errors or issues:
   - Go to the "Settings" tab
   - Scroll down to "Factory reboot" section
   - Click on "View logs"

## Advanced Configuration

### Custom Domain

To use a custom domain with your Space:

1. Go to the "Settings" tab of your Space
2. Scroll down to the "Custom domain" section
3. Enter your domain name (e.g., `gaia.yourdomain.com`)
4. Follow the instructions to set up DNS records

### Upgrading Hardware

For better performance or to handle more traffic:

1. Go to the "Settings" tab of your Space
2. Scroll down to the "Space hardware" section
3. Choose a higher tier (note: this will incur costs)
   - CPU Upgrade: For faster processing
   - GPU: For model hosting or intensive processing
   - Memory Boost: For handling larger datasets

### Persistent Storage

To enable persistent storage for your Space:

1. Go to the "Settings" tab of your Space
2. Scroll down to the "Persistent storage" section
3. Enable persistent storage (up to 10GB for free)

This allows you to store data that persists between restarts, such as logs or cached results.

### Authentication

To restrict access to authenticated users:

1. In your `app.py`, modify the launch parameters:

```python
demo.launch(auth=("username", "password"))
```

2. Or for more flexible authentication:

```python
demo.launch(auth_message="Enter GAIA password",
            auth=lambda u, p: p == os.environ.get("GAIA_PASSWORD", "default_password"))
```

3. Add the `GAIA_PASSWORD` environment variable in your Space settings

### Scheduled Restarts

For long-running deployments, scheduled restarts can help maintain stability:

1. Go to the "Settings" tab of your Space
2. Scroll down to the "Factory reboot" section
3. Set a schedule for automatic reboots (e.g., daily or weekly)

## Optimizing for Hugging Face Spaces

### Reducing Startup Time

To reduce startup time and improve user experience:

1. Lazy-load components when possible:

```python
def load_agent():
    if not hasattr(load_agent, "agent"):
        load_agent.agent = GaiaAgent(config=config)
    return load_agent.agent

def process_query(query, history):
    agent = load_agent()
    return agent.run(query)
```

2. Use caching for expensive operations:

```python
import functools

@functools.lru_cache(maxsize=100)
def get_cached_result(query):
    # Expensive operation
    return result
```

### Memory Usage Optimization

To stay within Hugging Face's memory limits:

1. Limit model usage:

```python
config.set("models.default", "gpt-3.5-turbo")  # Uses less memory than GPT-4
```

2. Implement efficient memory management:

```python
# Clear working memory after each query
def process_query(query, history):
    agent = load_agent()
    result = agent.run(query)
    agent.reset(clear_memory=False)  # Clear working memory but keep conversation history
    return result
```

3. Disable memory-intensive features in the configuration:

```python
config.set("memory.supabase.enabled", False)  # Use simpler memory system
config.set("tools.image_analysis.enabled", False)  # Disable memory-intensive tools
```

## Monitoring and Maintenance

### Monitoring Usage

Monitor your Space's usage and performance:

1. Go to the "Settings" tab of your Space
2. Scroll down to the "Metrics" section
3. View CPU, memory, and disk usage over time

### Updating Your Deployment

To update your GAIA deployment:

1. Make changes to your local repository
2. Commit and push to your Hugging Face Space:

```bash
git add .
git commit -m "Update GAIA deployment"
git push space main
```

### Handling Errors

If you encounter errors in your deployment:

1. Check the Space logs for error messages
2. Implement better error handling in your code:

```python
def process_query(query, history):
    try:
        agent = load_agent()
        return agent.run(query)
    except Exception as e:
        # Log the error
        print(f"Error processing query: {str(e)}")
        # Return a user-friendly message
        return "I'm having trouble processing your request. Please try again or ask a different question."
```

3. Set up monitoring to be notified of errors

## Conclusion

You now have GAIA deployed on Hugging Face Spaces, making it accessible as a web application. This allows you to share your agent with others, collaborate with the community, and benefit from Hugging Face's infrastructure.

For more advanced deployments or custom integrations, consider exploring:

- [Local Deployment Guide](local.md) for self-hosting options
- [API Documentation](../api/agent.md) for programmatic integrations
- [Hugging Face Spaces Documentation](https://huggingface.co/docs/hub/spaces) for more Spaces features

If you encounter any issues or have questions, refer to the [troubleshooting section](#handling-errors) or create an issue on the project's GitHub repository.