Spaces:
Sleeping
Sleeping
Commit ·
a3a1d34
1
Parent(s): 7fa18f1
Update instructions and added dataset
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ A simple interface for the text adventure AI agent assignment.
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
TITLE = "Playing Zork has never been so boring"
|
| 10 |
|
|
@@ -22,27 +25,39 @@ This project provides:
|
|
| 22 |
CLONE_INSTRUCTIONS = """
|
| 23 |
## Getting Started
|
| 24 |
|
| 25 |
-
###
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
```
|
| 29 |
https://huggingface.co/spaces/LLM-course/text-adventure-template
|
| 30 |
```
|
| 31 |
|
| 32 |
-
### 2. Clone
|
| 33 |
|
| 34 |
```bash
|
| 35 |
git clone https://huggingface.co/spaces/YOUR_USERNAME/text-adventure-agent
|
| 36 |
-
cd text-adventure-agent
|
| 37 |
```
|
| 38 |
|
| 39 |
-
### 3. Implement
|
| 40 |
|
| 41 |
Edit these files:
|
| 42 |
- `agent.py` - Your ReAct agent implementation (implement the `StudentAgent` class)
|
| 43 |
- `mcp_server.py` - Your MCP server implementation (add tools like `play_action`, `memory`, etc.)
|
| 44 |
|
| 45 |
-
### 4. Test
|
| 46 |
|
| 47 |
```bash
|
| 48 |
# Test MCP server interactively
|
|
@@ -52,17 +67,67 @@ fastmcp dev mcp_server.py
|
|
| 52 |
python run_agent.py --agent . --game lostpig -v -n 20
|
| 53 |
```
|
| 54 |
|
| 55 |
-
### 5. Push
|
| 56 |
|
| 57 |
-
|
| 58 |
-
git add -A
|
| 59 |
-
git commit -m "Implement my agent"
|
| 60 |
-
git push
|
| 61 |
-
```
|
| 62 |
-
|
| 63 |
-
Then submit your Space URL on the course platform.
|
| 64 |
"""
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
demo = gr.Blocks(title=TITLE)
|
| 67 |
|
| 68 |
with demo:
|
|
@@ -70,6 +135,21 @@ with demo:
|
|
| 70 |
gr.Markdown(DESCRIPTION)
|
| 71 |
gr.Markdown("---")
|
| 72 |
gr.Markdown(CLONE_INSTRUCTIONS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch()
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
+
from huggingface_hub import HfApi
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
TITLE = "Playing Zork has never been so boring"
|
| 13 |
|
|
|
|
| 25 |
CLONE_INSTRUCTIONS = """
|
| 26 |
## Getting Started
|
| 27 |
|
| 28 |
+
### 0. Clone this space
|
| 29 |
|
| 30 |
+
```bash
|
| 31 |
+
git clone https://huggingface.co/spaces/LLM-course/Agentic-zork
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
This includes:
|
| 35 |
+
- run_agent.py: Script to run agents on text adventure games
|
| 36 |
+
- evaluation/: Evaluation scripts and utilities
|
| 37 |
+
- games/: Text adventure game environments
|
| 38 |
+
- submission_template/: Template for your agent submission
|
| 39 |
+
- example_submission/: Example agent implementation
|
| 40 |
+
|
| 41 |
+
### 1. Fork the template space
|
| 42 |
+
|
| 43 |
+
Fork the template space on Hugging Face:
|
| 44 |
```
|
| 45 |
https://huggingface.co/spaces/LLM-course/text-adventure-template
|
| 46 |
```
|
| 47 |
|
| 48 |
+
### 2. Clone your fork locally
|
| 49 |
|
| 50 |
```bash
|
| 51 |
git clone https://huggingface.co/spaces/YOUR_USERNAME/text-adventure-agent
|
|
|
|
| 52 |
```
|
| 53 |
|
| 54 |
+
### 3. Implement your agent
|
| 55 |
|
| 56 |
Edit these files:
|
| 57 |
- `agent.py` - Your ReAct agent implementation (implement the `StudentAgent` class)
|
| 58 |
- `mcp_server.py` - Your MCP server implementation (add tools like `play_action`, `memory`, etc.)
|
| 59 |
|
| 60 |
+
### 4. Test locally
|
| 61 |
|
| 62 |
```bash
|
| 63 |
# Test MCP server interactively
|
|
|
|
| 67 |
python run_agent.py --agent . --game lostpig -v -n 20
|
| 68 |
```
|
| 69 |
|
| 70 |
+
### 5. Push to your space
|
| 71 |
|
| 72 |
+
### 6. Submit your space URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
|
| 75 |
+
DATASET_REPO = "LLM-course/zork-submission"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def submit_space(space_url: str, profile: gr.OAuthProfile | None) -> str:
|
| 79 |
+
"""Submit a space URL to the dataset."""
|
| 80 |
+
if profile is None:
|
| 81 |
+
return "Please log in with your HuggingFace account first (button above)."
|
| 82 |
+
|
| 83 |
+
if not space_url or not space_url.strip():
|
| 84 |
+
return "Please enter your Space URL."
|
| 85 |
+
|
| 86 |
+
space_url = space_url.strip()
|
| 87 |
+
|
| 88 |
+
# Validate URL format
|
| 89 |
+
if not ("huggingface.co/spaces/" in space_url or "hf.co/spaces/" in space_url):
|
| 90 |
+
return "Invalid Space URL. It should look like: https://huggingface.co/spaces/username/space-name"
|
| 91 |
+
|
| 92 |
+
username = profile.username
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
api = HfApi()
|
| 96 |
+
|
| 97 |
+
# Try to load existing submissions
|
| 98 |
+
try:
|
| 99 |
+
submissions_path = api.hf_hub_download(
|
| 100 |
+
repo_id=DATASET_REPO,
|
| 101 |
+
filename="submissions.json",
|
| 102 |
+
repo_type="dataset",
|
| 103 |
+
)
|
| 104 |
+
with open(submissions_path, "r") as f:
|
| 105 |
+
submissions = json.load(f)
|
| 106 |
+
except Exception:
|
| 107 |
+
submissions = {}
|
| 108 |
+
|
| 109 |
+
# Update with new submission (overwrites previous for same user)
|
| 110 |
+
submissions[username] = {
|
| 111 |
+
"space_url": space_url,
|
| 112 |
+
"submitted_at": datetime.now().isoformat(),
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
# Save back to dataset
|
| 116 |
+
submissions_json = json.dumps(submissions, indent=2)
|
| 117 |
+
api.upload_file(
|
| 118 |
+
path_or_fileobj=submissions_json.encode(),
|
| 119 |
+
path_in_repo="submissions.json",
|
| 120 |
+
repo_id=DATASET_REPO,
|
| 121 |
+
repo_type="dataset",
|
| 122 |
+
commit_message=f"Update submission for {username}",
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
return f"Submission successful! Space URL '{space_url}' recorded for user '{username}'."
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return f"Error submitting: {str(e)}"
|
| 129 |
+
|
| 130 |
+
|
| 131 |
demo = gr.Blocks(title=TITLE)
|
| 132 |
|
| 133 |
with demo:
|
|
|
|
| 135 |
gr.Markdown(DESCRIPTION)
|
| 136 |
gr.Markdown("---")
|
| 137 |
gr.Markdown(CLONE_INSTRUCTIONS)
|
| 138 |
+
|
| 139 |
+
# Submission section
|
| 140 |
+
gr.LoginButton()
|
| 141 |
+
space_input = gr.Textbox(
|
| 142 |
+
label="Your Space URL",
|
| 143 |
+
placeholder="https://huggingface.co/spaces/your-username/text-adventure-agent",
|
| 144 |
+
)
|
| 145 |
+
submit_btn = gr.Button("Click HERE to Submit", variant="primary")
|
| 146 |
+
result_text = gr.Textbox(label="Status", interactive=False)
|
| 147 |
+
|
| 148 |
+
submit_btn.click(
|
| 149 |
+
fn=submit_space,
|
| 150 |
+
inputs=[space_input],
|
| 151 |
+
outputs=[result_text],
|
| 152 |
+
)
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
demo.launch()
|