willco-afk commited on
Commit
0697116
·
verified ·
1 Parent(s): 7ad2218

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -39
app.py CHANGED
@@ -1,42 +1,64 @@
1
- import gradio as gr
 
2
  import numpy as np
3
  import os
4
 
5
- # Update the file name to the correct one
6
- q_table_path = 'q_table_frozenlake.npy'
7
-
8
- # Ensure the Q-table file exists in the expected path
9
- if not os.path.exists(q_table_path):
10
- raise FileNotFoundError(f"Q-table file '{q_table_path}' not found. Please upload it to the correct directory.")
11
-
12
- # Load the Q-table
13
- q_table = np.load(q_table_path)
14
-
15
- # Define a function to make predictions based on the Q-table
16
- def predict_action(state):
17
- try:
18
- # Convert the input state into a format compatible with the Q-table
19
- state = list(map(int, state.split(','))) # Example: '0,1,2' => [0, 1, 2]
20
-
21
- # Ensure state indices are within the bounds of the Q-table
22
- if any(s < 0 or s >= q_table.shape[0] for s in state):
23
- return "Error: State values are out of range for the Q-table."
24
-
25
- # Predict the action based on the state
26
- action = np.argmax(q_table[state[0]]) # Assuming the first value in state is the row index
27
- action_mapping = {0: "Move Left", 1: "Move Down", 2: "Move Right", 3: "Move Up"}
28
- return f"The predicted action is: {action_mapping.get(action, 'Invalid Action')}"
29
- except Exception as e:
30
- return f"Error: {str(e)}"
31
-
32
- # Create the Gradio interface with a more playful UI
33
- iface = gr.Interface(
34
- fn=predict_action,
35
- inputs=gr.Textbox(label="Enter State (comma-separated values)"), # You could also add a dropdown or grid for a more playful input
36
- outputs=gr.Textbox(label="Predicted Action"),
37
- live=True,
38
- theme="huggingface", # Choose a theme that matches the playful environment
39
- )
40
-
41
- # Launch the interface
42
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()