Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,101 +0,0 @@
|
|
| 1 |
-
import gradio as ui
|
| 2 |
-
import os
|
| 3 |
-
import subprocess
|
| 4 |
-
import threading
|
| 5 |
-
import sys
|
| 6 |
-
|
| 7 |
-
# Variable globale pour stocker le processus du bot
|
| 8 |
-
processus_bot = None
|
| 9 |
-
logs_visuels = ""
|
| 10 |
-
|
| 11 |
-
def lancer_voyager(api_key, server_ip, server_port):
|
| 12 |
-
global processus_bot, logs_visuels
|
| 13 |
-
|
| 14 |
-
if not api_key or not server_ip:
|
| 15 |
-
return "❌ Erreur : La clé API OpenAI et l'IP du serveur sont obligatoires !"
|
| 16 |
-
|
| 17 |
-
if processus_bot is not None:
|
| 18 |
-
return "⚠️ Voyager est déjà en train de tourner !"
|
| 19 |
-
|
| 20 |
-
# On injecte la clé API dans l'environnement du conteneur
|
| 21 |
-
os.environ["OPENAI_API_KEY"] = api_key
|
| 22 |
-
|
| 23 |
-
# Écriture d'un mini script de démarrage pour exécuter Voyager en tâche de fond
|
| 24 |
-
script_content = f"""
|
| 25 |
-
from voyager import Voyager
|
| 26 |
-
|
| 27 |
-
voyager = Voyager(
|
| 28 |
-
mc_port={server_port},
|
| 29 |
-
mc_ip="{server_ip}",
|
| 30 |
-
openai_api_key="{api_key}",
|
| 31 |
-
azure_login=None # Utilise le mode offline pour se connecter sans compte Microsoft officiel
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# Lance l'apprentissage automatique
|
| 35 |
-
voyager.learn()
|
| 36 |
-
"""
|
| 37 |
-
with open("run_bot.py", "w") as f:
|
| 38 |
-
f.write(script_content)
|
| 39 |
-
|
| 40 |
-
# Lancement du processus en tâche de fond
|
| 41 |
-
try:
|
| 42 |
-
processus_bot = subprocess.Popen(
|
| 43 |
-
[sys.executable, "run_bot.py"],
|
| 44 |
-
stdout=subprocess.PIPE,
|
| 45 |
-
stderr=subprocess.STDOUT,
|
| 46 |
-
text=True
|
| 47 |
-
)
|
| 48 |
-
return "🚀 Voyager a été démarré avec succès ! Surveille les logs à droite."
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"❌ Erreur lors du lancement : {str(e)}"
|
| 51 |
-
|
| 52 |
-
def arreter_voyager():
|
| 53 |
-
global processus_bot
|
| 54 |
-
if processus_bot is None:
|
| 55 |
-
return "Le bot n'est pas actif."
|
| 56 |
-
|
| 57 |
-
processus_bot.terminate()
|
| 58 |
-
processus_bot = None
|
| 59 |
-
return "🛑 Voyager a été arrêté."
|
| 60 |
-
|
| 61 |
-
def rafraichir_logs():
|
| 62 |
-
global processus_bot
|
| 63 |
-
# Si le processus tourne, on récupère ce qu'il écrit dans la console
|
| 64 |
-
if processus_bot and processus_bot.stdout:
|
| 65 |
-
# Lecture non-bloquante d'une ligne
|
| 66 |
-
ligne = processus_bot.stdout.readline()
|
| 67 |
-
if ligne:
|
| 68 |
-
return ligne
|
| 69 |
-
return "En attente de logs ou bot éteint..."
|
| 70 |
-
|
| 71 |
-
# --- Design de l'interface du Panel ---
|
| 72 |
-
with ui.Blocks(theme=ui.themes.Soft()) as panel:
|
| 73 |
-
ui.Markdown("# 🤖 Voyager Agent - Panel de Contrôle Minecraft")
|
| 74 |
-
ui.Markdown("Pilote ton agent autonome directement depuis ce Space Hugging Face.")
|
| 75 |
-
|
| 76 |
-
with ui.Row():
|
| 77 |
-
with ui.Column(scale=1):
|
| 78 |
-
ui.Markdown("### ⚙️ Configuration")
|
| 79 |
-
input_key = ui.Textbox(label="Clé API OpenAI (sk-...)", type="password", placeholder="Entre ta clé secrète")
|
| 80 |
-
input_ip = ui.Textbox(label="IP du serveur Minecraft", placeholder="ex: mon-serveur.aternos.me")
|
| 81 |
-
input_port = ui.Number(label="Port du serveur", value=25565, precision=0)
|
| 82 |
-
|
| 83 |
-
with ui.Row():
|
| 84 |
-
btn_start = ui.Button("🚀 Démarrer l'Agent", variant="primary")
|
| 85 |
-
btn_stop = ui.Button("🛑 Stopper", variant="stop")
|
| 86 |
-
|
| 87 |
-
status_output = ui.Textbox(label="Statut du Panel", interactive=False)
|
| 88 |
-
|
| 89 |
-
with ui.Column(scale=1):
|
| 90 |
-
ui.Markdown("### 🖥️ Console en Direct")
|
| 91 |
-
console_output = ui.TextArea(label="Logs d'apprentissage de Voyager", interactive=False, lines=15)
|
| 92 |
-
# Bouton de rafraîchissement des logs (simulé en continu via l'interface Gradio)
|
| 93 |
-
btn_refresh = ui.Timer(1)
|
| 94 |
-
|
| 95 |
-
# Assignation des fonctions aux boutons
|
| 96 |
-
btn_start.click(lancer_voyager, inputs=[input_key, input_ip, input_port], outputs=status_output)
|
| 97 |
-
btn_stop.click(arreter_voyager, outputs=status_output)
|
| 98 |
-
btn_refresh.tick(rafraichir_logs, outputs=console_output)
|
| 99 |
-
|
| 100 |
-
# Lancement de l'application sur le port requis par Hugging Face
|
| 101 |
-
panel.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|