Commit
ยท
09f2477
1
Parent(s):
681d5a6
README changed
Browse files
README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ๐ฎ Connect Four AI with Explainability
|
| 2 |
|
| 3 |
A fully interpretable Connect Four game AI powered by **Minimax + Alpha-Beta Pruning** with **LLM-generated natural language explanations**. Play against an intelligent opponent that explains its strategic decisions in real-time.
|
|
@@ -43,9 +55,9 @@ A fully interpretable Connect Four game AI powered by **Minimax + Alpha-Beta Pru
|
|
| 43 |
- Heuristic board evaluation (threats, board control)
|
| 44 |
- Returns move score and algorithm statistics
|
| 45 |
|
| 46 |
-
### 3. **Natural Language Explanations** (`llm_explanation.py`
|
| 47 |
- Converts algorithm output to human-readable insights
|
| 48 |
-
- Uses
|
| 49 |
- Graceful fallback if API unavailable
|
| 50 |
|
| 51 |
### 4. **Interactive UI** (`app.py`)
|
|
@@ -56,97 +68,21 @@ A fully interpretable Connect Four game AI powered by **Minimax + Alpha-Beta Pru
|
|
| 56 |
|
| 57 |
---
|
| 58 |
|
| 59 |
-
## ๐ Quick Start
|
| 60 |
|
| 61 |
### Prerequisites
|
| 62 |
- HuggingFace account (free at https://huggingface.co)
|
| 63 |
- HuggingFace API token (get it [here](https://huggingface.co/settings/tokens))
|
| 64 |
|
| 65 |
-
###
|
| 66 |
-
|
| 67 |
-
1. **Create a new Space**
|
| 68 |
-
- Go to https://huggingface.co/spaces
|
| 69 |
-
- Click "Create new Space"
|
| 70 |
-
- Choose "Streamlit" as SDK
|
| 71 |
-
- Name it `connect-four-ai`
|
| 72 |
-
|
| 73 |
-
2. **Add your HF Token as Secret**
|
| 74 |
-
- Go to Space Settings โ Secrets and variables
|
| 75 |
-
- Add: `HF_TOKEN` = your token from step above
|
| 76 |
-
- Click Save
|
| 77 |
-
|
| 78 |
-
3. **Upload Files**
|
| 79 |
-
- In Files tab, upload:
|
| 80 |
-
- `app.py`
|
| 81 |
-
- `connect_four_game.py`
|
| 82 |
-
- `minimax_ai.py`
|
| 83 |
-
- `llm_explanation.py`
|
| 84 |
-
- `requirements.txt`
|
| 85 |
-
- Create `.streamlit/` folder and upload `config.toml`
|
| 86 |
-
|
| 87 |
-
4. **Wait for Build**
|
| 88 |
-
- Space will auto-deploy (1-2 minutes)
|
| 89 |
-
- Once ready, click the app URL to play!
|
| 90 |
-
|
| 91 |
-
**That's it! Your AI is now live! ๐**
|
| 92 |
-
|
| 93 |
-
---
|
| 94 |
-
|
| 95 |
-
## ๐ป Local Installation
|
| 96 |
-
|
| 97 |
-
### Requirements
|
| 98 |
-
- Python 3.9+
|
| 99 |
-
- Ollama (for local LLM inference)
|
| 100 |
|
| 101 |
-
|
| 102 |
|
| 103 |
-
1. **
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
2. **Install Ollama**
|
| 110 |
-
- Download from https://ollama.ai
|
| 111 |
-
- Run: `ollama pull phi3:latest` (or your preferred model)
|
| 112 |
-
|
| 113 |
-
3. **Install Python dependencies**
|
| 114 |
-
```bash
|
| 115 |
-
pip install -r requirements.txt
|
| 116 |
-
```
|
| 117 |
-
|
| 118 |
-
4. **Start Ollama** (in a separate terminal)
|
| 119 |
-
```bash
|
| 120 |
-
ollama serve
|
| 121 |
-
```
|
| 122 |
-
|
| 123 |
-
5. **Run the app**
|
| 124 |
-
```bash
|
| 125 |
-
streamlit run app.py
|
| 126 |
-
```
|
| 127 |
-
|
| 128 |
-
6. **Play!**
|
| 129 |
-
- App opens at `http://localhost:8501`
|
| 130 |
-
- Select a column and click Play
|
| 131 |
-
- Watch AI analyze and explain its moves
|
| 132 |
-
|
| 133 |
-
---
|
| 134 |
-
|
| 135 |
-
## ๐ File Structure
|
| 136 |
-
|
| 137 |
-
```
|
| 138 |
-
connect-four-ai/
|
| 139 |
-
โโโ app.py # Streamlit UI (main entry point)
|
| 140 |
-
โโโ connect_four_game.py # Game engine & logic
|
| 141 |
-
โโโ minimax_ai.py # AI decision-making algorithm
|
| 142 |
-
โโโ llm_explanation.py # Move explanations (local Ollama)
|
| 143 |
-
โโโ llm_explanation_hf.py # Move explanations (HF Spaces)
|
| 144 |
-
โโโ requirements.txt # Dependencies (local)
|
| 145 |
-
โโโ requirements_hf.txt # Dependencies (HF Spaces)
|
| 146 |
-
โโโ .streamlit/
|
| 147 |
-
โ โโโ config.toml # Streamlit theme config
|
| 148 |
-
โโโ README.md # This file
|
| 149 |
-
```
|
| 150 |
|
| 151 |
---
|
| 152 |
|
|
@@ -193,17 +129,7 @@ connect-four-ai/
|
|
| 193 |
|
| 194 |
## ๐ค LLM Explanations
|
| 195 |
|
| 196 |
-
|
| 197 |
-
- Uses Phi-3 or Llama model running on your machine
|
| 198 |
-
- Fast (<1 second)
|
| 199 |
-
- No rate limits
|
| 200 |
-
- Privacy-focused (everything stays local)
|
| 201 |
-
|
| 202 |
-
### HuggingFace Spaces Version
|
| 203 |
-
- Uses Mistral-7B via HuggingFace Inference API
|
| 204 |
-
- Slightly slower (2-5 seconds)
|
| 205 |
-
- Free tier: ~1000 API calls/day
|
| 206 |
-
- Graceful fallback if rate-limited
|
| 207 |
|
| 208 |
### What Explanations Include
|
| 209 |
- **Strategic Insight**: Why this move is strong
|
|
@@ -211,84 +137,58 @@ connect-four-ai/
|
|
| 211 |
- **Board Evaluation**: How good the position is
|
| 212 |
- **Human Language**: No technical jargon
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
---
|
| 215 |
|
| 216 |
## โ๏ธ Configuration
|
| 217 |
|
| 218 |
### Adjust AI Difficulty
|
| 219 |
-
|
| 220 |
-
```python
|
| 221 |
-
search_depth = st.sidebar.slider("AI Search Depth", 3, 10, 7)
|
| 222 |
-
```
|
| 223 |
- **3-4**: Easy (quick decisions, basic strategy)
|
| 224 |
- **5-6**: Medium (balanced thinking, ~400ms)
|
| 225 |
- **7-8**: Hard (deep analysis, ~800ms)
|
| 226 |
- **9-10**: Very Hard (exhaustive search, 1-2 seconds)
|
| 227 |
|
| 228 |
-
### Change LLM Model
|
| 229 |
-
In `llm_explanation_hf.py`:
|
| 230 |
-
```python
|
| 231 |
-
self.model = 'mistralai/Mistral-7B-Instruct-v0.3' # Change this
|
| 232 |
-
```
|
| 233 |
-
Other options: `HuggingFaceH4/zephyr-7b-beta`, `meta-llama/Llama-2-7b-chat`, etc.
|
| 234 |
-
|
| 235 |
---
|
| 236 |
|
| 237 |
## ๐ง Troubleshooting
|
| 238 |
|
| 239 |
-
###
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
- Install Ollama from https://ollama.ai
|
| 243 |
-
- Make sure `ollama serve` is running in background
|
| 244 |
-
|
| 245 |
-
**"Connection refused to localhost:11434"**
|
| 246 |
-
- Start Ollama: `ollama serve`
|
| 247 |
-
- Or pull model: `ollama pull phi3:latest`
|
| 248 |
|
| 249 |
-
|
| 250 |
-
-
|
| 251 |
-
-
|
| 252 |
|
| 253 |
-
###
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
|
| 256 |
-
-
|
| 257 |
-
-
|
| 258 |
-
-
|
| 259 |
-
|
| 260 |
-
**"Rate limit exceeded"**
|
| 261 |
-
- Wait 24 hours (free tier resets daily)
|
| 262 |
-
- Upgrade to HF Pro ($9/month)
|
| 263 |
-
- Or use local Ollama instead
|
| 264 |
-
|
| 265 |
-
**Build errors**
|
| 266 |
-
- Verify all files uploaded to root directory
|
| 267 |
-
- Check `requirements.txt` has correct packages
|
| 268 |
-
- Click Restart button
|
| 269 |
|
| 270 |
---
|
| 271 |
|
| 272 |
## ๐ Performance
|
| 273 |
|
| 274 |
-
###
|
| 275 |
-
- **Move Decision**: 300-800ms (depends on
|
| 276 |
-
- **Explanation**:
|
| 277 |
-
- **Total Response**: <1 second
|
| 278 |
-
- **Uptime**: While your app is running
|
| 279 |
-
|
| 280 |
-
### HuggingFace Spaces Performance
|
| 281 |
-
- **Move Decision**: 300-800ms (identical algorithm)
|
| 282 |
-
- **Explanation**: 2-5 seconds (HF API inference)
|
| 283 |
- **Total Response**: 3-6 seconds
|
| 284 |
-
- **Uptime**: 24/7 (always available)
|
| 285 |
-
- **Rate Limit**: ~1000 calls/day (free tier)
|
| 286 |
|
| 287 |
---
|
| 288 |
|
| 289 |
## ๐ Learning Outcomes
|
| 290 |
|
| 291 |
-
Playing with this
|
| 292 |
|
| 293 |
โ
**Classical AI Algorithms**
|
| 294 |
- Minimax decision-making
|
|
@@ -300,12 +200,6 @@ Playing with this project, you'll understand:
|
|
| 300 |
- Natural language generation for AI decisions
|
| 301 |
- Bridging the gap between algorithms and humans
|
| 302 |
|
| 303 |
-
โ
**Full-Stack Development**
|
| 304 |
-
- Python backend (game logic)
|
| 305 |
-
- Streamlit web UI
|
| 306 |
-
- API integration (HF Spaces)
|
| 307 |
-
- Cloud deployment
|
| 308 |
-
|
| 309 |
โ
**Game Theory Concepts**
|
| 310 |
- Perfect information games
|
| 311 |
- Heuristic evaluation functions
|
|
@@ -313,25 +207,19 @@ Playing with this project, you'll understand:
|
|
| 313 |
|
| 314 |
---
|
| 315 |
|
| 316 |
-
##
|
| 317 |
-
|
| 318 |
-
### Option 1: Local (Recommended for Development)
|
| 319 |
-
- **Setup**: 30 minutes (Ollama + dependencies)
|
| 320 |
-
- **Speed**: Fast (<1 sec explanations)
|
| 321 |
-
- **Cost**: Free
|
| 322 |
-
- **Limit**: None
|
| 323 |
-
- **Access**: Local only
|
| 324 |
-
|
| 325 |
-
### Option 2: HuggingFace Spaces (Recommended for Sharing)
|
| 326 |
-
- **Setup**: 10 minutes (token + upload)
|
| 327 |
-
- **Speed**: Moderate (2-5 sec explanations)
|
| 328 |
-
- **Cost**: Free tier (~1000 requests/day)
|
| 329 |
-
- **Limit**: Free tier has daily limits
|
| 330 |
-
- **Access**: Global via URL
|
| 331 |
|
| 332 |
-
|
| 333 |
-
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
---
|
| 337 |
|
|
@@ -359,7 +247,7 @@ For details: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
| 359 |
- **Alpha-Beta Pruning**: Optimization technique (Knuth & Moore, 1970s)
|
| 360 |
- **Streamlit**: Web UI framework (Streamlit Inc.)
|
| 361 |
- **HuggingFace**: LLM inference & Spaces hosting
|
| 362 |
-
- **
|
| 363 |
|
| 364 |
---
|
| 365 |
|
|
@@ -377,19 +265,11 @@ For details: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
| 377 |
### Tools & Frameworks
|
| 378 |
- Streamlit: https://streamlit.io
|
| 379 |
- HuggingFace: https://huggingface.co
|
| 380 |
-
-
|
| 381 |
-
|
| 382 |
-
---
|
| 383 |
-
|
| 384 |
-
## ๐ค Support & Contact
|
| 385 |
-
|
| 386 |
-
- **Issues**: Check Troubleshooting section above
|
| 387 |
-
- **Questions**: Refer to documentation files
|
| 388 |
-
- **Feedback**: Suggestions welcome!
|
| 389 |
|
| 390 |
---
|
| 391 |
|
| 392 |
-
## ๐
|
| 393 |
|
| 394 |
This project demonstrates:
|
| 395 |
- โ
Classical AI algorithms (Minimax, Alpha-Beta Pruning)
|
|
@@ -403,4 +283,5 @@ This project demonstrates:
|
|
| 403 |
|
| 404 |
**Last Updated**: November 2025
|
| 405 |
**License**: CC BY-NC-SA 4.0
|
| 406 |
-
**Python Version**: 3.9+
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Connect Four AI with Explainability
|
| 3 |
+
emoji: ๐ฎ
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: "1.28.1"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: cc-by-nc-sa-4.0
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
# ๐ฎ Connect Four AI with Explainability
|
| 14 |
|
| 15 |
A fully interpretable Connect Four game AI powered by **Minimax + Alpha-Beta Pruning** with **LLM-generated natural language explanations**. Play against an intelligent opponent that explains its strategic decisions in real-time.
|
|
|
|
| 55 |
- Heuristic board evaluation (threats, board control)
|
| 56 |
- Returns move score and algorithm statistics
|
| 57 |
|
| 58 |
+
### 3. **Natural Language Explanations** (`llm_explanation.py`)
|
| 59 |
- Converts algorithm output to human-readable insights
|
| 60 |
+
- Uses HuggingFace Inference API (Mistral-7B LLM)
|
| 61 |
- Graceful fallback if API unavailable
|
| 62 |
|
| 63 |
### 4. **Interactive UI** (`app.py`)
|
|
|
|
| 68 |
|
| 69 |
---
|
| 70 |
|
| 71 |
+
## ๐ Quick Start
|
| 72 |
|
| 73 |
### Prerequisites
|
| 74 |
- HuggingFace account (free at https://huggingface.co)
|
| 75 |
- HuggingFace API token (get it [here](https://huggingface.co/settings/tokens))
|
| 76 |
|
| 77 |
+
### Setup (Already Running on Spaces!)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
This Space is ready to use! Just:
|
| 80 |
|
| 81 |
+
1. **Play!** Select a column (0-6) and click "Play"
|
| 82 |
+
2. **Watch AI think** - See the AI analyze and make its move
|
| 83 |
+
3. **Read explanations** - Understand why it chose that move
|
| 84 |
+
4. **Adjust difficulty** - Use the sidebar slider to change AI strength (3-10)
|
| 85 |
+
5. **New game** - Click "New Game" to restart
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
---
|
| 88 |
|
|
|
|
| 129 |
|
| 130 |
## ๐ค LLM Explanations
|
| 131 |
|
| 132 |
+
This Space uses **HuggingFace Inference API** with **Mistral-7B** to generate explanations.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
### What Explanations Include
|
| 135 |
- **Strategic Insight**: Why this move is strong
|
|
|
|
| 137 |
- **Board Evaluation**: How good the position is
|
| 138 |
- **Human Language**: No technical jargon
|
| 139 |
|
| 140 |
+
### Rate Limiting
|
| 141 |
+
- **Free tier**: ~1000 API calls/day (~100-200 game moves)
|
| 142 |
+
- **If limit hit**: Automatic fallback explanations (game still works!)
|
| 143 |
+
- **Reset**: Resets every 24 hours
|
| 144 |
+
|
| 145 |
---
|
| 146 |
|
| 147 |
## โ๏ธ Configuration
|
| 148 |
|
| 149 |
### Adjust AI Difficulty
|
| 150 |
+
Use the sidebar slider:
|
|
|
|
|
|
|
|
|
|
| 151 |
- **3-4**: Easy (quick decisions, basic strategy)
|
| 152 |
- **5-6**: Medium (balanced thinking, ~400ms)
|
| 153 |
- **7-8**: Hard (deep analysis, ~800ms)
|
| 154 |
- **9-10**: Very Hard (exhaustive search, 1-2 seconds)
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
---
|
| 157 |
|
| 158 |
## ๐ง Troubleshooting
|
| 159 |
|
| 160 |
+
### "HuggingFace Token Not Found"
|
| 161 |
+
- Contact Space owner to verify token is set
|
| 162 |
+
- Token should be added to Spaces Secrets (HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
### "Rate limit exceeded"
|
| 165 |
+
- Wait 24 hours for limit reset (free tier)
|
| 166 |
+
- Or upgrade to HF Pro for higher limits
|
| 167 |
|
| 168 |
+
### Slow responses
|
| 169 |
+
- Reduce search depth using the sidebar slider
|
| 170 |
+
- This makes AI think faster
|
| 171 |
|
| 172 |
+
### App won't load
|
| 173 |
+
- Refresh page
|
| 174 |
+
- Clear browser cache
|
| 175 |
+
- Try a different browser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
---
|
| 178 |
|
| 179 |
## ๐ Performance
|
| 180 |
|
| 181 |
+
### Response Times
|
| 182 |
+
- **Move Decision**: 300-800ms (depends on difficulty)
|
| 183 |
+
- **Explanation**: 2-5 seconds (HuggingFace API)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
- **Total Response**: 3-6 seconds
|
| 185 |
+
- **Uptime**: 24/7 (always available on Spaces)
|
|
|
|
| 186 |
|
| 187 |
---
|
| 188 |
|
| 189 |
## ๐ Learning Outcomes
|
| 190 |
|
| 191 |
+
Playing with this AI, you'll understand:
|
| 192 |
|
| 193 |
โ
**Classical AI Algorithms**
|
| 194 |
- Minimax decision-making
|
|
|
|
| 200 |
- Natural language generation for AI decisions
|
| 201 |
- Bridging the gap between algorithms and humans
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
โ
**Game Theory Concepts**
|
| 204 |
- Perfect information games
|
| 205 |
- Heuristic evaluation functions
|
|
|
|
| 207 |
|
| 208 |
---
|
| 209 |
|
| 210 |
+
## ๐ File Structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
```
|
| 213 |
+
connect-four-ai/
|
| 214 |
+
โโโ app.py # Streamlit UI (main entry point)
|
| 215 |
+
โโโ connect_four_game.py # Game engine & logic
|
| 216 |
+
โโโ minimax_ai.py # AI decision-making algorithm
|
| 217 |
+
โโโ llm_explanation.py # Move explanations (HF API)
|
| 218 |
+
โโโ requirements.txt # Dependencies
|
| 219 |
+
โโโ .streamlit/
|
| 220 |
+
โ โโโ config.toml # Streamlit theme config
|
| 221 |
+
โโโ README.md # This file
|
| 222 |
+
```
|
| 223 |
|
| 224 |
---
|
| 225 |
|
|
|
|
| 247 |
- **Alpha-Beta Pruning**: Optimization technique (Knuth & Moore, 1970s)
|
| 248 |
- **Streamlit**: Web UI framework (Streamlit Inc.)
|
| 249 |
- **HuggingFace**: LLM inference & Spaces hosting
|
| 250 |
+
- **Mistral AI**: Language model provider
|
| 251 |
|
| 252 |
---
|
| 253 |
|
|
|
|
| 265 |
### Tools & Frameworks
|
| 266 |
- Streamlit: https://streamlit.io
|
| 267 |
- HuggingFace: https://huggingface.co
|
| 268 |
+
- HuggingFace Spaces: https://huggingface.co/docs/hub/spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
---
|
| 271 |
|
| 272 |
+
## ๐ Have Fun!
|
| 273 |
|
| 274 |
This project demonstrates:
|
| 275 |
- โ
Classical AI algorithms (Minimax, Alpha-Beta Pruning)
|
|
|
|
| 283 |
|
| 284 |
**Last Updated**: November 2025
|
| 285 |
**License**: CC BY-NC-SA 4.0
|
| 286 |
+
**Python Version**: 3.9+
|
| 287 |
+
**Framework**: Streamlit 1.28.1+
|