Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
from agent_zero import AgentZero
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
agent = AgentZero()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def chat_fn(message, history):
|
| 10 |
-
"""
|
| 11 |
-
Fonction de chat compatible avec le composant ChatInterface de Gradio 5.
|
| 12 |
-
"""
|
| 13 |
try:
|
|
|
|
| 14 |
response = agent.run(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return response
|
| 16 |
except Exception as e:
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
demo = gr.ChatInterface(
|
| 21 |
fn=chat_fn,
|
| 22 |
-
title="Agent Zero –
|
| 23 |
-
description="
|
| 24 |
-
examples=["Vérifie la syntaxe du fichier main.py", "Prépare une mutation pour optimiser les imports"],
|
| 25 |
theme="soft"
|
| 26 |
)
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
-
#
|
| 30 |
-
# show_error=True aide au débogage pendant le développement
|
| 31 |
demo.launch(server_name="0.0.0.0", show_error=True)
|
| 32 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import asyncio
|
| 4 |
+
import threading
|
| 5 |
from agent_zero import AgentZero
|
| 6 |
+
from telegram import Bot
|
| 7 |
|
| 8 |
+
# Récupération des secrets Hugging Face
|
| 9 |
+
TOKEN = os.getenv("BOT_TELEGRAM")
|
| 10 |
+
CHANNEL_ID = os.getenv("CHANNEL_TELEGRAM")
|
| 11 |
+
|
| 12 |
+
# Initialisation de l'Agent et du Bot
|
| 13 |
agent = AgentZero()
|
| 14 |
+
bot = Bot(token=TOKEN) if TOKEN else None
|
| 15 |
+
|
| 16 |
+
def send_to_telegram(message):
|
| 17 |
+
"""Envoie une notification au canal Telegram si configuré."""
|
| 18 |
+
if bot and CHANNEL_ID:
|
| 19 |
+
try:
|
| 20 |
+
# On utilise une boucle d'événement séparée pour l'envoi asynchrone
|
| 21 |
+
loop = asyncio.new_event_loop()
|
| 22 |
+
asyncio.set_event_loop(loop)
|
| 23 |
+
loop.run_until_complete(bot.send_message(chat_id=CHANNEL_ID, text=message))
|
| 24 |
+
loop.close()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Erreur Telegram : {e}")
|
| 27 |
|
| 28 |
def chat_fn(message, history):
|
| 29 |
+
"""Fonction de chat Gradio qui communique aussi vers Telegram."""
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
+
# 1. L'agent génère la réponse
|
| 32 |
response = agent.run(message)
|
| 33 |
+
|
| 34 |
+
# 2. Notification Telegram (Centre de commandement)
|
| 35 |
+
log_msg = f"🔔 ORDRE : {message}\n\n🤖 RÉPONSE : {response}"
|
| 36 |
+
threading.Thread(target=send_to_telegram, args=(log_msg,)).start()
|
| 37 |
+
|
| 38 |
return response
|
| 39 |
except Exception as e:
|
| 40 |
+
err_msg = f"⚠️ Erreur : {str(e)}"
|
| 41 |
+
threading.Thread(target=send_to_telegram, args=(err_msg,)).start()
|
| 42 |
+
return err_msg
|
| 43 |
|
| 44 |
+
# Interface Gradio 5
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
fn=chat_fn,
|
| 47 |
+
title="Agent Zero – Command Center",
|
| 48 |
+
description="Pilotage via Telegram (Secrets : BOT_TELEGRAM actif)",
|
|
|
|
| 49 |
theme="soft"
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
# Lancement du serveur pour Hugging Face
|
|
|
|
| 54 |
demo.launch(server_name="0.0.0.0", show_error=True)
|
| 55 |
|