Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,28 @@ from stable_baselines3 import DQN
|
|
| 4 |
import imageio
|
| 5 |
import time
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# TensorFlow figyelmeztetések elrejtése
|
| 9 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 10 |
|
| 11 |
-
st.set_page_config(page_title="Lunar Lander AI", page_icon="🚀")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# --- FŐ CÍM ---
|
| 14 |
st.title("🌕 Lunar Lander AI")
|
| 15 |
st.markdown("**Deep Q-Network (DQN)** algoritmus demonstrációja.")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
# --- 1. SZEKCIÓ: AI DEMO (Visszajátszás) ---
|
| 19 |
@st.cache_resource
|
| 20 |
def load_model():
|
| 21 |
if os.path.exists("lunar_lander_model.zip"):
|
|
@@ -23,42 +33,64 @@ def load_model():
|
|
| 23 |
else:
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
|
| 27 |
model = load_model()
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
else:
|
| 32 |
-
# Konténer a videónak
|
| 33 |
-
demo_container = st.container()
|
| 34 |
-
|
| 35 |
-
with demo_container:
|
| 36 |
-
st.subheader("🤖 Így csinálja a gép:")
|
| 37 |
-
start_button = st.button("AI Indítása / Végtelen ciklus ▶️")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
env = gym.make("LunarLander-v3", render_mode="rgb_array")
|
| 45 |
episode_count = 0
|
| 46 |
fps = 30
|
| 47 |
-
|
|
|
|
| 48 |
while True:
|
| 49 |
episode_count += 1
|
| 50 |
-
|
| 51 |
-
# Takarítás
|
| 52 |
-
video_placeholder.empty()
|
| 53 |
status_text.info(f"🔄 {episode_count}. szimuláció számolása a háttérben...")
|
| 54 |
-
|
| 55 |
obs, info = env.reset()
|
| 56 |
done = False
|
| 57 |
truncated = False
|
| 58 |
total_reward = 0
|
| 59 |
-
frames = []
|
| 60 |
-
|
| 61 |
-
# Szimuláció
|
| 62 |
while not done and not truncated:
|
| 63 |
action, _states = model.predict(obs, deterministic=True)
|
| 64 |
obs, reward, done, truncated, info = env.step(action)
|
|
@@ -69,14 +101,14 @@ else:
|
|
| 69 |
gif_path = "replay.gif"
|
| 70 |
imageio.mimsave(gif_path, frames, fps=fps)
|
| 71 |
video_duration = len(frames) / fps
|
| 72 |
-
|
| 73 |
# Lejátszás
|
| 74 |
-
status_text.
|
| 75 |
video_placeholder.image(gif_path, caption=f"{episode_count}. Leszállás")
|
| 76 |
-
|
| 77 |
-
# Várakozás
|
| 78 |
time.sleep(video_duration)
|
| 79 |
-
|
| 80 |
# Eredmény kiírása
|
| 81 |
if total_reward >= 200:
|
| 82 |
msg = f"✅ SIKER! Profi munka. Pontszám: {total_reward:.2f}"
|
|
@@ -85,41 +117,6 @@ else:
|
|
| 85 |
msg = f"⚠️ OK. Biztonságos. Pontszám: {total_reward:.2f}"
|
| 86 |
else:
|
| 87 |
msg = f"💥 ZUHANÁS! Pontszám: {total_reward:.2f}"
|
| 88 |
-
|
| 89 |
status_text.markdown(f"### {msg}")
|
| 90 |
-
time.sleep(2)
|
| 91 |
-
|
| 92 |
-
# --- 2. SZEKCIÓ: EMBERI KIHÍVÁS (LETÖLTÉS) ---
|
| 93 |
-
st.markdown("---") # Elválasztó vonal
|
| 94 |
-
st.header("🎮 Te le tudod tenni?")
|
| 95 |
-
|
| 96 |
-
col1, col2 = st.columns([2, 1])
|
| 97 |
-
|
| 98 |
-
with col1:
|
| 99 |
-
st.write("""
|
| 100 |
-
Az AI már megtanulta... de vajon neked sikerülne?
|
| 101 |
-
Töltsd le a játékot, és próbáld ki a saját gépeden az eredeti, nehéz fizikával!
|
| 102 |
-
|
| 103 |
-
**A csomag tartalma:**
|
| 104 |
-
* 🐍 `lunar_lander.py` (A játék kódja)
|
| 105 |
-
* ⚙️ `INDITAS.bat` (Automatikus telepítő és indító)
|
| 106 |
-
""")
|
| 107 |
-
|
| 108 |
-
with col2:
|
| 109 |
-
# Letöltés gomb
|
| 110 |
-
zip_filename = "lunar_lander_challenge.zip"
|
| 111 |
-
|
| 112 |
-
if os.path.exists(zip_filename):
|
| 113 |
-
with open(zip_filename, "rb") as fp:
|
| 114 |
-
st.download_button(
|
| 115 |
-
label="📥 JÁTÉK LETÖLTÉSE\n(Windows .zip)",
|
| 116 |
-
data=fp,
|
| 117 |
-
file_name=zip_filename,
|
| 118 |
-
mime="application/zip",
|
| 119 |
-
help="Kattints a letöltéshez, csomagold ki, és indítsd el a .bat fájlt!"
|
| 120 |
-
)
|
| 121 |
-
else:
|
| 122 |
-
st.warning("A letölthető fájl (lunar_lander_challenge.zip) még nincs feltöltve.")
|
| 123 |
-
|
| 124 |
-
st.markdown("---")
|
| 125 |
-
st.caption("Készítette: Gemini & Faragó Csaba | Powered by Stable Baselines3 & Streamlit")
|
|
|
|
| 4 |
import imageio
|
| 5 |
import time
|
| 6 |
import os
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
# TensorFlow figyelmeztetések elrejtése
|
| 10 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 11 |
|
| 12 |
+
st.set_page_config(page_title="Lunar Lander AI", page_icon="🚀", layout="wide")
|
| 13 |
+
|
| 14 |
+
# --- SEGÉDFÜGGVÉNY: HTML LETÖLTÉS (Hogy ne akadjon be a loop miatt) ---
|
| 15 |
+
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
| 16 |
+
with open(bin_file, 'rb') as f:
|
| 17 |
+
data = f.read()
|
| 18 |
+
bin_str = base64.b64encode(data).decode()
|
| 19 |
+
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}" style="text-decoration: none;">'
|
| 20 |
+
href += f'<button style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 12px;">📥 {file_label} LETÖLTÉSE</button>'
|
| 21 |
+
href += '</a>'
|
| 22 |
+
return href
|
| 23 |
|
| 24 |
# --- FŐ CÍM ---
|
| 25 |
st.title("🌕 Lunar Lander AI")
|
| 26 |
st.markdown("**Deep Q-Network (DQN)** algoritmus demonstrációja.")
|
| 27 |
|
| 28 |
+
# --- OLDALSÁV (MODOLL BETÖLTÉS) ---
|
|
|
|
| 29 |
@st.cache_resource
|
| 30 |
def load_model():
|
| 31 |
if os.path.exists("lunar_lander_model.zip"):
|
|
|
|
| 33 |
else:
|
| 34 |
return None
|
| 35 |
|
|
|
|
| 36 |
model = load_model()
|
| 37 |
|
| 38 |
+
# --- FŐHASÁBOK ---
|
| 39 |
+
col_game, col_download = st.columns([2, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# --- JOBB OLDAL: LETÖLTÉS (MINDIG LÁTHATÓ ÉS MŰKÖDIK) ---
|
| 42 |
+
with col_download:
|
| 43 |
+
st.info("🎮 **Próbáld ki te is!**")
|
| 44 |
+
st.write("Az AI már megtanulta... de vajon neked sikerülne? Töltsd le a játékot a saját gépedre!")
|
| 45 |
+
|
| 46 |
+
zip_filename = "lunar_lander_challenge.zip"
|
| 47 |
+
if os.path.exists(zip_filename):
|
| 48 |
+
# HTML Linket használunk a gomb helyett, így a loop nem blokkolja!
|
| 49 |
+
download_html = get_binary_file_downloader_html(zip_filename, "JÁTÉK CSOMAG")
|
| 50 |
+
st.markdown(download_html, unsafe_allow_html=True)
|
| 51 |
+
st.caption("Windows .zip | Tartalmazza: Python kód + Automata indító")
|
| 52 |
+
else:
|
| 53 |
+
st.warning("A letölthető fájl feltöltés alatt...")
|
| 54 |
|
| 55 |
+
# --- BAL OLDAL: AI DEMO ---
|
| 56 |
+
with col_game:
|
| 57 |
+
if model is None:
|
| 58 |
+
st.error("⚠️ Hiányzik a 'lunar_lander_model.zip'!")
|
| 59 |
+
else:
|
| 60 |
+
st.subheader("🤖 AI Visszajátszás")
|
| 61 |
+
|
| 62 |
+
# Helyőrzők a UI elemeknek
|
| 63 |
+
start_button_placeholder = st.empty()
|
| 64 |
+
status_text = st.empty()
|
| 65 |
+
video_placeholder = st.empty()
|
| 66 |
+
|
| 67 |
+
# A gomb a placeholderben jön létre
|
| 68 |
+
start_clicked = start_button_placeholder.button("▶️ Szimuláció Indítása", type="primary")
|
| 69 |
+
|
| 70 |
+
if start_clicked:
|
| 71 |
+
# 1. ELTÜNTETJÜK A GOMBOT
|
| 72 |
+
start_button_placeholder.empty()
|
| 73 |
+
|
| 74 |
+
# Inicializálás
|
| 75 |
env = gym.make("LunarLander-v3", render_mode="rgb_array")
|
| 76 |
episode_count = 0
|
| 77 |
fps = 30
|
| 78 |
+
|
| 79 |
+
# VÉGTELEN CIKLUS
|
| 80 |
while True:
|
| 81 |
episode_count += 1
|
| 82 |
+
|
| 83 |
+
# Takarítás az új kör előtt
|
| 84 |
+
video_placeholder.empty()
|
| 85 |
status_text.info(f"🔄 {episode_count}. szimuláció számolása a háttérben...")
|
| 86 |
+
|
| 87 |
obs, info = env.reset()
|
| 88 |
done = False
|
| 89 |
truncated = False
|
| 90 |
total_reward = 0
|
| 91 |
+
frames = []
|
| 92 |
+
|
| 93 |
+
# Szimuláció ("láthatatlanul" és gyorsan)
|
| 94 |
while not done and not truncated:
|
| 95 |
action, _states = model.predict(obs, deterministic=True)
|
| 96 |
obs, reward, done, truncated, info = env.step(action)
|
|
|
|
| 101 |
gif_path = "replay.gif"
|
| 102 |
imageio.mimsave(gif_path, frames, fps=fps)
|
| 103 |
video_duration = len(frames) / fps
|
| 104 |
+
|
| 105 |
# Lejátszás
|
| 106 |
+
status_text.success(f"🎬 {episode_count}. kör visszajátszása...")
|
| 107 |
video_placeholder.image(gif_path, caption=f"{episode_count}. Leszállás")
|
| 108 |
+
|
| 109 |
+
# Várakozás
|
| 110 |
time.sleep(video_duration)
|
| 111 |
+
|
| 112 |
# Eredmény kiírása
|
| 113 |
if total_reward >= 200:
|
| 114 |
msg = f"✅ SIKER! Profi munka. Pontszám: {total_reward:.2f}"
|
|
|
|
| 117 |
msg = f"⚠️ OK. Biztonságos. Pontszám: {total_reward:.2f}"
|
| 118 |
else:
|
| 119 |
msg = f"💥 ZUHANÁS! Pontszám: {total_reward:.2f}"
|
| 120 |
+
|
| 121 |
status_text.markdown(f"### {msg}")
|
| 122 |
+
time.sleep(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|