Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ ACTION_MAP_USER = {
|
|
| 35 |
}
|
| 36 |
|
| 37 |
# ----------------------------------------------------------------------
|
| 38 |
-
# 1. PRÉPARATION DU MODÈLE ET DE L'ENVIRONNEMENT
|
| 39 |
# ----------------------------------------------------------------------
|
| 40 |
|
| 41 |
model = None
|
|
@@ -58,8 +58,6 @@ try:
|
|
| 58 |
exec(f.read(), env_globals)
|
| 59 |
|
| 60 |
MiRobotEnv = env_globals['MiRobotEnv']
|
| 61 |
-
print("Classe MiRobotEnv définie avec succès.")
|
| 62 |
-
|
| 63 |
|
| 64 |
# --- 2. Enregistrement de l'environnement Custom ---
|
| 65 |
register(
|
|
@@ -75,7 +73,7 @@ try:
|
|
| 75 |
env = gym.make(ENV_ID)
|
| 76 |
env.reset()
|
| 77 |
|
| 78 |
-
#
|
| 79 |
initial_faim = env.unwrapped.state[ETAT_FAIM] * 100
|
| 80 |
initial_humeur = env.unwrapped.state[ETAT_HUMEUR]
|
| 81 |
|
|
@@ -88,7 +86,6 @@ except Exception as e:
|
|
| 88 |
# 2. LOGIQUE DU JEU
|
| 89 |
# ----------------------------------------------------------------------
|
| 90 |
|
| 91 |
-
# L'état initial du jeu
|
| 92 |
game_state_initial = {
|
| 93 |
'level': INITIAL_LEVEL,
|
| 94 |
'puppy_pos': [GRID_SIZE // 2, GRID_SIZE // 2],
|
|
@@ -101,7 +98,7 @@ def _get_env_state(env_instance):
|
|
| 101 |
"""Accès sécurisé à l'état de l'environnement, même avec un wrapper."""
|
| 102 |
if env_instance is None:
|
| 103 |
return None
|
| 104 |
-
#
|
| 105 |
return env_instance.unwrapped.state
|
| 106 |
|
| 107 |
def _reset_game(reward_path):
|
|
@@ -135,7 +132,7 @@ def handle_user_command(current_state, command_text, reward_path):
|
|
| 135 |
|
| 136 |
game_state['reward_asset_path'] = reward_path
|
| 137 |
|
| 138 |
-
# Vérification de la faim (défaite)
|
| 139 |
faim_actuelle = current_env_state[ETAT_FAIM]
|
| 140 |
if faim_actuelle >= FAIM_PENALTY_THRESHOLD:
|
| 141 |
game_state['message'] = f'💔 Défaite ! MiRobot a trop faim ({faim_actuelle:.0%}). Jeu réinitialisé.'
|
|
@@ -148,7 +145,7 @@ def handle_user_command(current_state, command_text, reward_path):
|
|
| 148 |
|
| 149 |
current_env_state[CMD_AVANCER] = 0.0
|
| 150 |
current_env_state[CMD_TOURNER] = 0.0
|
| 151 |
-
env.step(0)
|
| 152 |
|
| 153 |
faim_display = current_env_state[ETAT_FAIM] * 100
|
| 154 |
humeur_display = current_env_state[ETAT_HUMEUR]
|
|
@@ -162,6 +159,10 @@ def handle_user_command(current_state, command_text, reward_path):
|
|
| 162 |
current_env_state[CMD_TOURNER] = 1.0 if command_action_name.startswith("TOURNER") else 0.0
|
| 163 |
|
| 164 |
mirobot_action_id, _ = model.predict(current_env_state, deterministic=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
new_obs, reward, terminated, truncated, info = env.step(mirobot_action_id)
|
| 166 |
|
| 167 |
|
|
@@ -180,7 +181,8 @@ def handle_user_command(current_state, command_text, reward_path):
|
|
| 180 |
dy = 1 if ry > py else -1
|
| 181 |
|
| 182 |
else:
|
| 183 |
-
|
|
|
|
| 184 |
game_state['message'] = f"😥 MiRobot a désobéi ! Il a fait '{real_action_name}' au lieu de '{command_action_name}'. Récompense RL: {reward:.2f}"
|
| 185 |
|
| 186 |
# Mise à jour de la position
|
|
@@ -188,7 +190,7 @@ def handle_user_command(current_state, command_text, reward_path):
|
|
| 188 |
new_y = np.clip(game_state['puppy_pos'][1] + dy, 0, GRID_SIZE - 1)
|
| 189 |
game_state['puppy_pos'] = [new_x, new_y]
|
| 190 |
|
| 191 |
-
current_env_state = _get_env_state(env)
|
| 192 |
faim_display = current_env_state[ETAT_FAIM] * 100
|
| 193 |
humeur_display = current_env_state[ETAT_HUMEUR]
|
| 194 |
return game_state, command_text, new_x, new_y, faim_display, humeur_display, game_state['message']
|
|
@@ -208,7 +210,7 @@ def handle_bravo(current_state):
|
|
| 208 |
if px == rx and py == ry:
|
| 209 |
game_state['level'] += 1
|
| 210 |
|
| 211 |
-
# Modification de l'état
|
| 212 |
current_env_state[ETAT_FAIM] = np.clip(current_env_state[ETAT_FAIM] - 0.5, 0.0, 1.0)
|
| 213 |
current_env_state[ETAT_HUMEUR] = np.clip(current_env_state[ETAT_HUMEUR] + 0.5, -1.0, 1.0)
|
| 214 |
|
|
@@ -247,7 +249,6 @@ def _draw_grid(px, py, rx, ry, reward_path):
|
|
| 247 |
if is_puppy:
|
| 248 |
style += "background-color: #d4edda;"
|
| 249 |
else:
|
| 250 |
-
# Affichage de l'image
|
| 251 |
content += f"<img src='{reward_src}' style='width: 80%; height: 80%; object-fit: contain;'/>"
|
| 252 |
style += "background-color: #fff3cd;"
|
| 253 |
|
|
@@ -267,7 +268,7 @@ def update_reward_pos(current_state, reward_x, reward_y, reward_path):
|
|
| 267 |
|
| 268 |
|
| 269 |
# ----------------------------------------------------------------------
|
| 270 |
-
# 3. INTERFACE GRADIO
|
| 271 |
# ----------------------------------------------------------------------
|
| 272 |
|
| 273 |
initial_grid_html = _draw_grid(game_state_initial['puppy_pos'][0], game_state_initial['puppy_pos'][1],
|
|
@@ -290,7 +291,7 @@ else:
|
|
| 290 |
gr.Markdown(
|
| 291 |
f"""
|
| 292 |
# MiRobot - Le Jeu d'Obéissance 🐾
|
| 293 |
-
Bienvenue dans la simulation interactive
|
| 294 |
**Objectif :** Guider MiRobot vers la récompense en donnant des ordres. Attention, sa **Faim** augmente à chaque pas !
|
| 295 |
"""
|
| 296 |
)
|
|
@@ -298,7 +299,7 @@ else:
|
|
| 298 |
with gr.Row():
|
| 299 |
|
| 300 |
with gr.Column(scale=2):
|
| 301 |
-
#
|
| 302 |
reward_image = gr.Image(label="1. Télécharger Image Récompense (Obligatoire)", type="filepath", height=150, width=150)
|
| 303 |
|
| 304 |
with gr.Row():
|
|
@@ -331,7 +332,7 @@ else:
|
|
| 331 |
faim_state = gr.State(initial_faim)
|
| 332 |
humeur_state = gr.State(initial_humeur)
|
| 333 |
|
| 334 |
-
# --- ÉVÉNEMENTS
|
| 335 |
|
| 336 |
reward_x.change(
|
| 337 |
fn=update_reward_pos,
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
# ----------------------------------------------------------------------
|
| 38 |
+
# 1. PRÉPARATION DU MODÈLE ET DE L'ENVIRONNEMENT
|
| 39 |
# ----------------------------------------------------------------------
|
| 40 |
|
| 41 |
model = None
|
|
|
|
| 58 |
exec(f.read(), env_globals)
|
| 59 |
|
| 60 |
MiRobotEnv = env_globals['MiRobotEnv']
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# --- 2. Enregistrement de l'environnement Custom ---
|
| 63 |
register(
|
|
|
|
| 73 |
env = gym.make(ENV_ID)
|
| 74 |
env.reset()
|
| 75 |
|
| 76 |
+
# Lecture initiale sécurisée de l'état
|
| 77 |
initial_faim = env.unwrapped.state[ETAT_FAIM] * 100
|
| 78 |
initial_humeur = env.unwrapped.state[ETAT_HUMEUR]
|
| 79 |
|
|
|
|
| 86 |
# 2. LOGIQUE DU JEU
|
| 87 |
# ----------------------------------------------------------------------
|
| 88 |
|
|
|
|
| 89 |
game_state_initial = {
|
| 90 |
'level': INITIAL_LEVEL,
|
| 91 |
'puppy_pos': [GRID_SIZE // 2, GRID_SIZE // 2],
|
|
|
|
| 98 |
"""Accès sécurisé à l'état de l'environnement, même avec un wrapper."""
|
| 99 |
if env_instance is None:
|
| 100 |
return None
|
| 101 |
+
# Utilise .unwrapped pour accéder à l'instance de MiRobotEnv
|
| 102 |
return env_instance.unwrapped.state
|
| 103 |
|
| 104 |
def _reset_game(reward_path):
|
|
|
|
| 132 |
|
| 133 |
game_state['reward_asset_path'] = reward_path
|
| 134 |
|
| 135 |
+
# 2. Vérification de la faim (défaite)
|
| 136 |
faim_actuelle = current_env_state[ETAT_FAIM]
|
| 137 |
if faim_actuelle >= FAIM_PENALTY_THRESHOLD:
|
| 138 |
game_state['message'] = f'💔 Défaite ! MiRobot a trop faim ({faim_actuelle:.0%}). Jeu réinitialisé.'
|
|
|
|
| 145 |
|
| 146 |
current_env_state[CMD_AVANCER] = 0.0
|
| 147 |
current_env_state[CMD_TOURNER] = 0.0
|
| 148 |
+
env.step(0)
|
| 149 |
|
| 150 |
faim_display = current_env_state[ETAT_FAIM] * 100
|
| 151 |
humeur_display = current_env_state[ETAT_HUMEUR]
|
|
|
|
| 159 |
current_env_state[CMD_TOURNER] = 1.0 if command_action_name.startswith("TOURNER") else 0.0
|
| 160 |
|
| 161 |
mirobot_action_id, _ = model.predict(current_env_state, deterministic=True)
|
| 162 |
+
|
| 163 |
+
# **CORRECTION CRUCIALE**: Convertir le tableau NumPy en entier
|
| 164 |
+
mirobot_action_id = mirobot_action_id.item()
|
| 165 |
+
|
| 166 |
new_obs, reward, terminated, truncated, info = env.step(mirobot_action_id)
|
| 167 |
|
| 168 |
|
|
|
|
| 181 |
dy = 1 if ry > py else -1
|
| 182 |
|
| 183 |
else:
|
| 184 |
+
# Cette ligne est maintenant sûre car mirobot_action_id est un entier
|
| 185 |
+
real_action_name = ACTION_MAP_MODEL[mirobot_action_id]
|
| 186 |
game_state['message'] = f"😥 MiRobot a désobéi ! Il a fait '{real_action_name}' au lieu de '{command_action_name}'. Récompense RL: {reward:.2f}"
|
| 187 |
|
| 188 |
# Mise à jour de la position
|
|
|
|
| 190 |
new_y = np.clip(game_state['puppy_pos'][1] + dy, 0, GRID_SIZE - 1)
|
| 191 |
game_state['puppy_pos'] = [new_x, new_y]
|
| 192 |
|
| 193 |
+
current_env_state = _get_env_state(env)
|
| 194 |
faim_display = current_env_state[ETAT_FAIM] * 100
|
| 195 |
humeur_display = current_env_state[ETAT_HUMEUR]
|
| 196 |
return game_state, command_text, new_x, new_y, faim_display, humeur_display, game_state['message']
|
|
|
|
| 210 |
if px == rx and py == ry:
|
| 211 |
game_state['level'] += 1
|
| 212 |
|
| 213 |
+
# Modification de l'état
|
| 214 |
current_env_state[ETAT_FAIM] = np.clip(current_env_state[ETAT_FAIM] - 0.5, 0.0, 1.0)
|
| 215 |
current_env_state[ETAT_HUMEUR] = np.clip(current_env_state[ETAT_HUMEUR] + 0.5, -1.0, 1.0)
|
| 216 |
|
|
|
|
| 249 |
if is_puppy:
|
| 250 |
style += "background-color: #d4edda;"
|
| 251 |
else:
|
|
|
|
| 252 |
content += f"<img src='{reward_src}' style='width: 80%; height: 80%; object-fit: contain;'/>"
|
| 253 |
style += "background-color: #fff3cd;"
|
| 254 |
|
|
|
|
| 268 |
|
| 269 |
|
| 270 |
# ----------------------------------------------------------------------
|
| 271 |
+
# 3. INTERFACE GRADIO
|
| 272 |
# ----------------------------------------------------------------------
|
| 273 |
|
| 274 |
initial_grid_html = _draw_grid(game_state_initial['puppy_pos'][0], game_state_initial['puppy_pos'][1],
|
|
|
|
| 291 |
gr.Markdown(
|
| 292 |
f"""
|
| 293 |
# MiRobot - Le Jeu d'Obéissance 🐾
|
| 294 |
+
Bienvenue dans la simulation interactive du modèle IA **{REPO_ID}** !
|
| 295 |
**Objectif :** Guider MiRobot vers la récompense en donnant des ordres. Attention, sa **Faim** augmente à chaque pas !
|
| 296 |
"""
|
| 297 |
)
|
|
|
|
| 299 |
with gr.Row():
|
| 300 |
|
| 301 |
with gr.Column(scale=2):
|
| 302 |
+
# Utilisation de gr.Image pour une meilleure gestion des uploads
|
| 303 |
reward_image = gr.Image(label="1. Télécharger Image Récompense (Obligatoire)", type="filepath", height=150, width=150)
|
| 304 |
|
| 305 |
with gr.Row():
|
|
|
|
| 332 |
faim_state = gr.State(initial_faim)
|
| 333 |
humeur_state = gr.State(initial_humeur)
|
| 334 |
|
| 335 |
+
# --- ÉVÉNEMENTS ---
|
| 336 |
|
| 337 |
reward_x.change(
|
| 338 |
fn=update_reward_pos,
|