Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,64 @@
|
|
1 |
-
import
|
|
|
2 |
import numpy as np
|
3 |
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import hf_hub_download, Repository
|
2 |
+
import gym
|
3 |
import numpy as np
|
4 |
import os
|
5 |
|
6 |
+
# Define your username and repo name
|
7 |
+
username = "willco-afk" # Your Hugging Face username
|
8 |
+
repo_name = "frozenslippery" # Your Hugging Face Space name
|
9 |
+
|
10 |
+
# Initialize your environment
|
11 |
+
env = gym.make("FrozenLake-v1", is_slippery=True) # Adjust based on your specific environment
|
12 |
+
|
13 |
+
# Correct file path where the Q-table is located
|
14 |
+
repo_id = "willco-afk/frozenslippery"
|
15 |
+
file_path = "q_table_frozenlake.npy" # Path to the Q-table file in the repo
|
16 |
+
|
17 |
+
# Try downloading the Q-table
|
18 |
+
try:
|
19 |
+
download_path = hf_hub_download(repo_id=repo_id, filename=file_path)
|
20 |
+
# Load the Q-table
|
21 |
+
q_table = np.load(download_path)
|
22 |
+
except Exception as e:
|
23 |
+
print(f"Error downloading the Q-table: {e}")
|
24 |
+
# Handle the error (for example, by uploading the Q-table manually if needed)
|
25 |
+
|
26 |
+
# Save the model (Q-table) as a .npz file in the repo's folder
|
27 |
+
model_filename = "q_table_frozenlake.npz"
|
28 |
+
np.save(model_filename, q_table)
|
29 |
+
|
30 |
+
# Initialize the Hugging Face repo for the Space (no need to create it again)
|
31 |
+
repo = Repository(local_dir=repo_name, clone_from=f"{username}/{repo_name}")
|
32 |
+
|
33 |
+
# Add and push the model file to Hugging Face Hub
|
34 |
+
repo.git_add(model_filename) # Add the Q-table to the repo
|
35 |
+
repo.git_commit("Add trained Q-table") # Commit the Q-table
|
36 |
+
repo.git_push() # Push the changes to Hugging Face Hub
|
37 |
+
|
38 |
+
# Write the README file with details
|
39 |
+
readme_content = "# FrozenLake RL Model\n\n"
|
40 |
+
readme_content += "This model represents a Q-learning agent for the `FrozenLake-v1` environment with `is_slippery=True`.\n\n"
|
41 |
+
readme_content += "### Usage Instructions\n\n"
|
42 |
+
readme_content += "To use this model, you need to initialize the FrozenLake environment using OpenAI's gym:\n\n"
|
43 |
+
readme_content += "```python\n"
|
44 |
+
readme_content += "import gym\n"
|
45 |
+
readme_content += "env = gym.make('FrozenLake-v1', is_slippery=True)\n"
|
46 |
+
readme_content += "```\n\n"
|
47 |
+
readme_content += "### Model Details\n\n"
|
48 |
+
readme_content += "This model uses a Q-table learned through Q-learning in the `FrozenLake-v1` environment. The agent was trained using the following parameters:\n\n"
|
49 |
+
readme_content += "- **Learning Rate:** 0.1\n"
|
50 |
+
readme_content += "- **Discount Factor (gamma):** 0.99\n"
|
51 |
+
readme_content += "- **Exploration Rate (epsilon):** Decays from 1.0 to 0.01\n"
|
52 |
+
readme_content += "- **Training Episodes:** 1000\n"
|
53 |
+
readme_content += "- **Max Steps per Episode:** 100\n\n"
|
54 |
+
readme_content += "### About the Environment\n\n"
|
55 |
+
readme_content += "The `FrozenLake-v1` environment is a gridworld where the agent must navigate a frozen lake while avoiding holes. It can slip based on the `is_slippery` parameter, making the environment stochastic.\n"
|
56 |
+
|
57 |
+
# Write the README file
|
58 |
+
with open(f"{repo_name}/README.md", "w") as readme_file:
|
59 |
+
readme_file.write(readme_content)
|
60 |
+
|
61 |
+
# Add and push the README file
|
62 |
+
repo.git_add("README.md")
|
63 |
+
repo.git_commit("Add README for FrozenLake RL model")
|
64 |
+
repo.git_push()
|