willco-afk commited on
Commit
343eca6
·
verified ·
1 Parent(s): b35c1de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -37
app.py CHANGED
@@ -7,9 +7,6 @@ import os
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
@@ -23,42 +20,47 @@ 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()
 
7
  username = "willco-afk" # Your Hugging Face username
8
  repo_name = "frozenslippery" # Your Hugging Face Space name
9
 
 
 
 
10
  # Correct file path where the Q-table is located
11
  repo_id = "willco-afk/frozenslippery"
12
  file_path = "q_table_frozenlake.npy" # Path to the Q-table file in the repo
 
20
  print(f"Error downloading the Q-table: {e}")
21
  # Handle the error (for example, by uploading the Q-table manually if needed)
22
 
23
+ # Check if the Q-table was loaded successfully
24
+ if 'q_table' in locals():
25
+ # Save the model (Q-table) as a .npz file in the repo's folder
26
+ model_filename = "q_table_frozenlake.npz"
27
+ np.save(model_filename, q_table)
28
+
29
+ # Initialize the Hugging Face repo for the Space (no need to create it again)
30
+ repo = Repository(local_dir=repo_name, clone_from=f"{username}/{repo_name}")
31
 
32
+ # Add and push the model file to Hugging Face Hub
33
+ repo.git_add(model_filename) # Add the Q-table to the repo
34
+ repo.git_commit("Add trained Q-table") # Commit the Q-table
35
+ repo.git_push() # Push the changes to Hugging Face Hub
36
 
37
+ # Write the README file with details
38
+ readme_content = "# FrozenLake RL Model\n\n"
39
+ readme_content += "This model represents a Q-learning agent for the `FrozenLake-v1` environment with `is_slippery=True`.\n\n"
40
+ readme_content += "### Usage Instructions\n\n"
41
+ readme_content += "To use this model, you need to initialize the FrozenLake environment using OpenAI's gym:\n\n"
42
+ readme_content += "```python\n"
43
+ readme_content += "import gym\n"
44
+ readme_content += "env = gym.make('FrozenLake-v1', is_slippery=True)\n"
45
+ readme_content += "```\n\n"
46
+ readme_content += "### Model Details\n\n"
47
+ 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"
48
+ readme_content += "- **Learning Rate:** 0.1\n"
49
+ readme_content += "- **Discount Factor (gamma):** 0.99\n"
50
+ readme_content += "- **Exploration Rate (epsilon):** Decays from 1.0 to 0.01\n"
51
+ readme_content += "- **Training Episodes:** 1000\n"
52
+ readme_content += "- **Max Steps per Episode:** 100\n\n"
53
+ readme_content += "### About the Environment\n\n"
54
+ 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"
55
 
56
+ # Write the README file
57
+ with open(f"{repo_name}/README.md", "w") as readme_file:
58
+ readme_file.write(readme_content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ # Add and push the README file
61
+ repo.git_add("README.md")
62
+ repo.git_commit("Add README for FrozenLake RL model")
63
+ repo.git_push()
64
 
65
+ else:
66
+ print("Q-table was not loaded, skipping further operations.")