Spaces:
Sleeping
Sleeping
lbtwyk
commited on
Commit
·
fed1ca7
1
Parent(s):
09545e1
Update README to focus on RL training pipeline
Browse files- .gitignore +2 -1
- README.md +140 -443
- RL/infer_battleground_cloud.py +96 -20
.gitignore
CHANGED
|
@@ -2,4 +2,5 @@ battleground_*
|
|
| 2 |
models
|
| 3 |
RL/datasets/
|
| 4 |
AGENT*
|
| 5 |
-
__pycache__
|
|
|
|
|
|
| 2 |
models
|
| 3 |
RL/datasets/
|
| 4 |
AGENT*
|
| 5 |
+
__pycache__
|
| 6 |
+
.*
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: DeepBattler
|
| 3 |
emoji: 🧠
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
|
@@ -7,487 +7,184 @@ sdk: docker
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
---
|
| 12 |
-
# DeepBattler - Your BEST LLM Battlegrounds Coach/Friend!🍻🍻 <a id="english"></a>
|
| 13 |
-
|
| 14 |
-
**[English](#english)** | **[中文](#chinese)** | **[日本語](#japanese)**
|
| 15 |
-
|
| 16 |
-
### Well met, hero! I'm DeepBattler, the tavern master who brews brilliant plays, belly laughs, and more pep than a dancing Murloc on espresso! 🍻🐟
|
| 17 |
-
|
| 18 |
-
DeepBattler, a LLM-Driven Hearthstone Battlegrounds enthusiast like us. DeepBattler seamlessly integrates with the Hearthstone Deck Tracker (HDT) plugin to provide you with **real-time strategic advice**. Whether you're aiming to climb the ranks or just improve your game experience, DeepBattler has got your back!
|
| 19 |
-
|
| 20 |
-
DeepBattler's strength can match that of the **top 0.1% players on EU servers (8K ELO)**, thanks to its insightful, voice-assisted guidance that helps you make the best decisions on the fly. Let’s take your gameplay to the next level!
|
| 21 |
-
|
| 22 |
-
**Demos can be found here! [YouTube Link](https://www.youtube.com/watch?v=A9XKPx1COfc&t=66s)**
|
| 23 |
-
|
| 24 |
-
## System Components
|
| 25 |
-
|
| 26 |
-
### 1. Hearthstone Deck Tracker (HDT) Plugin
|
| 27 |
-
- **Real-Time Monitoring:** Keeps track of your game state as it happens
|
| 28 |
-
- **JSON Outputs:** Provides clear, structured data
|
| 29 |
-
- **Efficient Data Handling:** Ensures smooth performance
|
| 30 |
-
- **In-Depth Insights:** Offers comprehensive analysis of your gameplay
|
| 31 |
-
|
| 32 |
-
### 2. LLM-Powered Python Agent
|
| 33 |
-
- **Advanced Analysis:** Utilizes powerful language model capabilities
|
| 34 |
-
- **Strategic Advice:** Gives you real-time tactical recommendations
|
| 35 |
-
- **Voice Communication:** Interact naturally with voice commands
|
| 36 |
-
- **Adaptive Decisions:** Adjusts strategies based on different game scenarios
|
| 37 |
-
|
| 38 |
-
## Setup and Configuration
|
| 39 |
-
|
| 40 |
-
### Plugin Setup
|
| 41 |
-
1. **Open the `DeepBattlerPlugin/DeepBattlerPlugin.csproj` file.**
|
| 42 |
-
- Instead of modifying individual class files, ensure your project references are correctly set up in the `.csproj` file.
|
| 43 |
-
|
| 44 |
-
2. **Add Dependencies:**
|
| 45 |
-
To ensure **DeepBattlerPlugin** functions correctly, you only need to add the following two dependencies:
|
| 46 |
-
|
| 47 |
-
1. **HearthDb.dll**
|
| 48 |
-
2. **HearthstoneDeckTracker.exe**
|
| 49 |
-
|
| 50 |
-
#### Adding Dependencies to Your Visual Studio Project
|
| 51 |
-
|
| 52 |
-
Follow these steps to add the two dependencies to your Visual Studio project:
|
| 53 |
-
|
| 54 |
-
1. **Open Your Project**
|
| 55 |
-
- Open your plugin project in Visual Studio (e.g., `DeepBattlerPlugin`).
|
| 56 |
-
|
| 57 |
-
2. **Add References**
|
| 58 |
-
- Right-click on the project name and select **"Add"** > **"Reference..."**.
|
| 59 |
-
|
| 60 |
-
3. **Browse and Select Dependencies**
|
| 61 |
-
- In the **"Reference Manager"** window, select the **"Browse"** tab.
|
| 62 |
-
- Click the **"Browse"** button and navigate to the directory containing `HearthDb.dll` and `HearthstoneDeckTracker.exe`.
|
| 63 |
-
- **HearthDb.dll**: Typically located in the HDT installation directory.
|
| 64 |
-
- **HearthstoneDeckTracker.exe**: Also located in the HDT installation directory.
|
| 65 |
-
- Select both files and click **"Add"**.
|
| 66 |
-
|
| 67 |
-
4. **Confirm Addition**
|
| 68 |
-
- After adding, click **"OK"** to confirm the references.
|
| 69 |
-
|
| 70 |
-
#### Setting "Copy Local" Property (Optional)
|
| 71 |
-
|
| 72 |
-
To ensure these dependencies are copied to the output directory during the build process, set their **"Copy Local"** property to **"True"**:
|
| 73 |
-
|
| 74 |
-
1. **Expand References**
|
| 75 |
-
- In the **"Solution Explorer"**, expand the **"References"** node.
|
| 76 |
-
|
| 77 |
-
2. **Set Properties**
|
| 78 |
-
- Select the recently added `HearthDb.dll` and `HearthstoneDeckTracker.exe` references.
|
| 79 |
-
- Right-click each reference and choose **"Properties"**.
|
| 80 |
-
- In the **Properties** window, set **"Copy Local"** to **"True"**.
|
| 81 |
-
|
| 82 |
-
#### Important Notes
|
| 83 |
-
|
| 84 |
-
- **Compatibility**: Ensure that the versions of `HearthDb.dll` and `HearthstoneDeckTracker.exe` you are using are compatible with your current version of **Hearthstone Deck Tracker (HDT)** to avoid potential compatibility issues.
|
| 85 |
-
- **Plugin Directory**: After completing the above steps, make sure to place the compiled `DeepBattlerPlugin.dll` into HDT's `Plugins` folder so that HDT can correctly load your plugin.
|
| 86 |
-
|
| 87 |
-
3. **Configure the Plugin Path**
|
| 88 |
-
- Open the `DeepBattlerPlugin/DeepBattlerPlugin.csproj` file.
|
| 89 |
-
- Set the `_path` variable to your absolute game state file path:
|
| 90 |
-
```csharp
|
| 91 |
-
private readonly string _path = @"C:\Your\Absolute\Path\To\game_state.json";
|
| 92 |
-
```
|
| 93 |
-
|
| 94 |
-
4. **Build the Plugin**
|
| 95 |
-
- Build the plugin. The compiled `DeepBattlerPlugin.dll` will be located under `DeepBattlerPlugin/bin/Debug`.
|
| 96 |
-
|
| 97 |
-
5. **Install the Plugin in HDT**
|
| 98 |
-
1. Open Hearthstone Deck Tracker (HDT).
|
| 99 |
-
2. Copy the plugin files to the HDT plugins directory:
|
| 100 |
-
- Default location: `%AppData%\Hearthstone Deck Tracker\Plugins`
|
| 101 |
-
3. Launch Hearthstone Deck Tracker.
|
| 102 |
-
4. Enable the plugin in HDT under `Options -> Plugins`.
|
| 103 |
-
|
| 104 |
-

|
| 105 |
-
|
| 106 |
-
### LLM Agent Setup
|
| 107 |
-
|
| 108 |
-
#### Using OpenAI GPT
|
| 109 |
-
1. **Install the required Python packages:**
|
| 110 |
-
```bash
|
| 111 |
-
pip install openai playsound==1.2.2
|
| 112 |
-
```
|
| 113 |
-
*Note: Version 1.2.2 of `playsound` is required for compatibility.*
|
| 114 |
-
|
| 115 |
-
2. **Add your OpenAI API key in `Openai_caller.py`:**
|
| 116 |
-
```python
|
| 117 |
-
api_key = "your-openai-api-key-here"
|
| 118 |
-
```
|
| 119 |
-
|
| 120 |
-
3. **Launch the LLM agent:**
|
| 121 |
-
```bash
|
| 122 |
-
python Openai_caller.py
|
| 123 |
-
```
|
| 124 |
-
|
| 125 |
-
---
|
| 126 |
-
|
| 127 |
-
#### Using Google Gemma
|
| 128 |
-
1. **Install the required Python packages:**
|
| 129 |
-
```bash
|
| 130 |
-
pip install keras_hub jax keras gtts playsound==1.2.2
|
| 131 |
-
```
|
| 132 |
-
*Note: Version 1.2.2 of `playsound` is required for compatibility.*
|
| 133 |
-
|
| 134 |
-
2. **Set up the Gemma environment:**
|
| 135 |
-
Your script (`Gemma_caller.py`) includes the following environment configurations:
|
| 136 |
-
```python
|
| 137 |
-
import os
|
| 138 |
-
os.environ["KERAS_BACKEND"] = "jax"
|
| 139 |
-
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "1.00"
|
| 140 |
-
```
|
| 141 |
-
|
| 142 |
-
3. **Prepare the necessary files:**
|
| 143 |
-
- `game_state.json`: A JSON file to provide the current game state.
|
| 144 |
-
- `Prompt.txt`: A text file containing the system prompt for Gemma.
|
| 145 |
|
| 146 |
-
|
| 147 |
-
```bash
|
| 148 |
-
python Gemma_caller.py
|
| 149 |
-
```
|
| 150 |
|
| 151 |
-
##
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
Feel free to use and tweak the software, but don’t sell or distribute it commercially without permission.
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
|
| 177 |
-
|
| 178 |
-
[2024]
|
| 179 |
|
| 180 |
-
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
### 英雄,好久不见!我是DeepBattler——一位既能端出妙计良策,又能端出热茶闲聊的酒馆掌柜,嘴皮子比鱼人还溜,招式比醉拳还灵!🍵🐟
|
| 187 |
-
|
| 188 |
-
DeepBattler,是一款专为《炉石传说》酒馆战棋打造的先进助手。由大语言模型(LLM)驱动,集成了海量的游戏数据和随从选择分析,作者也提供了开放的串口,让你可以非常轻易地修改并添加你的偏好。DeepBattler无缝集成《炉石传说》卡组跟踪器(HDT)插件,为你提供**实时战略建议**。无论你是想提升排名还是改善游戏技巧,DeepBattler都能助你一臂之力!
|
| 189 |
-
|
| 190 |
-
DeepBattler的实力可以匹敌**欧服排名前0.1%的玩家**,得益于其深入的语音辅助指导,帮助你在关键时刻做出最佳决策。让我们一起提升你的游戏水平吧!
|
| 191 |
-
|
| 192 |
-
## 系统组件
|
| 193 |
-
|
| 194 |
-
### 1. 《炉石传说》卡组跟踪器(HDT)插件
|
| 195 |
-
- **实时监控:** 实时跟踪你的游戏状态
|
| 196 |
-
- **JSON输出:** 提供清晰、结构化的数据
|
| 197 |
-
- **高效数据处理:** 确保流畅运行
|
| 198 |
-
- **深入洞察:** 提供全面的游戏分析
|
| 199 |
-
|
| 200 |
-
### 2. LLM驱动的Python代理
|
| 201 |
-
- **高级分析:** 利用强大的语言模型功能
|
| 202 |
-
- **战略建议:** 提供实时战术建议
|
| 203 |
-
- **语音通信:** 自然的语音交互
|
| 204 |
-
- **自适应决策:** 根据不同游戏情境调整策略
|
| 205 |
-
|
| 206 |
-
## 设置与配置
|
| 207 |
-
|
| 208 |
-
### 插件设置
|
| 209 |
-
1. **打开 `DeepBattlerPlugin/DeepBattlerPlugin.csproj` 文件。**
|
| 210 |
-
- 不再修改单个类文件,而是确保项目引用在 `.csproj` 文件中正确设置。
|
| 211 |
-
|
| 212 |
-
2. **添加依赖项:**
|
| 213 |
-
为了确保 **DeepBattlerPlugin** 正常运行,您仅需添加以下两个依赖项:
|
| 214 |
-
|
| 215 |
-
1. **HearthDb.dll**
|
| 216 |
-
2. **HearthstoneDeckTracker.exe**
|
| 217 |
-
|
| 218 |
-
#### 将依赖项添加到 Visual Studio 项目
|
| 219 |
-
|
| 220 |
-
请按照以下步骤在 Visual Studio 中添加这两个依赖项:
|
| 221 |
-
|
| 222 |
-
1. **打开您的项目**
|
| 223 |
-
- 在 Visual Studio 中打开您的插件项目(例如,DeepBattlerPlugin)。
|
| 224 |
-
|
| 225 |
-
2. **添加引用**
|
| 226 |
-
- 右键点击项目名称,选择 **“添加”** > **“引用...”**。
|
| 227 |
-
|
| 228 |
-
3. **浏览并选择依赖项**
|
| 229 |
-
- 在弹出的 **“引用管理器”** 窗口中,选择 **“浏览”** 选项卡。
|
| 230 |
-
- 点击 **“浏览”** 按钮,导航到包含 `HearthDb.dll` 和 `HearthstoneDeckTracker.exe` 的目录。
|
| 231 |
-
- **HearthDb.dll**:通常位于 HDT 的安装目录下。
|
| 232 |
-
- **HearthstoneDeckTracker.exe**:同样位于 HDT 的安装目录中。
|
| 233 |
-
- 选择这两个文件后,点击 **“添加”**。
|
| 234 |
-
|
| 235 |
-
4. **确认添加**
|
| 236 |
-
- 添加完毕后,点击 **“确定”** 以确认引用。
|
| 237 |
-
|
| 238 |
-
#### 设置“复制到本地”属性(可选)
|
| 239 |
-
|
| 240 |
-
为了确保在构建项目时,这些依赖项会被复制到输出目录,您可以设置它们的 **“复制到本地”** 属性:
|
| 241 |
-
|
| 242 |
-
1. **展开引用**
|
| 243 |
-
- 在 **“解决方案资源管理器”** 中,展开 **“引用”**(**References**)。
|
| 244 |
-
|
| 245 |
-
2. **设置属性**
|
| 246 |
-
- 选择刚刚添加的 `HearthDb.dll` 和 `HearthstoneDeckTracker.exe` 引用。
|
| 247 |
-
- 右键点击每个引用,选择 **“属性”**。
|
| 248 |
-
- 在属性窗口中,将 **“复制到本地”**(**Copy Local**) 设置为 **“True”**。
|
| 249 |
-
|
| 250 |
-
#### 注意事项
|
| 251 |
-
|
| 252 |
-
- **兼容性**:确保您使用的 `HearthDb.dll` 和 `HearthstoneDeckTracker.exe` 版本与您当前的 **Hearthstone Deck Tracker (HDT)** 版本兼容,以避免潜在的兼容性问题。
|
| 253 |
-
- **插件目录**:完成上述步骤后,确保将编译生成的 `DeepBattlerPlugin.dll` 放置在 HDT 的 `Plugins` 文件夹中,以便 HDT 能够正确加载您的插件。
|
| 254 |
-
|
| 255 |
-
3. **配置插件路径**
|
| 256 |
-
- 打开 `DeepBattlerPlugin/DeepBattlerPlugin.csproj` 文件。
|
| 257 |
-
- 将 `_path` 变量设置为你的 `game_state.json` 的绝对路径:
|
| 258 |
-
```csharp
|
| 259 |
-
private readonly string _path = @"C:\Your\Absolute\Path\To\game_state.json";
|
| 260 |
-
```
|
| 261 |
-
|
| 262 |
-
4. **构建插件**
|
| 263 |
-
- 构建插件。编译后的 `DeepBattlerPlugin.dll` 位于 `DeepBattlerPlugin/bin/Debug` 目录下。
|
| 264 |
-
|
| 265 |
-
5. **安装插件到HDT**
|
| 266 |
-
1. 打开《炉石传说》卡组跟踪器(HDT)。
|
| 267 |
-
2. 将插件文件复制到HDT的插件目录:
|
| 268 |
-
- 默认位置:`%AppData%\Hearthstone Deck Tracker\Plugins`
|
| 269 |
-
3. 启动《炉石传说》卡组跟踪器。
|
| 270 |
-
4. 在HDT的 `选项 -> 插件` 下启用插件。
|
| 271 |
-
|
| 272 |
-

|
| 273 |
-
|
| 274 |
-
### LLM代理设置
|
| 275 |
-
1. **安装所需的Python包:**
|
| 276 |
-
```bash
|
| 277 |
-
pip install openai playsound==1.2.2
|
| 278 |
-
```
|
| 279 |
-
*注意:需要兼容性,请使用 `playsound` 的1.2.2版本。*
|
| 280 |
-
|
| 281 |
-
2. **在 `DeepBattler.py` 中添加你的OpenAI API密钥:**
|
| 282 |
-
```python
|
| 283 |
-
api_key = "your-openai-api-key-here"
|
| 284 |
-
```
|
| 285 |
-
|
| 286 |
-
3. **启动LLM代理:**
|
| 287 |
-
```bash
|
| 288 |
-
python DeepBattler.py
|
| 289 |
-
```
|
| 290 |
-
|
| 291 |
-
## 自定义非商业许可证
|
| 292 |
-
|
| 293 |
-
© [2024] [William-Dic]
|
| 294 |
-
|
| 295 |
-
您可以自由地为个人、教育或非商业目的使用、复制、修改和分享本软件。以下是您需要了解的内容:
|
| 296 |
-
|
| 297 |
-
1. **非商业使用**
|
| 298 |
-
您可以自由使用和调整本软件,但未经许可不得将其用于商业销售或分发。
|
| 299 |
-
|
| 300 |
-
2. **《炉石传说》知识产权**
|
| 301 |
-
本工具使用了暴雪娱乐的《炉石传说》中的资产。使用DeepBattler时,请确保遵守暴雪的条款。本工具与暴雪无关联,也未得到暴雪的认可。
|
| 302 |
-
|
| 303 |
-
3. **承认外部贡献**
|
| 304 |
-
DeepBattler包含了HearthSim开发的《炉石传说》卡组跟踪器(HDT)的组件。HDT及其组件的所有权归HearthSim及其贡献者所有。这不意味着HearthSim拥有或认可本工具。
|
| 305 |
-
|
| 306 |
-
4. **无担保**
|
| 307 |
-
本软件按“原样”提供。我们对因使用本软件而产生的任何问题不承担责任。
|
| 308 |
-
|
| 309 |
-
5. **衍生作品**
|
| 310 |
-
如果您修改或基于本软件开发衍生作品,请包含本许可证并遵守其条款。
|
| 311 |
-
|
| 312 |
-
6. **再分发**
|
| 313 |
-
如果您分享本软件或任何衍生作品,请保留本许可证和版权声明。
|
| 314 |
-
|
| 315 |
-
使用DeepBattler,即表示您同意这些条款。
|
| 316 |
-
|
| 317 |
-
[William-Dic]
|
| 318 |
-
[2024]
|
| 319 |
|
| 320 |
-
|
| 321 |
|
| 322 |
-
# DeepBattler - あなた専用の大型モデル Battlegrounds アシスタント! 🍻🍻 <a id="japanese"></a>
|
| 323 |
-
|
| 324 |
-
**[English](#english)** | **[中文](#chinese)** | **[日本語](#japanese)**
|
| 325 |
-
|
| 326 |
-
### お久しぶりです、英雄!私はDeepBattler、妙案も熱いお茶も提供する酒場のマスターです!口の回転はムルロックより速く、動きは居合斬りよりキレがある…でも足はちゃっかり畳に引っかかるタイプです!
|
| 327 |
-
|
| 328 |
-
DeepBattlerへようこそ。『ハースストーン』のバトルグラウンド向けに特化した最新のアシスタントです。大型言語モデル(LLM)を搭載し、『ハースストーン』デックトラッカー(HDT)プラグインとシームレスに統合して、**リアルタイムの戦略アドバイス**を提供します。ランキングを上げたい方も、ゲームスキルを向上させたい方も、DeepBattlerがサポートします!
|
| 329 |
-
|
| 330 |
-
DeepBattlerの実力は**EUサーバーの上位0.1%のプレイヤーに匹敵します**。音声支援ガイダンスにより、重要な場面で最適な判断を下す手助けをします。さあ、一緒にゲームをレベルアップしましょう!
|
| 331 |
-
|
| 332 |
-
## システムコンポーネント
|
| 333 |
-
|
| 334 |
-
### 1. 『ハースストーン』デックトラッカー(HDT)プラグイン
|
| 335 |
-
- **リアルタイム監視:** ゲーム状態をリアルタイムで追跡
|
| 336 |
-
- **JSON出力:** 明確で構造化されたデータを提供
|
| 337 |
-
- **効率的なデータ処理:** スムーズなパフォーマンスを保証
|
| 338 |
-
- **詳細なインサイト:** ゲームの分析を包括的に提供
|
| 339 |
-
|
| 340 |
-
### 2. LLM搭載Pythonエージェント
|
| 341 |
-
- **高度な分析:** 強力な言語モデル機能を活用
|
| 342 |
-
- **戦略的アドバイス:** リアルタイムで戦術的な提案を提供
|
| 343 |
-
- **音声コミュニケーション:** 自然な音声インタラクション
|
| 344 |
-
- **適応型の意思決定:** ゲームの状況に応じて戦略を調整
|
| 345 |
-
|
| 346 |
-
## セットアップと構成
|
| 347 |
-
|
| 348 |
-
### プラグインセットアップ
|
| 349 |
-
1. **`DeepBattlerPlugin/DeepBattlerPlugin.csproj` ファイルを開きます。**
|
| 350 |
-
- 個々のクラスファイルを変更する代わりに、`.csproj` ファイル内でプロジェクトの参照が正しく設定されていることを確認してください。
|
| 351 |
-
|
| 352 |
-
2. **依存関係を追加する:**
|
| 353 |
-
**DeepBattlerPlugin** が正しく機能するために、以下の2つの依存関係を追加する必要があります:
|
| 354 |
-
|
| 355 |
-
1. **HearthDb.dll**
|
| 356 |
-
2. **HearthstoneDeckTracker.exe**
|
| 357 |
-
|
| 358 |
-
#### Visual Studio プロジェクトに依存関係を追加する方法
|
| 359 |
-
|
| 360 |
-
以下の手順に従って、Visual Studio プロジェクトにこれらの依存関係を追加してください:
|
| 361 |
-
|
| 362 |
-
1. **プロジェクトを開く**
|
| 363 |
-
- Visual Studio でプラグインプロジェクト(例:DeepBattlerPlugin)を開きます。
|
| 364 |
-
|
| 365 |
-
2. **参照を追加する**
|
| 366 |
-
- プロジェクト名を右クリックし、**「追加」** > **「参照...」** を選択します。
|
| 367 |
-
|
| 368 |
-
3. **依存関係をブラウズして選択する**
|
| 369 |
-
- ポップアップした **「参照マネージャー」** ウィンドウで、**「ブラウズ」** タブを選択します。
|
| 370 |
-
- **「ブラウズ」** ボタンをクリックし、`HearthDb.dll` と `HearthstoneDeckTracker.exe` が含まれるディレクトリに移動します。
|
| 371 |
-
- **HearthDb.dll**:通常、HDTのインストールディレクトリにあります。
|
| 372 |
-
- **HearthstoneDeckTracker.exe**:同様に、HDTのインストールディレクトリ��あります。
|
| 373 |
-
- 両方のファイルを選択し、**「追加」** をクリックします。
|
| 374 |
-
|
| 375 |
-
4. **追加を確認する**
|
| 376 |
-
- 追加が完了したら、**「OK」** をクリックして参照を確認します。
|
| 377 |
-
|
| 378 |
-
#### 「コピー ローカル」プロパティの設定(オプション)
|
| 379 |
-
|
| 380 |
-
ビルドプロセス中にこれらの依存関係が出力ディレクトリにコピーされるようにするために、**「コピー ローカル」** プロパティを **「True」** に設定します:
|
| 381 |
-
|
| 382 |
-
1. **参照を展開する**
|
| 383 |
-
- **「ソリューションエクスプローラー」** で、**「参照」**(**References**)ノードを展開します。
|
| 384 |
-
|
| 385 |
-
2. **プロパティを設定する**
|
| 386 |
-
- 追加した `HearthDb.dll` と `HearthstoneDeckTracker.exe` の参照を選択します。
|
| 387 |
-
- 各参照を右クリックし、**「プロパティ」** を選択します。
|
| 388 |
-
- **プロパティウィンドウ**で、**「コピー ローカル」**(**Copy Local**) を **「True」** に設定します。
|
| 389 |
-
|
| 390 |
-
#### 注意事項
|
| 391 |
-
|
| 392 |
-
- **互換性**:使用している `HearthDb.dll` と `HearthstoneDeckTracker.exe` のバージョンが現在の **Hearthstone Deck Tracker (HDT)** のバージョンと互換性があることを確認してください。互換性の問題を避けるためです。
|
| 393 |
-
- **プラグインディレクトリ**:上記の手順を完了した後、コンパイルされた `DeepBattlerPlugin.dll` を HDT の `Plugins` フォルダに配置し、HDT がプラグインを正しくロードできるようにしてください。
|
| 394 |
-
|
| 395 |
-
3. **プラグインパスの設定**
|
| 396 |
-
- `DeepBattlerPlugin/DeepBattlerPlugin.csproj` ファイルを開きます。
|
| 397 |
-
- `_path` 変数をあなたの `game_state.json` の絶対パスに設定します:
|
| 398 |
-
```csharp
|
| 399 |
-
private readonly string _path = @"C:\Your\Absolute\Path\To\game_state.json";
|
| 400 |
-
```
|
| 401 |
-
|
| 402 |
-
4. **プラグインをビルドする**
|
| 403 |
-
- プラグインをビルドします。コンパイルされた `DeepBattlerPlugin.dll` は `DeepBattlerPlugin/bin/Debug` に配置されます。
|
| 404 |
-
|
| 405 |
-
5. **HDTへのプラグインのインストール**
|
| 406 |
-
1. 『ハースストーン』デックトラッカー(HDT)を開きます。
|
| 407 |
-
2. プラグインファイルをHDTのプラグインディレクトリにコピーします:
|
| 408 |
-
- デフォルトの場所:`%AppData%\Hearthstone Deck Tracker\Plugins`
|
| 409 |
-
3. 『ハースストーン』デックトラッカーを起動します。
|
| 410 |
-
4. HDTの `オプション -> プラグイン` でプラグインを有効にします。
|
| 411 |
-
|
| 412 |
-

|
| 413 |
-
|
| 414 |
-
### LLMエージェントのセットアップ
|
| 415 |
-
|
| 416 |
-
## OpenAI GPTを使用する場合
|
| 417 |
-
|
| 418 |
-
1. **必要なPythonパッケージをインストールします:**
|
| 419 |
```bash
|
| 420 |
-
|
| 421 |
```
|
| 422 |
-
*注意:互換性のため、`playsound`のバージョンは必ず`1.2.2`を使用してください。*
|
| 423 |
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
```
|
| 428 |
|
| 429 |
-
|
| 430 |
-
```
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
```
|
| 433 |
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
-
|
| 437 |
|
| 438 |
-
|
| 439 |
|
| 440 |
-
1. **必要なPythonパッケージをインストールします:**
|
| 441 |
```bash
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
```
|
| 444 |
-
*注意:playsoundは互換性のため必ずバージョン1.2.2を使用してください。*
|
| 445 |
|
| 446 |
-
|
| 447 |
-
スクリプト(`Gemma_caller.py`)に以下の環境設定を含めてください:
|
| 448 |
-
```python
|
| 449 |
-
import os
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
| 453 |
```
|
| 454 |
|
| 455 |
-
|
| 456 |
-
以下のファイルが必要です:
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
|
| 461 |
-
4. **Gemmaエージェントを起動します:**
|
| 462 |
```bash
|
| 463 |
-
|
|
|
|
| 464 |
```
|
| 465 |
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
© [2024] [William-Dic]
|
| 469 |
|
| 470 |
-
|
| 471 |
|
| 472 |
-
|
| 473 |
-
本ソフトウェアを自由に使用および調整できますが、許可なく商業目的で販売または配布しないでください。
|
| 474 |
|
| 475 |
-
|
| 476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
-
|
| 479 |
-
DeepBattlerには、HearthSimが開発したハースストーンデックトラッカー(HDT)のコンポーネントが含まれています。HDTおよびそのコンポーネントの全ての権利はHearthSimおよびその貢献者に帰属します。これはHearthSimによる所有権や承認を意味するものではありません。
|
| 480 |
|
| 481 |
-
|
| 482 |
-
|
| 483 |
|
| 484 |
-
|
| 485 |
-
本ソフトウェアを修正または基にして派生作品を作成する場合は、このライセンスを含め、その条項に従ってください。
|
| 486 |
|
| 487 |
-
|
| 488 |
-
本ソフトウェアまたはその派生作品を共有する場合は、このライセンスおよび著作権表示を保持してください。
|
| 489 |
|
| 490 |
-
DeepBattler
|
| 491 |
|
| 492 |
-
|
| 493 |
-
[2024]
|
|
|
|
| 1 |
---
|
| 2 |
+
title: DeepBattler-RL
|
| 3 |
emoji: 🧠
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# DeepBattler-RL: Reinforcement Learning Agents for Hearthstone Battlegrounds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
This repository contains the **RL training and inference pipeline** for DeepBattler, combining **RLHF (Reinforcement Learning from Human Feedback)** on human expert actions with **RLAIF** optimized using **GRPO (Group Relative Policy Optimization)** to train a policy model for Hearthstone Battlegrounds decision-making.
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
## Overview
|
| 15 |
|
| 16 |
+
DeepBattler-RL fine-tunes a Qwen3-4B-Instruct model in two stages: an SFT warmup on human expert trajectories (RLHF-style) and a GRPO phase that uses multi-candidate feedback (RLAIF) to do the heavy lifting. The trained model is served via a FastAPI endpoint for real-time inference.
|
| 17 |
|
| 18 |
+
**Key Features:**
|
| 19 |
+
- **SFT + GRPO Training Pipeline** - SFT warmup on human expert (RLHF-style) data, then GRPO as the main optimization step
|
| 20 |
+
- **RLHF + Multi-Candidate RLAIF** - Human expert actions as `expert` candidates plus additional medium/bad actions for preference-based GRPO
|
| 21 |
+
- **LoRA Fine-tuning** - Efficient parameter-efficient training with PEFT
|
| 22 |
+
- **FastAPI Inference Server** - Production-ready API for action generation
|
| 23 |
+
- **Docker Deployment** - Ready for HuggingFace Spaces or self-hosted deployment
|
| 24 |
|
| 25 |
+
## Project Structure
|
|
|
|
| 26 |
|
| 27 |
+
```
|
| 28 |
+
DeepBattler-RL/
|
| 29 |
+
├── RL/ # Core RL training & evaluation
|
| 30 |
+
│ ├── train_battleground_rlaif.py # SFT + GRPO training pipeline
|
| 31 |
+
│ ├── train_battleground_rlaif_gamehistory.py # Training with game history context
|
| 32 |
+
│ ├── eval_battleground_rlaif.py # Evaluation scripts
|
| 33 |
+
│ ├── infer_battleground_cloud.py # Cloud inference utilities
|
| 34 |
+
│ ├── battleground_nl_utils.py # Game state to natural language conversion
|
| 35 |
+
│ └── datasets/ # Training data (JSONL format)
|
| 36 |
+
├── app.py # FastAPI inference server
|
| 37 |
+
├── Dockerfile # Docker deployment config
|
| 38 |
+
├── requirements.txt # Python dependencies
|
| 39 |
+
├── Agent/ # LLM agent callers (OpenAI, Gemma)
|
| 40 |
+
└── DeepBattlerPlugin/ # HDT plugin for game state extraction
|
| 41 |
+
```
|
| 42 |
|
| 43 |
+
## Quick Start
|
| 44 |
|
| 45 |
+
### Installation
|
|
|
|
| 46 |
|
| 47 |
+
```bash
|
| 48 |
+
pip install -r requirements.txt
|
| 49 |
+
```
|
| 50 |
|
| 51 |
+
**Requirements:**
|
| 52 |
+
- Python 3.10+
|
| 53 |
+
- PyTorch >= 2.1.0
|
| 54 |
+
- CUDA (recommended for training)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
### Running the Inference Server
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
```bash
|
| 59 |
+
uvicorn app:app --host 0.0.0.0 --port 7860
|
| 60 |
```
|
|
|
|
| 61 |
|
| 62 |
+
The server loads:
|
| 63 |
+
- **Base Model:** `Qwen/Qwen3-4B-Instruct-2507`
|
| 64 |
+
- **LoRA Adapter:** `iteratehack/battleground-rlaif-qwen-gamehistory-grpo`
|
| 65 |
+
|
| 66 |
+
### API Usage
|
| 67 |
+
|
| 68 |
+
**POST `/generate_actions`**
|
| 69 |
+
|
| 70 |
+
```json
|
| 71 |
+
{
|
| 72 |
+
"phase": "PlayerTurn",
|
| 73 |
+
"turn": 5,
|
| 74 |
+
"state": {
|
| 75 |
+
"game_state": { ... },
|
| 76 |
+
"tavern": [ ... ],
|
| 77 |
+
"hand": [ ... ],
|
| 78 |
+
"board": [ ... ]
|
| 79 |
+
},
|
| 80 |
+
"max_new_tokens": 256,
|
| 81 |
+
"temperature": 0.2
|
| 82 |
+
}
|
| 83 |
```
|
| 84 |
|
| 85 |
+
**Response:**
|
| 86 |
+
```json
|
| 87 |
+
{
|
| 88 |
+
"actions": [
|
| 89 |
+
{"type": "BUY_FROM_TAVERN", "tavern_index": 2, "card_name": "Sellemental"},
|
| 90 |
+
{"type": "PLAY_FROM_HAND", "hand_index": 0, "board_index": 0},
|
| 91 |
+
{"type": "END_TURN"}
|
| 92 |
+
],
|
| 93 |
+
"raw_completion": "..."
|
| 94 |
+
}
|
| 95 |
```
|
| 96 |
|
| 97 |
+
## Training
|
| 98 |
+
|
| 99 |
+
### Dataset Format
|
| 100 |
+
|
| 101 |
+
Training data is stored in JSONL format under `RL/datasets/`:
|
| 102 |
+
|
| 103 |
+
```json
|
| 104 |
+
{
|
| 105 |
+
"game_id": "...",
|
| 106 |
+
"step_id": 0,
|
| 107 |
+
"turn": 3,
|
| 108 |
+
"phase": "PlayerTurn",
|
| 109 |
+
"state": { ... },
|
| 110 |
+
"candidates": [
|
| 111 |
+
{"role": "expert", "action": {...}, "reward": 1.0},
|
| 112 |
+
{"role": "medium", "action": {...}, "reward": 0.5},
|
| 113 |
+
{"role": "bad", "action": {...}, "reward": -0.5}
|
| 114 |
+
]
|
| 115 |
+
}
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
Here the `expert` role corresponds to human expert actions (the RLHF component), while the other roles provide additional candidates used for RLAIF with GRPO.
|
| 119 |
|
| 120 |
+
### Running Training
|
| 121 |
|
| 122 |
+
**SFT + GRPO Pipeline:**
|
| 123 |
|
|
|
|
| 124 |
```bash
|
| 125 |
+
python RL/train_battleground_rlaif.py \
|
| 126 |
+
--model Qwen/Qwen3-4B-Instruct \
|
| 127 |
+
--data RL/datasets/battleground_rlaif_multicandidate.jsonl \
|
| 128 |
+
--output ./battleground_rlaif_qwen \
|
| 129 |
+
--sft_epochs 3 \
|
| 130 |
+
--grpo_epochs 3
|
| 131 |
```
|
|
|
|
| 132 |
|
| 133 |
+
**With Game History Context:**
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
```bash
|
| 136 |
+
python RL/train_battleground_rlaif_gamehistory.py \
|
| 137 |
+
--model Qwen/Qwen3-4B-Instruct \
|
| 138 |
+
--output ./battleground_rlaif_qwen_gamehistory
|
| 139 |
```
|
| 140 |
|
| 141 |
+
### Training Configuration
|
|
|
|
| 142 |
|
| 143 |
+
| Parameter | Default | Description |
|
| 144 |
+
|-----------|---------|-------------|
|
| 145 |
+
| `--model` | `Qwen/Qwen3-4B-Instruct` | Base model path |
|
| 146 |
+
| `--sft_epochs` | 3 | SFT training epochs |
|
| 147 |
+
| `--grpo_epochs` | 3 | GRPO training epochs |
|
| 148 |
+
| `--per_device_batch_size` | 4 | Batch size per GPU |
|
| 149 |
+
| `--sft_learning_rate` | 1e-5 | SFT learning rate |
|
| 150 |
+
| `--grpo_learning_rate` | 5e-6 | GRPO learning rate |
|
| 151 |
+
| `--max_seq_length` | 1024 | Maximum sequence length |
|
| 152 |
+
| `--skip_sft` | False | Skip SFT phase |
|
| 153 |
+
| `--skip_grpo` | False | Skip GRPO phase |
|
| 154 |
+
|
| 155 |
+
## Docker Deployment
|
| 156 |
|
|
|
|
| 157 |
```bash
|
| 158 |
+
docker build -t deepbattler-rl .
|
| 159 |
+
docker run -p 7860:7860 --gpus all deepbattler-rl
|
| 160 |
```
|
| 161 |
|
| 162 |
+
For HuggingFace Spaces, the Dockerfile is pre-configured for automatic deployment.
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
## Action Types
|
| 165 |
|
| 166 |
+
The model outputs JSON action sequences with these action types:
|
|
|
|
| 167 |
|
| 168 |
+
| Action Type | Description |
|
| 169 |
+
|-------------|-------------|
|
| 170 |
+
| `BUY_FROM_TAVERN` | Purchase a minion from the tavern |
|
| 171 |
+
| `PLAY_FROM_HAND` | Play a minion from hand to board |
|
| 172 |
+
| `SELL_FROM_BOARD` | Sell a minion from the board |
|
| 173 |
+
| `HERO_POWER` | Activate hero power |
|
| 174 |
+
| `ROLL` | Refresh the tavern |
|
| 175 |
+
| `UPGRADE_TAVERN` | Upgrade tavern tier |
|
| 176 |
+
| `FREEZE` | Freeze the current tavern |
|
| 177 |
+
| `END_TURN` | End the current turn |
|
| 178 |
|
| 179 |
+
## Related Components
|
|
|
|
| 180 |
|
| 181 |
+
- **DeepBattlerPlugin/** - C# HDT plugin that extracts game state to JSON
|
| 182 |
+
- **Agent/** - Python agents for real-time voice-assisted gameplay (OpenAI/Gemma)
|
| 183 |
|
| 184 |
+
For the full DeepBattler experience with HDT integration, see the main [DeepBattler repository](https://github.com/William-Dic/DeepBattler).
|
|
|
|
| 185 |
|
| 186 |
+
## License
|
|
|
|
| 187 |
|
| 188 |
+
This software is available for personal, educational, and non-commercial use. See the main DeepBattler repository for full license terms.
|
| 189 |
|
| 190 |
+
---
|
|
|
RL/infer_battleground_cloud.py
CHANGED
|
@@ -3,6 +3,10 @@
|
|
| 3 |
#
|
| 4 |
# Cloud-based inference script for a fine-tuned Battlegrounds Qwen model hosted on Hugging Face.
|
| 5 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Usage examples:
|
| 7 |
# PYTHONPATH=. python RL/infer_battleground_cloud.py \
|
| 8 |
# --input RL/datasets/game_history_2_flat.json \
|
|
@@ -23,6 +27,7 @@ import json
|
|
| 23 |
from pathlib import Path
|
| 24 |
from typing import Any, Dict, List, Optional
|
| 25 |
|
|
|
|
| 26 |
from huggingface_hub import InferenceClient
|
| 27 |
|
| 28 |
from RL.battleground_nl_utils import game_state_to_natural_language
|
|
@@ -146,7 +151,7 @@ def parse_actions_from_completion(text: str) -> Optional[List[Dict[str, Any]]]:
|
|
| 146 |
return actions
|
| 147 |
|
| 148 |
|
| 149 |
-
def
|
| 150 |
client: InferenceClient,
|
| 151 |
examples: List[Dict[str, Any]],
|
| 152 |
input_mode: str = "json",
|
|
@@ -178,6 +183,43 @@ def run_inference(
|
|
| 178 |
return results
|
| 179 |
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
def load_examples(path: str) -> List[Dict[str, Any]]:
|
| 182 |
p = Path(path)
|
| 183 |
if not p.exists():
|
|
@@ -213,28 +255,37 @@ def parse_args() -> argparse.Namespace:
|
|
| 213 |
required=True,
|
| 214 |
help="Path to output JSONL file with actions and raw completions.",
|
| 215 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
parser.add_argument(
|
| 217 |
"--model-id",
|
| 218 |
default=None,
|
| 219 |
help=(
|
| 220 |
"Hugging Face model repo id (e.g. iteratehack/deepbattler-battleground-gamehistory). "
|
| 221 |
-
"
|
| 222 |
),
|
| 223 |
)
|
| 224 |
parser.add_argument(
|
| 225 |
"--endpoint",
|
| 226 |
default=None,
|
| 227 |
help=(
|
| 228 |
-
"Full URL of a dedicated Inference Endpoint. If provided
|
| 229 |
-
"over --model-id."
|
| 230 |
),
|
| 231 |
)
|
| 232 |
parser.add_argument(
|
| 233 |
"--hf-token",
|
| 234 |
default=None,
|
| 235 |
help=(
|
| 236 |
-
"Hugging Face access token. If omitted, the token
|
| 237 |
-
"or HF_TOKEN env var
|
| 238 |
),
|
| 239 |
)
|
| 240 |
parser.add_argument(
|
|
@@ -245,10 +296,21 @@ def parse_args() -> argparse.Namespace:
|
|
| 245 |
)
|
| 246 |
parser.add_argument("--max-new-tokens", type=int, default=256)
|
| 247 |
parser.add_argument("--temperature", type=float, default=0.2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
args = parser.parse_args()
|
| 250 |
-
if not args.
|
| 251 |
-
parser.error("
|
| 252 |
|
| 253 |
return args
|
| 254 |
|
|
@@ -256,22 +318,36 @@ def parse_args() -> argparse.Namespace:
|
|
| 256 |
def main() -> None:
|
| 257 |
args = parse_args()
|
| 258 |
|
| 259 |
-
if args.endpoint:
|
| 260 |
-
client = InferenceClient(args.endpoint, token=args.hf_token)
|
| 261 |
-
else:
|
| 262 |
-
client = InferenceClient(args.model_id, token=args.hf_token)
|
| 263 |
-
|
| 264 |
examples = load_examples(args.input)
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
save_results(args.output, results)
|
| 273 |
print(f"Wrote {len(results)} rows to {args.output}")
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
| 277 |
main()
|
|
|
|
| 3 |
#
|
| 4 |
# Cloud-based inference script for a fine-tuned Battlegrounds Qwen model hosted on Hugging Face.
|
| 5 |
#
|
| 6 |
+
# Backends supported:
|
| 7 |
+
# 1. Hugging Face Space exposing /generate_actions (preferred for this project)
|
| 8 |
+
# 2. Hugging Face Inference Endpoint / Hosted model via InferenceClient
|
| 9 |
+
#
|
| 10 |
# Usage examples:
|
| 11 |
# PYTHONPATH=. python RL/infer_battleground_cloud.py \
|
| 12 |
# --input RL/datasets/game_history_2_flat.json \
|
|
|
|
| 27 |
from pathlib import Path
|
| 28 |
from typing import Any, Dict, List, Optional
|
| 29 |
|
| 30 |
+
import requests
|
| 31 |
from huggingface_hub import InferenceClient
|
| 32 |
|
| 33 |
from RL.battleground_nl_utils import game_state_to_natural_language
|
|
|
|
| 151 |
return actions
|
| 152 |
|
| 153 |
|
| 154 |
+
def run_inference_via_client(
|
| 155 |
client: InferenceClient,
|
| 156 |
examples: List[Dict[str, Any]],
|
| 157 |
input_mode: str = "json",
|
|
|
|
| 183 |
return results
|
| 184 |
|
| 185 |
|
| 186 |
+
def run_inference_via_space(
|
| 187 |
+
space_url: str,
|
| 188 |
+
examples: List[Dict[str, Any]],
|
| 189 |
+
max_new_tokens: int = 256,
|
| 190 |
+
temperature: float = 0.2,
|
| 191 |
+
timeout: int = 120,
|
| 192 |
+
hf_token: Optional[str] = None,
|
| 193 |
+
) -> List[Dict[str, Any]]:
|
| 194 |
+
"""Call the deployed Space /generate_actions endpoint for each example."""
|
| 195 |
+
|
| 196 |
+
base_url = space_url.rstrip("/")
|
| 197 |
+
endpoint = f"{base_url}/generate_actions"
|
| 198 |
+
headers = {"Content-Type": "application/json"}
|
| 199 |
+
if hf_token:
|
| 200 |
+
headers["Authorization"] = f"Bearer {hf_token}"
|
| 201 |
+
|
| 202 |
+
results: List[Dict[str, Any]] = []
|
| 203 |
+
for ex in examples:
|
| 204 |
+
payload = {
|
| 205 |
+
"phase": ex.get("phase"),
|
| 206 |
+
"turn": ex.get("turn"),
|
| 207 |
+
"state": ex.get("state", {}),
|
| 208 |
+
"max_new_tokens": max_new_tokens,
|
| 209 |
+
"temperature": temperature,
|
| 210 |
+
}
|
| 211 |
+
resp = requests.post(endpoint, json=payload, headers=headers, timeout=timeout)
|
| 212 |
+
resp.raise_for_status()
|
| 213 |
+
data = resp.json()
|
| 214 |
+
|
| 215 |
+
out_row = dict(ex)
|
| 216 |
+
out_row["actions"] = data.get("actions")
|
| 217 |
+
out_row["raw_completion"] = data.get("raw_completion")
|
| 218 |
+
results.append(out_row)
|
| 219 |
+
|
| 220 |
+
return results
|
| 221 |
+
|
| 222 |
+
|
| 223 |
def load_examples(path: str) -> List[Dict[str, Any]]:
|
| 224 |
p = Path(path)
|
| 225 |
if not p.exists():
|
|
|
|
| 255 |
required=True,
|
| 256 |
help="Path to output JSONL file with actions and raw completions.",
|
| 257 |
)
|
| 258 |
+
parser.add_argument(
|
| 259 |
+
"--space-url",
|
| 260 |
+
default=None,
|
| 261 |
+
help=(
|
| 262 |
+
"URL of the Hugging Face Space hosting /generate_actions (e.g. "
|
| 263 |
+
"https://iteratehack-deepbattler.hf.space). If provided, the script calls "
|
| 264 |
+
"that endpoint instead of the Inference API."
|
| 265 |
+
),
|
| 266 |
+
)
|
| 267 |
parser.add_argument(
|
| 268 |
"--model-id",
|
| 269 |
default=None,
|
| 270 |
help=(
|
| 271 |
"Hugging Face model repo id (e.g. iteratehack/deepbattler-battleground-gamehistory). "
|
| 272 |
+
"Used only if --space-url is omitted."
|
| 273 |
),
|
| 274 |
)
|
| 275 |
parser.add_argument(
|
| 276 |
"--endpoint",
|
| 277 |
default=None,
|
| 278 |
help=(
|
| 279 |
+
"Full URL of a dedicated Inference Endpoint. If provided (and --space-url missing), "
|
| 280 |
+
"this takes precedence over --model-id."
|
| 281 |
),
|
| 282 |
)
|
| 283 |
parser.add_argument(
|
| 284 |
"--hf-token",
|
| 285 |
default=None,
|
| 286 |
help=(
|
| 287 |
+
"Hugging Face access token. Needed for private Spaces/models. If omitted, use the token "
|
| 288 |
+
"from `huggingface-cli login` or HF_TOKEN env var."
|
| 289 |
),
|
| 290 |
)
|
| 291 |
parser.add_argument(
|
|
|
|
| 296 |
)
|
| 297 |
parser.add_argument("--max-new-tokens", type=int, default=256)
|
| 298 |
parser.add_argument("--temperature", type=float, default=0.2)
|
| 299 |
+
parser.add_argument(
|
| 300 |
+
"--request-timeout",
|
| 301 |
+
type=int,
|
| 302 |
+
default=120,
|
| 303 |
+
help="Timeout (seconds) for HTTP requests when using --space-url",
|
| 304 |
+
)
|
| 305 |
+
parser.add_argument(
|
| 306 |
+
"--print-results",
|
| 307 |
+
action="store_true",
|
| 308 |
+
help="Print each output row (JSON) to stdout after inference.",
|
| 309 |
+
)
|
| 310 |
|
| 311 |
args = parser.parse_args()
|
| 312 |
+
if not any([args.space_url, args.endpoint, args.model_id]):
|
| 313 |
+
parser.error("Provide --space-url, --endpoint, or --model-id")
|
| 314 |
|
| 315 |
return args
|
| 316 |
|
|
|
|
| 318 |
def main() -> None:
|
| 319 |
args = parse_args()
|
| 320 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
examples = load_examples(args.input)
|
| 322 |
+
if args.space_url:
|
| 323 |
+
results = run_inference_via_space(
|
| 324 |
+
args.space_url,
|
| 325 |
+
examples,
|
| 326 |
+
max_new_tokens=args.max_new_tokens,
|
| 327 |
+
temperature=args.temperature,
|
| 328 |
+
timeout=args.request_timeout,
|
| 329 |
+
hf_token=args.hf_token,
|
| 330 |
+
)
|
| 331 |
+
else:
|
| 332 |
+
if args.endpoint:
|
| 333 |
+
client = InferenceClient(args.endpoint, token=args.hf_token)
|
| 334 |
+
else:
|
| 335 |
+
client = InferenceClient(args.model_id, token=args.hf_token)
|
| 336 |
+
|
| 337 |
+
results = run_inference_via_client(
|
| 338 |
+
client,
|
| 339 |
+
examples,
|
| 340 |
+
input_mode=args.input_mode,
|
| 341 |
+
max_new_tokens=args.max_new_tokens,
|
| 342 |
+
temperature=args.temperature,
|
| 343 |
+
)
|
| 344 |
save_results(args.output, results)
|
| 345 |
print(f"Wrote {len(results)} rows to {args.output}")
|
| 346 |
|
| 347 |
+
if args.print_results:
|
| 348 |
+
for row in results:
|
| 349 |
+
print(json.dumps(row, ensure_ascii=False))
|
| 350 |
+
|
| 351 |
|
| 352 |
if __name__ == "__main__":
|
| 353 |
main()
|