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
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
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
423
424
425
426
427
428
429
430
431
432
---
title: Web3 Research Co-Pilot
emoji: πŸš€
colorFrom: blue
colorTo: purple
sdk: docker
sdk_version: "latest"
app_file: app.py
pinned: false
---

# πŸš€ Web3 Research Co-Pilot

AI-powered cryptocurrency research assistant with real-time market data and professional analysis.

**🌐 Live Demo**: https://archcoder-web3-copilot.hf.space

## ⚑ Quick Start

### 1. Get API Key
Get your **Google Gemini API key** from [Google AI Studio](https://aistudio.google.com/)

### 2. Run Locally
```bash
git clone https://github.com/Transcendental-Programmer/web3-research-agent.git
cd web3-research-agent
pip install -r requirements.txt

# Create .env file
echo "GEMINI_API_KEY=your_api_key_here" > .env

# Run the app
python app.py
```

### 3. Access
Open http://localhost:7860 in your browser

## πŸ”§ Optional Setup

Add these to your `.env` for enhanced features:
```bash
COINGECKO_API_KEY=your_coingecko_key    # Higher rate limits
ETHERSCAN_API_KEY=your_etherscan_key    # Blockchain data
```

## 🐳 Deploy to HuggingFace Spaces

1. Fork this repo
2. Create a new [HuggingFace Space](https://huggingface.co/spaces) with Docker SDK
3. Set environment variables in Space settings
4. Connect your repo and deploy

## ✨ Features

- **AI Analysis**: Google Gemini-powered insights
- **Live Data**: CoinGecko, DeFiLlama, Etherscan APIs  
- **Visualizations**: Interactive charts and graphs
- **Professional UI**: Clean, responsive interface
- **Multi-chain**: Ethereum, DeFi, Layer 2 support

## 🎯 Usage Examples

- "What's Bitcoin's price and market sentiment?"
- "Show me top DeFi protocols by TVL"
- "Analyze Ethereum gas prices"
- "Compare BTC vs ETH performance"

## οΏ½ Troubleshooting

**Can't access localhost:7860?**
- Check if app is running: `ps aux | grep "python app.py"`
- Try your local IP: `hostname -I` then http://YOUR_IP:7860
- In cloud environments, use provided forwarded URL

**API errors?**
- Verify GEMINI_API_KEY in .env file
- Check API key permissions

## πŸ“ Project Structure

```
web3-research-agent/
β”œβ”€β”€ app.py              # Main FastAPI app
β”œβ”€β”€ requirements.txt    # Dependencies
β”œβ”€β”€ Dockerfile         # Container config
β”œβ”€β”€ test_suite.py      # Tests
└── src/               # Source code
    β”œβ”€β”€ research_agent.py
    β”œβ”€β”€ visualizations.py
    └── ...
```

---

**Built for the Web3 community** ❀️
   pip install -r requirements.txt
   ```

3. **Set up environment variables**
   
   Create a `.env` file in the project root:
   ```bash
   cp .env.example .env
   ```
   
   Edit `.env` with your API keys:
   ```properties
   # Required
   GEMINI_API_KEY=your_gemini_api_key_here
   
   # Optional (for enhanced functionality)
   COINGECKO_API_KEY=your_coingecko_api_key_here
   ETHERSCAN_API_KEY=your_etherscan_api_key_here
   ```

4. **Run the application**
   ```bash
   python app.py
   ```

5. **Access the interface**
   - **Local Development**: http://localhost:7860
   - **Production/Cloud**: Check your hosting platform's port forwarding
   - **Docker**: http://localhost:7860

## πŸš€ HuggingFace Spaces Deployment

This project is configured for HuggingFace Spaces deployment with Docker SDK.

### Quick Deploy to HF Spaces

1. **Fork/Clone this repository**
2. **Create a new HuggingFace Space**:
   - Go to [HuggingFace Spaces](https://huggingface.co/spaces)
   - Click "Create new Space"
   - Choose "Docker" as the SDK
   - Upload your repository files

3. **Configure Environment Variables**:
   In your Space settings, add:
   ```
   GEMINI_API_KEY=your_gemini_api_key
   COINGECKO_API_KEY=your_coingecko_api_key  
   ETHERSCAN_API_KEY=your_etherscan_api_key
   ```

4. **Deploy via Git**:
   ```bash
   git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
   git push hf main
   ```

### HF Spaces Configuration

The project includes a proper `README.md` header for HF Spaces:
```yaml
title: Web3 Research Co-Pilot
emoji: πŸš€
colorFrom: blue
colorTo: purple
sdk: docker
app_file: app.py
```

## 🐳 Docker Deployment

### Local Docker

```bash
# Build the image
docker build -t web3-research-copilot .

# Run the container
docker run -p 7860:7860 \
  -e GEMINI_API_KEY=your_key_here \
  -e COINGECKO_API_KEY=your_key_here \
  -e ETHERSCAN_API_KEY=your_key_here \
  web3-research-copilot
```

### HuggingFace Spaces

This project is optimized for HuggingFace Spaces deployment:

1. **Create a new Space**:
   - Go to [HuggingFace Spaces](https://huggingface.co/spaces)
   - Click "Create new Space"
   - Choose "Docker" as the SDK
   - Set visibility to "Public" or "Private"

2. **Configure Environment Variables**:
   In your Space settings, add:
   ```
   GEMINI_API_KEY=your_gemini_api_key
   COINGECKO_API_KEY=your_coingecko_api_key  
   ETHERSCAN_API_KEY=your_etherscan_api_key
   ```

3. **Deploy**:
   ```bash
   git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
   git push hf main
   ```

**Live Example**: https://archcoder-web3-copilot.hf.space

## πŸ”„ Development Setup

### Adding HuggingFace Remote

To deploy to your HuggingFace Space:

```bash
# Add HuggingFace remote
git remote add hf https://huggingface.co/spaces/ArchCoder/web3-copilot

# Or for your own space
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME

# Deploy to HF Spaces
git push hf main
```

## πŸ“Š Testing

Run the comprehensive test suite:

```bash
python test_suite.py
```

Expected output:
```
πŸš€ Web3 Research Co-Pilot - Test Suite
==================================================
βœ… All imports successful
βœ… Configuration validated
βœ… Visualizations working
βœ… Tools initialized
βœ… Service functional
βœ… API endpoints healthy
βœ… Performance acceptable

Tests passed: 7/7
Success rate: 100.0%
πŸŽ‰ All tests passed!
```

## πŸ—οΈ Project Structure

```
web3-research-agent/
β”œβ”€β”€ app.py                      # Main FastAPI application
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ pyproject.toml             # Project configuration
β”œβ”€β”€ uv.lock                    # Dependency lock file
β”œβ”€β”€ test_suite.py              # Comprehensive test suite
β”œβ”€β”€ .env.example               # Environment template
β”œβ”€β”€ src/                       # Source code
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ research_agent.py      # Main agent logic
β”‚   β”œβ”€β”€ enhanced_agent.py      # Enhanced agent features
β”‚   β”œβ”€β”€ config.py              # Configuration management
β”‚   β”œβ”€β”€ cache_manager.py       # Response caching
β”‚   β”œβ”€β”€ api_clients.py         # External API clients
β”‚   β”œβ”€β”€ defillama_client.py    # DeFiLlama integration
β”‚   β”œβ”€β”€ news_aggregator.py     # News and social data
β”‚   β”œβ”€β”€ portfolio_analyzer.py  # Portfolio analysis
β”‚   └── visualizations.py      # Chart generation
└── README.md                  # This file
```

## πŸ”§ Configuration

### Required Environment Variables

| Variable | Description | Required |
|----------|-------------|----------|
| `GEMINI_API_KEY` | Google Gemini API key for AI analysis | βœ… |

### Optional Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `COINGECKO_API_KEY` | CoinGecko API key for enhanced rate limits | None |
| `ETHERSCAN_API_KEY` | Etherscan API key for blockchain data | None |
| `AIRAA_WEBHOOK_URL` | AIRAA integration webhook URL | None |
| `AIRAA_API_KEY` | AIRAA API authentication key | None |

### Getting API Keys

1. **Google Gemini API**: 
   - Go to [Google AI Studio](https://aistudio.google.com/)
   - Create a new API key
   - Copy the key to your `.env` file

2. **CoinGecko API** (optional):
   - Sign up at [CoinGecko](https://www.coingecko.com/api)
   - Get your free API key
   - Provides higher rate limits

3. **Etherscan API** (optional):
   - Register at [Etherscan](https://etherscan.io/apis)
   - Create a free API key
   - Enables blockchain data queries

## 🎯 Usage Examples

### Market Analysis
```
"Analyze Bitcoin price trends and institutional adoption patterns"
```

### DeFi Research
```
"Compare top DeFi protocols by TVL, yield, and risk metrics"
```

### Layer 2 Analysis
```
"Evaluate Ethereum Layer 2 scaling solutions and adoption metrics"
```

### Yield Optimization
```
"Identify optimal yield farming strategies across multiple chains"
```

## 🌐 API Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/` | GET | Web interface |
| `/status` | GET | System status and configuration |
| `/query` | POST | Process research queries |
| `/health` | GET | Health check |

### Query API Example

```bash
curl -X POST "http://localhost:7860/query" \
  -H "Content-Type: application/json" \
  -d '{"query": "What is the current Bitcoin price?"}'
```

## πŸ› Troubleshooting

### Port Access Issues

**Problem**: Can't access the app on http://localhost:7860 or http://0.0.0.0:7860

**Solutions**:

1. **Check if the app is running**:
   ```bash
   ps aux | grep "python app.py"
   ```

2. **Verify port binding**:
   ```bash
   netstat -tlnp | grep :7860
   ```

3. **For Development Environments (VS Code, etc.)**:
   - Look for port forwarding notifications
   - Check your IDE's "Ports" or "Forwarded Ports" tab
   - Use the forwarded URL provided by your development environment

4. **For Cloud/Remote Environments**:
   - The app binds to `0.0.0.0:7860` for external access
   - Use your platform's provided URL (not localhost)
   - Check firewall rules if on a VPS/server

5. **Local Network Access**:
   ```bash
   # Find your local IP
   hostname -I
   # Access via: http://YOUR_IP:7860
   ```

### Common Issues

1. **"GEMINI_API_KEY not configured"**
   - Ensure you've set the API key in your `.env` file
   - Verify the key is valid and has proper permissions

2. **"Connection refused" on port 7860**
   - Check if another process is using port 7860: `lsof -i :7860`
   - Ensure the app started successfully: `python app.py`

3. **Import errors**
   - Ensure all dependencies are installed: `pip install -r requirements.txt`
   - Check Python version compatibility (3.11+)

4. **Slow visualization loading**
   - Check your internet connection
   - API rate limits may be affecting data retrieval

### Getting Help

- Check the test suite: `python test_suite.py`
- Review logs in the terminal output
- Verify API keys are configured correctly
- Open an issue on GitHub with error details

## 🀝 Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Run the test suite
6. Submit a pull request

## πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

## πŸ™ Acknowledgments

- **Google Gemini** for AI capabilities
- **CoinGecko** for comprehensive market data
- **DeFiLlama** for DeFi protocol analytics
- **Etherscan** for blockchain data
- **FastAPI** for the web framework
- **Plotly** for interactive visualizations

---

**Built with ❀️ for the Web3 community**