|
|
import os |
|
|
import sys |
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) |
|
|
|
|
|
import logging |
|
|
|
|
|
import gradio as gr |
|
|
import spaces |
|
|
|
|
|
from unpredictable_lord.chat import chat_with_llm_stream |
|
|
from unpredictable_lord.mcp_tools import ( |
|
|
execute_turn, |
|
|
get_game_state, |
|
|
init_game, |
|
|
list_available_advice, |
|
|
) |
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
logger.info(f"ZeroGPU: {spaces.config.Config.zero_gpu}") |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="Unpredictable Lord") as demo: |
|
|
gr.Markdown("# Unpredictable Lord\nLord Advisor AI Simulation") |
|
|
|
|
|
with gr.Tabs(): |
|
|
|
|
|
with gr.TabItem("Chat"): |
|
|
chatbot = gr.Chatbot(label="Lord AI", height=600, type="messages") |
|
|
|
|
|
with gr.Row(): |
|
|
msg = gr.Textbox( |
|
|
label="Your Advice", |
|
|
placeholder="My Lord, I have a proposal...", |
|
|
scale=4, |
|
|
) |
|
|
submit_btn = gr.Button("Submit", scale=1) |
|
|
|
|
|
clear = gr.Button("Clear History") |
|
|
|
|
|
def user(user_message, history): |
|
|
|
|
|
return "", history + [{"role": "user", "content": user_message}] |
|
|
|
|
|
def bot(history): |
|
|
|
|
|
user_message = history[-1]["content"] |
|
|
history_for_model = history[:-1] |
|
|
|
|
|
for updated_history in chat_with_llm_stream( |
|
|
user_message, history_for_model |
|
|
): |
|
|
yield updated_history |
|
|
|
|
|
msg.submit( |
|
|
user, [msg, chatbot], [msg, chatbot], queue=False, show_api=False |
|
|
).then(bot, chatbot, chatbot, show_api=False) |
|
|
|
|
|
submit_btn.click( |
|
|
user, [msg, chatbot], [msg, chatbot], queue=False, show_api=False |
|
|
).then(bot, chatbot, chatbot, show_api=False) |
|
|
|
|
|
clear.click(lambda: None, None, chatbot, queue=False, show_api=False) |
|
|
|
|
|
|
|
|
with gr.TabItem("MCP Server"): |
|
|
gr.Markdown( |
|
|
""" |
|
|
## MCP Server Guide |
|
|
|
|
|
This application functions as an **MCP (Model Context Protocol) Server**. |
|
|
External LLMs can connect to this server and use game management tools. |
|
|
|
|
|
### Connection URL |
|
|
|
|
|
``` |
|
|
https://<space-name>.hf.space/gradio_api/mcp/ |
|
|
``` |
|
|
|
|
|
For local development: |
|
|
``` |
|
|
http://localhost:7860/gradio_api/mcp/ |
|
|
``` |
|
|
|
|
|
### How to Connect |
|
|
|
|
|
#### Claude Desktop / Cursor / VS Code |
|
|
|
|
|
Add the following to your MCP settings configuration: |
|
|
|
|
|
```json |
|
|
{ |
|
|
"mcpServers": { |
|
|
"unpredictable-lord": { |
|
|
"url": "https://<space-name>.hf.space/gradio_api/mcp/" |
|
|
} |
|
|
} |
|
|
} |
|
|
``` |
|
|
|
|
|
### Available Tools |
|
|
|
|
|
| Tool | Description | |
|
|
|------|-------------| |
|
|
| `init_game` | Initialize a new game session. Returns a session_id and available advice options. | |
|
|
| `get_game_state` | Get the current game state for a session. | |
|
|
| `list_available_advice` | Get all available advice options for execute_turn. | |
|
|
| `execute_turn` | Execute a turn with the given advice. Returns whether advice was adopted and results. | |
|
|
|
|
|
### Usage Flow |
|
|
|
|
|
1. Call `init_game(personality)` to start a new game session |
|
|
2. User gives free-form advice to the Lord AI |
|
|
3. Lord AI interprets advice and calls `execute_turn(session_id, advice)` |
|
|
4. Lord AI explains the result to the user (adopted/rejected, action taken, effects) |
|
|
5. Repeat from step 2 until game over |
|
|
""" |
|
|
) |
|
|
|
|
|
gr.Markdown("### Test: Initialize Game") |
|
|
with gr.Row(): |
|
|
personality_input = gr.Dropdown( |
|
|
choices=["cautious", "idealist", "populist"], |
|
|
value="cautious", |
|
|
label="Lord Personality", |
|
|
) |
|
|
init_btn = gr.Button("Start New Game") |
|
|
|
|
|
init_output = gr.JSON(label="Game Session Info") |
|
|
|
|
|
init_btn.click(fn=init_game, inputs=personality_input, outputs=init_output) |
|
|
|
|
|
gr.Markdown("### Test: Get Game State") |
|
|
with gr.Row(): |
|
|
session_id_input = gr.Textbox( |
|
|
label="Session ID", |
|
|
placeholder="Enter session_id from init_game", |
|
|
) |
|
|
get_state_btn = gr.Button("Get State") |
|
|
|
|
|
state_output = gr.JSON(label="Current Game State") |
|
|
|
|
|
get_state_btn.click( |
|
|
fn=get_game_state, inputs=session_id_input, outputs=state_output |
|
|
) |
|
|
|
|
|
gr.Markdown("### Test: List Available Advice") |
|
|
list_advice_btn = gr.Button("List Advice Options") |
|
|
advice_output = gr.JSON(label="Available Advice Options") |
|
|
|
|
|
list_advice_btn.click( |
|
|
fn=list_available_advice, inputs=[], outputs=advice_output |
|
|
) |
|
|
|
|
|
gr.Markdown("### Test: Execute Turn") |
|
|
with gr.Row(): |
|
|
exec_session_id = gr.Textbox( |
|
|
label="Session ID", |
|
|
placeholder="Enter session_id", |
|
|
) |
|
|
exec_advice = gr.Dropdown( |
|
|
choices=[ |
|
|
"increase_tax", |
|
|
"decrease_tax", |
|
|
"expand_territory", |
|
|
"improve_diplomacy", |
|
|
"public_festival", |
|
|
"build_infrastructure", |
|
|
"do_nothing", |
|
|
], |
|
|
value="do_nothing", |
|
|
label="Advice", |
|
|
) |
|
|
exec_btn = gr.Button("Execute Turn") |
|
|
|
|
|
exec_output = gr.JSON(label="Turn Result") |
|
|
|
|
|
exec_btn.click( |
|
|
fn=execute_turn, |
|
|
inputs=[exec_session_id, exec_advice], |
|
|
outputs=exec_output, |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(mcp_server=True) |
|
|
|